Subversion Repositories oidplus

Rev

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

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