]> git.datanom.net - qtadmin.git/blob - lib/utils.inc.php
Fix bug in session handler
[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 implements Serializable {
6
7 private static $_instance = null;
8 private $server;
9 private $user;
10 private $is_admin;
11 private $loginStatus;
12 private $timeout;
13 private $header = '<!DOCTYPE html>
14 <html>
15 <head>
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>
22 <script src="__ROOT__js/checkbox.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>. &copy; 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 private function __construct() {
32 global $CFG;
33
34 $this->server = $_SERVER;
35
36 $this->user = null;
37 $this->is_admin = false;
38 $this->loginStatus = 'Not logged in';
39
40 $this->startSession();
41
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 $_SESSION['Utils'] = serialize($this);
59 }
60
61 private function __clone() {}
62
63 public function serialize() {
64 file_put_contents('/tmp/dump', 'Serialize called: '.var_export($this, true), FILE_APPEND);
65 return serialize(get_object_vars($this));
66 }
67
68 public function unserialize($data) {
69 $values = unserialize($data);
70 foreach ($values as $key=>$value) {
71 $this->$key = $value;
72 }
73 }
74
75 private function startSession() {
76 global $CFG;
77
78 if (isset($CFG->session_timeout)) {
79 $this->timeout = $CFG->session_timeout * 60;
80 } else {
81 $this->timeout = 20 * 60;
82 }
83
84 if (ini_get('session.gc_maxlifetime') != $this->timeout)
85 ini_set('session.gc_maxlifetime', $this->timeout);
86 if (ini_get('session.cookie_lifetime') != $this->timeout)
87 ini_set('session.cookie_lifetime', $this->timeout);
88
89 session_start();
90
91 //echo ini_get('session.gc_maxlifetime').':'.ini_get('session.cookie_lifetime');
92 }
93
94 public static function getInstance() {
95 global $CFG;
96
97 if (!is_object(self::$_instance)) {
98 if (isset($_SESSION['Utils'])) {
99 self::$_instance = unserialize($_SESSION['Utils']);
100 file_put_contents('/tmp/dump', 'Unserialize called: '.var_export($this, true), FILE_APPEND);
101 } else {
102 self::$_instance = new Utils();
103 }
104 }
105
106 $time = $_SERVER['REQUEST_TIME'];
107 if (isset($_SESSION['LAST_ACTIVITY']) &&
108 ($time - $_SESSION['LAST_ACTIVITY']) >= self::$_instance->timeout) {
109 echo 'R_TIME: '.date('c', $time).' L_ACT: '.date('c', $_SESSION['LAST_ACTIVITY']);
110 exit;
111 session_unset();
112 session_destroy();
113 self::$_instance->user = null;
114 self::$_instance->is_admin = false;
115 } else {
116 $_SESSION['LAST_ACTIVITY'] = $time;
117 }
118
119 return self::$_instance;
120 }
121
122 public function logout() {
123 $_SESSION = array();
124 if (ini_get('session.use_cookies')) {
125 $params = session_get_cookie_params();
126 setcookie(session_name(), '', time() - 42000,
127 $params['path'], $params['domain'],
128 $params['secure'], $params['httponly']);
129 }
130 session_unset();
131 session_destroy();
132 $this->user = null;
133 $this->is_admin = false;
134 }
135
136 public function isAdmin() {
137 //file_put_contents('/tmp/login.txt', var_export($this, true));
138 return $this->is_admin;
139 }
140
141 public function login($user, $pw) {
142 global $CFG;
143 $result = false;
144
145 unset($_SESSION['user']);
146 unset($_SESSION['is_admin']);
147 $this->user = null;
148 $this->is_admin = false;
149
150 $p = explode('@', $user);
151 if (count($p) != 2) {
152 $this->loginStatus = 'Bad username';
153 return false;
154 }
155 $domain = $p[1];
156 $dn = "mail=$user,ou=Users,domainName=$domain,$CFG->ldap_base_dn";
157 $filter = "(&(objectclass=mailUser)(accountStatus=active)(mail=$user))";
158 $ds = @ldap_connect($CFG->ldap_dsn);
159 if ($ds) {
160 @ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
161 $r = @ldap_bind($ds, $dn, $pw);
162 if ($r) {
163 $sr = @ldap_search($ds, $CFG->ldap_base_dn, $filter, array('mail','domainglobaladmin'));
164 $info = @ldap_get_entries($ds, $sr); // array
165 if ($info['count'] > 0) {
166 $_SESSION['user'] = $user;
167 $this->user = $user;
168 $result = true;
169 $this->loginStatus = 'OK';
170 $admin = 'NO';
171 if (isset($info[0]['domainglobaladmin'])) {
172 $admin = $info[0]['domainglobaladmin'][0];
173 $admin = strtoupper($admin);
174 }
175 $this->is_admin = ($admin == 'YES') ? true : false;
176 $_SESSION['is_admin'] = $this->is_admin;
177 } else {
178 $this->loginStatus = 'Login failed';
179 }
180 } else {
181 $this->loginStatus = ldap_error($ds);
182 }
183 @ldap_close($ds);
184 } else {
185 $this->loginStatus = 'Connect to LDAP server failed';
186 }
187
188 return $result;
189 }
190
191 public function getLoginStatus() {
192 return $this->loginStatus;
193 }
194
195 public function isLoggedIn() {
196 global $CFG;
197 $loggedIn = false;
198
199 if ($this->user) {
200 $loggedIn = true;
201 } else if (isset($_SESSION['user'])) {
202 $this->user = $_SESSION['user'];
203 $loggedIn = true;
204 } else {
205 if ($CFG->auth_method == 'HTTP_AUTH') {
206 if (isset($this->server['PHP_AUTH_USER'])) {
207 $this->user = $this->server['PHP_AUTH_USER'];
208 $loggedIn = true;
209 }
210 }
211 }
212
213 if ($loggedIn == false) {
214 echo '$this->user: '.$this->user.' $_SESSION[\'user\']: '.$_SESSION['user'];
215 echo 'R_TIME: '.date('c', $_SERVER['REQUEST_TIME']).' L_ACT: '.date('c', $_SESSION['LAST_ACTIVITY']);
216 exit;
217 }
218 return $loggedIn;
219 }
220
221 public function getUser() {
222 $this->isLoggedIn();
223 return $this->user;
224 }
225
226 public function getHeader() {
227 return $this->header;
228 }
229
230 public function getFooter() {
231 return $this->footer;
232 }
233
234 public function getHeading() {
235 return $this->heading;
236 }
237
238 public function setHeading($heading) {
239 global $CFG;
240
241 $timeout = $CFG->session_timeout * 60 * 1000;
242 $this->heading = str_replace('__TITLE__', $heading, $this->heading);
243 $this->header = str_replace('__TITLE__', $heading, $this->header);
244 $this->header = str_replace('__ROOT__', $CFG->wwwroot, $this->header);
245 $this->header = str_replace('__TIMEOUT__', $timeout, $this->header);
246 }
247
248 public function convertContent($code) {
249 $table = array(
250 'V' => 'Virus',
251 'B' => 'Banned',
252 'U' => 'Unchecked',
253 'S' => 'Spam',
254 'Y' => 'Spammy',
255 'M' => 'Bad Mime',
256 'H' => 'Bad Header',
257 'O' => 'Over sized',
258 'T' => 'MTA err',
259 'C' => 'Clean'
260 );
261
262 $string = $table[$code];
263 if (empty($string))
264 $string = 'Unknown';
265
266 return $string;
267 }
268
269 }
This page took 0.08792 seconds and 6 git commands to generate.