Subversion Repositories oidplus

Rev

Rev 1266 | 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 OIDplusPageRaLogEvents extends OIDplusPagePluginRa {
  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:ra_log') {
  45.                         $ra_email = $parts[1] ?? null;
  46.                         if ($ra_email == null) return;
  47.  
  48.                         $handled = true;
  49.  
  50.                         $out['title'] = _L('Log messages for RA %1',$ra_email);
  51.                         $out['icon'] = file_exists(__DIR__.'/img/main_icon.png') ? OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png' : '';
  52.  
  53.                         if (!OIDplus::authUtils()->isRaLoggedIn($ra_email) && !OIDplus::authUtils()->isAdminLoggedIn()) {
  54.                                 throw new OIDplusHtmlException(_L('You need to <a %1>log in</a> as the requested RA %2.',OIDplus::gui()->link('oidplus:login$ra$'.$ra_email),'<b>'.htmlentities($ra_email).'</b>'), $out['title'], 401);
  55.                         }
  56.  
  57.                         $res = OIDplus::db()->query("select * from ###ra where email = ?", array($ra_email));
  58.                         if (!$res->any()) {
  59.                                 throw new OIDplusHtmlException(_L('RA "%1" does not exist','<b>'.htmlentities($ra_email).'</b>'), $out['title']);
  60.                         }
  61.  
  62.  
  63.                         // TODO: !!! correctly implement page scrolling!!! Problem: We cannot use "limit" because this is MySQL. We cannot use "top" because it is SQL server
  64.                         //           We cannot use  id>? and id<? like in admin_log, because users don't have all IDs, just a few, so we cannot filter by ID
  65.                         $page = $parts[2] ?? null;
  66.                         if ($page == null) {
  67.                                 $res = OIDplus::db()->query("select max(lo.id) as cnt from ###log lo ".
  68.                                                             "left join ###log_user lu on lu.log_id = lo.id ".
  69.                                                             "where lu.username = ? " .
  70.                                                             "order by lo.unix_ts desc", array($ra_email));
  71.                                 $page = floor($res->fetch_array()['cnt'] / 50) + 1;
  72.                         }
  73.                         $min = ($page-1) * 50 + 1;
  74.                         $max = ($page  ) * 50;
  75.  
  76.                         $res = OIDplus::db()->query("select lo.unix_ts, lo.addr, lo.event, lu.severity from ###log lo ".
  77.                                                     "left join ###log_user lu on lu.log_id = lo.id ".
  78.                                                     "where lu.username = ? " .
  79.                                                     "and   lo.id >= ? and lo.id <= ? ".
  80.                                                     "order by lo.unix_ts desc", array($ra_email, $min, $max));
  81.  
  82.                         $out['text'] = '<h2>'._L('Page %1 (Log ID %2 till %3)', $page, $min, $max).'</h2>';
  83.  
  84.                         $out['text'] .= '<p>';
  85.                         if (!is_null($parts[2] ?? null)) $out['text'] .= '<a '.OIDplus::gui()->link($parts[0].'$'.$parts[1].'$'.($page+1)).'>Newer log entries</a> -- ';
  86.                         $out['text'] .= '<a '.OIDplus::gui()->link($parts[0].'$'.$parts[1].'$'.($page-1)).'>Older log entries</a>';
  87.                         $out['text'] .= '<p>';
  88.  
  89.                         if ($res->any()) {
  90.                                 $out['text'] .= '<pre>';
  91.                                 while ($row = $res->fetch_array()) {
  92.                                         $addr = empty($row['addr']) ? _L('no address') : $row['addr'];
  93.  
  94.                                         $out['text'] .= '<span class="severity_'.$row['severity'].'">' . date('Y-m-d H:i:s', (int)$row['unix_ts']) . ': ' . htmlentities($row["event"])." (" . htmlentities($addr) . ")</span>\n";
  95.                                 }
  96.                                 $out['text'] .= '</pre>';
  97.                         } else {
  98.                                 $out['text'] .= '<p>'._L('There are no log entries on this page').'</p>';
  99.                         }
  100.  
  101.                         // TODO: List logs in a table instead of a <pre> text
  102.                 }
  103.         }
  104.  
  105.         /**
  106.          * @param array $json
  107.          * @param string|null $ra_email
  108.          * @param bool $nonjs
  109.          * @param string $req_goto
  110.          * @return bool
  111.          * @throws OIDplusException
  112.          */
  113.         public function tree(array &$json, string $ra_email=null, bool $nonjs=false, string $req_goto=''): bool {
  114.                 if (!$ra_email) return false;
  115.                 if (!OIDplus::authUtils()->isRaLoggedIn($ra_email) && !OIDplus::authUtils()->isAdminLoggedIn()) return false;
  116.  
  117.                 if (file_exists(__DIR__.'/img/main_icon16.png')) {
  118.                         $tree_icon = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon16.png';
  119.                 } else {
  120.                         $tree_icon = null; // default icon (folder)
  121.                 }
  122.  
  123.                 $json[] = array(
  124.                         'id' => 'oidplus:ra_log$'.$ra_email,
  125.                         'icon' => $tree_icon,
  126.                         'text' => _L('RA log events')
  127.                 );
  128.  
  129.                 return true;
  130.         }
  131.  
  132.         /**
  133.          * @param string $request
  134.          * @return array|false
  135.          */
  136.         public function tree_search(string $request) {
  137.                 return false;
  138.         }
  139. }
  140.