Subversion Repositories oidplus

Rev

Rev 1223 | Rev 1445 | 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.                 if (explode('$',$id)[0] == 'oidplus:ra_log') {
  44.                         $handled = true;
  45.  
  46.                         $ra_email = explode('$',$id)[1];
  47.  
  48.                         $out['title'] = _L('Log messages for RA %1',$ra_email);
  49.                         $out['icon'] = file_exists(__DIR__.'/img/main_icon.png') ? OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png' : '';
  50.  
  51.                         if (!OIDplus::authUtils()->isRaLoggedIn($ra_email) && !OIDplus::authUtils()->isAdminLoggedIn()) {
  52.                                 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);
  53.                         }
  54.  
  55.                         $res = OIDplus::db()->query("select * from ###ra where email = ?", array($ra_email));
  56.                         if (!$res->any()) {
  57.                                 throw new OIDplusHtmlException(_L('RA "%1" does not exist','<b>'.htmlentities($ra_email).'</b>'), $out['title']);
  58.                         }
  59.  
  60.                         $res = OIDplus::db()->query("select lo.unix_ts, lo.addr, lo.event, lu.severity from ###log lo ".
  61.                                                     "left join ###log_user lu on lu.log_id = lo.id ".
  62.                                                     "where lu.username = ? " .
  63.                                                     "order by lo.unix_ts desc", array($ra_email));
  64.                         if ($res->any()) {
  65.                                 $out['text'] = '<pre>';
  66.                                 while ($row = $res->fetch_array()) {
  67.                                         $addr = empty($row['addr']) ? _L('no address') : $row['addr'];
  68.  
  69.                                         $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";
  70.                                 }
  71.                                 $out['text'] .= '</pre>';
  72.                         } else {
  73.                                 $out['text'] .= '<p>'._L('Currently there are no log entries').'</p>';
  74.                         }
  75.  
  76.                         // TODO: List logs in a table instead of a <pre> text
  77.                         // TODO: Load only X events and then re-load new events via AJAX when the user scrolls down
  78.                 }
  79.         }
  80.  
  81.         /**
  82.          * @param array $json
  83.          * @param string|null $ra_email
  84.          * @param bool $nonjs
  85.          * @param string $req_goto
  86.          * @return bool
  87.          * @throws OIDplusException
  88.          */
  89.         public function tree(array &$json, string $ra_email=null, bool $nonjs=false, string $req_goto=''): bool {
  90.                 if (!$ra_email) return false;
  91.                 if (!OIDplus::authUtils()->isRaLoggedIn($ra_email) && !OIDplus::authUtils()->isAdminLoggedIn()) return false;
  92.  
  93.                 if (file_exists(__DIR__.'/img/main_icon16.png')) {
  94.                         $tree_icon = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon16.png';
  95.                 } else {
  96.                         $tree_icon = null; // default icon (folder)
  97.                 }
  98.  
  99.                 $json[] = array(
  100.                         'id' => 'oidplus:ra_log$'.$ra_email,
  101.                         'icon' => $tree_icon,
  102.                         'text' => _L('RA log events')
  103.                 );
  104.  
  105.                 return true;
  106.         }
  107.  
  108.         /**
  109.          * @param string $request
  110.          * @return array|false
  111.          */
  112.         public function tree_search(string $request) {
  113.                 return false;
  114.         }
  115. }
  116.