Subversion Repositories oidplus

Rev

Rev 291 | Go to most recent revision | Blame | Last modification | View Log | RSS feed

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