Subversion Repositories oidplus

Rev

Rev 1005 | Rev 1007 | 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
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
if (!defined('INSIDE_OIDPLUS')) die();
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
 
1005 daniel-mar 60
                        foreach (OIDplus::getAllPlugins() as $plugin) {
1000 daniel-mar 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'] .= '<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
 
90
                                        if ($severity == 1) $sev_hf = _L('OK');
91
                                        else if ($severity == 2) $sev_hf = _L('Informational');
92
                                        else if ($severity == 3) $sev_hf = _L('Warnings');
93
                                        else if ($severity == 4) $sev_hf = _L('Errors');
94
                                        else if ($severity == 5) $sev_hf = _L('Critical issues');
95
                                        else $sev_hf = _L('Severity %1', $severity-1);
96
 
97
                                        $out['text'] .= '<h2><span class="severity_'.$severity.'">'.$sev_hf.'</span></h2>';
98
                                        foreach ($htmlMessages as $htmlMessage) {
99
                                                $out['text'] .= '<p><span class="severity_'.$severity.'">'.$htmlMessage.'</span></p>';
100
                                        }
101
                                }
102
                        }
103
                }
104
        }
105
 
106
        public function tree(&$json, $ra_email=null, $nonjs=false, $req_goto='') {
107
                if (!OIDplus::authUtils()->isAdminLoggedIn()) return false;
108
 
109
                if (file_exists(__DIR__.'/img/main_icon16.png')) {
110
                        $tree_icon = OIDplus::webpath(__DIR__,OIDplus::PATH_RELATIVE).'img/main_icon16.png';
111
                } else {
112
                        $tree_icon = null; // default icon (folder)
113
                }
114
 
115
                $json[] = array(
116
                        'id' => 'oidplus:notifications$admin',
117
                        'icon' => $tree_icon,
118
                        'text' => _L('Notifications')
119
                );
120
 
121
                return true;
122
        }
123
 
124
        public function tree_search($request) {
125
                return false;
126
        }
1006 daniel-mar 127
 
128
        public function implementsFeature($id) {
129
                if (strtolower($id) == '1.3.6.1.4.1.37476.2.5.2.3.8') return true; // getNotifications()
130
                return false;
131
        }
132
 
133
        private function getNotificationsCheckDirAccess($dir) {
134
                $notifications = array();
135
                // Attention! This check does not work if OIDplus is password protected!
136
                //            The only real solution is to check via JavaScript, which is done by setup/
137
                $url = OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE_CANONICAL).$dir;
138
                $access_worked = url_get_contents($url) !== false;
139
 
140
                if (!$access_worked) {
141
                        $url_alt = OIDplus::webpath(null,OIDplus::PATH_ABSOLUTE).$dir;
142
                        if ($url != $url_alt) {
143
                                $access_worked = url_get_contents($url_alt) !== false;
144
                        }
145
                }
146
 
147
                if ($access_worked) {
148
                        // Re-use message taken from setup/includes/setup_base.js
149
                        $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>');
150
                        $notifications[] = array('CRIT', $msg);
151
                }
152
                return $notifications;
153
        }
154
 
155
        public function getNotifications($user=null): array {
156
                // Interface 1.3.6.1.4.1.37476.2.5.2.3.8
157
                // These are some basic "system" checks, no checks from other plugin. So we add them to our plugin instead.
158
                $notifications = array();
159
                if ((!$user || ($user == 'admin')) && OIDplus::authUtils()->isAdminLoggedIn()) {
160
                        // Check if critical directories are world-readable
161
                        // see setup/includes/setup_base.js
162
                        $forbidden_dirs = array(
163
                                "userdata/index.html",
164
                                "res/ATTENTION.TXT",
165
                                "dev/index.html",
166
                                "includes/index.html",
167
                                "setup/includes/index.html"
168
                                //"plugins/viathinksoft/publicPages/100_whois/whois/cli/index.html"
169
                        );
170
                        foreach ($forbidden_dirs as $dir) {
171
                                $notifications = array_merge($notifications, $this->getNotificationsCheckDirAccess($dir));
172
                        }
173
 
174
                        // Check if cache directory is writeable
175
                        if (!is_writeable(OIDplus::localpath(null).'userdata/cache/')) {
176
                                $notifications[] = array('ERR', _L('Directory %1 is not writeable.', 'userdata/cache/'));
177
                        }
178
                }
179
                return $notifications;
180
        }
181
 
1000 daniel-mar 182
}