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