Subversion Repositories oidplus

Rev

Rev 419 | Rev 730 | 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 - 2021 Daniel Marschall, ViaThinkSoft
  6.  *
  7.  * Licensed under the Apache License, Version 2.0 (the "License");
  8.  * you may not use this file except in compliance with the License.
  9.  * You may obtain a copy of the License at
  10.  *
  11.  *     http://www.apache.org/licenses/LICENSE-2.0
  12.  *
  13.  * Unless required by applicable law or agreed to in writing, software
  14.  * distributed under the License is distributed on an "AS IS" BASIS,
  15.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16.  * See the License for the specific language governing permissions and
  17.  * limitations under the License.
  18.  */
  19.  
  20. if (!defined('INSIDE_OIDPLUS')) die();
  21.  
  22. class OIDplusLogger {
  23.  
  24.         private static function split_maskcodes($maskcodes) {
  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.                 //                              )
  46.  
  47.                 $out = array();
  48.                 $sevs = array(); // Note: The severity block will repeat for the next components if not changed explicitly
  49.  
  50.                 $code = '';
  51.                 $sev = '';
  52.                 $bracket_level = 0;
  53.                 $is_escaping = false;
  54.                 $inside_severity_block = false;
  55.                 for ($i=0; $i<strlen($maskcodes); $i++) {
  56.                         $char = $maskcodes[$i];
  57.  
  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.                                 }
  110.                         } else {
  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.                                 }
  159.                         }
  160.                 }
  161.                 if ($code != '') $out[] = array($sevs,$code);
  162.                 if ($inside_severity_block) return false;
  163.  
  164.                 return $out;
  165.         }
  166.  
  167.         public static function log($maskcodes, $event) {
  168.                 $loggerPlugins = OIDplus::getLoggerPlugins();
  169.                 if (count($loggerPlugins) == 0) return false;
  170.  
  171.                 // What is a mask code?
  172.                 // A mask code gives information about the log event:
  173.                 // 1. The severity (info, warning, error)
  174.                 // 2. In which logbook(s) the event shall be placed
  175.                 // Example:
  176.                 // The event would be:
  177.                 // "Person 'X' moves from house 'A' to house 'B'"
  178.                 // This event would affect the person X and the two houses,
  179.                 // so, instead of logging into 3 logbooks separately,
  180.                 // you would create a mask code that tells the system
  181.                 // to put the message into the logbooks of person X,
  182.                 // house A and house B.
  183.  
  184.                 $users = array();
  185.                 $objects = array();
  186.  
  187.                 // A mask code with multiple components is split into single codes
  188.                 // using '+' or '/', e.g. "OID(x)+RA(x)" would be split to "OID(x)" and "RA(x)"
  189.                 // which would result in the message being placed in the logbook of OID x,
  190.                 // and the logbook of the RA owning OID x.
  191.                 $maskcodes_ary = self::split_maskcodes($maskcodes);
  192.                 if ($maskcodes_ary === false) {
  193.                         throw new OIDplusException(_L('Invalid maskcode "%1" (failed to split)',$maskcodes));
  194.                 }
  195.                 foreach ($maskcodes_ary as list($sevs,$maskcode)) {
  196.                         // At the beginning of each mask code, you can define a severity.
  197.                         // If you have a mask code with multiple components, you don't have to place the
  198.                         // severity for each component. You can just leave it at the beginning.
  199.                         // e.g. "[WARN]OID(x)+RA(x)" is equal to "[WARN]OID(x)+[WARN]RA(x)"
  200.                         // You can also put different severities for the components:
  201.                         // e.g. "[INFO]OID(x)+[WARN]RA(x)" would be a info for the OID, but a warning for the RA.
  202.                         // If you want to make the severity dependent on wheather the user is logged in or not,
  203.                         // prepend "?" or "!" and use '/' as delimiter
  204.                         // 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
  205.                         $severity = 0; // default severity = none
  206.                         $severity_online = 0;
  207.                         foreach ($sevs as $sev) {
  208.                                 switch (strtoupper($sev)) {
  209.                                         // [OK]   = Success
  210.                                         //          Numeric value: 1
  211.                                         //          Rule of thumb: YOU have done something and it was successful
  212.                                         case '?OK':
  213.                                                 $severity_online = 1;
  214.                                                 break;
  215.                                         case '!OK':
  216.                                         case  'OK':
  217.                                                 $severity = 1;
  218.                                                 break;
  219.                                         // [INFO] = Informational
  220.                                         //          Numeric value: 2
  221.                                         //          Rule of thumb: Someone else has done something (that affects you) and it was successful
  222.                                         case '?INFO':
  223.                                                 $severity_online = 2;
  224.                                                 break;
  225.                                         case '!INFO':
  226.                                         case  'INFO':
  227.                                                 $severity = 2;
  228.                                                 break;
  229.                                         // [WARN] = Warning
  230.                                         //          Numeric value: 3
  231.                                         //          Rule of thumb: Something happened (probably someone did something) and it affects you
  232.                                         case '?WARN':
  233.                                                 $severity_online = 3;
  234.                                                 break;
  235.                                         case '!WARN':
  236.                                         case  'WARN':
  237.                                                 $severity = 3;
  238.                                                 break;
  239.                                         // [ERR]  = Error
  240.                                         //          Numeric value: 4
  241.                                         //          Rule of thumb: Something failed (probably someone did something) and it affects you
  242.                                         case '?ERR':
  243.                                                 $severity_online = 4;
  244.                                                 break;
  245.                                         case '!ERR':
  246.                                         case  'ERR':
  247.                                                 $severity = 4;
  248.                                                 break;
  249.                                         // [CRIT] = Critical
  250.                                         //          Numeric value: 5
  251.                                         //          Rule of thumb: Something happened (probably someone did something) which is not an error,
  252.                                         //          but some critical situation (e.g. hardware failure), and it affects you
  253.                                         case '?CRIT':
  254.                                                 $severity_online = 5;
  255.                                                 break;
  256.                                         case '!CRIT':
  257.                                         case  'CRIT':
  258.                                                 $severity = 5;
  259.                                                 break;
  260.                                         default:
  261.                                                 throw new OIDplusException(_L('Invalid maskcode "%1" (Unknown severity "%2")',$maskcodes,$sev));
  262.                                 }
  263.                         }
  264.  
  265.                         // OID(x)       Save log entry into the logbook of: Object "x"
  266.                         $m = array();
  267.                         if (preg_match('@^OID\((.+)\)$@ismU', $maskcode, $m)) {
  268.                                 $object_id = $m[1];
  269.                                 $objects[] = array($severity, $object_id);
  270.                                 if ($object_id == '') throw new OIDplusException(_L('OID logger mask requires OID'));
  271.                         }
  272.  
  273.                         // SUPOID(x)    Save log entry into the logbook of: Parent of object "x"
  274.                         else if (preg_match('@^SUPOID\((.+)\)$@ismU', $maskcode, $m)) {
  275.                                 $object_id         = $m[1];
  276.                                 if ($object_id == '') throw new OIDplusException(_L('SUPOID logger mask requires OID'));
  277.                                 $obj = OIDplusObject::parse($object_id);
  278.                                 if ($obj) {
  279.                                         if ($objParent = $obj->getParent()) {
  280.                                                 $parent = $objParent->nodeId();
  281.                                                 $objects[] = array($severity, $parent);
  282.                                         } else {
  283.                                                 //throw new OIDplusException(_L('%1 has no parent',$object_id));
  284.                                         }
  285.                                 } else {
  286.                                         throw new OIDplusException(_L('SUPOID logger mask: Invalid object %1',$object_id));
  287.                                 }
  288.                         }
  289.  
  290.                         // OIDRA(x)?    Save log entry into the logbook of: Logged in RA of object "x"
  291.                         // Remove or replace "?" by "!" if the entity does not need to be logged in
  292.                         else if (preg_match('@^OIDRA\((.+)\)([\?\!])$@ismU', $maskcode, $m)) {
  293.                                 $object_id         = $m[1];
  294.                                 $ra_need_login     = $m[2] == '?';
  295.                                 if ($object_id == '') throw new OIDplusException(_L('OIDRA logger mask requires OID'));
  296.                                 $obj = OIDplusObject::parse($object_id);
  297.                                 if ($obj) {
  298.                                         if ($ra_need_login) {
  299.                                                 foreach (OIDplus::authUtils()->loggedInRaList() as $ra) {
  300.                                                         if ($obj->userHasWriteRights($ra)) $users[] = array($severity_online, $ra->raEmail());
  301.                                                 }
  302.                                         } else {
  303.                                                 // $users[] = array($severity, $obj->getRa()->raEmail());
  304.                                                 foreach (OIDplusRA::getAllRAs() as $ra) {
  305.                                                         if ($obj->userHasWriteRights($ra)) $users[] = array($severity, $ra->raEmail());
  306.                                                 }
  307.                                         }
  308.                                 } else {
  309.                                         throw new OIDplusException(_L('OIDRA logger mask: Invalid object "%1"',$object_id));
  310.                                 }
  311.                         }
  312.  
  313.                         // SUPOIDRA(x)? Save log entry into the logbook of: Logged in RA that owns the superior object of "x"
  314.                         // Remove or replace "?" by "!" if the entity does not need to be logged in
  315.                         else if (preg_match('@^SUPOIDRA\((.+)\)([\?\!])$@ismU', $maskcode, $m)) {
  316.                                 $object_id         = $m[1];
  317.                                 $ra_need_login     = $m[2] == '?';
  318.                                 if ($object_id == '') throw new OIDplusException(_L('SUPOIDRA logger mask requires OID'));
  319.                                 $obj = OIDplusObject::parse($object_id);
  320.                                 if ($obj) {
  321.                                         if ($ra_need_login) {
  322.                                                 foreach (OIDplus::authUtils()->loggedInRaList() as $ra) {
  323.                                                         if ($obj->userHasParentalWriteRights($ra)) $users[] = array($severity_online, $ra->raEmail());
  324.                                                 }
  325.                                         } else {
  326.                                                 if ($objParent = $obj->getParent()) {
  327.                                                         // $users[] = array($severity, $objParent->getRa()->raEmail());
  328.                                                         foreach (OIDplusRA::getAllRAs() as $ra) {
  329.                                                                 if ($obj->userHasParentalWriteRights($ra)) $users[] = array($severity, $ra->raEmail());
  330.                                                         }
  331.                                                 } else {
  332.                                                         //throw new OIDplusException(_L('%1 has no parent, therefore also no parent RA',$object_id));
  333.                                                 }
  334.                                         }
  335.                                 } else {
  336.                                         throw new OIDplusException(_L('SUPOIDRA logger mask: Invalid object "%1"',$object_id));
  337.                                 }
  338.                         }
  339.  
  340.                         // RA(x)?       Save log entry into the logbook of: Logged in RA "x"
  341.                         // Remove or replace "?" by "!" if the entity does not need to be logged in
  342.                         else if (preg_match('@^RA\((.*)\)([\?\!])$@ismU', $maskcode, $m)) {
  343.                                 $ra_email          = $m[1];
  344.                                 $ra_need_login     = $m[2] == '?';
  345.                                 if (!empty($ra_email)) {
  346.                                         if ($ra_need_login && OIDplus::authUtils()->isRaLoggedIn($ra_email)) {
  347.                                                 $users[] = array($severity_online, $ra_email);
  348.                                         } else if (!$ra_need_login) {
  349.                                                 $users[] = array($severity, $ra_email);
  350.                                         }
  351.                                 }
  352.                         }
  353.  
  354.                         // A?   Save log entry into the logbook of: A logged in admin
  355.                         // Remove or replace "?" by "!" if the entity does not need to be logged in
  356.                         else if (preg_match('@^A([\?\!])$@ismU', $maskcode, $m)) {
  357.                                 $admin_need_login = $m[1] == '?';
  358.                                 if ($admin_need_login && OIDplus::authUtils()->isAdminLoggedIn()) {
  359.                                         $users[] = array($severity_online, 'admin');
  360.                                 } else if (!$admin_need_login) {
  361.                                         $users[] = array($severity, 'admin');
  362.                                 }
  363.                         }
  364.  
  365.                         // Unexpected
  366.                         else {
  367.                                 throw new OIDplusException(_L('Unexpected logger component "%1" in mask code "%2"',$maskcode,$maskcodes));
  368.                         }
  369.                 }
  370.  
  371.                 // Now write the log message
  372.  
  373.                 $result = false;
  374.  
  375.                 foreach ($loggerPlugins as $plugin) {
  376.                         $reason = '';
  377.                         if ($plugin->available($reason)) {
  378.                                 $result |= $plugin->log($event, $users, $objects);
  379.                         }
  380.                 }
  381.  
  382.                 return $result;
  383.  
  384.         }
  385. }