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