Subversion Repositories oidplus

Rev

Rev 1180 | Rev 1189 | 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 OIDplusPageAdminNotifications extends OIDplusPagePluginAdmin
  27.         implements INTF_OID_1_3_6_1_4_1_37476_2_5_2_3_8 /* getNotifications */
  28. {
  29.  
  30.         /**
  31.          * @param bool $html
  32.          * @return void
  33.          */
  34.         public function init(bool $html=true) {
  35.         }
  36.  
  37.         /**
  38.          * @param string $id
  39.          * @param array $out
  40.          * @param bool $handled
  41.          * @return void
  42.          * @throws OIDplusException
  43.          */
  44.         public function gui(string $id, array &$out, bool &$handled) {
  45.                 $parts = explode('$',$id);
  46.                 $id = $parts[0];
  47.  
  48.                 if ($id == 'oidplus:notifications') {
  49.                         $handled = true;
  50.                         $ra_email = $parts[1] ?? null/*no filter*/;
  51.  
  52.                         $out['title'] = _L('Notifications');
  53.                         $out['icon'] = file_exists(__DIR__.'/img/main_icon.png') ? OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png' : '';
  54.  
  55.                         if ($ra_email == 'admin') {
  56.                                 if (!OIDplus::authUtils()->isAdminLoggedIn()) {
  57.                                         $out['icon'] = 'img/error.png';
  58.                                         $out['text'] = '<p>'._L('You need to <a %1>log in</a> as administrator.',OIDplus::gui()->link('oidplus:login$admin')).'</p>';
  59.                                         return;
  60.                                 }
  61.                         } else if ($ra_email) {
  62.                                 if (!OIDplus::authUtils()->isRaLoggedIn($ra_email) && !OIDplus::authUtils()->isAdminLoggedIn()) {
  63.                                         $out['icon'] = 'img/error.png';
  64.                                         $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>';
  65.                                         return;
  66.                                 }
  67.                         } else {
  68.                                 if ((OIDplus::authUtils()->raNumLoggedIn() == 0) && !OIDplus::authUtils()->isAdminLoggedIn()) {
  69.                                         $out['icon'] = 'img/error.png';
  70.                                         $out['text'] = '<p>'._L('You need to <a %1>log in</a>.',OIDplus::gui()->link('oidplus:login')).'</p>';
  71.                                         return;
  72.                                 }
  73.                         }
  74.  
  75.                         $notifications_by_sev = array();
  76.  
  77.                         foreach (OIDplus::getAllPlugins() as $plugin) {
  78.                                 if ($plugin instanceof INTF_OID_1_3_6_1_4_1_37476_2_5_2_3_8) {
  79.                                         try {
  80.                                                 $notifications = $plugin->getNotifications($ra_email);
  81.                                         } catch (\Exception $e) {
  82.                                                 $notifications = array(
  83.                                                         ['CRIT', _L('The plugin %1 crashed during the notification-check. Message: %2', get_class($plugin), $e->getMessage())]
  84.                                                 );
  85.                                         }
  86.                                         if ($notifications) {
  87.                                                 foreach ($notifications as $notification) {
  88.                                                         list($severity, $htmlMessage) = $notification;
  89.  
  90.                                                         // Same severities as the log plugin (also same CSS classes)
  91.                                                         if ($severity == 'OK')   $severity = 1; // (this makes no sense)
  92.                                                         else if ($severity == 'INFO') $severity = 2;
  93.                                                         else if ($severity == 'WARN') $severity = 3;
  94.                                                         else if ($severity == 'ERR')  $severity = 4;
  95.                                                         else if ($severity == 'CRIT') $severity = 5;
  96.                                                         else {
  97.                                                                 $htmlMessage = _L('The plugin %1 returned an invalid severity (%2). Message: %3', get_class($plugin), $severity, $htmlMessage);
  98.                                                                 $severity = 5; // CRIT
  99.                                                         }
  100.  
  101.                                                         if (!isset($notifications_by_sev[$severity])) $notifications_by_sev[$severity] = array();
  102.                                                         $notifications_by_sev[$severity][] = $htmlMessage;
  103.                                                 }
  104.                                         }
  105.                                 }
  106.                         }
  107.  
  108.                         if (count($notifications_by_sev) == 0) {
  109.  
  110.                                 $out['text'] .= '<br><p><i>'._L('No notifications').'</i></p>';
  111.  
  112.                         } else {
  113.                                 krsort($notifications_by_sev);
  114.  
  115.                                 foreach ($notifications_by_sev as $severity => $htmlMessages) {
  116.                                         if (count($htmlMessages) == 0) continue;
  117.  
  118.                                         if ($severity == 1) $sev_hf = _L('OK');
  119.                                         else if ($severity == 2) $sev_hf = _L('Informational');
  120.                                         else if ($severity == 3) $sev_hf = _L('Warnings');
  121.                                         else if ($severity == 4) $sev_hf = _L('Errors');
  122.                                         else if ($severity == 5) $sev_hf = _L('Critical issues');
  123.                                         else assert(false);
  124.  
  125.                                         $out['text'] .= '<h2><span class="severity_'.$severity.'">'.$sev_hf.' ('.count($htmlMessages).')</span></h2>';
  126.                                         $out['text'] .= '<span class="severity_'.$severity.'"><ol>';
  127.                                         foreach ($htmlMessages as $htmlMessage) {
  128.                                                 $out['text'] .= '<li>'.$htmlMessage.'</li>';
  129.                                         }
  130.                                         $out['text'] .= '</ol></span>';
  131.                                 }
  132.                         }
  133.                 }
  134.         }
  135.  
  136.         /**
  137.          * @param array $json
  138.          * @param string|null $ra_email
  139.          * @param bool $nonjs
  140.          * @param string $req_goto
  141.          * @return bool
  142.          * @throws OIDplusException
  143.          */
  144.         public function tree(array &$json, string $ra_email=null, bool $nonjs=false, string $req_goto=''): bool {
  145.                 if (!OIDplus::authUtils()->isAdminLoggedIn()) return false;
  146.  
  147.                 if (file_exists(__DIR__.'/img/main_icon16.png')) {
  148.                         $tree_icon = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon16.png';
  149.                 } else {
  150.                         $tree_icon = null; // default icon (folder)
  151.                 }
  152.  
  153.                 $json[] = array(
  154.                         'id' => 'oidplus:notifications$admin',
  155.                         'icon' => $tree_icon,
  156.                         'text' => _L('Notifications')
  157.                 );
  158.  
  159.                 return true;
  160.         }
  161.  
  162.         /**
  163.          * @param string $request
  164.          * @return array|false
  165.          */
  166.         public function tree_search(string $request) {
  167.                 return false;
  168.         }
  169.  
  170.         /**
  171.          * Checks if the system can be accessed publicly
  172.          * Attention! This check does not work if OIDplus is password protected (solution would be to check via JavaScript,
  173.          * which is done in setup/), or the URL is in the IntraNet rather than the Internet (only solution would be a
  174.          * remote URL check service)
  175.          * @param string $dir
  176.          * @return false|string
  177.          * @throws OIDplusException
  178.          */
  179.         private function webAccessWorks(string $dir) {
  180.                 $url = OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE_CANONICAL).$dir;
  181.                 $require_ssl = str_starts_with(strtolower($url),'https:');
  182.                 if (!url_get_contents_available($require_ssl)) return false;
  183.                 $access_worked = url_get_contents($url) !== false;
  184.                 if ($access_worked) return $url;
  185.  
  186.                 if (!$access_worked) {
  187.                         $url_alt = OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE).$dir;
  188.                         if ($url != $url_alt) {
  189.                                 $access_worked = url_get_contents($url_alt) !== false;
  190.                                 if ($access_worked) return $url;
  191.                         }
  192.                 }
  193.  
  194.                 return false;
  195.         }
  196.  
  197.         /**
  198.          * @param string $dir
  199.          * @return array
  200.          * @throws OIDplusException
  201.          */
  202.         private function getNotificationsCheckDirAccess(string $dir): array {
  203.                 $notifications = array();
  204.                 if (($url = $this->webAccessWorks($dir)) !== false) {
  205.                         // Re-use message taken from setup/includes/setup_base.js
  206.                         $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>');
  207.                         $notifications[] = array('CRIT', $msg);
  208.                 }
  209.                 return $notifications;
  210.         }
  211.  
  212.         /**
  213.          * Implements interface INTF_OID_1_3_6_1_4_1_37476_2_5_2_3_8
  214.          * These are some basic "system" checks, no checks from other plugin. So we add them to our plugin instead.
  215.          * @param string|null $user
  216.          * @return array
  217.          * @throws OIDplusException
  218.          */
  219.         public function getNotifications(string $user=null): array {
  220.                 $notifications = array();
  221.                 if ((!$user || ($user == 'admin')) && OIDplus::authUtils()->isAdminLoggedIn()) {
  222.                         // Check if critical directories are world-readable
  223.                         if ($this->webAccessWorks('index.php') === false) {
  224.                                 $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'));
  225.                         } else {
  226.                                 // see setup/includes/setup_base.js
  227.                                 $forbidden_dirs = array(
  228.                                         "userdata/index.html",
  229.                                         "res/ATTENTION.TXT",
  230.                                         "dev/index.html",
  231.                                         "includes/index.html",
  232.                                         "setup/includes/index.html"
  233.                                         //"plugins/viathinksoft/publicPages/100_whois/whois/cli/index.html"
  234.                                 );
  235.                                 foreach ($forbidden_dirs as $dir) {
  236.                                         $notifications = array_merge($notifications, $this->getNotificationsCheckDirAccess($dir));
  237.                                 }
  238.                         }
  239.  
  240.                         // Check if cache directory is writeable
  241.                         $cache_dir = OIDplus::localpath(null).'userdata/cache/';
  242.                         if (!is_dir($cache_dir)) {
  243.                                 $notifications[] = array('ERR', _L('Directory %1 does not exist', $cache_dir));
  244.                         } else if (!isFileOrPathWritable($cache_dir)) {
  245.                                 $notifications[] = array('ERR', _L('Directory %1 is not writeable. Please check the permissions!', $cache_dir));
  246.                         }
  247.                 }
  248.                 return $notifications;
  249.         }
  250.  
  251. }
  252.