]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | /* vim: set ts=4 tw=4 sw=4 noet: */ | |
3 | require_once $CFG->root .'config.php'; | |
4 | require_once $CFG->root . 'lib/session_handler.inc.php'; | |
5 | ||
6 | class Utils { | |
7 | ||
8 | private $timeout = false; | |
9 | private $settings; | |
10 | private $log_level; | |
11 | private $log_method; | |
12 | private $header = '<!DOCTYPE html> | |
13 | <html> | |
14 | <head> | |
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> | |
21 | <script src="__ROOT__js/checkbox.js"></script> | |
22 | <script src="__ROOT__js/forms.js"></script> | |
23 | <title>__TITLE__</title> | |
24 | </head> | |
25 | <body>'; | |
26 | private $footer = '<p class="footer">Powered by <a href="https://qtadmin.datanom.net" | |
27 | title="Goto QtAdmin homepage">QtAdmin</a>. © 2015 by Michael Rasmussen</p></body></html>'; | |
28 | private $heading = '<p id="time" class="time">Session timeout: | |
29 | <span id="timer"></span></p><h1 class="h1">__TITLE__</h1>'; | |
30 | ||
31 | public function __construct() { | |
32 | global $CFG; | |
33 | ||
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 | ||
46 | $this->log("Init Utils", 4); | |
47 | ||
48 | $this->log("__construct[1]: user ".var_export($this->settings['user'], true), 3); | |
49 | $this->startSession(); | |
50 | $this->log("__construct[2]: user ".var_export($this->settings['user'], true), 3); | |
51 | ||
52 | if (! isset($_SESSION['settings'])) { | |
53 | $this->initSettings(); | |
54 | } | |
55 | $this->log("__construct[3]: user ".var_export($this->settings['user'], true), 3); | |
56 | $this->settings = $_SESSION['settings']; | |
57 | $this->log("__construct[4]: user ".var_export($this->settings['user'], true), 3); | |
58 | ||
59 | if ($CFG->auth_method == 'HTTP_AUTH') { | |
60 | if (isset($_SERVER['PHP_AUTH_USER'])) { | |
61 | $this->settings['user'] = $_SERVER['PHP_AUTH_USER']; | |
62 | $this->settings['loginStatus'] = 'OK'; | |
63 | if ($CFG->admin_user == $this->settings['user']) | |
64 | $this->settings['admin'] = true; | |
65 | } | |
66 | } | |
67 | } | |
68 | ||
69 | private function log($message, $level = 1) { | |
70 | global $CFG; | |
71 | ||
72 | if ($level > $this->log_level) | |
73 | return; | |
74 | ||
75 | $time = date('c'); | |
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': | |
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 | } | |
96 | file_put_contents($file, "[$time]: $message\n", FILE_APPEND | LOCK_EX); | |
97 | chmod($file, 0600); | |
98 | break; | |
99 | case 'stderr': | |
100 | file_put_contents('php://stderr', "[$time]: $message\n"); | |
101 | break; | |
102 | case 'syslog': | |
103 | syslog($priority, $message); | |
104 | break; | |
105 | } | |
106 | } | |
107 | ||
108 | private function initSettings() { | |
109 | $this->log("InitSettings", 4); | |
110 | ||
111 | if ('' == session_id()) { | |
112 | $this->startSession(); | |
113 | } | |
114 | ||
115 | if (false !== $this->timeout) { | |
116 | $timeout = $this->timeout; | |
117 | } else { | |
118 | $timeout = 0; | |
119 | } | |
120 | ||
121 | $this->settings = array( | |
122 | 'user' => null, | |
123 | 'admin' => false, | |
124 | 'loginStatus' => 'Not logged in', | |
125 | 'timeout' => $timeout | |
126 | ); | |
127 | ||
128 | $_SESSION['settings'] = $this->settings; | |
129 | } | |
130 | ||
131 | private function startSession() { | |
132 | global $CFG; | |
133 | ||
134 | $this->log("startSession", 4); | |
135 | ||
136 | if (isset($CFG->session_timeout)) { | |
137 | $this->timeout = $CFG->session_timeout * 60; | |
138 | } else { | |
139 | $this->timeout = 20 * 60; | |
140 | } | |
141 | ||
142 | if (ini_get('session.gc_maxlifetime') != $this->timeout) | |
143 | ini_set('session.gc_maxlifetime', $this->timeout); | |
144 | //if (ini_get('session.cookie_lifetime') != $this->timeout) | |
145 | // ini_set('session.cookie_lifetime', $this->timeout); | |
146 | ini_set('session.cookie_lifetime', 0); | |
147 | ||
148 | session_start(); | |
149 | } | |
150 | ||
151 | private function checkSession() { | |
152 | global $CFG; | |
153 | ||
154 | $this->log("checkSession", 4); | |
155 | ||
156 | if ('' == session_id()) { | |
157 | $this->startSession(); | |
158 | } | |
159 | ||
160 | $time = $_SERVER['REQUEST_TIME']; | |
161 | if (isset($_SESSION['LAST_ACTIVITY']) && | |
162 | ($time - $_SESSION['LAST_ACTIVITY']) >= $this->settings['timeout']) { | |
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); | |
165 | $this->logout(); | |
166 | } else { | |
167 | $_SESSION['LAST_ACTIVITY'] = $time; | |
168 | } | |
169 | } | |
170 | ||
171 | public function logout() { | |
172 | $this->log("logout", 4); | |
173 | ||
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 | } | |
180 | ||
181 | if ('' != session_id()) { | |
182 | $_SESSION = array(); | |
183 | session_unset(); | |
184 | session_destroy(); | |
185 | } | |
186 | $this->settings = array(); | |
187 | } | |
188 | ||
189 | public function isAdmin() { | |
190 | $admin = false; | |
191 | ||
192 | $this->log("isAdmin", 4); | |
193 | ||
194 | if (isset($this->settings['admin'])) { | |
195 | $admin = $this->settings['admin']; | |
196 | } | |
197 | ||
198 | return $admin; | |
199 | } | |
200 | ||
201 | public function login($user, $pw) { | |
202 | global $CFG; | |
203 | $result = false; | |
204 | ||
205 | $this->log("login", 4); | |
206 | ||
207 | if ('' == session_id()) { | |
208 | $this->startSession(); | |
209 | } | |
210 | ||
211 | $this->settings['user'] = null; | |
212 | $this->settings['admin'] = false; | |
213 | ||
214 | $p = explode('@', $user); | |
215 | if (count($p) != 2) { | |
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'; | |
240 | } | |
241 | } else { | |
242 | $this->settings['loginStatus'] = ldap_error($ds); | |
243 | } | |
244 | @ldap_close($ds); | |
245 | } else { | |
246 | $this->settings['loginStatus'] = 'Connect to LDAP server failed'; | |
247 | } | |
248 | } | |
249 | ||
250 | $_SESSION['settings'] = $this->settings; | |
251 | ||
252 | return $result; | |
253 | } | |
254 | ||
255 | public function getLoginStatus() { | |
256 | $status = 'Not logged in'; | |
257 | ||
258 | $this->log("getLoginStatus", 4); | |
259 | ||
260 | if (isset($this->settings['loginStatus'])) { | |
261 | $status = $this->settings['loginStatus']; | |
262 | } | |
263 | ||
264 | return $status; | |
265 | } | |
266 | ||
267 | public function isLoggedIn() { | |
268 | global $CFG; | |
269 | $loggedIn = false; | |
270 | ||
271 | $this->log("isLoggedIn[1]: user ".var_export($this->settings['user'], true), 3); | |
272 | ||
273 | if ('' == session_id()) { | |
274 | $this->startSession(); | |
275 | } | |
276 | ||
277 | $this->log("isLoggedIn[2]: user ".var_export($this->settings['user'], true), 3); | |
278 | $this->checkSession(); | |
279 | $this->log("isLoggedIn[3]: user ".var_export($this->settings['user'], true), 3); | |
280 | ||
281 | if (isset($this->settings['user'])) { | |
282 | if ($this->settings['user'] != null) { | |
283 | $loggedIn = true; | |
284 | } else { | |
285 | if ($CFG->auth_method == 'HTTP_AUTH') { | |
286 | if (isset($_SERVER['PHP_AUTH_USER'])) { | |
287 | $this->settings['user'] = $_SERVER['PHP_AUTH_USER']; | |
288 | $loggedIn = true; | |
289 | } | |
290 | } | |
291 | } | |
292 | } | |
293 | ||
294 | if ($loggedIn == false) { | |
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); | |
297 | } | |
298 | ||
299 | $_SESSION['settings'] = $this->settings; | |
300 | ||
301 | return $loggedIn; | |
302 | } | |
303 | ||
304 | public function getUser() { | |
305 | $user = null; | |
306 | ||
307 | $this->log("getUser", 4); | |
308 | ||
309 | if ($this->isLoggedIn()) { | |
310 | $user = $this->settings['user']; | |
311 | } | |
312 | ||
313 | return $user; | |
314 | } | |
315 | ||
316 | public function getHeader() { | |
317 | $this->log("getHeader", 4); | |
318 | ||
319 | return $this->header; | |
320 | } | |
321 | ||
322 | public function getFooter() { | |
323 | $this->log("getFooter", 4); | |
324 | ||
325 | return $this->footer; | |
326 | } | |
327 | ||
328 | public function getHeading() { | |
329 | $this->log("getHeading", 4); | |
330 | ||
331 | return $this->heading; | |
332 | } | |
333 | ||
334 | public function setHeading($heading) { | |
335 | global $CFG; | |
336 | ||
337 | $this->log("setHeading", 4); | |
338 | ||
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) { | |
347 | $this->log("convertContent", 4); | |
348 | ||
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 | } | |
368 | ||
369 | } |