Subversion Repositories oidplus

Rev

Rev 1050 | Rev 1116 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. <?php
  2.  
  3. /*
  4.  * OIDplus 2.0
  5.  * Copyright 2019 - 2023 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.  
  20. namespace ViaThinkSoft\OIDplus;
  21.  
  22. // phpcs:disable PSR1.Files.SideEffects
  23. \defined('INSIDE_OIDPLUS') or die;
  24. // phpcs:enable PSR1.Files.SideEffects
  25.  
  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.  
  37.         public static function available(&$reason)/*: bool*/ {
  38.                 if (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN') {
  39.                         $reason = _L('Functionality only available on Windows systems');
  40.                         return false;
  41.                 }
  42.  
  43.                 if (!class_exists('COM')) {
  44.                         $reason = _L('To use %1, please enable the lines "extension=%2" and "extension_dir=ext" in your PHP.ini file.','ViaThinkSoftSimpleEventLog','com_dotnet');
  45.                         return false;
  46.                 }
  47.  
  48.                 try {
  49.                         $x = new \COM(self::CLASS_ViaThinkSoftSimpleEventLog);
  50.                         $reason = '?'; // LogSimulate() must actively clear it if everything is OK
  51.                         $x->LogSimulate(self::LOGPROVIDER, self::LOGEVENT_MSG_SUCCESS, 'TEST', $reason);/** @phpstan-ignore-line */
  52.                         return $reason != '';
  53.                 } catch (\Exception $e) {
  54.                         $reason = $e->getMessage();
  55.                         return false;
  56.                 }
  57.         }
  58.  
  59.         private static function convertOIDplusToWindowsSeverity($sev) {
  60.                 switch ($sev) {
  61.                         case 0:
  62.                                 return self::LOGEVENT_MSG_INFORMATIONAL; // undefined
  63.                         case 1:
  64.                                 return self::LOGEVENT_MSG_SUCCESS;
  65.                         case 2:
  66.                                 return self::LOGEVENT_MSG_INFORMATIONAL;
  67.                         case 3:
  68.                                 return self::LOGEVENT_MSG_WARNING;
  69.                         case 4:
  70.                                 return self::LOGEVENT_MSG_ERROR;
  71.                         case 5:
  72.                                 return self::LOGEVENT_MSG_WARNING;
  73.                         default:
  74.                                 return self::LOGEVENT_MSG_INFORMATIONAL; // actually an internal error
  75.                 }
  76.         }
  77.  
  78.         public static function log($event, $users, $objects)/*: bool*/ {
  79.                 if (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN') {
  80.                         return false;
  81.                 }
  82.  
  83.                 if (!class_exists('COM')) {
  84.                         return false;
  85.                 }
  86.  
  87.                 try {
  88.                         $x = new \COM(self::CLASS_ViaThinkSoftSimpleEventLog);
  89.  
  90.                         $admin_severity = 0;
  91.                         foreach ($users as list($severity, $username)) {
  92.                                 // Since the Windows Event Log is mostly for admins, we use the severity an admin would expect
  93.                                 if ($username == 'admin') $admin_severity = $severity;
  94.                         }
  95.  
  96.                         $x->LogEvent(self::LOGPROVIDER, self::convertOIDplusToWindowsSeverity($admin_severity), $event);/** @phpstan-ignore-line */
  97.  
  98.                         return true;
  99.                 } catch (\Exception $e) {
  100.                         return false;
  101.                 }
  102.  
  103.         }
  104.  
  105. }
  106.