Subversion Repositories oidplus

Rev

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