Subversion Repositories oidplus

Rev

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