Subversion Repositories oidplus

Rev

Rev 360 | Rev 419 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  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.  
  22.         private static function split_maskcodes($maskcodes) {
  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.                 //                              )
  44.  
  45.                 $out = array();
  46.                 $sevs = array(); // Note: The severity block will repeat for the next components if not changed explicitly
  47.  
  48.                 $code = '';
  49.                 $sev = '';
  50.                 $bracket_level = 0;
  51.                 $is_escaping = false;
  52.                 $inside_severity_block = false;
  53.                 for ($i=0; $i<strlen($maskcodes); $i++) {
  54.                         $char = $maskcodes[$i];
  55.  
  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.                                 }
  108.                         } else {
  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.                                 }
  157.                         }
  158.                 }
  159.                 if ($code != '') $out[] = array($sevs,$code);
  160.                 if ($inside_severity_block) return false;
  161.  
  162.                 return $out;
  163.         }
  164.  
  165.         public static function log($maskcodes, $event) {
  166.                 $loggerPlugins = OIDplus::getLoggerPlugins();
  167.                 if (count($loggerPlugins) == 0) return false;
  168.  
  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.
  181.  
  182.                 $users = array();
  183.                 $objects = array();
  184.  
  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.
  189.                 $maskcodes_ary = self::split_maskcodes($maskcodes);
  190.                 if ($maskcodes_ary === false) {
  191.                         throw new OIDplusException(_L('Invalid maskcode "%1" (failed to split)',$maskcodes));
  192.                 }
  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
  204.                         $severity_online = 0;
  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(_L('Invalid maskcode "%1" (Unknown severity "%2")',$maskcodes,$sev));
  260.                                 }
  261.                         }
  262.  
  263.                         // OID(x)       Save log entry into the logbook of: Object "x"
  264.                         $m = array();                  
  265.                         if (preg_match('@^OID\((.+)\)$@ismU', $maskcode, $m)) {
  266.                                 $object_id = $m[1];
  267.                                 $objects[] = array($severity, $object_id);
  268.                                 if ($object_id == '') throw new OIDplusException(_L('OID logger mask requires OID'));
  269.                         }
  270.  
  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];
  274.                                 if ($object_id == '') throw new OIDplusException(_L('SUPOID logger mask requires OID'));
  275.                                 $obj = OIDplusObject::parse($object_id);
  276.                                 if ($obj) {
  277.                                         $parent = $obj->getParent()->nodeId();
  278.                                         $objects[] = array($severity, $parent);
  279.                                 } else {
  280.                                         throw new OIDplusException(_L('SUPOID logger mask: Invalid object %1',$object_id));
  281.                                 }
  282.                         }
  283.  
  284.                         // OIDRA(x)?    Save log entry into the logbook of: Logged in RA of object "x"
  285.                         // Remove or replace "?" by "!" if the entity does not need to be logged in
  286.                         else if (preg_match('@^OIDRA\((.+)\)([\?\!])$@ismU', $maskcode, $m)) {
  287.                                 $object_id         = $m[1];
  288.                                 $ra_need_login     = $m[2] == '?';
  289.                                 if ($object_id == '') throw new OIDplusException(_L('OIDRA logger mask requires OID'));
  290.                                 $obj = OIDplusObject::parse($object_id);
  291.                                 if ($obj) {
  292.                                         if ($ra_need_login) {
  293.                                                 foreach (OIDplus::authUtils()->loggedInRaList() as $ra) {
  294.                                                         if ($obj->userHasWriteRights($ra)) $users[] = array($severity_online, $ra->raEmail());
  295.                                                 }
  296.                                         } else {
  297.                                                 // $users[] = array($severity, $obj->getRa()->raEmail());
  298.                                                 foreach (OIDplusRA::getAllRAs() as $ra) {
  299.                                                         if ($obj->userHasWriteRights($ra)) $users[] = array($severity, $ra->raEmail());
  300.                                                 }
  301.                                         }
  302.                                 } else {
  303.                                         throw new OIDplusException(_L('OIDRA logger mask: Invalid object "%1"',$object_id));
  304.                                 }
  305.                         }
  306.  
  307.                         // SUPOIDRA(x)? Save log entry into the logbook of: Logged in RA that owns the superior object of "x"
  308.                         // Remove or replace "?" by "!" if the entity does not need to be logged in
  309.                         else if (preg_match('@^SUPOIDRA\((.+)\)([\?\!])$@ismU', $maskcode, $m)) {
  310.                                 $object_id         = $m[1];
  311.                                 $ra_need_login     = $m[2] == '?';
  312.                                 if ($object_id == '') throw new OIDplusException(_L('SUPOIDRA logger mask requires OID'));
  313.                                 $obj = OIDplusObject::parse($object_id);
  314.                                 if ($obj) {
  315.                                         if ($ra_need_login) {
  316.                                                 foreach (OIDplus::authUtils()->loggedInRaList() as $ra) {
  317.                                                         if ($obj->userHasParentalWriteRights($ra)) $users[] = array($severity_online, $ra->raEmail());
  318.                                                 }
  319.                                         } else {
  320.                                                 // $users[] = array($severity, $obj->getParent()->getRa()->raEmail());
  321.                                                 foreach (OIDplusRA::getAllRAs() as $ra) {
  322.                                                         if ($obj->userHasParentalWriteRights($ra)) $users[] = array($severity, $ra->raEmail());
  323.                                                 }
  324.                                         }
  325.                                 } else {
  326.                                         throw new OIDplusException(_L('SUPOIDRA logger mask: Invalid object "%1"',$object_id));
  327.                                 }
  328.                         }
  329.  
  330.                         // RA(x)?       Save log entry into the logbook of: Logged in RA "x"
  331.                         // Remove or replace "?" by "!" if the entity does not need to be logged in
  332.                         else if (preg_match('@^RA\((.*)\)([\?\!])$@ismU', $maskcode, $m)) {
  333.                                 $ra_email          = $m[1];
  334.                                 $ra_need_login     = $m[2] == '?';
  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.                                         }
  341.                                 }
  342.                         }
  343.  
  344.                         // A?   Save log entry into the logbook of: A logged in admin
  345.                         // Remove or replace "?" by "!" if the entity does not need to be logged in
  346.                         else if (preg_match('@^A([\?\!])$@ismU', $maskcode, $m)) {
  347.                                 $admin_need_login = $m[1] == '?';
  348.                                 if ($admin_need_login && OIDplus::authUtils()->isAdminLoggedIn()) {
  349.                                         $users[] = array($severity_online, 'admin');
  350.                                 } else if (!$admin_need_login) {
  351.                                         $users[] = array($severity, 'admin');
  352.                                 }
  353.                         }
  354.  
  355.                         // Unexpected
  356.                         else {
  357.                                 throw new OIDplusException(_L('Unexpected logger component "%1" in mask code "%2"',$maskcode,$maskcodes));
  358.                         }
  359.                 }
  360.  
  361.                 // Now write the log message
  362.  
  363.                 $result = false;
  364.  
  365.                 foreach ($loggerPlugins as $plugin) {
  366.                         $reason = '';
  367.                         if ($plugin->available($reason)) {
  368.                                 $result |= $plugin->log($event, $users, $objects);
  369.                         }
  370.                 }
  371.  
  372.                 return $result;
  373.  
  374.         }
  375. }