Subversion Repositories oidplus

Rev

Rev 1366 | 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 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'] / 50) + 1;
  57.                         }
  58.                         $min = ($page-1) * 50 + 1;
  59.                         $max = ($page  ) * 50;
  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.                         if ($res->any()) {
  73.                                 $out['text'] .= '<pre>';
  74.                                 while ($row = $res->fetch_array()) {
  75.                                         $severity = 0;
  76.                                         $contains_messages_for_me = false;
  77.                                         // ---
  78.                                         $users = array();
  79.                                         $res2 = OIDplus::db()->query("select username, severity from ###log_user ".
  80.                                                                      "where log_id = ?", array((int)$row['id']));
  81.                                         while ($row2 = $res2->fetch_array()) {
  82.                                                 $users[] = $row2['username'];
  83.                                                 if ($row2['username'] == 'admin') {
  84.                                                         $severity = $row2['severity'];
  85.                                                         $contains_messages_for_me = true;
  86.                                                 }
  87.                                         }
  88.                                         $users = count($users) > 0 ? '; '._L('affected users: %1',implode(', ',$users)) : '';
  89.                                         // ---
  90.                                         $objects = array();
  91.                                         $res2 = OIDplus::db()->query("select object, severity from ###log_object ".
  92.                                                                      "where log_id = ?", array((int)$row['id']));
  93.                                         while ($row2 = $res2->fetch_array()) {
  94.                                                 $objects[] = $row2['object'];
  95.                                         }
  96.                                         $objects = count($objects) > 0 ? '; '._L('affected objects: %1',implode(', ',$objects)) : '';
  97.                                         // ---
  98.                                         $addr = empty($row['addr']) ? _L('no address') : $row['addr'];
  99.                                         // ---
  100.                                         if ($contains_messages_for_me) $out['text'] .= '<b>';
  101.                                         $out['text'] .= '<span class="severity_'.$severity.'">' . date('Y-m-d H:i:s', (int)$row['unix_ts']) . ': ' . htmlentities($row["event"])." (" . htmlentities($addr.$users.$objects) . ")</span>\n";
  102.                                         if ($contains_messages_for_me) $out['text'] .= '</b>';
  103.                                 }
  104.                                 $out['text'] .= '</pre>';
  105.                         } else {
  106.                                 $out['text'] .= '<p>'._L('There are no log entries on this page').'</p>';
  107.                         }
  108.  
  109.                         // TODO: List logs in a table instead of a <pre> text
  110.                 }
  111.         }
  112.  
  113.         /**
  114.          * @param array $json
  115.          * @param string|null $ra_email
  116.          * @param bool $nonjs
  117.          * @param string $req_goto
  118.          * @return bool
  119.          * @throws OIDplusException
  120.          */
  121.         public function tree(array &$json, string $ra_email=null, bool $nonjs=false, string $req_goto=''): bool {
  122.                 if (!OIDplus::authUtils()->isAdminLoggedIn()) return false;
  123.  
  124.                 if (file_exists(__DIR__.'/img/main_icon16.png')) {
  125.                         $tree_icon = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon16.png';
  126.                 } else {
  127.                         $tree_icon = null; // default icon (folder)
  128.                 }
  129.  
  130.                 $json[] = array(
  131.                         'id' => 'oidplus:system_log',
  132.                         'icon' => $tree_icon,
  133.                         'text' => _L('All log messages')
  134.                 );
  135.  
  136.                 return true;
  137.         }
  138.  
  139.         /**
  140.          * @param string $request
  141.          * @return array|false
  142.          */
  143.         public function tree_search(string $request) {
  144.                 return false;
  145.         }
  146. }
  147.