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