Subversion Repositories oidplus

Rev

Rev 1445 | 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 OIDplusPageAdminLogEvents extends OIDplusPagePluginAdmin {
  27.  
  28.         /**
  29.          * @param bool $html
  30.          * @return void
  31.          */
  32.         public function init(bool $html=true) {
  33.         }
  34.  
  35.         /**
  36.          * @param string $id
  37.          * @param array $out
  38.          * @param bool $handled
  39.          * @return void
  40.          * @throws OIDplusException
  41.          */
  42.         public function gui(string $id, array &$out, bool &$handled) {
  43.                 $parts = explode('$', $id);
  44.                 if ($parts[0] == 'oidplus:system_log') {
  45.                         $handled = true;
  46.                         $out['title'] = _L('All log messages');
  47.                         $out['icon'] = file_exists(__DIR__.'/img/main_icon.png') ? OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png' : '';
  48.  
  49.                         if (!OIDplus::authUtils()->isAdminLoggedIn()) {
  50.                                 throw new OIDplusHtmlException(_L('You need to <a %1>log in</a> as administrator.',OIDplus::gui()->link('oidplus:login$admin')), $out['title'], 401);
  51.                         }
  52.  
  53.                         $page = $parts[1] ?? null;
  54.                         if ($page == null) {
  55.                                 $res = OIDplus::db()->query("select max(id) as cnt from ###log");
  56.                                 $page = floor($res->fetch_array()['cnt'] / 500) + 1;
  57.                         }
  58.                         $min = ($page-1) * 500 + 1;
  59.                         $max = ($page  ) * 500;
  60.  
  61.                         $res = OIDplus::db()->query("select id, unix_ts, addr, event from ###log ".
  62.                                                     "where id >= ? and id <= ? ".
  63.                                                     "order by unix_ts desc", [$min, $max]);
  64.  
  65.                         $out['text'] = '<h2>'._L('Page %1 (Log ID %2 till %3)', $page, $min, $max).'</h2>';
  66.  
  67.                         $out['text'] .= '<p>';
  68.                         if (!is_null($parts[1] ?? null)) $out['text'] .= '<a '.OIDplus::gui()->link($parts[0].'$'.($page+1)).'>Newer log entries</a> -- ';
  69.                         $out['text'] .= '<a '.OIDplus::gui()->link($parts[0].'$'.($page-1)).'>Older log entries</a>';
  70.                         $out['text'] .= '<p>';
  71.  
  72.                         $out['text'] .= '<div class="container box"><div id="suboid_table" class="table-responsive">';
  73.                         $out['text'] .= '<table class="table table-bordered table-striped">';
  74.                         $out['text'] .= '<thead>';
  75.                         $out['text'] .= '<tr><th>'._L('Time').'</th><th>'._L('Event').'</th><th>'._L('Affected users').'</th><th>'._L('Affected objects').'</th><th>'._L('IP Address').'</th></tr>';
  76.                         $out['text'] .= '</thead>';
  77.                         $out['text'] .= '<tbody>';
  78.  
  79.                         if ($res->any()) {
  80.                                 while ($row = $res->fetch_array()) {
  81.                                         $severity = 0;
  82.                                         $contains_messages_for_me = false;
  83.                                         // ---
  84.                                         $users = array();
  85.                                         $res2 = OIDplus::db()->query("select username, severity from ###log_user ".
  86.                                                                      "where log_id = ?", array((int)$row['id']));
  87.                                         while ($row2 = $res2->fetch_array()) {
  88.                                                 $users[] = $row2['username'];
  89.                                                 if ($row2['username'] == 'admin') {
  90.                                                         $severity = $row2['severity'];
  91.                                                         $contains_messages_for_me = true;
  92.                                                 }
  93.                                         }
  94.                                         $users = implode("\n",$users);
  95.                                         // ---
  96.                                         $objects = array();
  97.                                         $res2 = OIDplus::db()->query("select object, severity from ###log_object ".
  98.                                                                      "where log_id = ?", array((int)$row['id']));
  99.                                         while ($row2 = $res2->fetch_array()) {
  100.                                                 $objects[] = $row2['object'];
  101.                                         }
  102.                                         $objects = implode("\n",$objects);
  103.                                         // ---
  104.                                         $addr = empty($row['addr']) ? _L('no address') : $row['addr'];
  105.                                         // ---
  106.  
  107.                                         $a = '<span class="severity_'.$severity.'">';
  108.                                         $b = '</span>';
  109.                                         if ($contains_messages_for_me) $a = '<b>'.$a;
  110.                                         if ($contains_messages_for_me) $b = $b.'</b>';
  111.                                         $out['text'] .= '<tr>';
  112.                                         $out['text'] .= '<td>'.$a.date('Y-m-d H:i:s', (int)$row['unix_ts']).$b.'</td>';
  113.                                         $out['text'] .= '<td>'.$a.htmlentities($row['event']).$b.'</td>';
  114.                                         $out['text'] .= '<td>'.$a.nl2br(htmlentities($users)).$b.'</td>';
  115.                                         $out['text'] .= '<td>'.$a.nl2br(htmlentities($objects)).$b.'</td>';
  116.                                         $out['text'] .= '<td>'.$a.htmlentities($addr).$b.'</td>';
  117.                                         $out['text'] .= '<tr>';
  118.  
  119.                                 }
  120.                         } else {
  121.                                 $out['text'] .= '<tr><td colspan="5">'._L('There are no log entries on this page').'</td></tr>';
  122.                         }
  123.  
  124.                         $out['text'] .= '</tbody>';
  125.                         $out['text'] .= '</table>';
  126.                         $out['text'] .= '</div></div>';
  127.  
  128.                 }
  129.         }
  130.  
  131.         /**
  132.          * @param array $json
  133.          * @param string|null $ra_email
  134.          * @param bool $nonjs
  135.          * @param string $req_goto
  136.          * @return bool
  137.          * @throws OIDplusException
  138.          */
  139.         public function tree(array &$json, string $ra_email=null, bool $nonjs=false, string $req_goto=''): bool {
  140.                 if (!OIDplus::authUtils()->isAdminLoggedIn()) return false;
  141.  
  142.                 if (file_exists(__DIR__.'/img/main_icon16.png')) {
  143.                         $tree_icon = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon16.png';
  144.                 } else {
  145.                         $tree_icon = null; // default icon (folder)
  146.                 }
  147.  
  148.                 $json[] = array(
  149.                         'id' => 'oidplus:system_log',
  150.                         'icon' => $tree_icon,
  151.                         'text' => _L('All log messages')
  152.                 );
  153.  
  154.                 return true;
  155.         }
  156.  
  157.         /**
  158.          * @param string $request
  159.          * @return array|false
  160.          */
  161.         public function tree_search(string $request) {
  162.                 return false;
  163.         }
  164. }
  165.