Subversion Repositories oidplus

Rev

Rev 1187 | Rev 1224 | 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
         */
1185 daniel-mar 32
        public function available(string &$reason): bool {
635 daniel-mar 33
                $reason = '';
34
                return true;
35
        }
36
 
1116 daniel-mar 37
        /**
1197 daniel-mar 38
         * @param OIDplusLogEvent $event
1116 daniel-mar 39
         * @return bool
40
         * @throws OIDplusException
41
         */
1197 daniel-mar 42
        public function log(OIDplusLogEvent $event): bool {
1130 daniel-mar 43
                $addr = $_SERVER['REMOTE_ADDR'] ?? '';
1197 daniel-mar 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?!
635 daniel-mar 45
                $log_id = OIDplus::dbIsolated()->insert_id();
1116 daniel-mar 46
                if ($log_id === 0) {
635 daniel-mar 47
                        $res = OIDplus::dbIsolated()->query("select max(id) as last_id from ###log");
790 daniel-mar 48
                        if (!$res->any()) throw new OIDplusException(_L('Could not log event'));
635 daniel-mar 49
                        $row = $res->fetch_array();
50
                        $log_id = $row['last_id'];
51
                        if ($log_id == 0) throw new OIDplusException(_L('Could not log event'));
52
                }
53
 
54
                $object_dupe_check = array();
55
                $user_dupe_check = array();
1197 daniel-mar 56
                foreach ($event->getTargets() as $target) {
57
                        $severity = $target->getSeverity();
58
                        if ($target instanceof OIDplusLogTargetObject) {
59
                                $object = $target->getObject();
60
                                if (in_array($object, $object_dupe_check)) continue;
61
                                $object_dupe_check[] = $object;
62
                                OIDplus::dbIsolated()->query("insert into ###log_object (log_id, severity, object) values (?, ?, ?)", array((int)$log_id, (int)$severity, $object));
63
                        } else if ($target instanceof OIDplusLogTargetUser) {
64
                                $username = $target->getUsername();
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((int)$log_id, (int)$severity, $username));
68
                        } else {
69
                                assert(false);
70
                        }
635 daniel-mar 71
                }
72
 
73
                return true;
74
        }
75
 
786 daniel-mar 76
}