]>
Commit | Line | Data |
---|---|---|
6df4b805 | 1 | <?php |
6b3d5ba9 | 2 | /* vim: set ts=4 tw=0 sw=4 noet: */ |
6df4b805 | 3 | require_once $CFG->root .'config.php'; |
6ead258e | 4 | require_once $CFG->root . 'lib/session_handler.inc.php'; |
6df4b805 | 5 | |
3056d117 MR |
6 | class Utils { |
7 | ||
2b6294e9 | 8 | private $timeout = false; |
3056d117 | 9 | private $settings; |
01cc21cf MR |
10 | private $log_level; |
11 | private $log_method; | |
b95d1cdb | 12 | private $header = '<!DOCTYPE html> |
6df4b805 MR |
13 | <html> |
14 | <head> | |
b95d1cdb MR |
15 | <meta charset="utf-8"> |
16 | <link rel="stylesheet" href="css/styles.css"> | |
17 | <script> | |
18 | var timeout = __TIMEOUT__; | |
19 | </script> | |
20 | <script src="__ROOT__js/timer.js"></script> | |
5c7b972e | 21 | <script src="__ROOT__js/checkbox.js"></script> |
7ef22e21 | 22 | <script src="__ROOT__js/forms.js"></script> |
b95d1cdb | 23 | <title>__TITLE__</title> |
6df4b805 | 24 | </head> |
60aad80e MR |
25 | <body><div id="container">'; |
26 | private $footer = '</div><div id="footer"><p>Powered by <a href="https://qtadmin.datanom.net" | |
27 | title="Goto QtAdmin homepage">QtAdmin</a>. © 2015 by Michael Rasmussen</p> | |
3039de29 | 28 | </div></body></html>'; |
b95d1cdb MR |
29 | private $heading = '<p id="time" class="time">Session timeout: |
30 | <span id="timer"></span></p><h1 class="h1">__TITLE__</h1>'; | |
31 | ||
3056d117 | 32 | public function __construct() { |
b95d1cdb MR |
33 | global $CFG; |
34 | ||
01cc21cf MR |
35 | if (isset($CFG->log_level)) { |
36 | $this->log_level = $CFG->log_level; | |
37 | } else { | |
38 | $this->log_level = 1; | |
39 | } | |
40 | ||
41 | if (isset($CFG->log_method)) { | |
42 | $this->log_method = $CFG->log_method; | |
43 | } else { | |
44 | $this->log_level = 'syslog'; | |
45 | } | |
46 | ||
519a15b5 MR |
47 | $this->log("Init Utils", 4); |
48 | ||
9da61a01 | 49 | $this->log("__construct[1]: user ".var_export($this->settings['user'], true), 3); |
a675b383 | 50 | $this->startSession(); |
9da61a01 | 51 | $this->log("__construct[2]: user ".var_export($this->settings['user'], true), 3); |
a675b383 | 52 | |
3056d117 MR |
53 | if (! isset($_SESSION['settings'])) { |
54 | $this->initSettings(); | |
55 | } | |
9da61a01 | 56 | $this->log("__construct[3]: user ".var_export($this->settings['user'], true), 3); |
3056d117 | 57 | $this->settings = $_SESSION['settings']; |
9da61a01 | 58 | $this->log("__construct[4]: user ".var_export($this->settings['user'], true), 3); |
3056d117 MR |
59 | |
60 | if ($CFG->auth_method == 'HTTP_AUTH') { | |
86fb546e MR |
61 | if (isset($_SERVER['PHP_AUTH_USER'])) { |
62 | $this->settings['user'] = $_SERVER['PHP_AUTH_USER']; | |
3056d117 MR |
63 | $this->settings['loginStatus'] = 'OK'; |
64 | if ($CFG->admin_user == $this->settings['user']) | |
65 | $this->settings['admin'] = true; | |
b95d1cdb MR |
66 | } |
67 | } | |
b95d1cdb MR |
68 | } |
69 | ||
01cc21cf MR |
70 | private function log($message, $level = 1) { |
71 | global $CFG; | |
72 | ||
73 | if ($level > $this->log_level) | |
74 | return; | |
75 | ||
76 | $time = date('c'); | |
01cc21cf MR |
77 | |
78 | $priority = LOG_INFO; | |
79 | switch ($level) { | |
80 | case 1: $priority = LOG_ERR; break; | |
81 | case 2: $priority = LOG_WARNING; break; | |
82 | case 3: $priority = LOG_INFO; break; | |
83 | case 4: $priority = LOG_DEBUG; break; | |
84 | } | |
85 | ||
86 | switch ($this->log_method) { | |
87 | case 'file': | |
7b561609 MR |
88 | if (isset($CFG->log_file)) { |
89 | if ($CFG->log_file[0] == '/') { | |
90 | $file = $CFG->log_file; | |
91 | } else { | |
92 | $file = $CFG->root.$CFG->log_file; | |
93 | } | |
94 | } else { | |
95 | $file = $CFG->root.'qtadmin.log'; | |
96 | } | |
815fed0c | 97 | file_put_contents($file, "[$time]: $message\n", FILE_APPEND | LOCK_EX); |
ecc5e773 | 98 | chmod($file, 0600); |
7b561609 | 99 | break; |
01cc21cf | 100 | case 'stderr': |
815fed0c | 101 | file_put_contents('php://stderr', "[$time]: $message\n"); |
7b561609 | 102 | break; |
01cc21cf | 103 | case 'syslog': |
2dd58fe8 | 104 | syslog($priority, $message); |
01cc21cf | 105 | break; |
d6be2d1a | 106 | } |
01cc21cf MR |
107 | } |
108 | ||
3056d117 | 109 | private function initSettings() { |
2dd58fe8 MR |
110 | $this->log("InitSettings", 4); |
111 | ||
3056d117 MR |
112 | if ('' == session_id()) { |
113 | $this->startSession(); | |
114 | } | |
b95d1cdb | 115 | |
2b6294e9 MR |
116 | if (false !== $this->timeout) { |
117 | $timeout = $this->timeout; | |
118 | } else { | |
119 | $timeout = 0; | |
120 | } | |
121 | ||
3056d117 | 122 | $this->settings = array( |
3056d117 MR |
123 | 'user' => null, |
124 | 'admin' => false, | |
125 | 'loginStatus' => 'Not logged in', | |
2b6294e9 | 126 | 'timeout' => $timeout |
3056d117 | 127 | ); |
6072c905 | 128 | |
3056d117 | 129 | $_SESSION['settings'] = $this->settings; |
6072c905 MR |
130 | } |
131 | ||
a675b383 | 132 | private function startSession() { |
b95d1cdb MR |
133 | global $CFG; |
134 | ||
2dd58fe8 MR |
135 | $this->log("startSession", 4); |
136 | ||
b95d1cdb | 137 | if (isset($CFG->session_timeout)) { |
2b6294e9 | 138 | $this->timeout = $CFG->session_timeout * 60; |
b95d1cdb | 139 | } else { |
2b6294e9 | 140 | $this->timeout = 20 * 60; |
b95d1cdb MR |
141 | } |
142 | ||
2b6294e9 MR |
143 | if (ini_get('session.gc_maxlifetime') != $this->timeout) |
144 | ini_set('session.gc_maxlifetime', $this->timeout); | |
7b561609 MR |
145 | //if (ini_get('session.cookie_lifetime') != $this->timeout) |
146 | // ini_set('session.cookie_lifetime', $this->timeout); | |
147 | ini_set('session.cookie_lifetime', 0); | |
a675b383 MR |
148 | |
149 | session_start(); | |
7d9c7fe2 MR |
150 | } |
151 | ||
3056d117 | 152 | private function checkSession() { |
7d9c7fe2 MR |
153 | global $CFG; |
154 | ||
2dd58fe8 MR |
155 | $this->log("checkSession", 4); |
156 | ||
39023189 MR |
157 | if ('' == session_id()) { |
158 | $this->startSession(); | |
159 | } | |
160 | ||
b95d1cdb | 161 | $time = $_SERVER['REQUEST_TIME']; |
7d9c7fe2 | 162 | if (isset($_SESSION['LAST_ACTIVITY']) && |
3056d117 | 163 | ($time - $_SESSION['LAST_ACTIVITY']) >= $this->settings['timeout']) { |
07124c37 MR |
164 | $this->log('R_TIME: '.date('c', $time).' L_ACT: '.date('c', $_SESSION['LAST_ACTIVITY']. |
165 | 'Test: '.($time - $_SESSION['LAST_ACTIVITY'])).' >= '.$this->settings['timeout'], 3); | |
3056d117 | 166 | $this->logout(); |
a675b383 MR |
167 | } else { |
168 | $_SESSION['LAST_ACTIVITY'] = $time; | |
169 | } | |
b95d1cdb MR |
170 | } |
171 | ||
0da9e6e7 MR |
172 | private function getCSRFPreventionToken($ticket) { |
173 | return array('CSRFPreventionToken: ' . $ticket->CSRFPreventionToken); | |
174 | } | |
175 | ||
176 | private function getRestTicket($username, $password) { | |
6ba8e4d3 MR |
177 | global $CFG; |
178 | ||
0da9e6e7 MR |
179 | $result = false; |
180 | $url = $CFG->wblistadm_url . '/ticket'; | |
181 | ||
182 | $data = "username=$username&password=$password"; | |
183 | $response = $this->RESTCall($url, $data, $cookiesIn = ''); | |
184 | if ($response['http_code'] >= 200 && $response['http_code'] <= 204) { | |
185 | $data = json_decode($response['content']); | |
186 | $_SESSION['ticket'] = $data->data; | |
187 | $_SESSION['cookies'] = $response['cookies']; | |
188 | $result = true; | |
189 | } | |
190 | ||
191 | return $result; | |
192 | } | |
193 | ||
194 | public function makeRestCall($method, $data = null) { | |
6ba8e4d3 MR |
195 | global $CFG; |
196 | ||
0da9e6e7 MR |
197 | $result; |
198 | ||
f1c0988b | 199 | $url = $CFG->wblistadm_url . "$method"; |
0da9e6e7 MR |
200 | $token = $this->getCSRFPreventionToken($_SESSION['ticket']); |
201 | $response = $this->RESTCall($url, $data, $_SESSION['cookies'], $token); | |
202 | ||
203 | if ($response['http_code'] >= 200 && $response['http_code'] <= 204) { | |
204 | if ($data) { | |
205 | // HTTP POST | |
206 | $result = true; | |
207 | } else { | |
208 | // HTTP GET | |
209 | $data = json_decode($response['content']); | |
210 | $result = $data->data; | |
211 | } | |
212 | } else { | |
213 | $result = ($data) ? false : array(); | |
214 | } | |
215 | ||
216 | return $result; | |
217 | } | |
218 | ||
219 | private function RESTCall($url, $data = null, $cookiesIn = '', $headers = null) { | |
220 | $options = array( | |
221 | CURLOPT_RETURNTRANSFER => true, // return web page | |
222 | CURLOPT_HEADER => true, //return headers in addition to content | |
223 | CURLOPT_FOLLOWLOCATION => true, // follow redirects | |
224 | CURLOPT_ENCODING => "", // handle all encodings | |
225 | CURLOPT_AUTOREFERER => true, // set referer on redirect | |
226 | CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect | |
227 | CURLOPT_TIMEOUT => 120, // timeout on response | |
228 | CURLOPT_MAXREDIRS => 10, // stop after 10 redirects | |
229 | CURLINFO_HEADER_OUT => true, | |
230 | CURLOPT_SSL_VERIFYPEER => false, // Disabled SSL Cert checks | |
231 | CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, | |
232 | CURLOPT_COOKIE => $cookiesIn | |
233 | ); | |
234 | ||
235 | if ($data) { | |
236 | $options[CURLOPT_POST] = 1; | |
237 | $options[CURLOPT_POSTFIELDS] = $data; | |
238 | } | |
239 | ||
240 | if ($headers) { | |
241 | $options[CURLOPT_HTTPHEADER] = $headers; | |
242 | } | |
243 | ||
244 | $ch = curl_init($url); | |
245 | curl_setopt_array($ch, $options); | |
246 | $rough_content = curl_exec($ch); | |
247 | $err = curl_errno($ch); | |
248 | $errmsg = curl_error($ch); | |
249 | $header = curl_getinfo($ch); | |
250 | curl_close($ch); | |
251 | ||
252 | $header_content = substr($rough_content, 0, $header['header_size']); | |
253 | $body_content = trim(str_replace($header_content, '', $rough_content)); | |
254 | $pattern = "#Set-Cookie:\\s+(?<cookie>[^=]+=[^;]+)#m"; | |
255 | preg_match_all($pattern, $header_content, $matches); | |
256 | $cookiesOut = implode("; ", $matches['cookie']); | |
257 | ||
258 | $header['errno'] = $err; | |
259 | $header['errmsg'] = $errmsg; | |
260 | $header['headers'] = $header_content; | |
261 | $header['content'] = $body_content; | |
262 | $header['cookies'] = $cookiesOut; | |
263 | ||
264 | return $header; | |
265 | } | |
266 | ||
b95d1cdb | 267 | public function logout() { |
2dd58fe8 MR |
268 | $this->log("logout", 4); |
269 | ||
b95d1cdb MR |
270 | if (ini_get('session.use_cookies')) { |
271 | $params = session_get_cookie_params(); | |
272 | setcookie(session_name(), '', time() - 42000, | |
273 | $params['path'], $params['domain'], | |
274 | $params['secure'], $params['httponly']); | |
275 | } | |
39023189 MR |
276 | |
277 | if ('' != session_id()) { | |
278 | $_SESSION = array(); | |
279 | session_unset(); | |
280 | session_destroy(); | |
281 | } | |
3056d117 | 282 | $this->settings = array(); |
b95d1cdb MR |
283 | } |
284 | ||
285 | public function isAdmin() { | |
3056d117 MR |
286 | $admin = false; |
287 | ||
2dd58fe8 MR |
288 | $this->log("isAdmin", 4); |
289 | ||
3056d117 MR |
290 | if (isset($this->settings['admin'])) { |
291 | $admin = $this->settings['admin']; | |
292 | } | |
293 | ||
294 | return $admin; | |
b95d1cdb MR |
295 | } |
296 | ||
297 | public function login($user, $pw) { | |
298 | global $CFG; | |
299 | $result = false; | |
300 | ||
2dd58fe8 MR |
301 | $this->log("login", 4); |
302 | ||
3056d117 MR |
303 | if ('' == session_id()) { |
304 | $this->startSession(); | |
305 | } | |
306 | ||
307 | $this->settings['user'] = null; | |
308 | $this->settings['admin'] = false; | |
b95d1cdb MR |
309 | |
310 | $p = explode('@', $user); | |
311 | if (count($p) != 2) { | |
3056d117 MR |
312 | $this->settings['loginStatus'] = 'Bad username'; |
313 | } else { | |
314 | $domain = $p[1]; | |
315 | $dn = "mail=$user,ou=Users,domainName=$domain,$CFG->ldap_base_dn"; | |
316 | $filter = "(&(objectclass=mailUser)(accountStatus=active)(mail=$user))"; | |
317 | $ds = @ldap_connect($CFG->ldap_dsn); | |
318 | if ($ds) { | |
319 | @ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3); | |
320 | $r = @ldap_bind($ds, $dn, $pw); | |
321 | if ($r) { | |
322 | $sr = @ldap_search($ds, $CFG->ldap_base_dn, $filter, array('mail','domainglobaladmin')); | |
323 | $info = @ldap_get_entries($ds, $sr); // array | |
324 | if ($info['count'] > 0) { | |
5ec97892 | 325 | // Log in to wblistadm server and get CSRFPreventionToken |
0da9e6e7 MR |
326 | if ($this->getRestTicket($user, $pw)) { |
327 | $this->settings['user'] = $user; | |
328 | $result = true; | |
329 | $this->settings['loginStatus'] = 'OK'; | |
330 | $admin = 'NO'; | |
331 | if (isset($info[0]['domainglobaladmin'])) { | |
332 | $admin = $info[0]['domainglobaladmin'][0]; | |
333 | $admin = strtoupper($admin); | |
334 | } | |
335 | $this->settings['admin'] = ($admin == 'YES') ? true : false; | |
336 | } else { | |
337 | $this->settings['loginStatus'] = 'Login failed'; | |
338 | } | |
3056d117 MR |
339 | } else { |
340 | $this->settings['loginStatus'] = 'Login failed'; | |
b95d1cdb | 341 | } |
6df4b805 | 342 | } else { |
3056d117 | 343 | $this->settings['loginStatus'] = ldap_error($ds); |
6df4b805 | 344 | } |
3056d117 | 345 | @ldap_close($ds); |
6df4b805 | 346 | } else { |
3056d117 | 347 | $this->settings['loginStatus'] = 'Connect to LDAP server failed'; |
6df4b805 | 348 | } |
6df4b805 MR |
349 | } |
350 | ||
3056d117 | 351 | $_SESSION['settings'] = $this->settings; |
6e081c5f | 352 | |
b95d1cdb MR |
353 | return $result; |
354 | } | |
355 | ||
356 | public function getLoginStatus() { | |
3056d117 MR |
357 | $status = 'Not logged in'; |
358 | ||
2dd58fe8 MR |
359 | $this->log("getLoginStatus", 4); |
360 | ||
3056d117 MR |
361 | if (isset($this->settings['loginStatus'])) { |
362 | $status = $this->settings['loginStatus']; | |
363 | } | |
364 | ||
365 | return $status; | |
b95d1cdb MR |
366 | } |
367 | ||
368 | public function isLoggedIn() { | |
369 | global $CFG; | |
370 | $loggedIn = false; | |
371 | ||
65f27692 | 372 | $this->log("isLoggedIn[1]: user ".var_export($this->settings['user'], true), 3); |
2dd58fe8 | 373 | |
3056d117 MR |
374 | if ('' == session_id()) { |
375 | $this->startSession(); | |
376 | } | |
377 | ||
65f27692 | 378 | $this->log("isLoggedIn[2]: user ".var_export($this->settings['user'], true), 3); |
39023189 | 379 | $this->checkSession(); |
65f27692 | 380 | $this->log("isLoggedIn[3]: user ".var_export($this->settings['user'], true), 3); |
39023189 | 381 | |
3056d117 MR |
382 | if (isset($this->settings['user'])) { |
383 | if ($this->settings['user'] != null) { | |
384 | $loggedIn = true; | |
385 | } else { | |
386 | if ($CFG->auth_method == 'HTTP_AUTH') { | |
86fb546e MR |
387 | if (isset($_SERVER['PHP_AUTH_USER'])) { |
388 | $this->settings['user'] = $_SERVER['PHP_AUTH_USER']; | |
3056d117 MR |
389 | $loggedIn = true; |
390 | } | |
b95d1cdb MR |
391 | } |
392 | } | |
393 | } | |
394 | ||
85ec6a84 | 395 | if ($loggedIn == false) { |
7b561609 MR |
396 | $this->log('$this->settings: '.var_export($this->settings, true), 3); |
397 | $this->log('R_TIME: '.date('c', $_SERVER['REQUEST_TIME']).' L_ACT: '.date('c', $_SESSION['LAST_ACTIVITY']), 3); | |
18d80742 | 398 | } |
6e081c5f | 399 | |
3056d117 | 400 | $_SESSION['settings'] = $this->settings; |
6e081c5f | 401 | |
b95d1cdb MR |
402 | return $loggedIn; |
403 | } | |
404 | ||
405 | public function getUser() { | |
3056d117 MR |
406 | $user = null; |
407 | ||
2dd58fe8 MR |
408 | $this->log("getUser", 4); |
409 | ||
3056d117 MR |
410 | if ($this->isLoggedIn()) { |
411 | $user = $this->settings['user']; | |
412 | } | |
413 | ||
414 | return $user; | |
b95d1cdb MR |
415 | } |
416 | ||
3039de29 MR |
417 | public function authorized($recipient) { |
418 | $authorized = false; | |
419 | ||
cdd7c88a MR |
420 | $this->log("authorized '$recipient'", 3); |
421 | ||
3039de29 MR |
422 | if ($this->isAdmin() || $this->getUser() == $recipient) { |
423 | $authorized = true; | |
424 | } | |
181e3b1f MR |
425 | $msg = ($authorized) ? 'authorize' : 'not authorize'; |
426 | $this->log("$msg '".$this->getUser()."' rcpt '$recipient'", 3); | |
3039de29 MR |
427 | |
428 | return $authorized; | |
429 | } | |
430 | ||
b95d1cdb | 431 | public function getHeader() { |
2dd58fe8 MR |
432 | $this->log("getHeader", 4); |
433 | ||
b95d1cdb MR |
434 | return $this->header; |
435 | } | |
436 | ||
437 | public function getFooter() { | |
2dd58fe8 MR |
438 | $this->log("getFooter", 4); |
439 | ||
b95d1cdb MR |
440 | return $this->footer; |
441 | } | |
442 | ||
443 | public function getHeading() { | |
2dd58fe8 MR |
444 | $this->log("getHeading", 4); |
445 | ||
b95d1cdb MR |
446 | return $this->heading; |
447 | } | |
448 | ||
449 | public function setHeading($heading) { | |
450 | global $CFG; | |
451 | ||
2dd58fe8 MR |
452 | $this->log("setHeading", 4); |
453 | ||
b95d1cdb MR |
454 | $timeout = $CFG->session_timeout * 60 * 1000; |
455 | $this->heading = str_replace('__TITLE__', $heading, $this->heading); | |
456 | $this->header = str_replace('__TITLE__', $heading, $this->header); | |
457 | $this->header = str_replace('__ROOT__', $CFG->wwwroot, $this->header); | |
458 | $this->header = str_replace('__TIMEOUT__', $timeout, $this->header); | |
459 | } | |
460 | ||
461 | public function convertContent($code) { | |
2dd58fe8 MR |
462 | $this->log("convertContent", 4); |
463 | ||
b95d1cdb MR |
464 | $table = array( |
465 | 'V' => 'Virus', | |
466 | 'B' => 'Banned', | |
467 | 'U' => 'Unchecked', | |
468 | 'S' => 'Spam', | |
469 | 'Y' => 'Spammy', | |
470 | 'M' => 'Bad Mime', | |
471 | 'H' => 'Bad Header', | |
472 | 'O' => 'Over sized', | |
473 | 'T' => 'MTA err', | |
474 | 'C' => 'Clean' | |
475 | ); | |
476 | ||
477 | $string = $table[$code]; | |
478 | if (empty($string)) | |
479 | $string = 'Unknown'; | |
480 | ||
481 | return $string; | |
482 | } | |
6df4b805 | 483 | |
6df4b805 | 484 | } |