Subversion Repositories oidplus

Rev

Rev 1185 | Rev 1221 | 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 OIDplusLoggerPluginWindowsEventLog extends OIDplusLoggerPlugin {
27
 
1130 daniel-mar 28
        /**
29
         *
30
         */
635 daniel-mar 31
        const CLASS_ViaThinkSoftSimpleEventLog = '{E4270053-A217-498C-B395-9EF33187E8C2}';
32
 
1130 daniel-mar 33
        /**
34
         *
35
         */
635 daniel-mar 36
        const LOGEVENT_MSG_SUCCESS       = 0;
1130 daniel-mar 37
 
38
        /**
39
         *
40
         */
635 daniel-mar 41
        const LOGEVENT_MSG_INFORMATIONAL = 1;
1130 daniel-mar 42
 
43
        /**
44
         *
45
         */
635 daniel-mar 46
        const LOGEVENT_MSG_WARNING       = 2;
1130 daniel-mar 47
 
48
        /**
49
         *
50
         */
635 daniel-mar 51
        const LOGEVENT_MSG_ERROR         = 3;
52
 
1130 daniel-mar 53
        /**
54
         * "Source name" (should be registered in the registry = mapped to a message file DLL)
55
         */
56
        const LOGPROVIDER = 'OIDplus';
635 daniel-mar 57
 
1116 daniel-mar 58
        /**
59
         * @param string $reason
60
         * @return bool
61
         */
1185 daniel-mar 62
        public function available(string &$reason): bool {
635 daniel-mar 63
                if (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN') {
1000 daniel-mar 64
                        $reason = _L('Functionality only available on Windows systems');
635 daniel-mar 65
                        return false;
66
                }
67
 
68
                if (!class_exists('COM')) {
69
                        $reason = _L('To use %1, please enable the lines "extension=%2" and "extension_dir=ext" in your PHP.ini file.','ViaThinkSoftSimpleEventLog','com_dotnet');
70
                        return false;
71
                }
72
 
73
                try {
1050 daniel-mar 74
                        $x = new \COM(self::CLASS_ViaThinkSoftSimpleEventLog);
635 daniel-mar 75
                        $reason = '?'; // LogSimulate() must actively clear it if everything is OK
76
                        $x->LogSimulate(self::LOGPROVIDER, self::LOGEVENT_MSG_SUCCESS, 'TEST', $reason);/** @phpstan-ignore-line */
77
                        return $reason != '';
1050 daniel-mar 78
                } catch (\Exception $e) {
635 daniel-mar 79
                        $reason = $e->getMessage();
80
                        return false;
81
                }
82
        }
83
 
1116 daniel-mar 84
        /**
85
         * @param int $sev
86
         * @return int
87
         */
88
        private static function convertOIDplusToWindowsSeverity(int $sev): int {
635 daniel-mar 89
                switch ($sev) {
90
                        case 0:
91
                                return self::LOGEVENT_MSG_INFORMATIONAL; // undefined
92
                        case 1:
93
                                return self::LOGEVENT_MSG_SUCCESS;
94
                        case 2:
95
                                return self::LOGEVENT_MSG_INFORMATIONAL;
96
                        case 3:
97
                                return self::LOGEVENT_MSG_WARNING;
98
                        case 4:
99
                                return self::LOGEVENT_MSG_ERROR;
100
                        case 5:
101
                                return self::LOGEVENT_MSG_WARNING;
102
                        default:
103
                                return self::LOGEVENT_MSG_INFORMATIONAL; // actually an internal error
104
                }
105
        }
106
 
1116 daniel-mar 107
        /**
1197 daniel-mar 108
         * @param OIDplusLogEvent $event
1116 daniel-mar 109
         * @return bool
110
         */
1197 daniel-mar 111
        public function log(OIDplusLogEvent $event): bool {
635 daniel-mar 112
                if (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN') {
113
                        return false;
114
                }
115
 
116
                if (!class_exists('COM')) {
117
                        return false;
118
                }
119
 
120
                try {
1050 daniel-mar 121
                        $x = new \COM(self::CLASS_ViaThinkSoftSimpleEventLog);
635 daniel-mar 122
 
123
                        $admin_severity = 0;
1197 daniel-mar 124
                        foreach ($event->getTargets() as $target) {
125
                                if ($target instanceof OIDplusLogTargetUser) {
126
                                        // Since the Windows Event Log is mostly for admins, we use the severity an admin would expect
127
                                        if ($target->getUsername() === 'admin') $admin_severity = $target->getSeverity();
128
                                } else if ($target instanceof OIDplusLogTargetObject) {
129
                                        // Nothing here
130
                                } else {
131
                                        assert(false);
132
                                }
635 daniel-mar 133
                        }
1197 daniel-mar 134
                        $x->LogEvent(self::LOGPROVIDER, self::convertOIDplusToWindowsSeverity($admin_severity), $event->getMessage()); /** @phpstan-ignore-line */
635 daniel-mar 135
 
136
                        return true;
1050 daniel-mar 137
                } catch (\Exception $e) {
635 daniel-mar 138
                        return false;
139
                }
140
 
141
        }
142
 
143
}