]> git.datanom.net - qtadmin.git/blame - lib/utils.inc.php
Fix bug in session handler
[qtadmin.git] / lib / utils.inc.php
CommitLineData
6df4b805
MR
1<?php
2/* vim: set ts=4 tw=0 sw=4 noet: */
3require_once $CFG->root .'config.php';
4
5class Utils {
6
b95d1cdb
MR
7 private static $_instance = null;
8 private $server;
9 private $user;
10 private $is_admin;
11 private $loginStatus;
7d9c7fe2 12 private $timeout;
b95d1cdb 13 private $header = '<!DOCTYPE html>
6df4b805
MR
14<html>
15<head>
b95d1cdb
MR
16 <meta charset="utf-8">
17 <link rel="stylesheet" href="css/styles.css">
18 <script>
19 var timeout = __TIMEOUT__;
20 </script>
21 <script src="__ROOT__js/timer.js"></script>
5c7b972e 22 <script src="__ROOT__js/checkbox.js"></script>
b95d1cdb 23 <title>__TITLE__</title>
6df4b805
MR
24</head>
25<body>';
bb06f172 26 private $footer = '<p class="footer">Powered by <a href="https://qtadmin.datanom.net"
b706c65f 27 title="Goto QtAdmin homepage">QtAdmin</a>. &copy; 2015 by Michael Rasmussen</p></body></html>';
b95d1cdb
MR
28 private $heading = '<p id="time" class="time">Session timeout:
29 <span id="timer"></span></p><h1 class="h1">__TITLE__</h1>';
30
31 private function __construct() {
32 global $CFG;
33
34 $this->server = $_SERVER;
b95d1cdb
MR
35
36 $this->user = null;
37 $this->is_admin = false;
38 $this->loginStatus = 'Not logged in';
39
a675b383
MR
40 $this->startSession();
41
b95d1cdb
MR
42 if (isset($_SESSION['user'])) {
43 $this->user = $_SESSION['user'];
44 $this->loginStatus = 'OK';
45 $this->is_admin = $_SESSION['is_admin'];
46 } else {
47 if ($CFG->auth_method == 'HTTP_AUTH') {
48 if (isset($this->server['PHP_AUTH_USER'])) {
49 $this->user = $this->server['PHP_AUTH_USER'];
50 $this->loginStatus = 'OK';
51 if ($CFG->admin_user == $this->user)
52 $this->is_admin = true;
53 }
54 }
55 }
56 $_SESSION['user'] = $this->user;
57 $_SESSION['is_admin'] = $this->is_admin;
58 }
59
60 private function __clone() {}
61
a675b383 62 private function startSession() {
b95d1cdb
MR
63 global $CFG;
64
b95d1cdb 65 if (isset($CFG->session_timeout)) {
7d9c7fe2 66 $this->timeout = $CFG->session_timeout * 60;
b95d1cdb 67 } else {
7d9c7fe2 68 $this->timeout = 20 * 60;
b95d1cdb
MR
69 }
70
7d9c7fe2
MR
71 if (ini_get('session.gc_maxlifetime') != $this->timeout)
72 ini_set('session.gc_maxlifetime', $this->timeout);
73 if (ini_get('session.cookie_lifetime') != $this->timeout)
74 ini_set('session.cookie_lifetime', $this->timeout);
a675b383
MR
75
76 session_start();
77
7d9c7fe2
MR
78 //echo ini_get('session.gc_maxlifetime').':'.ini_get('session.cookie_lifetime');
79 }
80
81 public static function getInstance() {
82 global $CFG;
83
84 if (!is_object(self::$_instance)) {
85 self::$_instance = new Utils();
86 }
87
b95d1cdb 88 $time = $_SERVER['REQUEST_TIME'];
7d9c7fe2
MR
89 if (isset($_SESSION['LAST_ACTIVITY']) &&
90 ($time - $_SESSION['LAST_ACTIVITY']) >= self::$_instance->timeout) {
c0519296 91 echo 'R_TIME: '.date('c', $time).' L_ACT: '.date('c', $_SESSION['LAST_ACTIVITY']);
b95d1cdb
MR
92 session_unset();
93 session_destroy();
b95d1cdb
MR
94 self::$_instance->user = null;
95 self::$_instance->is_admin = false;
a675b383
MR
96 } else {
97 $_SESSION['LAST_ACTIVITY'] = $time;
98 }
b95d1cdb
MR
99
100 return self::$_instance;
101 }
102
103 public function logout() {
104 $_SESSION = array();
105 if (ini_get('session.use_cookies')) {
106 $params = session_get_cookie_params();
107 setcookie(session_name(), '', time() - 42000,
108 $params['path'], $params['domain'],
109 $params['secure'], $params['httponly']);
110 }
111 session_unset();
112 session_destroy();
113 $this->user = null;
114 $this->is_admin = false;
115 }
116
117 public function isAdmin() {
118 //file_put_contents('/tmp/login.txt', var_export($this, true));
119 return $this->is_admin;
120 }
121
122 public function login($user, $pw) {
123 global $CFG;
124 $result = false;
125
126 unset($_SESSION['user']);
127 unset($_SESSION['is_admin']);
128 $this->user = null;
129 $this->is_admin = false;
130
131 $p = explode('@', $user);
132 if (count($p) != 2) {
133 $this->loginStatus = 'Bad username';
134 return false;
135 }
136 $domain = $p[1];
137 $dn = "mail=$user,ou=Users,domainName=$domain,$CFG->ldap_base_dn";
6df4b805
MR
138 $filter = "(&(objectclass=mailUser)(accountStatus=active)(mail=$user))";
139 $ds = @ldap_connect($CFG->ldap_dsn);
140 if ($ds) {
b95d1cdb 141 @ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
6df4b805
MR
142 $r = @ldap_bind($ds, $dn, $pw);
143 if ($r) {
144 $sr = @ldap_search($ds, $CFG->ldap_base_dn, $filter, array('mail','domainglobaladmin'));
145 $info = @ldap_get_entries($ds, $sr); // array
146 if ($info['count'] > 0) {
b95d1cdb
MR
147 $_SESSION['user'] = $user;
148 $this->user = $user;
149 $result = true;
150 $this->loginStatus = 'OK';
151 $admin = 'NO';
152 if (isset($info[0]['domainglobaladmin'])) {
153 $admin = $info[0]['domainglobaladmin'][0];
154 $admin = strtoupper($admin);
155 }
156 $this->is_admin = ($admin == 'YES') ? true : false;
157 $_SESSION['is_admin'] = $this->is_admin;
6df4b805
MR
158 } else {
159 $this->loginStatus = 'Login failed';
160 }
161 } else {
162 $this->loginStatus = ldap_error($ds);
163 }
164 @ldap_close($ds);
165 } else {
166 $this->loginStatus = 'Connect to LDAP server failed';
167 }
168
b95d1cdb
MR
169 return $result;
170 }
171
172 public function getLoginStatus() {
173 return $this->loginStatus;
174 }
175
176 public function isLoggedIn() {
177 global $CFG;
178 $loggedIn = false;
179
180 if ($this->user) {
181 $loggedIn = true;
182 } else if (isset($_SESSION['user'])) {
183 $this->user = $_SESSION['user'];
184 $loggedIn = true;
185 } else {
186 if ($CFG->auth_method == 'HTTP_AUTH') {
187 if (isset($this->server['PHP_AUTH_USER'])) {
188 $this->user = $this->server['PHP_AUTH_USER'];
189 $loggedIn = true;
190 }
191 }
192 }
193
194 return $loggedIn;
195 }
196
197 public function getUser() {
198 $this->isLoggedIn();
199 return $this->user;
200 }
201
202 public function getHeader() {
203 return $this->header;
204 }
205
206 public function getFooter() {
207 return $this->footer;
208 }
209
210 public function getHeading() {
211 return $this->heading;
212 }
213
214 public function setHeading($heading) {
215 global $CFG;
216
217 $timeout = $CFG->session_timeout * 60 * 1000;
218 $this->heading = str_replace('__TITLE__', $heading, $this->heading);
219 $this->header = str_replace('__TITLE__', $heading, $this->header);
220 $this->header = str_replace('__ROOT__', $CFG->wwwroot, $this->header);
221 $this->header = str_replace('__TIMEOUT__', $timeout, $this->header);
222 }
223
224 public function convertContent($code) {
225 $table = array(
226 'V' => 'Virus',
227 'B' => 'Banned',
228 'U' => 'Unchecked',
229 'S' => 'Spam',
230 'Y' => 'Spammy',
231 'M' => 'Bad Mime',
232 'H' => 'Bad Header',
233 'O' => 'Over sized',
234 'T' => 'MTA err',
235 'C' => 'Clean'
236 );
237
238 $string = $table[$code];
239 if (empty($string))
240 $string = 'Unknown';
241
242 return $string;
243 }
6df4b805 244
6df4b805 245}
This page took 0.097214 seconds and 5 git commands to generate.