Subversion Repositories oidplus

Rev

Rev 1014 | Rev 1086 | 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 - 2022 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. class OIDplusPageAdminNotifications extends OIDplusPagePluginAdmin {
  23.  
  24.         public function init($html=true) {
  25.         }
  26.  
  27.         public function gui($id, &$out, &$handled) {
  28.                 $parts = explode('$',$id);
  29.                 $id = $parts[0];
  30.  
  31.                 if ($id == 'oidplus:notifications') {
  32.                         $handled = true;
  33.                         $ra_email = isset($parts[1]) ? $parts[1] : null/*no filter*/;
  34.  
  35.                         $out['title'] = _L('Notifications');
  36.                         $out['icon'] = file_exists(__DIR__.'/img/main_icon.png') ? OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png' : '';
  37.  
  38.                         if ($ra_email == 'admin') {
  39.                                 if (!OIDplus::authUtils()->isAdminLoggedIn()) {
  40.                                         $out['icon'] = 'img/error.png';
  41.                                         $out['text'] = '<p>'._L('You need to <a %1>log in</a> as administrator.',OIDplus::gui()->link('oidplus:login$admin')).'</p>';
  42.                                         return;
  43.                                 }
  44.                         } else if ($ra_email) {
  45.                                 if (!OIDplus::authUtils()->isRaLoggedIn($ra_email) && !OIDplus::authUtils()->isAdminLoggedIn()) {
  46.                                         $out['icon'] = 'img/error.png';
  47.                                         $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>';
  48.                                         return;
  49.                                 }
  50.                         } else {
  51.                                 if ((OIDplus::authUtils()->raNumLoggedIn() == 0) && !OIDplus::authUtils()->isAdminLoggedIn()) {
  52.                                         $out['icon'] = 'img/error.png';
  53.                                         $out['text'] = '<p>'._L('You need to <a %1>log in</a>.',OIDplus::gui()->link('oidplus:login')).'</p>';
  54.                                         return;
  55.                                 }
  56.                         }
  57.  
  58.                         $notifications_by_sev = array();
  59.  
  60.                         foreach (OIDplus::getAllPlugins() as $plugin) {
  61.                                 if ($plugin->implementsFeature('1.3.6.1.4.1.37476.2.5.2.3.8')) {
  62.                                         $notifications = $plugin->getNotifications($ra_email);
  63.                                         if ($notifications) {
  64.                                                 foreach ($notifications as $notification) {
  65.                                                         list($severity, $htmlMessage) = $notification;
  66.  
  67.                                                         // Same severities as the log plugin (also same CSS classes)
  68.                                                         if ($severity == 'OK')   $severity = 1; // (this makes no sense)
  69.                                                         if ($severity == 'INFO') $severity = 2;
  70.                                                         if ($severity == 'WARN') $severity = 3;
  71.                                                         if ($severity == 'ERR')  $severity = 4;
  72.                                                         if ($severity == 'CRIT') $severity = 5;
  73.  
  74.                                                         if (!isset($notifications_by_sev[$severity])) $notifications_by_sev[$severity] = array();
  75.                                                         $notifications_by_sev[$severity][] = $htmlMessage;
  76.                                                 }
  77.                                         }
  78.                                 }
  79.                         }
  80.  
  81.                         if (count($notifications_by_sev) == 0) {
  82.  
  83.                                 $out['text'] .= '<br><p><i>'._L('No notifications').'</i></p>';
  84.  
  85.                         } else {
  86.                                 krsort($notifications_by_sev);
  87.  
  88.                                 foreach ($notifications_by_sev as $severity => $htmlMessages) {
  89.                                         if (count($htmlMessages) == 0) continue;
  90.  
  91.                                         if ($severity == 1) $sev_hf = _L('OK');
  92.                                         else if ($severity == 2) $sev_hf = _L('Informational');
  93.                                         else if ($severity == 3) $sev_hf = _L('Warnings');
  94.                                         else if ($severity == 4) $sev_hf = _L('Errors');
  95.                                         else if ($severity == 5) $sev_hf = _L('Critical issues');
  96.                                         else $sev_hf = _L('Severity %1', $severity-1);
  97.  
  98.                                         $out['text'] .= '<h2><span class="severity_'.$severity.'">'.$sev_hf.' ('.count($htmlMessages).')</span></h2>';
  99.                                         $out['text'] .= '<span class="severity_'.$severity.'"><ol>';
  100.                                         foreach ($htmlMessages as $htmlMessage) {
  101.                                                 $out['text'] .= '<li>'.$htmlMessage.'</li>';
  102.                                         }
  103.                                         $out['text'] .= '</ol></span>';
  104.                                 }
  105.                         }
  106.                 }
  107.         }
  108.  
  109.         public function tree(&$json, $ra_email=null, $nonjs=false, $req_goto='') {
  110.                 if (!OIDplus::authUtils()->isAdminLoggedIn()) return false;
  111.  
  112.                 if (file_exists(__DIR__.'/img/main_icon16.png')) {
  113.                         $tree_icon = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon16.png';
  114.                 } else {
  115.                         $tree_icon = null; // default icon (folder)
  116.                 }
  117.  
  118.                 $json[] = array(
  119.                         'id' => 'oidplus:notifications$admin',
  120.                         'icon' => $tree_icon,
  121.                         'text' => _L('Notifications')
  122.                 );
  123.  
  124.                 return true;
  125.         }
  126.  
  127.         public function tree_search($request) {
  128.                 return false;
  129.         }
  130.  
  131.         public function implementsFeature($id) {
  132.                 if (strtolower($id) == '1.3.6.1.4.1.37476.2.5.2.3.8') return true; // getNotifications()
  133.                 return false;
  134.         }
  135.  
  136.         private function webAccessWorks($dir) {
  137.                 // Attention! This check does not work if OIDplus is password protected!
  138.                 //            The only real solution is to check via JavaScript, which is done by setup/
  139.                 $url = OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE_CANONICAL).$dir;
  140.                 $access_worked = url_get_contents($url) !== false;
  141.                 if ($access_worked) return $url;
  142.  
  143.                 if (!$access_worked) {
  144.                         $url_alt = OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE).$dir;
  145.                         if ($url != $url_alt) {
  146.                                 $access_worked = url_get_contents($url_alt) !== false;
  147.                                 if ($access_worked) return $url;
  148.                         }
  149.                 }
  150.  
  151.                 return false;
  152.         }
  153.  
  154.         private function getNotificationsCheckDirAccess($dir) {
  155.                 $notifications = array();
  156.                 if (($url = $this->webAccessWorks($dir)) !== false) {
  157.                         // Re-use message taken from setup/includes/setup_base.js
  158.                         $msg = _L('Attention: The following directory is world-readable: %1 ! You need to configure your web server to restrict access to this directory! (For Apache see <i>.htaccess</i>, for Microsoft IIS see <i>web.config</i>, for Nginx see <i>nginx.conf</i>).','<a target="_blank" href="'.$url.'">'.$dir.'</a>');
  159.                         $notifications[] = array('CRIT', $msg);
  160.                 }
  161.                 return $notifications;
  162.         }
  163.  
  164.         public function getNotifications($user=null): array {
  165.                 // Interface 1.3.6.1.4.1.37476.2.5.2.3.8
  166.                 // These are some basic "system" checks, no checks from other plugin. So we add them to our plugin instead.
  167.                 $notifications = array();
  168.                 if ((!$user || ($user == 'admin')) && OIDplus::authUtils()->isAdminLoggedIn()) {
  169.                         // Check if critical directories are world-readable
  170.                         if ($this->webAccessWorks('index.php') === false) {
  171.                                 $notifications[] = array('INFO', _L("The system can't check if critical directories (%1) are readable via web-browser. Please verify it manually.", 'userdata, res, dev, includes, setup/includes'));
  172.                         } else {
  173.                                 // see setup/includes/setup_base.js
  174.                                 $forbidden_dirs = array(
  175.                                         "userdata/index.html",
  176.                                         "res/ATTENTION.TXT",
  177.                                         "dev/index.html",
  178.                                         "includes/index.html",
  179.                                         "setup/includes/index.html"
  180.                                         //"plugins/viathinksoft/publicPages/100_whois/whois/cli/index.html"
  181.                                 );
  182.                                 foreach ($forbidden_dirs as $dir) {
  183.                                         $notifications = array_merge($notifications, $this->getNotificationsCheckDirAccess($dir));
  184.                                 }
  185.                         }
  186.  
  187.                         // Check if cache directory is writeable
  188.                         if (!is_writeable(OIDplus::localpath(null).'userdata/cache/')) {
  189.                                 $notifications[] = array('ERR', _L('Directory %1 is not writeable. Please check the permissions!', 'userdata/cache/'));
  190.                         }
  191.                 }
  192.                 return $notifications;
  193.         }
  194.  
  195. }
  196.