Subversion Repositories oidplus

Rev

Rev 150 | Rev 236 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
115 daniel-mar 1
<?php
2
 
3
/*
4
 * OIDplus 2.0
5
 * Copyright 2019 Daniel Marschall, ViaThinkSoft
6
 *
7
 * Licensed under the Apache License, Version 2.0 (the "License");
8
 * you may not use this file except in compliance with the License.
9
 * You may obtain a copy of the License at
10
 *
11
 *     http://www.apache.org/licenses/LICENSE-2.0
12
 *
13
 * Unless required by applicable law or agreed to in writing, software
14
 * distributed under the License is distributed on an "AS IS" BASIS,
15
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
 * See the License for the specific language governing permissions and
17
 * limitations under the License.
18
 */
19
 
20
if (!defined('IN_OIDPLUS')) die();
21
 
22
class OIDplusLogger {
23
 
188 daniel-mar 24
        private static function split_maskcodes($maskcodes) {
25
                $out = array();
26
 
27
                $code = '';
28
                $bracket_level = 0;
29
                for ($i=0; $i<strlen($maskcodes); $i++) {
30
                        $char = $maskcodes[$i];
31
                        if ($char == '(') $bracket_level++;
32
                        if ($char == ')') {
33
                                $bracket_level--;
34
                                if ($bracket_level < 0) return false;
35
                        }
36
                        if ((($char == '+') || ($char == '/')) && ($bracket_level == 0)) {
37
                                $out[] = $code;
38
                                $code = '';
39
                        } else {
40
                                $code .= $char;
41
                        }
42
                }
43
                if ($code != '') $out[] = $code;
44
 
45
                return $out;
46
        }
47
 
115 daniel-mar 48
        public function log($maskcodes, $event) {
49
 
50
                $users = array();
51
                $objects = array();
52
 
188 daniel-mar 53
                $maskcodes_ary = self::split_maskcodes($maskcodes);
54
                if ($maskcodes_ary === false) {
55
                        throw new Exception("Invalid maskcode '$maskcodes'");
56
                }
57
                foreach ($maskcodes_ary as $maskcode) {
115 daniel-mar 58
                        // OID(x)       Save log entry into the logbook of: Object "x"
59
                        if (preg_match('@^OID\((.+)\)$@ismU', $maskcode, $m)) {
60
                                $object_id = $m[1];
61
                                $objects[] = $object_id;
116 daniel-mar 62
                                if ($object_id == '') throw new Exception("OID logger mask requires OID");
115 daniel-mar 63
                        }
64
 
65
                        // OIDRA(x)?    Save log entry into the logbook of: Logged in RA of object "x"
66
                        // Replace ? by ! if the entity does not need to be logged in
67
                        else if (preg_match('@^OIDRA\((.+)\)([\?\!])$@ismU', $maskcode, $m)) {
68
                                $object_id         = $m[1];
116 daniel-mar 69
                                $ra_need_login     = $m[2] == '?';
70
                                if ($object_id == '') throw new Exception("OIDRA logger mask requires OID");
115 daniel-mar 71
                                $obj = OIDplusObject::parse($object_id);
116 daniel-mar 72
                                if ($obj) {
73
                                        if ($ra_need_login) {
74
                                                foreach (OIDplus::authUtils()->loggedInRaList() as $ra) {
75
                                                        if ($obj->userHasWriteRights($ra)) $users[] = $ra->raEmail();
76
                                                }
77
                                        } else {
78
                                                // $users[] = $obj->getRa()->raEmail();
79
                                                foreach (OIDplusRA::getAllRAs() as $ra) {
80
                                                        if ($obj->userHasWriteRights($ra)) $users[] = $ra->raEmail();
81
                                                }
115 daniel-mar 82
                                        }
83
                                }
84
                        }
85
 
86
                        // SUPOIDRA(x)? Save log entry into the logbook of: Logged in RA that owns the superior object of "x"
87
                        // Replace ? by ! if the entity does not need to be logged in
88
                        else if (preg_match('@^SUPOIDRA\((.+)\)([\?\!])$@ismU', $maskcode, $m)) {
89
                                $object_id         = $m[1];
116 daniel-mar 90
                                $ra_need_login     = $m[2] == '?';
91
                                if ($object_id == '') throw new Exception("SUPOIDRA logger mask requires OID");
115 daniel-mar 92
                                $obj = OIDplusObject::parse($object_id);
116 daniel-mar 93
                                if ($obj) {
94
                                        if ($ra_need_login) {
95
                                                foreach (OIDplus::authUtils()->loggedInRaList() as $ra) {
96
                                                        if ($obj->userHasParentalWriteRights($ra)) $users[] = $ra->raEmail();
97
                                                }
98
                                        } else {
99
                                                // $users[] = $obj->getParent()->getRa()->raEmail();
100
                                                foreach (OIDplusRA::getAllRAs() as $ra) {
101
                                                        if ($obj->userHasParentalWriteRights($ra)) $users[] = $ra->raEmail();
102
                                                }
115 daniel-mar 103
                                        }
104
                                }
105
                        }
106
 
107
                        // RA(x)?       Save log entry into the logbook of: Logged in RA "x"
108
                        // Replace ? by ! if the entity does not need to be logged in
109
                        else if (preg_match('@^RA\((.+)\)([\?\!])$@ismU', $maskcode, $m)) {
110
                                $ra_email          = $m[1];
116 daniel-mar 111
                                $ra_need_login     = $m[2] == '?';
115 daniel-mar 112
                                if ($ra_need_login && OIDplus::authUtils()->isRaLoggedIn($ra_email)) {
113
                                        $users[] = $ra_email;
114
                                } else if (!$ra_need_login) {
115
                                        $users[] = $ra_email;
116
                                }
117
                        }
118
 
119
                        // A?   Save log entry into the logbook of: A logged in admin
120
                        // Replace ? by ! if the entity does not need to be logged in
121
                        else if (preg_match('@^A([\?\!])$@ismU', $maskcode, $m)) {
116 daniel-mar 122
                                $admin_need_login = $m[1] == '?';
115 daniel-mar 123
                                if ($admin_need_login && OIDplus::authUtils()->isAdminLoggedIn()) {
124
                                        $users[] = 'admin';
125
                                } else if (!$admin_need_login) {
126
                                        $users[] = 'admin';
127
                                }
128
                        }
129
 
130
                        // Unexpected
131
                        else {
132
                                throw new Exception("Unexpected logger mask code '$maskcode'");
133
                        }
116 daniel-mar 134
                }
115 daniel-mar 135
 
117 daniel-mar 136
                // Now write the log message
137
 
150 daniel-mar 138
                $addr = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : null;
139
                OIDplus::db()->query("insert into ".OIDPLUS_TABLENAME_PREFIX."log (addr, unix_ts, event) values (?, UNIX_TIMESTAMP(), ?)", array($addr, $event));
117 daniel-mar 140
                $log_id = OIDplus::db()->insert_id();
150 daniel-mar 141
                if ($log_id === false) {
142
                        $res = OIDplus::db()->query("select max(id) as last_id from ".OIDPLUS_TABLENAME_PREFIX."log");
143
                        $row = OIDplus::db()->fetch_array($res);
144
                        $log_id = $row['last_id'];
145
                }
117 daniel-mar 146
 
147
                foreach ($objects as $object) {
150 daniel-mar 148
                        OIDplus::db()->query("insert into ".OIDPLUS_TABLENAME_PREFIX."log_object (log_id, object) values (?, ?)", array($log_id, $object));
117 daniel-mar 149
                }
150
 
151
                foreach ($users as $user) {
150 daniel-mar 152
                        OIDplus::db()->query("insert into ".OIDPLUS_TABLENAME_PREFIX."log_user (log_id, user) values (?, ?)", array($log_id, $user));
117 daniel-mar 153
                }
154
 
115 daniel-mar 155
        }
156
}