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