Subversion Repositories oidplus

Rev

Rev 1014 | Rev 1086 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1014 Rev 1050
1
<?php
1
<?php
2
 
2
 
3
/*
3
/*
4
 * OIDplus 2.0
4
 * OIDplus 2.0
5
 * Copyright 2019 - 2022 Daniel Marschall, ViaThinkSoft
5
 * Copyright 2019 - 2022 Daniel Marschall, ViaThinkSoft
6
 *
6
 *
7
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * Licensed under the Apache License, Version 2.0 (the "License");
8
 * you may not use this file except in compliance with 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
9
 * You may obtain a copy of the License at
10
 *
10
 *
11
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *     http://www.apache.org/licenses/LICENSE-2.0
12
 *
12
 *
13
 * Unless required by applicable law or agreed to in writing, software
13
 * Unless required by applicable law or agreed to in writing, software
14
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * distributed under the License is distributed on an "AS IS" BASIS,
15
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
 * See the License for the specific language governing permissions and
16
 * See the License for the specific language governing permissions and
17
 * limitations under the License.
17
 * limitations under the License.
18
 */
18
 */
19
 
19
 
20
if (!defined('INSIDE_OIDPLUS')) die();
20
namespace ViaThinkSoft\OIDplus;
21
 
21
 
22
class OIDplusPageAdminNotifications extends OIDplusPagePluginAdmin {
22
class OIDplusPageAdminNotifications extends OIDplusPagePluginAdmin {
23
 
23
 
24
        public function init($html=true) {
24
        public function init($html=true) {
25
        }
25
        }
26
 
26
 
27
        public function gui($id, &$out, &$handled) {
27
        public function gui($id, &$out, &$handled) {
28
                $parts = explode('$',$id);
28
                $parts = explode('$',$id);
29
                $id = $parts[0];
29
                $id = $parts[0];
30
 
30
 
31
                if ($id == 'oidplus:notifications') {
31
                if ($id == 'oidplus:notifications') {
32
                        $handled = true;
32
                        $handled = true;
33
                        $ra_email = isset($parts[1]) ? $parts[1] : null/*no filter*/;
33
                        $ra_email = isset($parts[1]) ? $parts[1] : null/*no filter*/;
34
 
34
 
35
                        $out['title'] = _L('Notifications');
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' : '';
36
                        $out['icon'] = file_exists(__DIR__.'/img/main_icon.png') ? OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon.png' : '';
37
 
37
 
38
                        if ($ra_email == 'admin') {
38
                        if ($ra_email == 'admin') {
39
                                if (!OIDplus::authUtils()->isAdminLoggedIn()) {
39
                                if (!OIDplus::authUtils()->isAdminLoggedIn()) {
40
                                        $out['icon'] = 'img/error.png';
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>';
41
                                        $out['text'] = '<p>'._L('You need to <a %1>log in</a> as administrator.',OIDplus::gui()->link('oidplus:login$admin')).'</p>';
42
                                        return;
42
                                        return;
43
                                }
43
                                }
44
                        } else if ($ra_email) {
44
                        } else if ($ra_email) {
45
                                if (!OIDplus::authUtils()->isRaLoggedIn($ra_email) && !OIDplus::authUtils()->isAdminLoggedIn()) {
45
                                if (!OIDplus::authUtils()->isRaLoggedIn($ra_email) && !OIDplus::authUtils()->isAdminLoggedIn()) {
46
                                        $out['icon'] = 'img/error.png';
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>';
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;
48
                                        return;
49
                                }
49
                                }
50
                        } else {
50
                        } else {
51
                                if ((OIDplus::authUtils()->raNumLoggedIn() == 0) && !OIDplus::authUtils()->isAdminLoggedIn()) {
51
                                if ((OIDplus::authUtils()->raNumLoggedIn() == 0) && !OIDplus::authUtils()->isAdminLoggedIn()) {
52
                                        $out['icon'] = 'img/error.png';
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>';
53
                                        $out['text'] = '<p>'._L('You need to <a %1>log in</a>.',OIDplus::gui()->link('oidplus:login')).'</p>';
54
                                        return;
54
                                        return;
55
                                }
55
                                }
56
                        }
56
                        }
57
 
57
 
58
                        $notifications_by_sev = array();
58
                        $notifications_by_sev = array();
59
 
59
 
60
                        foreach (OIDplus::getAllPlugins() as $plugin) {
60
                        foreach (OIDplus::getAllPlugins() as $plugin) {
61
                                if ($plugin->implementsFeature('1.3.6.1.4.1.37476.2.5.2.3.8')) {
61
                                if ($plugin->implementsFeature('1.3.6.1.4.1.37476.2.5.2.3.8')) {
62
                                        $notifications = $plugin->getNotifications($ra_email);
62
                                        $notifications = $plugin->getNotifications($ra_email);
63
                                        if ($notifications) {
63
                                        if ($notifications) {
64
                                                foreach ($notifications as $notification) {
64
                                                foreach ($notifications as $notification) {
65
                                                        list($severity, $htmlMessage) = $notification;
65
                                                        list($severity, $htmlMessage) = $notification;
66
 
66
 
67
                                                        // Same severities as the log plugin (also same CSS classes)
67
                                                        // Same severities as the log plugin (also same CSS classes)
68
                                                        if ($severity == 'OK')   $severity = 1; // (this makes no sense)
68
                                                        if ($severity == 'OK')   $severity = 1; // (this makes no sense)
69
                                                        if ($severity == 'INFO') $severity = 2;
69
                                                        if ($severity == 'INFO') $severity = 2;
70
                                                        if ($severity == 'WARN') $severity = 3;
70
                                                        if ($severity == 'WARN') $severity = 3;
71
                                                        if ($severity == 'ERR')  $severity = 4;
71
                                                        if ($severity == 'ERR')  $severity = 4;
72
                                                        if ($severity == 'CRIT') $severity = 5;
72
                                                        if ($severity == 'CRIT') $severity = 5;
73
 
73
 
74
                                                        if (!isset($notifications_by_sev[$severity])) $notifications_by_sev[$severity] = array();
74
                                                        if (!isset($notifications_by_sev[$severity])) $notifications_by_sev[$severity] = array();
75
                                                        $notifications_by_sev[$severity][] = $htmlMessage;
75
                                                        $notifications_by_sev[$severity][] = $htmlMessage;
76
                                                }
76
                                                }
77
                                        }
77
                                        }
78
                                }
78
                                }
79
                        }
79
                        }
80
 
80
 
81
                        if (count($notifications_by_sev) == 0) {
81
                        if (count($notifications_by_sev) == 0) {
82
 
82
 
83
                                $out['text'] .= '<br><p><i>'._L('No notifications').'</i></p>';
83
                                $out['text'] .= '<br><p><i>'._L('No notifications').'</i></p>';
84
 
84
 
85
                        } else {
85
                        } else {
86
                                krsort($notifications_by_sev);
86
                                krsort($notifications_by_sev);
87
 
87
 
88
                                foreach ($notifications_by_sev as $severity => $htmlMessages) {
88
                                foreach ($notifications_by_sev as $severity => $htmlMessages) {
89
                                        if (count($htmlMessages) == 0) continue;
89
                                        if (count($htmlMessages) == 0) continue;
90
 
90
 
91
                                        if ($severity == 1) $sev_hf = _L('OK');
91
                                        if ($severity == 1) $sev_hf = _L('OK');
92
                                        else if ($severity == 2) $sev_hf = _L('Informational');
92
                                        else if ($severity == 2) $sev_hf = _L('Informational');
93
                                        else if ($severity == 3) $sev_hf = _L('Warnings');
93
                                        else if ($severity == 3) $sev_hf = _L('Warnings');
94
                                        else if ($severity == 4) $sev_hf = _L('Errors');
94
                                        else if ($severity == 4) $sev_hf = _L('Errors');
95
                                        else if ($severity == 5) $sev_hf = _L('Critical issues');
95
                                        else if ($severity == 5) $sev_hf = _L('Critical issues');
96
                                        else $sev_hf = _L('Severity %1', $severity-1);
96
                                        else $sev_hf = _L('Severity %1', $severity-1);
97
 
97
 
98
                                        $out['text'] .= '<h2><span class="severity_'.$severity.'">'.$sev_hf.' ('.count($htmlMessages).')</span></h2>';
98
                                        $out['text'] .= '<h2><span class="severity_'.$severity.'">'.$sev_hf.' ('.count($htmlMessages).')</span></h2>';
99
                                        $out['text'] .= '<span class="severity_'.$severity.'"><ol>';
99
                                        $out['text'] .= '<span class="severity_'.$severity.'"><ol>';
100
                                        foreach ($htmlMessages as $htmlMessage) {
100
                                        foreach ($htmlMessages as $htmlMessage) {
101
                                                $out['text'] .= '<li>'.$htmlMessage.'</li>';
101
                                                $out['text'] .= '<li>'.$htmlMessage.'</li>';
102
                                        }
102
                                        }
103
                                        $out['text'] .= '</ol></span>';
103
                                        $out['text'] .= '</ol></span>';
104
                                }
104
                                }
105
                        }
105
                        }
106
                }
106
                }
107
        }
107
        }
108
 
108
 
109
        public function tree(&$json, $ra_email=null, $nonjs=false, $req_goto='') {
109
        public function tree(&$json, $ra_email=null, $nonjs=false, $req_goto='') {
110
                if (!OIDplus::authUtils()->isAdminLoggedIn()) return false;
110
                if (!OIDplus::authUtils()->isAdminLoggedIn()) return false;
111
 
111
 
112
                if (file_exists(__DIR__.'/img/main_icon16.png')) {
112
                if (file_exists(__DIR__.'/img/main_icon16.png')) {
113
                        $tree_icon = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon16.png';
113
                        $tree_icon = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon16.png';
114
                } else {
114
                } else {
115
                        $tree_icon = null; // default icon (folder)
115
                        $tree_icon = null; // default icon (folder)
116
                }
116
                }
117
 
117
 
118
                $json[] = array(
118
                $json[] = array(
119
                        'id' => 'oidplus:notifications$admin',
119
                        'id' => 'oidplus:notifications$admin',
120
                        'icon' => $tree_icon,
120
                        'icon' => $tree_icon,
121
                        'text' => _L('Notifications')
121
                        'text' => _L('Notifications')
122
                );
122
                );
123
 
123
 
124
                return true;
124
                return true;
125
        }
125
        }
126
 
126
 
127
        public function tree_search($request) {
127
        public function tree_search($request) {
128
                return false;
128
                return false;
129
        }
129
        }
130
 
130
 
131
        public function implementsFeature($id) {
131
        public function implementsFeature($id) {
132
                if (strtolower($id) == '1.3.6.1.4.1.37476.2.5.2.3.8') return true; // getNotifications()
132
                if (strtolower($id) == '1.3.6.1.4.1.37476.2.5.2.3.8') return true; // getNotifications()
133
                return false;
133
                return false;
134
        }
134
        }
135
 
135
 
136
        private function webAccessWorks($dir) {
136
        private function webAccessWorks($dir) {
137
                // Attention! This check does not work if OIDplus is password protected!
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/
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;
139
                $url = OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE_CANONICAL).$dir;
140
                $access_worked = url_get_contents($url) !== false;
140
                $access_worked = url_get_contents($url) !== false;
141
                if ($access_worked) return $url;
141
                if ($access_worked) return $url;
142
 
142
 
143
                if (!$access_worked) {
143
                if (!$access_worked) {
144
                        $url_alt = OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE).$dir;
144
                        $url_alt = OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE).$dir;
145
                        if ($url != $url_alt) {
145
                        if ($url != $url_alt) {
146
                                $access_worked = url_get_contents($url_alt) !== false;
146
                                $access_worked = url_get_contents($url_alt) !== false;
147
                                if ($access_worked) return $url;
147
                                if ($access_worked) return $url;
148
                        }
148
                        }
149
                }
149
                }
150
 
150
 
151
                return false;
151
                return false;
152
        }
