Subversion Repositories oidplus

Rev

Rev 1187 | Rev 1224 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1187 Rev 1197
Line 33... Line 33...
33
                $reason = '';
33
                $reason = '';
34
                return true;
34
                return true;
35
        }
35
        }
36
 
36
 
37
        /**
37
        /**
38
         * @param string $event
38
         * @param OIDplusLogEvent $event
39
         * @param array $users
-
 
40
         * @param array $objects
-
 
41
         * @return bool
39
         * @return bool
42
         * @throws OIDplusException
40
         * @throws OIDplusException
43
         */
41
         */
44
        public function log(string $event, array $users, array $objects): bool {
42
        public function log(OIDplusLogEvent $event): bool {
45
                $addr = $_SERVER['REMOTE_ADDR'] ?? '';
43
                $addr = $_SERVER['REMOTE_ADDR'] ?? '';
46
                OIDplus::dbIsolated()->query("insert into ###log (addr, unix_ts, event) values (?, ?, ?)", array($addr, time(), $event)); // TODO: why unix_ts? Why not a database DATETIME field?!
44
                OIDplus::dbIsolated()->query("insert into ###log (addr, unix_ts, event) values (?, ?, ?)", array($addr, time(), $event->getMessage())); // TODO: why unix_ts? Why not a database DATETIME field?!
47
                $log_id = OIDplus::dbIsolated()->insert_id();
45
                $log_id = OIDplus::dbIsolated()->insert_id();
48
                if ($log_id === 0) {
46
                if ($log_id === 0) {
49
                        $res = OIDplus::dbIsolated()->query("select max(id) as last_id from ###log");
47
                        $res = OIDplus::dbIsolated()->query("select max(id) as last_id from ###log");
50
                        if (!$res->any()) throw new OIDplusException(_L('Could not log event'));
48
                        if (!$res->any()) throw new OIDplusException(_L('Could not log event'));
51
                        $row = $res->fetch_array();
49
                        $row = $res->fetch_array();
52
                        $log_id = $row['last_id'];
50
                        $log_id = $row['last_id'];
53
                        if ($log_id == 0) throw new OIDplusException(_L('Could not log event'));
51
                        if ($log_id == 0) throw new OIDplusException(_L('Could not log event'));
54
                }
52
                }
55
 
53
 
56
                $object_dupe_check = array();
54
                $object_dupe_check = array();
-
 
55
                $user_dupe_check = array();
57
                foreach ($objects as list($severity, $object)) {
56
                foreach ($event->getTargets() as $target) {
-
 
57
                        $severity = $target->getSeverity();
-
 
58
                        if ($target instanceof OIDplusLogTargetObject) {
-
 
59
                                $object = $target->getObject();
58
                        if (in_array($object, $object_dupe_check)) continue;
60
                                if (in_array($object, $object_dupe_check)) continue;
59
                        $object_dupe_check[] = $object;
61
                                $object_dupe_check[] = $object;
60
                        OIDplus::dbIsolated()->query("insert into ###log_object (log_id, severity, object) values (?, ?, ?)", array((int)$log_id, (int)$severity, $object));
62
                                OIDplus::dbIsolated()->query("insert into ###log_object (log_id, severity, object) values (?, ?, ?)", array((int)$log_id, (int)$severity, $object));
61
                }
-
 
62
 
-
 
63
                $user_dupe_check = array();
63
                        } else if ($target instanceof OIDplusLogTargetUser) {
64
                foreach ($users as list($severity, $username)) {
64
                                $username = $target->getUsername();
65
                        if (in_array($username, $user_dupe_check)) continue;
65
                                if (in_array($username, $user_dupe_check)) continue;
66
                        $user_dupe_check[] = $username;
66
                                $user_dupe_check[] = $username;
67
                        OIDplus::dbIsolated()->query("insert into ###log_user (log_id, severity, username) values (?, ?, ?)", array((int)$log_id, (int)$severity, $username));
67
                                OIDplus::dbIsolated()->query("insert into ###log_user (log_id, severity, username) values (?, ?, ?)", array((int)$log_id, (int)$severity, $username));
-
 
68
                        } else {
-
 
69
                                assert(false);
-
 
70
                        }
68
                }
71
                }
69
 
72
 
70
                return true;
73
                return true;
71
        }
74
        }
72
 
75