Subversion Repositories oidplus

Rev

Rev 1116 | Rev 1185 | 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.         /**
  29.          *
  30.          */
  31.         const CLASS_ViaThinkSoftSimpleEventLog = '{E4270053-A217-498C-B395-9EF33187E8C2}';
  32.  
  33.         /**
  34.          *
  35.          */
  36.         const LOGEVENT_MSG_SUCCESS       = 0;
  37.  
  38.         /**
  39.          *
  40.          */
  41.         const LOGEVENT_MSG_INFORMATIONAL = 1;
  42.  
  43.         /**
  44.          *
  45.          */
  46.         const LOGEVENT_MSG_WARNING       = 2;
  47.  
  48.         /**
  49.          *
  50.          */
  51.         const LOGEVENT_MSG_ERROR         = 3;
  52.  
  53.         /**
  54.          * "Source name" (should be registered in the registry = mapped to a message file DLL)
  55.          */
  56.         const LOGPROVIDER = 'OIDplus';
  57.  
  58.         /**
  59.          * @param string $reason
  60.          * @return bool
  61.          */
  62.         public static function available(string &$reason): bool {
  63.                 if (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN') {
  64.                         $reason = _L('Functionality only available on Windows systems');
  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 {
  74.                         $x = new \COM(self::CLASS_ViaThinkSoftSimpleEventLog);
  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 != '';
  78.                 } catch (\Exception $e) {
  79.                         $reason = $e->getMessage();
  80.                         return false;
  81.                 }
  82.         }
  83.  
  84.         /**
  85.          * @param int $sev
  86.          * @return int
  87.          */
  88.         private static function convertOIDplusToWindowsSeverity(int $sev): int {
  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.  
  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 {
  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 {
  123.                         $x = new \COM(self::CLASS_ViaThinkSoftSimpleEventLog);
  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;
  134.                 } catch (\Exception $e) {
  135.                         return false;
  136.                 }
  137.  
  138.         }
  139.  
  140. }
  141.