152
        }
153
 
153
 
154
        private function getNotificationsCheckDirAccess($dir) {
154
        private function getNotificationsCheckDirAccess($dir) {
155
                $notifications = array();
155
                $notifications = array();
156
                if (($url = $this->webAccessWorks($dir)) !== false) {
156
                if (($url = $this->webAccessWorks($dir)) !== false) {
157
                        // Re-use message taken from setup/includes/setup_base.js
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>');
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);
159
                        $notifications[] = array('CRIT', $msg);
160
                }
160
                }
161
                return $notifications;
161
                return $notifications;
162
        }
162
        }
163
 
163
 
164
        public function getNotifications($user=null): array {
164
        public function getNotifications($user=null): array {
165
                // Interface 1.3.6.1.4.1.37476.2.5.2.3.8
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.
166
                // These are some basic "system" checks, no checks from other plugin. So we add them to our plugin instead.
167
                $notifications = array();
167
                $notifications = array();
168
                if ((!$user || ($user == 'admin')) && OIDplus::authUtils()->isAdminLoggedIn()) {
168
                if ((!$user || ($user == 'admin')) && OIDplus::authUtils()->isAdminLoggedIn()) {
169
                        // Check if critical directories are world-readable
169
                        // Check if critical directories are world-readable
170
                        if ($this->webAccessWorks('index.php') === false) {
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'));
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 {
172
                        } else {
173
                                // see setup/includes/setup_base.js
173
                                // see setup/includes/setup_base.js
174
                                $forbidden_dirs = array(
174
                                $forbidden_dirs = array(
175
                                        "userdata/index.html",
175
                                        "userdata/index.html",
176
                                        "res/ATTENTION.TXT",
176
                                        "res/ATTENTION.TXT",
177
                                        "dev/index.html",
177
                                        "dev/index.html",
178
                                        "includes/index.html",
178
                                        "includes/index.html",
179
                                        "setup/includes/index.html"
179
                                        "setup/includes/index.html"
180
                                        //"plugins/viathinksoft/publicPages/100_whois/whois/cli/index.html"
180
                                        //"plugins/viathinksoft/publicPages/100_whois/whois/cli/index.html"
181
                                );
181
                                );
182
                                foreach ($forbidden_dirs as $dir) {
182
                                foreach ($forbidden_dirs as $dir) {
183
                                        $notifications = array_merge($notifications, $this->getNotificationsCheckDirAccess($dir));
183
                                        $notifications = array_merge($notifications, $this->getNotificationsCheckDirAccess($dir));
184
                                }
184
                                }
185
                        }
185
                        }
186
 
186
 
187
                        // Check if cache directory is writeable
187
                        // Check if cache directory is writeable
188
                        if (!is_writeable(OIDplus::localpath(null).'userdata/cache/')) {
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/'));
189
                                $notifications[] = array('ERR', _L('Directory %1 is not writeable. Please check the permissions!', 'userdata/cache/'));
190
                        }
190
                        }
191
                }
191
                }
192
                return $notifications;
192
                return $notifications;
193
        }
193
        }
194
 
194
 
195
}
195
}
196
 
196