]>
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); |
ecc5e773 | 95 | chmod($file, 0600); |
7b561609 | 96 | break; |
01cc21cf | 97 | case 'stderr': |
815fed0c | 98 | file_put_contents('php://stderr', "[$time]: $message\n"); |
7b561609 | 99 | break; |
01cc21cf | 100 | case 'syslog': |
2dd58fe8 | 101 | syslog($priority, $message); |
01cc21cf | 102 | break; |
d6be2d1a | 103 | } |
01cc21cf MR |
104 | } |
105 | ||
3056d117 | 106 | private function initSettings() { |
2dd58fe8 MR |
107 | $this->log("InitSettings", 4); |
108 | ||
3056d117 MR |
109 | if ('' == session_id()) { |
110 | $this->startSession(); | |
111 | } | |
b95d1cdb | 112 | |
2b6294e9 MR |
113 | if (false !== $this->timeout) { |
114 | $timeout = $this->timeout; | |
115 | } else { | |
116 | $timeout = 0; | |
117 | } | |
118 | ||
3056d117 | 119 | $this->settings = array( |
3056d117 MR |
120 | 'user' => null, |
121 | 'admin' => false, | |
122 | 'loginStatus' => 'Not logged in', | |
2b6294e9 | 123 | 'timeout' => $timeout |
3056d117 | 124 | ); |
6072c905 | 125 | |
3056d117 | 126 | $_SESSION['settings'] = $this->settings; |
6072c905 MR |
127 | } |
128 | ||
a675b383 | 129 | private function startSession() { |
b95d1cdb MR |
130 | global $CFG; |
131 | ||
2dd58fe8 MR |
132 | $this->log("startSession", 4); |
133 | ||
b95d1cdb | 134 | if (isset($CFG->session_timeout)) { |
2b6294e9 | 135 | $this->timeout = $CFG->session_timeout * 60; |
b95d1cdb | 136 | } else { |
2b6294e9 | 137 | $this->timeout = 20 * 60; |
b95d1cdb MR |
138 | } |
139 | ||
2b6294e9 MR |
140 | if (ini_get('session.gc_maxlifetime') != $this->timeout) |
141 | ini_set('session.gc_maxlifetime', $this->timeout); | |
7b561609 MR |
142 | //if (ini_get('session.cookie_lifetime') != $this->timeout) |
143 | // ini_set('session.cookie_lifetime', $this->timeout); | |
144 | ini_set('session.cookie_lifetime', 0); | |
a675b383 MR |
145 | |
146 | session_start(); | |
7d9c7fe2 MR |
147 | } |
148 | ||
3056d117 | 149 | private function checkSession() { |
7d9c7fe2 MR |
150 | global $CFG; |
151 | ||
2dd58fe8 MR |
152 | $this->log("checkSession", 4); |
153 | ||
39023189 MR |
154 | if ('' == session_id()) { |
155 | $this->startSession(); | |
156 | } | |
157 | ||
b95d1cdb | 158 | $time = $_SERVER['REQUEST_TIME']; |
7d9c7fe2 | 159 | if (isset($_SESSION['LAST_ACTIVITY']) && |
3056d117 | 160 | ($time - $_SESSION['LAST_ACTIVITY']) >= $this->settings['timeout']) { |
07124c37 MR |
161 | $this->log('R_TIME: '.date('c', $time).' L_ACT: '.date('c', $_SESSION['LAST_ACTIVITY']. |
162 | 'Test: '.($time - $_SESSION['LAST_ACTIVITY'])).' >= '.$this->settings['timeout'], 3); | |
3056d117 | 163 | $this->logout(); |
a675b383 MR |
164 | } else { |
165 | $_SESSION['LAST_ACTIVITY'] = $time; | |
166 | } | |
b95d1cdb MR |
167 | } |
168 | ||
169 | public function logout() { | |
2dd58fe8 MR |
170 | $this->log("logout", 4); |
171 | ||
b95d1cdb MR |
172 | if (ini_get('session.use_cookies')) { |
173 | $params = session_get_cookie_params(); | |
174 | setcookie(session_name(), '', time() - 42000, | |
175 | $params['path'], $params['domain'], | |
176 | $params['secure'], $params['httponly']); | |
177 | } | |
39023189 MR |
178 | |
179 | if ('' != session_id()) { | |
180 | $_SESSION = array(); | |
181 | session_unset(); | |
182 | session_destroy(); | |
183 | } | |
3056d117 | 184 | $this->settings = array(); |
b95d1cdb MR |
185 | } |
186 | ||
187 | public function isAdmin() { | |
3056d117 MR |
188 | $admin = false; |
189 | ||
2dd58fe8 MR |
190 | $this->log("isAdmin", 4); |
191 | ||
3056d117 MR |
192 | if (isset($this->settings['admin'])) { |
193 | $admin = $this->settings['admin']; | |
194 | } | |
195 | ||
196 | return $admin; | |
b95d1cdb MR |
197 | } |
198 | ||
199 | public function login($user, $pw) { | |
200 | global $CFG; | |
201 | $result = false; | |
202 | ||
2dd58fe8 MR |
203 | $this->log("login", 4); |
204 | ||
3056d117 MR |
205 | if ('' == session_id()) { |
206 | $this->startSession(); | |
207 | } | |
208 | ||
209 | $this->settings['user'] = null; | |
210 | $this->settings['admin'] = false; | |
b95d1cdb MR |
211 | |
212 | $p = explode('@', $user); | |
213 | if (count($p) != 2) { | |
3056d117 MR |
214 | $this->settings['loginStatus'] = 'Bad username'; |
215 | } else { | |
216 | $domain = $p[1]; | |
217 | $dn = "mail=$user,ou=Users,domainName=$domain,$CFG->ldap_base_dn"; | |
218 | $filter = "(&(objectclass=mailUser)(accountStatus=active)(mail=$user))"; | |
219 | $ds = @ldap_connect($CFG->ldap_dsn); | |
220 | if ($ds) { | |
221 | @ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3); | |
222 | $r = @ldap_bind($ds, $dn, $pw); | |
223 | if ($r) { | |
224 | $sr = @ldap_search($ds, $CFG->ldap_base_dn, $filter, array('mail','domainglobaladmin')); | |
225 | $info = @ldap_get_entries($ds, $sr); // array | |
226 | if ($info['count'] > 0) { | |
227 | $this->settings['user'] = $user; | |
228 | $result = true; | |
229 | $this->settings['loginStatus'] = 'OK'; | |
230 | $admin = 'NO'; | |
231 | if (isset($info[0]['domainglobaladmin'])) { | |
232 | $admin = $info[0]['domainglobaladmin'][0]; | |
233 | $admin = strtoupper($admin); | |
234 | } | |
235 | $this->settings['admin'] = ($admin == 'YES') ? true : false; | |
236 | } else { | |
237 | $this->settings['loginStatus'] = 'Login failed'; | |
b95d1cdb | 238 | } |
6df4b805 | 239 | } else { |
3056d117 | 240 | $this->settings['loginStatus'] = ldap_error($ds); |
6df4b805 | 241 | } |
3056d117 | 242 | @ldap_close($ds); |
6df4b805 | 243 | } else { |
3056d117 | 244 | $this->settings['loginStatus'] = 'Connect to LDAP server failed'; |
6df4b805 | 245 | } |
6df4b805 MR |
246 | } |
247 | ||
3056d117 | 248 | $_SESSION['settings'] = $this->settings; |
6e081c5f | 249 | |
b95d1cdb MR |
250 | return $result; |
251 | } | |
252 | ||
253 | public function getLoginStatus() { | |
3056d117 MR |
254 | $status = 'Not logged in'; |
255 | ||
2dd58fe8 MR |
256 | $this->log("getLoginStatus", 4); |
257 | ||
3056d117 MR |
258 | if (isset($this->settings['loginStatus'])) { |
259 | $status = $this->settings['loginStatus']; | |
260 | } | |
261 | ||
262 | return $status; | |
b95d1cdb MR |
263 | } |
264 | ||
265 | public function isLoggedIn() { | |
266 | global $CFG; | |
267 | $loggedIn = false; | |
268 | ||
65f27692 | 269 | $this->log("isLoggedIn[1]: user ".var_export($this->settings['user'], true), 3); |
2dd58fe8 | 270 | |
3056d117 MR |
271 | if ('' == session_id()) { |
272 | $this->startSession(); | |
273 | } | |
274 | ||
65f27692 | 275 | $this->log("isLoggedIn[2]: user ".var_export($this->settings['user'], true), 3); |
39023189 | 276 | $this->checkSession(); |
65f27692 | 277 | $this->log("isLoggedIn[3]: user ".var_export($this->settings['user'], true), 3); |
39023189 | 278 | |
3056d117 MR |
279 | if (isset($this->settings['user'])) { |
280 | if ($this->settings['user'] != null) { | |
281 | $loggedIn = true; | |
282 | } else { | |
283 | if ($CFG->auth_method == 'HTTP_AUTH') { | |
86fb546e MR |
284 | if (isset($_SERVER['PHP_AUTH_USER'])) { |
285 | $this->settings['user'] = $_SERVER['PHP_AUTH_USER']; | |
3056d117 MR |
286 | $loggedIn = true; |
287 | } | |
b95d1cdb MR |
288 | } |
289 | } | |
290 | } | |
291 | ||
85ec6a84 | 292 | if ($loggedIn == false) { |
7b561609 MR |
293 | $this->log('$this->settings: '.var_export($this->settings, true), 3); |
294 | $this->log('R_TIME: '.date('c', $_SERVER['REQUEST_TIME']).' L_ACT: '.date('c', $_SESSION['LAST_ACTIVITY']), 3); | |
18d80742 | 295 | } |
6e081c5f | 296 | |
3056d117 | 297 | $_SESSION['settings'] = $this->settings; |
6e081c5f | 298 | |
b95d1cdb MR |
299 | return $loggedIn; |
300 | } | |
301 | ||
302 | public function getUser() { | |
3056d117 MR |
303 | $user = null; |
304 | ||
2dd58fe8 MR |
305 | $this->log("getUser", 4); |
306 | ||
3056d117 MR |
307 | if ($this->isLoggedIn()) { |
308 | $user = $this->settings['user']; | |
309 | } | |
310 | ||
311 | return $user; | |
b95d1cdb MR |
312 | } |
313 | ||
314 | public function getHeader() { | |
2dd58fe8 MR |
315 | $this->log("getHeader", 4); |
316 | ||
b95d1cdb MR |
317 | return $this->header; |
318 | } | |
319 | ||
320 | public function getFooter() { | |
2dd58fe8 MR |
321 | $this->log("getFooter", 4); |
322 | ||
b95d1cdb MR |
323 | return $this->footer; |
324 | } | |
325 | ||
326 | public function getHeading() { | |
2dd58fe8 MR |
327 | $this->log("getHeading", 4); |
328 | ||
b95d1cdb MR |
329 | return $this->heading; |
330 | } | |
331 | ||
332 | public function setHeading($heading) { | |
333 | global $CFG; | |
334 | ||
2dd58fe8 MR |
335 | $this->log("setHeading", 4); |
336 | ||
b95d1cdb MR |
337 | $timeout = $CFG->session_timeout * 60 * 1000; |
338 | $this->heading = str_replace('__TITLE__', $heading, $this->heading); | |
339 | $this->header = str_replace('__TITLE__', $heading, $this->header); | |
340 | $this->header = str_replace('__ROOT__', $CFG->wwwroot, $this->header); | |
341 | $this->header = str_replace('__TIMEOUT__', $timeout, $this->header); | |
342 | } | |
343 | ||
344 | public function convertContent($code) { | |
2dd58fe8 MR |
345 | $this->log("convertContent", 4); |
346 | ||
b95d1cdb MR |
347 | $table = array( |
348 | 'V' => 'Virus', | |
349 | 'B' => 'Banned', | |
350 | 'U' => 'Unchecked', | |
351 | 'S' => 'Spam', | |
352 | 'Y' => 'Spammy', | |
353 | 'M' => 'Bad Mime', | |
354 | 'H' => 'Bad Header', | |
355 | 'O' => 'Over sized', | |
356 | 'T' => 'MTA err', | |
357 | 'C' => 'Clean' | |
358 | ); | |
359 | ||
360 | $string = $table[$code]; | |
361 | if (empty($string)) | |
362 | $string = 'Unknown'; | |
363 | ||
364 | return $string; | |
365 | } | |
6df4b805 | 366 | |
6df4b805 | 367 | } |