Subversion Repositories oidplus

Rev

Rev 1185 | 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 OIDplusLoggerPluginLinuxSyslog 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
                if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
34
                        $reason = _L('Functionality not available on Windows');
35
                        return false;
36
                }
37
 
38
                if (!@file_exists('/var/log/syslog')) {
39
                        $reason = _L('File %1 does not exist','/var/log/syslog');
40
                        return false;
41
                }
42
 
43
                if (@file_put_contents('/var/log/syslog', '', FILE_APPEND) === false) {
44
                        $reason = _L('File %1 is not writeable','/var/log/syslog');
45
                        return false;
46
                }
47
 
48
                $reason = '';
49
                return true;
50
        }
51
 
1116 daniel-mar 52
        /**
1197 daniel-mar 53
         * @param OIDplusLogEvent $event
1116 daniel-mar 54
         * @return bool
55
         */
1197 daniel-mar 56
        public function log(OIDplusLogEvent $event): bool {
635 daniel-mar 57
                if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') return false;
58
 
59
                if (!@file_exists('/var/log/syslog')) return false;
60
 
61
                $users_names = array();
1197 daniel-mar 62
                $objects_names = array();
63
                foreach ($event->getTargets() as $target) {
64
                        if ($target instanceof OIDplusLogTargetUser) {
65
                                $users_names[] = $target->getUsername();
66
                        } else if ($target instanceof OIDplusLogTargetObject) {
67
                                $objects_names[] = $target->getObject();
68
                        } else {
69
                                assert(false);
70
                        }
71
                }
635 daniel-mar 72
                $users_info = count($users_names) == 0 ? '' : ' ('._L('affected users: %1',implode(', ',$users_names)).')';
73
                $objects_info = count($objects_names) == 0 ? '' : ' ('._L('affected objects: %1',implode(', ',$objects_names)).')';
74
 
75
                $ts = date('Y-m-d H:i:s');
1130 daniel-mar 76
                $addr = $_SERVER['REMOTE_ADDR'] ?? _L('unknown');
1197 daniel-mar 77
                $line = "[$ts] [$addr] ".$event->getMessage().$users_info.$objects_info;
635 daniel-mar 78
 
79
                return @file_put_contents('/var/log/syslog', "$line\n", FILE_APPEND) !== false;
80
        }
81
}