Subversion Repositories oidplus

Rev

Rev 1197 | 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 OIDplusLoggerPluginUserdataLogfile extends OIDplusLoggerPlugin {
  27.  
  28.         /**
  29.          * @param string $reason
  30.          * @return bool
  31.          */
  32.         public function available(string &$reason): bool {
  33.                 if (!is_dir(OIDplus::localpath().'userdata/logs/')) {
  34.                         $reason = _L('Directory userdata/logs/ not existing');
  35.                         return false;
  36.                 }
  37.  
  38.                 if (@file_put_contents(OIDplus::localpath().'userdata/logs/oidplus.log', '', FILE_APPEND) === false) {
  39.                         $reason = _L('File userdata/logs/oidplus.log not writeable');
  40.                         return false;
  41.                 }
  42.  
  43.                 $reason = '';
  44.                 return true;
  45.         }
  46.  
  47.         /**
  48.          * @param OIDplusLogEvent $event
  49.          * @return bool
  50.          */
  51.         public function log(OIDplusLogEvent $event): bool {
  52.                 if (!is_dir(OIDplus::localpath().'userdata/logs/')) return false;
  53.  
  54.                 $users_names = array();
  55.                 $objects_names = array();
  56.                 foreach ($event->getTargets() as $target) {
  57.                         if ($target instanceof OIDplusLogTargetUser) {
  58.                                 $users_names[] = $target->getUsername();
  59.                         } else if ($target instanceof OIDplusLogTargetObject) {
  60.                                 $objects_names[] = $target->getObject();
  61.                         } else {
  62.                                 assert(false);
  63.                         }
  64.                 }
  65.                 $users_info = count($users_names) == 0 ? '' : ' ('._L('affected users: %1',implode(', ',$users_names)).')';
  66.                 $objects_info = count($objects_names) == 0 ? '' : ' ('._L('affected objects: %1',implode(', ',$objects_names)).')';
  67.  
  68.                 $ts = date('Y-m-d H:i:s');
  69.                 $addr = OIDplus::getClientIpAddress() ?: _L('unknown');
  70.  
  71.                 // Note: $ts was put into brackets, because there is probably a bug in fail2ban that does not allow the date/time being at offset 0
  72.                 // "WARNING Found a match for '020-05-11 22:50:58 [192.168.69.89] Failed login ..."
  73.                 $line = "[$ts] [$addr] ".$event->getMessage().$users_info.$objects_info;
  74.  
  75.                 return @file_put_contents(OIDplus::localpath().'userdata/logs/oidplus.log', "$line\n", FILE_APPEND) !== false;
  76.         }
  77. }