Subversion Repositories oidplus

Rev

Rev 1114 | Rev 1130 | 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
 
1116 daniel-mar 28
        /**
29
         * @param string $reason
30
         * @return bool
31
         */
32
        public static function available(string &$reason): bool {
635 daniel-mar 33
                $reason = '';
34
                return true;
35
        }
36
 
1116 daniel-mar 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 {
635 daniel-mar 45
                $addr = isset($_SERVER['REMOTE_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();
1116 daniel-mar 48
                if ($log_id === 0) {
635 daniel-mar 49
                        $res = OIDplus::dbIsolated()->query("select max(id) as last_id from ###log");
790 daniel-mar 50
                        if (!$res->any()) throw new OIDplusException(_L('Could not log event'));
635 daniel-mar 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
 
786 daniel-mar 73
}