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