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