Rev 1116 | 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 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 | */ |
||
62 | public static 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 | /** |
108 | * @param string $event |
||
109 | * @param array $users |
||
110 | * @param array $objects |
||
111 | * @return bool |
||
112 | */ |
||
113 | public static function log(string $event, array $users, array $objects): bool { |
||
635 | daniel-mar | 114 | if (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN') { |
115 | return false; |
||
116 | } |
||
117 | |||
118 | if (!class_exists('COM')) { |
||
119 | return false; |
||
120 | } |
||
121 | |||
122 | try { |
||
1050 | daniel-mar | 123 | $x = new \COM(self::CLASS_ViaThinkSoftSimpleEventLog); |
635 | daniel-mar | 124 | |
125 | $admin_severity = 0; |
||
126 | foreach ($users as list($severity, $username)) { |
||
127 | // Since the Windows Event Log is mostly for admins, we use the severity an admin would expect |
||
128 | if ($username == 'admin') $admin_severity = $severity; |
||
129 | } |
||
130 | |||
131 | $x->LogEvent(self::LOGPROVIDER, self::convertOIDplusToWindowsSeverity($admin_severity), $event);/** @phpstan-ignore-line */ |
||
132 | |||
133 | return true; |
||
1050 | daniel-mar | 134 | } catch (\Exception $e) { |
635 | daniel-mar | 135 | return false; |
136 | } |
||
137 | |||
138 | } |
||
139 | |||
140 | } |