Subversion Repositories oidplus

Rev

Rev 360 | Rev 419 | 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) {
360 daniel-mar 191
                        throw new OIDplusException(_L('Invalid maskcode "%1" (failed to split)',$maskcodes));
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:
360 daniel-mar 259
                                                throw new OIDplusException(_L('Invalid maskcode "%1" (Unknown severity "%2")',$maskcodes,$sev));
288 daniel-mar 260
                                }
261
                        }
289 daniel-mar 262
 
115 daniel-mar 263
                        // OID(x)       Save log entry into the logbook of: Object "x"
386 daniel-mar 264
                        $m = array();                  
115 daniel-mar 265
                        if (preg_match('@^OID\((.+)\)$@ismU', $maskcode, $m)) {
266
                                $object_id = $m[1];
288 daniel-mar 267
                                $objects[] = array($severity, $object_id);
360 daniel-mar 268
                                if ($object_id == '') throw new OIDplusException(_L('OID logger mask requires OID'));
115 daniel-mar 269
                        }
270
 
288 daniel-mar 271
                        // SUPOID(x)    Save log entry into the logbook of: Parent of object "x"
272
                        else if (preg_match('@^SUPOID\((.+)\)$@ismU', $maskcode, $m)) {
273
                                $object_id         = $m[1];
360 daniel-mar 274
                                if ($object_id == '') throw new OIDplusException(_L('SUPOID logger mask requires OID'));
288 daniel-mar 275
                                $obj = OIDplusObject::parse($object_id);
276
                                if ($obj) {
277
                                        $parent = $obj->getParent()->nodeId();
278
                                        $objects[] = array($severity, $parent);
279
                                } else {
360 daniel-mar 280
                                        throw new OIDplusException(_L('SUPOID logger mask: Invalid object %1',$object_id));
288 daniel-mar 281
                                }
282
                        }
283
 
115 daniel-mar 284
                        // OIDRA(x)?    Save log entry into the logbook of: Logged in RA of object "x"
288 daniel-mar 285
                        // Remove or replace "?" by "!" if the entity does not need to be logged in
115 daniel-mar 286
                        else if (preg_match('@^OIDRA\((.+)\)([\?\!])$@ismU', $maskcode, $m)) {
287
                                $object_id         = $m[1];
116 daniel-mar 288
                                $ra_need_login     = $m[2] == '?';
360 daniel-mar 289
                                if ($object_id == '') throw new OIDplusException(_L('OIDRA logger mask requires OID'));
115 daniel-mar 290
                                $obj = OIDplusObject::parse($object_id);
116 daniel-mar 291
                                if ($obj) {
292
                                        if ($ra_need_login) {
293
                                                foreach (OIDplus::authUtils()->loggedInRaList() as $ra) {
288 daniel-mar 294
                                                        if ($obj->userHasWriteRights($ra)) $users[] = array($severity_online, $ra->raEmail());
116 daniel-mar 295
                                                }
296
                                        } else {
288 daniel-mar 297
                                                // $users[] = array($severity, $obj->getRa()->raEmail());
116 daniel-mar 298
                                                foreach (OIDplusRA::getAllRAs() as $ra) {
288 daniel-mar 299
                                                        if ($obj->userHasWriteRights($ra)) $users[] = array($severity, $ra->raEmail());
116 daniel-mar 300
                                                }
115 daniel-mar 301
                                        }
288 daniel-mar 302
                                } else {
360 daniel-mar 303
                                        throw new OIDplusException(_L('OIDRA logger mask: Invalid object "%1"',$object_id));
115 daniel-mar 304
                                }
305
                        }
306
 
307
                        // SUPOIDRA(x)? Save log entry into the logbook of: Logged in RA that owns the superior object of "x"
288 daniel-mar 308
                        // Remove or replace "?" by "!" if the entity does not need to be logged in
