Subversion Repositories oidplus

Rev

Rev 1086 | Rev 1116 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
635 daniel-mar 1
<?php
2
 
3
/*
4
 * OIDplus 2.0
1086 daniel-mar 5
 * Copyright 2019 - 2023 Daniel Marschall, ViaThinkSoft
635 daniel-mar 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
 
1050 daniel-mar 20
namespace ViaThinkSoft\OIDplus;
635 daniel-mar 21
 
1086 daniel-mar 22
// phpcs:disable PSR1.Files.SideEffects
23
\defined('INSIDE_OIDPLUS') or die;
24
// phpcs:enable PSR1.Files.SideEffects
25
 
635 daniel-mar 26
class OIDplusLoggerPluginDatabase extends OIDplusLoggerPlugin {
27
 
28
        public static function available(&$reason)/*: bool*/ {
29
                $reason = '';
30
                return true;
31
        }
32
 
33
        public static function log($event, $users, $objects)/*: bool*/ {
34
                $addr = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '';
35
                OIDplus::dbIsolated()->query("insert into ###log (addr, unix_ts, event) values (?, ?, ?)", array($addr, time(), $event));
36
                $log_id = OIDplus::dbIsolated()->insert_id();
1114 daniel-mar 37
                if ($log_id == 0) {
635 daniel-mar 38
                        $res = OIDplus::dbIsolated()->query("select max(id) as last_id from ###log");
790 daniel-mar 39
                        if (!$res->any()) throw new OIDplusException(_L('Could not log event'));
635 daniel-mar 40
                        $row = $res->fetch_array();
41
                        $log_id = $row['last_id'];
42
                        if ($log_id == 0) throw new OIDplusException(_L('Could not log event'));
43
                }
44
 
45
                $object_dupe_check = array();
46
                foreach ($objects as list($severity, $object)) {
47
                        if (in_array($object, $object_dupe_check)) continue;
48
                        $object_dupe_check[] = $object;
49
                        OIDplus::dbIsolated()->query("insert into ###log_object (log_id, severity, object) values (?, ?, ?)", array($log_id, $severity, $object));
50
                }
51
 
52
                $user_dupe_check = array();
53
                foreach ($users as list($severity, $username)) {
54
                        if (in_array($username, $user_dupe_check)) continue;
55
                        $user_dupe_check[] = $username;
56
                        OIDplus::dbIsolated()->query("insert into ###log_user (log_id, severity, username) values (?, ?, ?)", array($log_id, $severity, $username));
57
                }
58
 
59
                return true;
60
        }
61
 
786 daniel-mar 62
}