Subversion Repositories oidplus

Rev

Rev 1116 | Rev 1172 | 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 static function available(string &$reason): bool {
  33.                 $reason = '';
  34.                 return true;
  35.         }
  36.  
  37.         /**
  38.          * @param string $event
  39.          * @param array $users
  40.          * @param array $objects
  41.          * @return bool
  42.          * @throws OIDplusException
  43.          */
  44.         public static function log(string $event, array $users, array $objects): bool {
  45.                 $addr = $_SERVER['REMOTE_ADDR'] ?? '';
  46.                 OIDplus::dbIsolated()->query("insert into ###log (addr, unix_ts, event) values (?, ?, ?)", array($addr, time(), $event));
  47.                 $log_id = OIDplus::dbIsolated()->insert_id();
  48.                 if ($log_id === 0) {
  49.                         $res = OIDplus::dbIsolated()->query("select max(id) as last_id from ###log");
  50.                         if (!$res->any()) throw new OIDplusException(_L('Could not log event'));
  51.                         $row = $res->fetch_array();
  52.                         $log_id = $row['last_id'];
  53.                         if ($log_id == 0) throw new OIDplusException(_L('Could not log event'));
  54.                 }
  55.  
  56.                 $object_dupe_check = array();
  57.                 foreach ($objects as list($severity, $object)) {
  58.                         if (in_array($object, $object_dupe_check)) continue;
  59.                         $object_dupe_check[] = $object;
  60.                         OIDplus::dbIsolated()->query("insert into ###log_object (log_id, severity, object) values (?, ?, ?)", array($log_id, $severity, $object));
  61.                 }
  62.  
  63.                 $user_dupe_check = array();
  64.                 foreach ($users as list($severity, $username)) {
  65.                         if (in_array($username, $user_dupe_check)) continue;
  66.                         $user_dupe_check[] = $username;
  67.                         OIDplus::dbIsolated()->query("insert into ###log_user (log_id, severity, username) values (?, ?, ?)", array($log_id, $severity, $username));
  68.                 }
  69.  
  70.                 return true;
  71.         }
  72.  
  73. }
  74.