Subversion Repositories oidplus

Rev

Rev 148 | Go to most recent revision | Blame | Last modification | View Log | RSS feed

  1. <?php
  2.  
  3. /*
  4.  * OIDplus 2.0
  5.  * Copyright 2019 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. if (!defined('IN_OIDPLUS')) die();
  21.  
  22. class OIDplusPageRaLogEvents extends OIDplusPagePlugin {
  23.         public function type() {
  24.                 return 'ra';
  25.         }
  26.  
  27.         public function priority() {
  28.                 return 200;
  29.         }
  30.  
  31.         public function action(&$handled) {
  32.         }
  33.  
  34.         public function init($html=true) {
  35.         }
  36.  
  37.         public function cfgSetValue($name, $value) {
  38.         }
  39.  
  40.         public function gui($id, &$out, &$handled) {
  41.                 if (explode('$',$id)[0] == 'oidplus:ra_log') {
  42.                         $handled = true;
  43.  
  44.                         $ra_email = explode('$',$id)[1];
  45.  
  46.                         $res = OIDplus::db()->query("select * from ".OIDPLUS_TABLENAME_PREFIX."ra where email = ?", array($ra_email));
  47.                         if (OIDplus::db()->num_rows($res) == 0) {
  48.                                 $out['icon'] = 'img/error_big.png';
  49.                                 $out['text'] = 'RA <b>'.htmlentities($ra_email).'</b> does not exist';
  50.                                 return $out;
  51.                         }
  52.  
  53.                         if (!OIDplus::authUtils()::isRaLoggedIn($ra_email) && !OIDplus::authUtils()::isAdminLoggedIn()) {
  54.                                 $out['icon'] = 'img/error_big.png';
  55.                                 $out['text'] .= '<p>You need to <a '.oidplus_link('oidplus:login').'>log in</a> as the requested RA <b>'.htmlentities($ra_email).'</b>.</p>';
  56.                                 return $out;
  57.                         }
  58.  
  59.                         $out['title'] = "Log entries for RA $ra_email";
  60.                         $out['icon'] = file_exists(__DIR__.'/icon_big.png') ? 'plugins/'.basename(dirname(__DIR__)).'/'.basename(__DIR__).'/icon_big.png' : '';
  61.  
  62.                         $res = OIDplus::db()->query("select lo.unix_ts, lo.addr, lo.event from ".OIDPLUS_TABLENAME_PREFIX."log lo ".
  63.                                                     "left join ".OIDPLUS_TABLENAME_PREFIX."log_user lu on lu.log_id = lo.id ".
  64.                                                     "where lu.user = ? " .
  65.                                                     "order by lo.unix_ts desc", array($ra_email));
  66.                         if (OIDplus::db()->num_rows($res) > 0) {
  67.                                 $out['text'] = '<pre>';
  68.                                 while ($row = OIDplus::db()->fetch_array($res)) {
  69.                                         $addr = empty($row['addr']) ? 'no address' : $row['addr'];
  70.  
  71.                                         $out['text'] .= date('Y-m-d H:i:s', $row['unix_ts']) . ': ' . htmlentities($row["event"])." (" . htmlentities($addr) . ")\n";
  72.                                 }
  73.                                 $out['text'] .= '</pre>';
  74.                         } else {
  75.                                 $out['text'] .= '<p>Currently there are no log entries</p>';
  76.                         }
  77.  
  78.                         // TODO: List logs in a table instead of a <pre> text
  79.                         // TODO: Load only X events and then re-load new events via AJAX when the user scrolls down
  80.                 }
  81.         }
  82.  
  83.         public function tree(&$json, $ra_email=null, $nonjs=false, $req_goto='') {
  84.                 if (file_exists(__DIR__.'/treeicon.png')) {
  85.                         $tree_icon = 'plugins/'.basename(dirname(__DIR__)).'/'.basename(__DIR__).'/treeicon.png';
  86.                 } else {
  87.                         $tree_icon = null; // default icon (folder)
  88.                 }
  89.  
  90.                 $json[] = array(
  91.                         'id' => 'oidplus:ra_log$'.$ra_email,
  92.                         'icon' => $tree_icon,
  93.                         'text' => 'RA log events'
  94.                 );
  95.  
  96.                 return true;
  97.         }
  98.  
  99.         public function tree_search($request) {
  100.                 return false;
  101.         }
  102. }
  103.  
  104. OIDplus::registerPagePlugin(new OIDplusPageRaLogEvents());
  105.