Subversion Repositories oidplus

Rev

Rev 288 | Rev 360 | 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
class OIDplusLogger {
21
 
188 daniel-mar 22
        private static function split_maskcodes($maskcodes) {
288 daniel-mar 23
                // This function splits a mask code containing multiple components
24
                // (delimited by '+' or '/') in single components
25
                // It takes care that '+' and '/' inside brackets won't be used to split the codes
26
                // Also, brackets can be escaped.
27
                // The severity block (optional, must be standing in front of a component)
28
                // is handled too. Inside the severity block, you may only use '/' to split components.
29
                // The severity block will be implicitly repeated from the previous components if a component
30
                // does not feature one.
31
                //
32
                // "[S]AAA(BBB)+CCC(DDD)"   ==> array(
33
                //                                 array(array("S"),"AAA(BBB)"),
34
                //                                 array(array("S"),"CCC(DDD)")
35
                //                              )
36
                // "[S]AAA(B+BB)+CCC(DDD)"  ==> array(
37
                //                                 array(array("S"),"AAA(B+BB)"),
38
                //                                 array(array("S"),"CCC(DDD)")
39
                //                              )
40
                // "[S]AAA(B\)BB)+CCC(DDD)" ==> array(
41
                //                                 array(array("S"),"AAA(B\)BB)"),
42
                //                                 array(array("S"),"CCC(DDD)")
43
                //                              )
289 daniel-mar 44
 
188 daniel-mar 45
                $out = array();
288 daniel-mar 46
                $sevs = array(); // Note: The severity block will repeat for the next components if not changed explicitly
289 daniel-mar 47
 
188 daniel-mar 48
                $code = '';
288 daniel-mar 49
                $sev = '';
188 daniel-mar 50
                $bracket_level = 0;
288 daniel-mar 51
                $is_escaping = false;
52
                $inside_severity_block = false;
188 daniel-mar 53
                for ($i=0; $i<strlen($maskcodes); $i++) {
54
                        $char = $maskcodes[$i];
289 daniel-mar 55
 
288 daniel-mar 56
                        if ($inside_severity_block) {
57
                                // Severity block (optional)
58
                                // e.g.  [?WARN/!OK] ==> $sevs = array("?WARN", "!OK")
59
                                if ($char == '\\') {
60
                                        if ($is_escaping) {
61
                                                $is_escaping = false;
62
                                                $sev .= $char;
63
                                        } else {
64
                                                $is_escaping = true;
65
                                        }
66
                                }
67
                                else if ($char == '[') {
68
                                        if ($is_escaping) {
69
                                                $is_escaping = false;
70
                                        } else {
71
                                                $bracket_level++;
72
                                        }
73
                                        $sev .= $char;
74
                                }
75
                                else if ($char == ']') {
76
                                        if ($is_escaping) {
77
                                                $is_escaping = false;
78
                                                $sev .= $char;
79
                                        } else {
80
                                                $bracket_level--;
81
                                                if ($bracket_level < 0) return false;
82
                                                if ($bracket_level == 0) {
83
                                                        $inside_severity_block = false;
84
                                                        if ($sev != '') $sevs[] = $sev;
85
                                                        $sev = '';
86
                                                } else {
87
                                                        $sev .= $char;
88
                                                }
89
                                        }
90
                                }
91
                                else if ((($char == '/')) && ($bracket_level == 1)) {
92
                                        if ($is_escaping) {
93
                                                $is_escaping = false;
94
                                                $sev .= $char;
95
                                        } else {
96
                                                if ($sev != '') $sevs[] = $sev;
97
                                                $sev = '';
98
                                        }
99
                                } else {
100
                                        if ($is_escaping) {
101
                                                // This would actually be an error, because we cannot escape this
102
                                                $is_escaping = false;
103
                                                $sev .= '\\' . $char;
104
                                        } else {
105
                                                $sev .= $char;
106
                                        }
107
                                }
188 daniel-mar 108
                        } else {
288 daniel-mar 109
                                // Normal data (after the severity block)
110
                                if (($char == '[') && ($code == '')) {
111
                                        $inside_severity_block = true;
112
                                        $bracket_level++;
113
                                        $sevs = array();
114
                                }
115
                                else if ($char == '\\') {
116
                                        if ($is_escaping) {
117
                                                $is_escaping = false;
118
                                                $code .= $char;
119
                                        } else {
120
                                                $is_escaping = true;
121
                                        }
122
                                }
123
                                else if ($char == '(') {
124
                                        if ($is_escaping) {
125
                                                $is_escaping = false;
126
                                        } else {
127
                                                $bracket_level++;
128
                                        }
129
                                        $code .= $char;
130
                                }
131
                                else if ($char == ')') {
132
                                        if ($is_escaping) {
133
                                                $is_escaping = false;
134
                                        } else {
135
                                                $bracket_level--;
136
                                                if ($bracket_level < 0) return false;
137
                                        }
138
                                        $code .= $char;
139
                                }
140
                                else if ((($char == '+') || ($char == '/')) && ($bracket_level == 0)) {
141
                                        if ($is_escaping) {
142
                                                $is_escaping = false;
143
                                                $code .= $char;
144
                                        } else {
145
                                                if ($code != '') $out[] = array($sevs,$code);
146
                                                $code = '';
147
                                        }
148
                                } else {
149
                                        if ($is_escaping) {
150
                                                // This would actually be an error, because we cannot escape this
151
                                                $is_escaping = false;
152
                                                $code .= '\\' . $char;
153
                                        } else {
154
                                                $code .= $char;
155
                                        }
156
                                }
188 daniel-mar 157
                        }
158
                }
288 daniel-mar 159
                if ($code != '') $out[] = array($sevs,$code);
160
                if ($inside_severity_block) return false;
188 daniel-mar 161
 
162
                return $out;
163
        }
164
 
263 daniel-mar 165
        public static function log($maskcodes, $event) {
289 daniel-mar 166
                $loggerPlugins = OIDplus::getLoggerPlugins();
167
                if (count($loggerPlugins) == 0) return false;
168
 
288 daniel-mar 169
                // What is a mask code?
170
                // A mask code gives information about the log event:
171
                // 1. The severity (info, warning, error)
172
                // 2. In which logbook(s) the event shall be placed
173
                // Example:
174
                // The event would be:
175
                // "Person 'X' moves from house 'A' to house 'B'"
176
                // This event would affect the person X and the two houses,
177
                // so, instead of logging into 3 logbooks separately,
178
                // you would create a mask code that tells the system
179
                // to put the message into the logbooks of person X,
180
                // house A and house B.
115 daniel-mar 181
 
182
                $users = array();
183
                $objects = array();
184
 
288 daniel-mar 185
                // A mask code with multiple components is split into single codes
186
                // using '+' or '/', e.g. "OID(x)+RA(x)" would be split to "OID(x)" and "RA(x)"
187
                // which would result in the message being placed in the logbook of OID x,
188
                // and the logbook of the RA owning OID x.
188 daniel-mar 189
                $maskcodes_ary = self::split_maskcodes($maskcodes);
190
                if ($maskcodes_ary === false) {
288 daniel-mar 191
                        throw new OIDplusException("Invalid maskcode '$maskcodes' (failed to split)");
188 daniel-mar 192
                }
288 daniel-mar 193
                foreach ($maskcodes_ary as list($sevs,$maskcode)) {
194
                        // At the beginning of each mask code, you can define a severity.
195
                        // If you have a mask code with multiple components, you don't have to place the
196
                        // severity for each component. You can just leave it at the beginning.
197
                        // e.g. "[WARN]OID(x)+RA(x)" is equal to "[WARN]OID(x)+[WARN]RA(x)"
198
                        // You can also put different severities for the components:
199
                        // e.g. "[INFO]OID(x)+[WARN]RA(x)" would be a info for the OID, but a warning for the RA.
200
                        // If you want to make the severity dependent on wheather the user is logged in or not,
201
                        // prepend "?" or "!" and use '/' as delimiter
202
                        // Example: "[?WARN/!OK]RA(x)" means: If RA is not logged in, it is a warning; if it is logged in, it is an success
203
                        $severity = 0; // default severity = none
289 daniel-mar 204
                        $severity_online = 0;
288 daniel-mar 205
                        foreach ($sevs as $sev) {
206
                                switch (strtoupper($sev)) {
207
                                        // [OK]   = Success
208
                                        //          Numeric value: 1
209
                                        //          Rule of thumb: YOU have done something and it was successful
210
                                        case '?OK':
211
                                                $severity_online = 1;
212
                                                break;
213
                                        case '!OK':
214
                                        case  'OK':
215
                                                $severity = 1;
216
                                                break;
217
                                        // [INFO] = Informational
218
                                        //          Numeric value: 2
219
                                        //          Rule of thumb: Someone else has done something (that affects you) and it was successful
220
                                        case '?INFO':
221
                                                $severity_online = 2;
222
                                                break;
223
                                        case '!INFO':
224
                                        case  'INFO':
225
                                                $severity = 2;
226
                                                break;
227
                                        // [WARN] = Warning
228
                                        //          Numeric value: 3
229
                                        //          Rule of thumb: Something happened (probably someone did something) and it affects you
230
                                        case '?WARN':
231
                                                $severity_online = 3;
232
                                                break;
233
                                        case '!WARN':
234
                                        case  'WARN':
235
                                                $severity = 3;
236
                                                break;
237
                                        // [ERR]  = Error
238
                                        //          Numeric value: 4
239
                                        //          Rule of thumb: Something failed (probably someone did something) and it affects you
240
                                        case '?ERR':
241
                                                $severity_online = 4;
242
                                                break;
243
                                        case '!ERR':
244
                                        case  'ERR':
245
                                                $severity = 4;
246
                                                break;
247
                                        // [CRIT] = Critical
248
                                        //          Numeric value: 5
249
                                        //          Rule of thumb: Something happened (probably someone did something) which is not an error,
250
                                        //          but some critical situation (e.g. hardware failure), and it affects you
251
                                        case '?CRIT':
252
                                                $severity_online = 5;
253
                                                break;
254
                                        case '!CRIT':
255
                                        case  'CRIT':
256
                                                $severity = 5;
257
                                                break;
258
                                        default:
259
                                                throw new OIDplusException("Invalid maskcode '$maskcodes' (Unknown severity '$sev')");
260
                                }
261
                        }
289 daniel-mar 262
 
115 daniel-mar 263
                        // OID(x)       Save log entry into the logbook of: Object "x"
264
                        if (preg_match('@^OID\((.+)\)$@ismU', $maskcode, $m)) {
265
                                $object_id = $m[1];
288 daniel-mar 266
                                $objects[] = array($severity, $object_id);
250 daniel-mar 267
                                if ($object_id == '') throw new OIDplusException("OID logger mask requires OID");
115 daniel-mar 268
                        }
269
 
288 daniel-mar 270
                        // SUPOID(x)    Save log entry into the logbook of: Parent of object "x"
271
                        else if (preg_match('@^SUPOID\((.+)\)$@ismU', $maskcode, $m)) {
272
                                $object_id         = $m[1];
273
                                if ($object_id == '') throw new OIDplusException("SUPOID logger mask requires OID");
274
                                $obj = OIDplusObject::parse($object_id);
275
                                if ($obj) {
276
                                        $parent = $obj->getParent()->nodeId();
277
                                        $objects[] = array($severity, $parent);
278
                                } else {
279
                                        throw new OIDplusException("SUPOID logger mask: Invalid object '$object_id'");
280
                                }
281
                        }
282
 
115 daniel-mar 283
                        // OIDRA(x)?    Save log entry into the logbook of: Logged in RA of object "x"
288 daniel-mar 284
                        // Remove or replace "?" by "!" if the entity does not need to be logged in
115 daniel-mar 285
                        else if (preg_match('@^OIDRA\((.+)\)([\?\!])$@ismU', $maskcode, $m)) {
286
                                $object_id         = $m[1];
116 daniel-mar 287
                                $ra_need_login     = $m[2] == '?';
250 daniel-mar 288
                                if ($object_id == '') throw new OIDplusException("OIDRA logger mask requires OID");
115 daniel-mar 289
                                $obj = OIDplusObject::parse($object_id);
116 daniel-mar 290
                                if ($obj) {
291
                                        if ($ra_need_login) {
292
                                                foreach (OIDplus::authUtils()->loggedInRaList() as $ra) {
288 daniel-mar 293
                                                        if ($obj->userHasWriteRights($ra)) $users[] = array($severity_online, $ra->raEmail());
116 daniel-mar 294
                                                }
295
                                        } else {
288 daniel-mar 296
                                                // $users[] = array($severity, $obj->getRa()->raEmail());
116 daniel-mar 297
                                                foreach (OIDplusRA::getAllRAs() as $ra) {
288 daniel-mar 298
                                                        if ($obj->userHasWriteRights($ra)) $users[] = array($severity, $ra->raEmail());
116 daniel-mar 299
                                                }
115 daniel-mar 300
                                        }
288 daniel-mar 301
                                } else {
302
                                        throw new OIDplusException("OIDRA logger mask: Invalid object '$object_id'");
115 daniel-mar 303
                                }
304
                        }
305
 
306
                        // SUPOIDRA(x)? Save log entry into the logbook of: Logged in RA that owns the superior object of "x"
288 daniel-mar 307
                        // Remove or replace "?" by "!" if the entity does not need to be logged in
115 daniel-mar 308
                        else if (preg_match('@^SUPOIDRA\((.+)\)([\?\!])$@ismU', $maskcode, $m)) {
309
                                $object_id         = $m[1];
116 daniel-mar 310
                                $ra_need_login     = $m[2] == '?';
250 daniel-mar 311
                                if ($object_id == '') throw new OIDplusException("SUPOIDRA logger mask requires OID");
115 daniel-mar 312
                                $obj = OIDplusObject::parse($object_id);
116 daniel-mar 313
                                if ($obj) {
314
                                        if ($ra_need_login) {
315
                                                foreach (OIDplus::authUtils()->loggedInRaList() as $ra) {
288 daniel-mar 316
                                                        if ($obj->userHasParentalWriteRights($ra)) $users[] = array($severity_online, $ra->raEmail());
116 daniel-mar 317
                                                }
318
                                        } else {
288 daniel-mar 319
                                                // $users[] = array($severity, $obj->getParent()->getRa()->raEmail());
116 daniel-mar 320
                                                foreach (OIDplusRA::getAllRAs() as $ra) {
288 daniel-mar 321
                                                        if ($obj->userHasParentalWriteRights($ra)) $users[] = array($severity, $ra->raEmail());
116 daniel-mar 322
                                                }
115 daniel-mar 323
                                        }
288 daniel-mar 324
                                } else {
325
                                        throw new OIDplusException("SUPOIDRA logger mask: Invalid object '$object_id'");
115 daniel-mar 326
                                }
327
                        }
328
 
329
                        // RA(x)?       Save log entry into the logbook of: Logged in RA "x"
288 daniel-mar 330
                        // Remove or replace "?" by "!" if the entity does not need to be logged in
331
                        else if (preg_match('@^RA\((.*)\)([\?\!])$@ismU', $maskcode, $m)) {
115 daniel-mar 332
                                $ra_email          = $m[1];
116 daniel-mar 333
                                $ra_need_login     = $m[2] == '?';
288 daniel-mar 334
                                if (!empty($ra_email)) {
335
                                        if ($ra_need_login && OIDplus::authUtils()->isRaLoggedIn($ra_email)) {
336
                                                $users[] = array($severity_online, $ra_email);
337
                                        } else if (!$ra_need_login) {
338
                                                $users[] = array($severity, $ra_email);
339
                                        }
115 daniel-mar 340
                                }
341
                        }
342
 
343
                        // A?   Save log entry into the logbook of: A logged in admin
288 daniel-mar 344
                        // Remove or replace "?" by "!" if the entity does not need to be logged in
115 daniel-mar 345
                        else if (preg_match('@^A([\?\!])$@ismU', $maskcode, $m)) {
116 daniel-mar 346
                                $admin_need_login = $m[1] == '?';
115 daniel-mar 347
                                if ($admin_need_login && OIDplus::authUtils()->isAdminLoggedIn()) {
288 daniel-mar 348
                                        $users[] = array($severity_online, 'admin');
115 daniel-mar 349
                                } else if (!$admin_need_login) {
288 daniel-mar 350
                                        $users[] = array($severity, 'admin');
115 daniel-mar 351
                                }
352
                        }
353
 
354
                        // Unexpected
355
                        else {
288 daniel-mar 356
                                throw new OIDplusException("Unexpected logger component '$maskcode' in mask code '$maskcodes'");
115 daniel-mar 357
                        }
116 daniel-mar 358
                }
115 daniel-mar 359
 
117 daniel-mar 360
                // Now write the log message
361
 
289 daniel-mar 362
                $result = false;
117 daniel-mar 363
 
289 daniel-mar 364
                foreach ($loggerPlugins as $plugin) {
365
                        $reason = '';
366
                        if ($plugin->available($reason)) {
367
                                $result |= $plugin->log($event, $users, $objects);
368
                        }
117 daniel-mar 369
                }
370
 
289 daniel-mar 371
                return $result;
117 daniel-mar 372
 
115 daniel-mar 373
        }
374
}