Subversion Repositories oidplus

Rev

Rev 325 | 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 OIDplusLoggerPluginLinuxSyslog extends OIDplusLoggerPlugin {
  21.  
  22.         public static function available(&$reason)/*: bool*/ {
  23.                 if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
  24.                         $reason = _L('Functionality not available on Windows');
  25.                         return false;
  26.                 }
  27.  
  28.                 if (!@file_exists('/var/log/syslog')) {
  29.                         $reason = _L('File %1 does not exist','/var/log/syslog');
  30.                         return false;
  31.                 }
  32.  
  33.                 if (@file_put_contents('/var/log/syslog', '', FILE_APPEND) === false) {
  34.                         $reason = _L('File %1 is not writeable','/var/log/syslog');
  35.                         return false;
  36.                 }
  37.  
  38.                 $reason = '';
  39.                 return true;
  40.         }
  41.  
  42.         public static function log($event, $users, $objects)/*: bool*/ {
  43.                 if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') return false;
  44.  
  45.                 if (!@file_exists('/var/log/syslog')) return false;
  46.  
  47.                 $users_names = array();
  48.                 foreach ($users as list($severity, $username)) $users_names[] = $username;
  49.                 $users_info = count($users_names) == 0 ? '' : ' ('._L('affected users: %1',implode(', ',$users_names)).')';
  50.  
  51.                 $objects_names = array();
  52.                 foreach ($objects as list($severity, $objectname)) $objects_names[] = $objectname;
  53.                 $objects_info = count($objects_names) == 0 ? '' : ' ('._L('affected objects: %1',implode(', ',$objects_names)).')';
  54.  
  55.                 $ts = date('Y-m-d H:i:s');
  56.                 $addr = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : _L('unknown');
  57.                 $line = "[$ts] [$addr] $event$users_info$objects_info";
  58.  
  59.                 return @file_put_contents('/var/log/syslog', "$line\n", FILE_APPEND) !== false;
  60.         }
  61. }