Subversion Repositories oidplus

Rev

Rev 1224 | 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 - 2023 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. namespace ViaThinkSoft\OIDplus;
  21.  
  22. // phpcs:disable PSR1.Files.SideEffects
  23. \defined('INSIDE_OIDPLUS') or die;
  24. // phpcs:enable PSR1.Files.SideEffects
  25.  
  26. class OIDplusLoggerPluginDatabase extends OIDplusLoggerPlugin {
  27.  
  28.         /**
  29.          * @param string $reason
  30.          * @return bool
  31.          */
  32.         public function available(string &$reason): bool {
  33.                 $reason = '';
  34.                 return true;
  35.         }
  36.  
  37.         /**
  38.          * @param OIDplusLogEvent $event
  39.          * @return bool
  40.          * @throws OIDplusException
  41.          */
  42.         public function log(OIDplusLogEvent $event): bool {
  43.                 $addr = OIDplus::getClientIpAddress() ?: '';
  44.                 OIDplus::dbIsolated()->query("insert into ###log (addr, unix_ts, event) values (?, ?, ?)", array($addr, time(), $event->getMessage())); // TODO: why unix_ts? Why not a database DATETIME field?!
  45.                 $log_id = OIDplus::dbIsolated()->insert_id();
  46.                 if ($log_id === 0) {
  47.                         $log_id = OIDplus::dbIsolated()->getScalar("select max(id) as last_id from ###log");
  48.                         if (!$log_id) throw new OIDplusException(_L('Could not log event'));
  49.                 }
  50.  
  51.                 $object_dupe_check = array();
  52.                 $user_dupe_check = array();
  53.                 foreach ($event->getTargets() as $target) {
  54.                         $severity = $target->getSeverity();
  55.                         if ($target instanceof OIDplusLogTargetObject) {
  56.                                 $object = $target->getObject();
  57.                                 if (in_array($object, $object_dupe_check)) continue;
  58.                                 $object_dupe_check[] = $object;
  59.                                 OIDplus::dbIsolated()->query("insert into ###log_object (log_id, severity, object) values (?, ?, ?)", array((int)$log_id, (int)$severity, $object));
  60.                         } else if ($target instanceof OIDplusLogTargetUser) {
  61.                                 $username = $target->getUsername();
  62.                                 if (in_array($username, $user_dupe_check)) continue;
  63.                                 $user_dupe_check[] = $username;
  64.                                 OIDplus::dbIsolated()->query("insert into ###log_user (log_id, severity, username) values (?, ?, ?)", array((int)$log_id, (int)$severity, $username));
  65.                         } else {
  66.                                 assert(false);
  67.                         }
  68.                 }
  69.  
  70.                 return true;
  71.         }
  72.  
  73. }
  74.