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