]>
Commit | Line | Data |
---|---|---|
6df4b805 MR |
1 | <?php |
2 | /* vim: set ts=4 tw=0 sw=4 noet: */ | |
3 | require_once $CFG->root .'config.php'; | |
4 | ||
3056d117 MR |
5 | class Utils { |
6 | ||
2b6294e9 | 7 | private $timeout = false; |
3056d117 | 8 | private $settings; |
01cc21cf MR |
9 | private $log_level; |
10 | private $log_method; | |
b95d1cdb | 11 | private $header = '<!DOCTYPE html> |
6df4b805 MR |
12 | <html> |
13 | <head> | |
b95d1cdb MR |
14 | <meta charset="utf-8"> |
15 | <link rel="stylesheet" href="css/styles.css"> | |
16 | <script> | |
17 | var timeout = __TIMEOUT__; | |
18 | </script> | |
19 | <script src="__ROOT__js/timer.js"></script> | |
5c7b972e | 20 | <script src="__ROOT__js/checkbox.js"></script> |
b95d1cdb | 21 | <title>__TITLE__</title> |
6df4b805 MR |
22 | </head> |
23 | <body>'; | |
bb06f172 | 24 | private $footer = '<p class="footer">Powered by <a href="https://qtadmin.datanom.net" |
b706c65f | 25 | title="Goto QtAdmin homepage">QtAdmin</a>. © 2015 by Michael Rasmussen</p></body></html>'; |
b95d1cdb MR |
26 | private $heading = '<p id="time" class="time">Session timeout: |
27 | <span id="timer"></span></p><h1 class="h1">__TITLE__</h1>'; | |
28 | ||
3056d117 | 29 | public function __construct() { |
b95d1cdb MR |
30 | global $CFG; |
31 | ||
01cc21cf MR |
32 | if (isset($CFG->log_level)) { |
33 | $this->log_level = $CFG->log_level; | |
34 | } else { | |
35 | $this->log_level = 1; | |
36 | } | |
37 | ||
38 | if (isset($CFG->log_method)) { | |
39 | $this->log_method = $CFG->log_method; | |
40 | } else { | |
41 | $this->log_level = 'syslog'; | |
42 | } | |
43 | ||
519a15b5 MR |
44 | $this->log("Init Utils", 4); |
45 | ||
9da61a01 | 46 | $this->log("__construct[1]: user ".var_export($this->settings['user'], true), 3); |
a675b383 | 47 | $this->startSession(); |
9da61a01 | 48 | $this->log("__construct[2]: user ".var_export($this->settings['user'], true), 3); |
a675b383 | 49 | |
3056d117 MR |
50 | if (! isset($_SESSION['settings'])) { |
51 | $this->initSettings(); | |
52 | } | |
9da61a01 | 53 | $this->log("__construct[3]: user ".var_export($this->settings['user'], true), 3); |
3056d117 | 54 | $this->settings = $_SESSION['settings']; |
9da61a01 | 55 | $this->log("__construct[4]: user ".var_export($this->settings['user'], true), 3); |
3056d117 MR |
56 | |
57 | if ($CFG->auth_method == 'HTTP_AUTH') { | |
86fb546e MR |
58 | if (isset($_SERVER['PHP_AUTH_USER'])) { |
59 | $this->settings['user'] = $_SERVER['PHP_AUTH_USER']; | |
3056d117 MR |
60 | $this->settings['loginStatus'] = 'OK'; |
61 | if ($CFG->admin_user == $this->settings['user']) | |
62 | $this->settings['admin'] = true; | |
b95d1cdb MR |
63 | } |
64 | } | |
b95d1cdb MR |
65 | } |
66 | ||
01cc21cf MR |
67 | private function log($message, $level = 1) { |
68 | global $CFG; | |
69 | ||
70 | if ($level > $this->log_level) | |
71 | return; | |
72 | ||
73 | $time = date('c'); | |
01cc21cf MR |
74 | |
75 | $priority = LOG_INFO; | |
76 | switch ($level) { | |
77 | case 1: $priority = LOG_ERR; break; | |
78 | case 2: $priority = LOG_WARNING; break; | |
79 | case 3: $priority = LOG_INFO; break; | |
80 | case 4: $priority = LOG_DEBUG; break; | |
81 | } | |
82 | ||
83 | switch ($this->log_method) { | |
84 | case 'file': | |
7b561609 MR |
85 | if (isset($CFG->log_file)) { |
86 | if ($CFG->log_file[0] == '/') { | |
87 | $file = $CFG->log_file; | |
88 | } else { | |
89 | $file = $CFG->root.$CFG->log_file; | |
90 | } | |
91 | } else { | |
92 | $file = $CFG->root.'qtadmin.log'; | |
93 | } | |
815fed0c | 94 | file_put_contents($file, "[$time]: $message\n", FILE_APPEND | LOCK_EX); |
7b561609 | 95 | break; |
01cc21cf | 96 | case 'stderr': |
815fed0c | 97 | file_put_contents('php://stderr', "[$time]: $message\n"); |
7b561609 | 98 | break; |
01cc21cf | 99 | case 'syslog': |
2dd58fe8 | 100 | syslog($priority, $message); |
01cc21cf | 101 | break; |
d6be2d1a | 102 | } |
01cc21cf MR |
103 | } |
104 | ||
3056d117 | 105 | private function initSettings() { |
2dd58fe8 MR |
106 | $this->log("InitSettings", 4); |
107 | ||
3056d117 MR |
108 | if ('' == session_id()) { |
109 | $this->startSession(); | |
110 | } | |
b95d1cdb | 111 | |
2b6294e9 MR |
112 | if (false !== $this->timeout) { |
113 | $timeout = $this->timeout; | |
114 | } else { | |
115 | $timeout = 0; | |
116 | } | |
117 | ||
3056d117 | 118 | $this->settings = array( |
3056d117 MR |
119 | 'user' => null, |
120 | 'admin' => false, | |
121 | 'loginStatus' => 'Not logged in', | |
2b6294e9 | 122 | 'timeout' => $timeout |
3056d117 | 123 | ); |
6072c905 | 124 | |
3056d117 | 125 | $_SESSION['settings'] = $this->settings; |
6072c905 MR |
126 | } |
127 | ||
a675b383 | 128 | private function startSession() { |
b95d1cdb MR |
129 | global $CFG; |
130 | ||
2dd58fe8 MR |
131 | $this->log("startSession", 4); |
132 | ||
b95d1cdb | 133 | if (isset($CFG->session_timeout)) { |
2b6294e9 | 134 | $this->timeout = $CFG->session_timeout * 60; |
b95d1cdb | 135 | } else { |
2b6294e9 | 136 | $this->timeout = 20 * 60; |
b95d1cdb MR |
137 | } |
138 | ||
2b6294e9 MR |
139 | if (ini_get('session.gc_maxlifetime') != $this->timeout) |
140 | ini_set('session.gc_maxlifetime', $this->timeout); | |
7b561609 MR |
141 | //if (ini_get('session.cookie_lifetime') != $this->timeout) |
142 | // ini_set('session.cookie_lifetime', $this->timeout); | |
143 | ini_set('session.cookie_lifetime', 0); | |
a675b383 MR |
144 | |
145 | session_start(); | |
7d9c7fe2 MR |
146 | } |
147 | ||
3056d117 | 148 | private function checkSession() { |
7d9c7fe2 MR |
149 | global $CFG; |
150 | ||
2dd58fe8 MR |
151 | $this->log("checkSession", 4); |
152 | ||
39023189 MR |
153 | if ('' == session_id()) { |
154 | $this->startSession(); | |
155 | } | |
156 | ||
b95d1cdb | 157 | $time = $_SERVER['REQUEST_TIME']; |
7d9c7fe2 | 158 | if (isset($_SESSION['LAST_ACTIVITY']) && |
3056d117 | 159 | ($time - $_SESSION['LAST_ACTIVITY']) >= $this->settings['timeout']) { |
07124c37 MR |
160 | $this->log('R_TIME: '.date('c', $time).' L_ACT: '.date('c', $_SESSION['LAST_ACTIVITY']. |
161 | 'Test: '.($time - $_SESSION['LAST_ACTIVITY'])).' >= '.$this->settings['timeout'], 3); | |
3056d117 | 162 | $this->logout(); |
a675b383 MR |
163 | } else { |
164 | $_SESSION['LAST_ACTIVITY'] = $time; | |
165 | } | |
b95d1cdb MR |
166 | } |
167 | ||
168 | public function logout() { | |
2dd58fe8 MR |
169 | $this->log("logout", 4); |
170 | ||
b95d1cdb MR |
171 | if (ini_get('session.use_cookies')) { |
172 | $params = session_get_cookie_params(); | |
173 | setcookie(session_name(), '', time() - 42000, | |
174 | $params['path'], $params['domain'], | |
175 | $params['secure'], $params['httponly']); | |
176 | } | |
39023189 MR |
177 | |
178 | if ('' != session_id()) { | |
179 | $_SESSION = array(); | |
180 | session_unset(); | |
181 | session_destroy(); | |
182 | } | |
3056d117 | 183 | $this->settings = array(); |
b95d1cdb MR |
184 | } |
185 | ||
186 | public function isAdmin() { | |
3056d117 MR |
187 | $admin = false; |
188 | ||
2dd58fe8 MR |
189 | $this->log("isAdmin", 4); |
190 | ||
3056d117 MR |
191 | if (isset($this->settings['admin'])) { |
192 | $admin = $this->settings['admin']; | |
193 | } | |
194 | ||
195 | return $admin; | |
b95d1cdb MR |
196 | } |
197 | ||
198 | public function login($user, $pw) { | |
199 | global $CFG; | |
200 | $result = false; | |
201 | ||
2dd58fe8 MR |
202 | $this->log("login", 4); |
203 | ||
3056d117 MR |
204 | if ('' == session_id()) { |
205 | $this->startSession(); | |
206 | } | |
207 | ||
208 | $this->settings['user'] = null; | |
209 | $this->settings['admin'] = false; | |
b95d1cdb MR |
210 | |
211 | $p = explode('@', $user); | |
212 | if (count($p) != 2) { | |
3056d117 MR |
213 | $this->settings['loginStatus'] = 'Bad username'; |
214 | } else { | |
215 | $domain = $p[1]; | |
216 | $dn = "mail=$user,ou=Users,domainName=$domain,$CFG->ldap_base_dn"; | |
217 | $filter = "(&(objectclass=mailUser)(accountStatus=active)(mail=$user))"; | |
218 | $ds = @ldap_connect($CFG->ldap_dsn); | |
219 | if ($ds) { | |
220 | @ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3); | |
221 | $r = @ldap_bind($ds, $dn, $pw); | |
222 | if ($r) { | |
223 | $sr = @ldap_search($ds, $CFG->ldap_base_dn, $filter, array('mail','domainglobaladmin')); | |
224 | $info = @ldap_get_entries($ds, $sr); // array | |
225 | if ($info['count'] > 0) { | |
226 | $this->settings['user'] = $user; | |
227 | $result = true; | |
228 | $this->settings['loginStatus'] = 'OK'; | |
229 | $admin = 'NO'; | |
230 | if (isset($info[0]['domainglobaladmin'])) { | |
231 | $admin = $info[0]['domainglobaladmin'][0]; | |
232 | $admin = strtoupper($admin); | |
233 | } | |
234 | $this->settings['admin'] = ($admin == 'YES') ? true : false; | |
235 | } else { | |
236 | $this->settings['loginStatus'] = 'Login failed'; | |
b95d1cdb | 237 | } |
6df4b805 | 238 | } else { |
3056d117 | 239 | $this->settings['loginStatus'] = ldap_error($ds); |
6df4b805 | 240 | } |
3056d117 | 241 | @ldap_close($ds); |
6df4b805 | 242 | } else { |
3056d117 | 243 | $this->settings['loginStatus'] = 'Connect to LDAP server failed'; |
6df4b805 | 244 | } |
6df4b805 MR |
245 | } |
246 | ||
3056d117 | 247 | $_SESSION['settings'] = $this->settings; |
6e081c5f | 248 | |
b95d1cdb MR |
249 | return $result; |
250 | } | |
251 | ||
252 | public function getLoginStatus() { | |
3056d117 MR |
253 | $status = 'Not logged in'; |
254 | ||
2dd58fe8 MR |
255 | $this->log("getLoginStatus", 4); |
256 | ||
3056d117 MR |
257 | if (isset($this->settings['loginStatus'])) { |
258 | $status = $this->settings['loginStatus']; | |
259 | } | |
260 | ||
261 | return $status; | |
b95d1cdb MR |
262 | } |
263 | ||
264 | public function isLoggedIn() { | |
265 | global $CFG; | |
266 | $loggedIn = false; | |
267 | ||
65f27692 | 268 | $this->log("isLoggedIn[1]: user ".var_export($this->settings['user'], true), 3); |
2dd58fe8 | 269 | |
3056d117 MR |
270 | if ('' == session_id()) { |
271 | $this->startSession(); | |
272 | } | |
273 | ||
65f27692 | 274 | $this->log("isLoggedIn[2]: user ".var_export($this->settings['user'], true), 3); |
39023189 | 275 | $this->checkSession(); |
65f27692 | 276 | $this->log("isLoggedIn[3]: user ".var_export($this->settings['user'], true), 3); |
39023189 | 277 | |
3056d117 MR |
278 | if (isset($this->settings['user'])) { |
279 | if ($this->settings['user'] != null) { | |
280 | $loggedIn = true; | |
281 | } else { | |
282 | if ($CFG->auth_method == 'HTTP_AUTH') { | |
86fb546e MR |
283 | if (isset($_SERVER['PHP_AUTH_USER'])) { |
284 | $this->settings['user'] = $_SERVER['PHP_AUTH_USER']; | |
3056d117 MR |
285 | $loggedIn = true; |
286 | } | |
b95d1cdb MR |
287 | } |
288 | } | |
289 | } | |
290 | ||
85ec6a84 | 291 | if ($loggedIn == false) { |
7b561609 MR |
292 | $this->log('$this->settings: '.var_export($this->settings, true), 3); |
293 | $this->log('R_TIME: '.date('c', $_SERVER['REQUEST_TIME']).' L_ACT: '.date('c', $_SESSION['LAST_ACTIVITY']), 3); | |
18d80742 | 294 | } |
6e081c5f | 295 | |
3056d117 | 296 | $_SESSION['settings'] = $this->settings; |
6e081c5f | 297 | |
b95d1cdb MR |
298 | return $loggedIn; |
299 | } | |
300 | ||
301 | public function getUser() { | |
3056d117 MR |
302 | $user = null; |
303 | ||
2dd58fe8 MR |
304 | $this->log("getUser", 4); |
305 | ||
3056d117 MR |
306 | if ($this->isLoggedIn()) { |
307 | $user = $this->settings['user']; | |
308 | } | |
309 | ||
310 | return $user; | |
b95d1cdb MR |
311 | } |
312 | ||
313 | public function getHeader() { | |
2dd58fe8 MR |
314 | $this->log("getHeader", 4); |
315 | ||
b95d1cdb MR |
316 | return $this->header; |
317 | } | |
318 | ||
319 | public function getFooter() { | |
2dd58fe8 MR |
320 | $this->log("getFooter", 4); |
321 | ||
b95d1cdb MR |
322 | return $this->footer; |
323 | } | |
324 | ||
325 | public function getHeading() { | |
2dd58fe8 MR |
326 | $this->log("getHeading", 4); |
327 | ||
b95d1cdb MR |
328 | return $this->heading; |
329 | } | |
330 | ||
331 | public function setHeading($heading) { | |
332 | global $CFG; | |
333 | ||
2dd58fe8 MR |
334 | $this->log("setHeading", 4); |
335 | ||
b95d1cdb MR |
336 | $timeout = $CFG->session_timeout * 60 * 1000; |
337 | $this->heading = str_replace('__TITLE__', $heading, $this->heading); | |
338 | $this->header = str_replace('__TITLE__', $heading, $this->header); | |
339 | $this->header = str_replace('__ROOT__', $CFG->wwwroot, $this->header); | |
340 | $this->header = str_replace('__TIMEOUT__', $timeout, $this->header); | |
341 | } | |
342 | ||
343 | public function convertContent($code) { | |
2dd58fe8 MR |
344 | $this->log("convertContent", 4); |
345 | ||
b95d1cdb MR |
346 | $table = array( |
347 | 'V' => 'Virus', | |
348 | 'B' => 'Banned', | |
349 | 'U' => 'Unchecked', | |
350 | 'S' => 'Spam', | |
351 | 'Y' => 'Spammy', | |
352 | 'M' => 'Bad Mime', | |
353 | 'H' => 'Bad Header', | |
354 | 'O' => 'Over sized', | |
355 | 'T' => 'MTA err', | |
356 | 'C' => 'Clean' | |
357 | ); | |
358 | ||
359 | $string = $table[$code]; | |
360 | if (empty($string)) | |
361 | $string = 'Unknown'; | |
362 | ||
363 | return $string; | |
364 | } | |
6df4b805 | 365 | |
6df4b805 | 366 | } |