]>
Commit | Line | Data |
---|---|---|
6df4b805 MR |
1 | <?php |
2 | /* vim: set ts=4 tw=0 sw=4 noet: */ | |
3 | require_once $CFG->root .'config.php'; | |
4 | ||
6072c905 | 5 | class Utils implements Serializable { |
6df4b805 | 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>. © 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; | |
6072c905 | 58 | $_SESSION['Utils'] = serialize($this); |
b95d1cdb MR |
59 | } |
60 | ||
61 | private function __clone() {} | |
62 | ||
6072c905 MR |
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 | ||
a675b383 | 75 | private function startSession() { |
b95d1cdb MR |
76 | global $CFG; |
77 | ||
b95d1cdb | 78 | if (isset($CFG->session_timeout)) { |
7d9c7fe2 | 79 | $this->timeout = $CFG->session_timeout * 60; |
b95d1cdb | 80 | } else { |
7d9c7fe2 | 81 | $this->timeout = 20 * 60; |
b95d1cdb MR |
82 | } |
83 | ||
7d9c7fe2 MR |
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); | |
a675b383 MR |
88 | |
89 | session_start(); | |
90 | ||
7d9c7fe2 MR |
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)) { | |
6072c905 MR |
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 | } | |
7d9c7fe2 MR |
104 | } |
105 | ||
b95d1cdb | 106 | $time = $_SERVER['REQUEST_TIME']; |
7d9c7fe2 MR |
107 | if (isset($_SESSION['LAST_ACTIVITY']) && |
108 | ($time - $_SESSION['LAST_ACTIVITY']) >= self::$_instance->timeout) { | |
c0519296 | 109 | echo 'R_TIME: '.date('c', $time).' L_ACT: '.date('c', $_SESSION['LAST_ACTIVITY']); |
2a577047 | 110 | exit; |
b95d1cdb MR |
111 | session_unset(); |
112 | session_destroy(); | |
cb950b16 | 113 | self::$_instance->user = null; |
b95d1cdb | 114 | self::$_instance->is_admin = false; |
6e081c5f | 115 | $_SESSION['Utils'] = serialize(self::$_instance); |
a675b383 MR |
116 | } else { |
117 | $_SESSION['LAST_ACTIVITY'] = $time; | |
118 | } | |
b95d1cdb MR |
119 | |
120 | return self::$_instance; | |
121 | } | |
122 | ||
123 | public function logout() { | |
124 | $_SESSION = array(); | |
125 | if (ini_get('session.use_cookies')) { | |
126 | $params = session_get_cookie_params(); | |
127 | setcookie(session_name(), '', time() - 42000, | |
128 | $params['path'], $params['domain'], | |
129 | $params['secure'], $params['httponly']); | |
130 | } | |
131 | session_unset(); | |
132 | session_destroy(); | |
133 | $this->user = null; | |
134 | $this->is_admin = false; | |
135 | } | |
136 | ||
137 | public function isAdmin() { | |
138 | //file_put_contents('/tmp/login.txt', var_export($this, true)); | |
139 | return $this->is_admin; | |
140 | } | |
141 | ||
142 | public function login($user, $pw) { | |
143 | global $CFG; | |
144 | $result = false; | |
145 | ||
146 | unset($_SESSION['user']); | |
147 | unset($_SESSION['is_admin']); | |
148 | $this->user = null; | |
149 | $this->is_admin = false; | |
150 | ||
151 | $p = explode('@', $user); | |
152 | if (count($p) != 2) { | |
153 | $this->loginStatus = 'Bad username'; | |
154 | return false; | |
155 | } | |
156 | $domain = $p[1]; | |
157 | $dn = "mail=$user,ou=Users,domainName=$domain,$CFG->ldap_base_dn"; | |
6df4b805 MR |
158 | $filter = "(&(objectclass=mailUser)(accountStatus=active)(mail=$user))"; |
159 | $ds = @ldap_connect($CFG->ldap_dsn); | |
160 | if ($ds) { | |
b95d1cdb | 161 | @ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3); |
6df4b805 MR |
162 | $r = @ldap_bind($ds, $dn, $pw); |
163 | if ($r) { | |
164 | $sr = @ldap_search($ds, $CFG->ldap_base_dn, $filter, array('mail','domainglobaladmin')); | |
165 | $info = @ldap_get_entries($ds, $sr); // array | |
166 | if ($info['count'] > 0) { | |
b95d1cdb MR |
167 | $_SESSION['user'] = $user; |
168 | $this->user = $user; | |
169 | $result = true; | |
170 | $this->loginStatus = 'OK'; | |
171 | $admin = 'NO'; | |
172 | if (isset($info[0]['domainglobaladmin'])) { | |
173 | $admin = $info[0]['domainglobaladmin'][0]; | |
174 | $admin = strtoupper($admin); | |
175 | } | |
176 | $this->is_admin = ($admin == 'YES') ? true : false; | |
177 | $_SESSION['is_admin'] = $this->is_admin; | |
6df4b805 MR |
178 | } else { |
179 | $this->loginStatus = 'Login failed'; | |
180 | } | |
181 | } else { | |
182 | $this->loginStatus = ldap_error($ds); | |
183 | } | |
184 | @ldap_close($ds); | |
185 | } else { | |
186 | $this->loginStatus = 'Connect to LDAP server failed'; | |
187 | } | |
188 | ||
6e081c5f MR |
189 | $_SESSION['Utils'] = serialize($this); |
190 | ||
b95d1cdb MR |
191 | return $result; |
192 | } | |
193 | ||
194 | public function getLoginStatus() { | |
195 | return $this->loginStatus; | |
196 | } | |
197 | ||
198 | public function isLoggedIn() { | |
199 | global $CFG; | |
200 | $loggedIn = false; | |
201 | ||
202 | if ($this->user) { | |
203 | $loggedIn = true; | |
204 | } else if (isset($_SESSION['user'])) { | |
205 | $this->user = $_SESSION['user']; | |
206 | $loggedIn = true; | |
207 | } else { | |
208 | if ($CFG->auth_method == 'HTTP_AUTH') { | |
209 | if (isset($this->server['PHP_AUTH_USER'])) { | |
210 | $this->user = $this->server['PHP_AUTH_USER']; | |
211 | $loggedIn = true; | |
212 | } | |
213 | } | |
214 | } | |
215 | ||
85ec6a84 | 216 | if ($loggedIn == false) { |
3679a8af | 217 | echo '$this->user: '.$this->user.' $_SESSION[\'user\']: '.$_SESSION['user']; |
18d80742 MR |
218 | echo 'R_TIME: '.date('c', $_SERVER['REQUEST_TIME']).' L_ACT: '.date('c', $_SESSION['LAST_ACTIVITY']); |
219 | exit; | |
220 | } | |
6e081c5f MR |
221 | |
222 | $_SESSION['Utils'] = serialize($this); | |
223 | ||
b95d1cdb MR |
224 | return $loggedIn; |
225 | } | |
226 | ||
227 | public function getUser() { | |
228 | $this->isLoggedIn(); | |
229 | return $this->user; | |
230 | } | |
231 | ||
232 | public function getHeader() { | |
233 | return $this->header; | |
234 | } | |
235 | ||
236 | public function getFooter() { | |
237 | return $this->footer; | |
238 | } | |
239 | ||
240 | public function getHeading() { | |
241 | return $this->heading; | |
242 | } | |
243 | ||
244 | public function setHeading($heading) { | |
245 | global $CFG; | |
246 | ||
247 | $timeout = $CFG->session_timeout * 60 * 1000; | |
248 | $this->heading = str_replace('__TITLE__', $heading, $this->heading); | |
249 | $this->header = str_replace('__TITLE__', $heading, $this->header); | |
250 | $this->header = str_replace('__ROOT__', $CFG->wwwroot, $this->header); | |
251 | $this->header = str_replace('__TIMEOUT__', $timeout, $this->header); | |
6e081c5f MR |
252 | |
253 | $_SESSION['Utils'] = serialize($this); | |
b95d1cdb MR |
254 | } |
255 | ||
256 | public function convertContent($code) { | |
257 | $table = array( | |
258 | 'V' => 'Virus', | |
259 | 'B' => 'Banned', | |
260 | 'U' => 'Unchecked', | |
261 | 'S' => 'Spam', | |
262 | 'Y' => 'Spammy', | |
263 | 'M' => 'Bad Mime', | |
264 | 'H' => 'Bad Header', | |
265 | 'O' => 'Over sized', | |
266 | 'T' => 'MTA err', | |
267 | 'C' => 'Clean' | |
268 | ); | |
269 | ||
270 | $string = $table[$code]; | |
271 | if (empty($string)) | |
272 | $string = 'Unknown'; | |
273 | ||
274 | return $string; | |
275 | } | |
6df4b805 | 276 | |
6df4b805 | 277 | } |