Subversion Repositories oidplus

Rev

Rev 1000 | Rev 1086 | 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
5
 * Copyright 2019 - 2021 Daniel Marschall, ViaThinkSoft
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
 
22
class OIDplusLoggerPluginWindowsEventLog extends OIDplusLoggerPlugin {
23
 
24
        const CLASS_ViaThinkSoftSimpleEventLog = '{E4270053-A217-498C-B395-9EF33187E8C2}';
25
 
26
        const LOGEVENT_MSG_SUCCESS       = 0;
27
        const LOGEVENT_MSG_INFORMATIONAL = 1;
28
        const LOGEVENT_MSG_WARNING       = 2;
29
        const LOGEVENT_MSG_ERROR         = 3;
30
 
31
        const LOGPROVIDER = 'OIDplus'; // "Source name" (should be registered in the registry = mapped to a message file DLL)
32
 
33
        public static function available(&$reason)/*: bool*/ {
34
                if (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN') {
1000 daniel-mar 35
                        $reason = _L('Functionality only available on Windows systems');
635 daniel-mar 36
                        return false;
37
                }
38
 
39
                if (!class_exists('COM')) {
40
                        $reason = _L('To use %1, please enable the lines "extension=%2" and "extension_dir=ext" in your PHP.ini file.','ViaThinkSoftSimpleEventLog','com_dotnet');
41
                        return false;
42
                }
43
 
44
                try {
1050 daniel-mar 45
                        $x = new \COM(self::CLASS_ViaThinkSoftSimpleEventLog);
635 daniel-mar 46
                        $reason = '?'; // LogSimulate() must actively clear it if everything is OK
47
                        $x->LogSimulate(self::LOGPROVIDER, self::LOGEVENT_MSG_SUCCESS, 'TEST', $reason);/** @phpstan-ignore-line */
48
                        return $reason != '';
1050 daniel-mar 49
                } catch (\Exception $e) {
635 daniel-mar 50
                        $reason = $e->getMessage();
51
                        return false;
52
                }
53
        }
54
 
55
        private static function convertOIDplusToWindowsSeverity($sev) {
56
                switch ($sev) {
57
                        case 0:
58
                                return self::LOGEVENT_MSG_INFORMATIONAL; // undefined
59
                        case 1:
60
                                return self::LOGEVENT_MSG_SUCCESS;
61
                        case 2:
62
                                return self::LOGEVENT_MSG_INFORMATIONAL;
63
                        case 3:
64
                                return self::LOGEVENT_MSG_WARNING;
65
                        case 4:
66
                                return self::LOGEVENT_MSG_ERROR;
67
                        case 5:
68
                                return self::LOGEVENT_MSG_WARNING;
69
                        default:
70
                                return self::LOGEVENT_MSG_INFORMATIONAL; // actually an internal error
71
                }
72
        }
73
 
74
        public static function log($event, $users, $objects)/*: bool*/ {
75
                if (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN') {
76
                        return false;
77
                }
78
 
79
                if (!class_exists('COM')) {
80
                        return false;
81
                }
82
 
83
                try {
1050 daniel-mar 84
                        $x = new \COM(self::CLASS_ViaThinkSoftSimpleEventLog);
635 daniel-mar 85
 
86
                        $admin_severity = 0;
87
                        foreach ($users as list($severity, $username)) {
88
                                // Since the Windows Event Log is mostly for admins, we use the severity an admin would expect
89
                                if ($username == 'admin') $admin_severity = $severity;
90
                        }
91
 
92
                        $x->LogEvent(self::LOGPROVIDER, self::convertOIDplusToWindowsSeverity($admin_severity), $event);/** @phpstan-ignore-line */
93
 
94
                        return true;
1050 daniel-mar 95
                } catch (\Exception $e) {
635 daniel-mar 96
                        return false;
97
                }
98
 
99
        }
100
 
101
}