115 daniel-mar 309
                        else if (preg_match('@^SUPOIDRA\((.+)\)([\?\!])$@ismU', $maskcode, $m)) {
310
                                $object_id         = $m[1];
116 daniel-mar 311
                                $ra_need_login     = $m[2] == '?';
360 daniel-mar 312
                                if ($object_id == '') throw new OIDplusException(_L('SUPOIDRA logger mask requires OID'));
115 daniel-mar 313
                                $obj = OIDplusObject::parse($object_id);
116 daniel-mar 314
                                if ($obj) {
315
                                        if ($ra_need_login) {
316
                                                foreach (OIDplus::authUtils()->loggedInRaList() as $ra) {
288 daniel-mar 317
                                                        if ($obj->userHasParentalWriteRights($ra)) $users[] = array($severity_online, $ra->raEmail());
116 daniel-mar 318
                                                }
319
                                        } else {
288 daniel-mar 320
                                                // $users[] = array($severity, $obj->getParent()->getRa()->raEmail());
116 daniel-mar 321
                                                foreach (OIDplusRA::getAllRAs() as $ra) {
288 daniel-mar 322
                                                        if ($obj->userHasParentalWriteRights($ra)) $users[] = array($severity, $ra->raEmail());
116 daniel-mar 323
                                                }
115 daniel-mar 324
                                        }
288 daniel-mar 325
                                } else {
360 daniel-mar 326
                                        throw new OIDplusException(_L('SUPOIDRA logger mask: Invalid object "%1"',$object_id));
115 daniel-mar 327
                                }
328
                        }
329
 
330
                        // RA(x)?       Save log entry into the logbook of: Logged in RA "x"
288 daniel-mar 331
                        // Remove or replace "?" by "!" if the entity does not need to be logged in
332
                        else if (preg_match('@^RA\((.*)\)([\?\!])$@ismU', $maskcode, $m)) {
115 daniel-mar 333
                                $ra_email          = $m[1];
116 daniel-mar 334
                                $ra_need_login     = $m[2] == '?';
288 daniel-mar 335
                                if (!empty($ra_email)) {
336
                                        if ($ra_need_login && OIDplus::authUtils()->isRaLoggedIn($ra_email)) {
337
                                                $users[] = array($severity_online, $ra_email);
338
                                        } else if (!$ra_need_login) {
339
                                                $users[] = array($severity, $ra_email);
340
                                        }
115 daniel-mar 341
                                }
342
                        }
343
 
344
                        // A?   Save log entry into the logbook of: A logged in admin
288 daniel-mar 345
                        // Remove or replace "?" by "!" if the entity does not need to be logged in
115 daniel-mar 346
                        else if (preg_match('@^A([\?\!])$@ismU', $maskcode, $m)) {
116 daniel-mar 347
                                $admin_need_login = $m[1] == '?';
115 daniel-mar 348
                                if ($admin_need_login && OIDplus::authUtils()->isAdminLoggedIn()) {
288 daniel-mar 349
                                        $users[] = array($severity_online, 'admin');
115 daniel-mar 350
                                } else if (!$admin_need_login) {
288 daniel-mar 351
                                        $users[] = array($severity, 'admin');
115 daniel-mar 352
                                }
353
                        }
354
 
355
                        // Unexpected
356
                        else {
360 daniel-mar 357
                                throw new OIDplusException(_L('Unexpected logger component "%1" in mask code "%2"',$maskcode,$maskcodes));
115 daniel-mar 358
                        }
116 daniel-mar 359
                }
115 daniel-mar 360
 
117 daniel-mar 361
                // Now write the log message
362
 
289 daniel-mar 363
                $result = false;
117 daniel-mar 364
 
289 daniel-mar 365
                foreach ($loggerPlugins as $plugin) {
366
                        $reason = '';
367
                        if ($plugin->available($reason)) {
368
                                $result |= $plugin->log($event, $users, $objects);
369
                        }
117 daniel-mar 370
                }
371
 
289 daniel-mar 372
                return $result;
117 daniel-mar 373
 
115 daniel-mar 374
        }
360 daniel-mar 375
}