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