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';
8 private $timeout = false;
12 private $header = '<!DOCTYPE html>
15 <meta charset="utf-8">
16 <link rel="stylesheet" href="css/styles.css">
18 var timeout = __TIMEOUT__;
20 <script src="__ROOT__js/timer.js"></script>
21 <script src="__ROOT__js/checkbox.js"></script>
22 <title>__TITLE__</title>
25 private $footer = '<p class="footer">Powered by <a href="https://qtadmin.datanom.net"
26 title="Goto QtAdmin homepage">QtAdmin</a>. © 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>';
30 public function __construct() {
33 if (isset($CFG->log_level
)) {
34 $this->log_level
= $CFG->log_level
;
39 if (isset($CFG->log_method
)) {
40 $this->log_method
= $CFG->log_method
;
42 $this->log_level
= 'syslog';
45 $this->log("Init Utils", 4);
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);
51 if (! isset($_SESSION['settings'])) {
52 $this->initSettings();
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);
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;
68 private function log($message, $level = 1) {
71 if ($level > $this->log_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;
84 switch ($this->log_method
) {
86 if (isset($CFG->log_file
)) {
87 if ($CFG->log_file
[0] == '/') {
88 $file = $CFG->log_file
;
90 $file = $CFG->root
.$CFG->log_file
;
93 $file = $CFG->root
.'qtadmin.log';
95 file_put_contents($file, "[$time]: $message\n", FILE_APPEND | LOCK_EX
);
99 file_put_contents('php://stderr', "[$time]: $message\n");
102 syslog($priority, $message);
107 private function initSettings() {
108 $this->log("InitSettings", 4);
110 if ('' == session_id()) {
111 $this->startSession();
114 if (false !== $this->timeout
) {
115 $timeout = $this->timeout
;
120 $this->settings
= array(
123 'loginStatus' => 'Not logged in',
124 'timeout' => $timeout
127 $_SESSION['settings'] = $this->settings
;
130 private function startSession() {
133 $this->log("startSession", 4);
135 if (isset($CFG->session_timeout
)) {
136 $this->timeout
= $CFG->session_timeout
* 60;
138 $this->timeout
= 20 * 60;
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);
150 private function checkSession() {
153 $this->log("checkSession", 4);
155 if ('' == session_id()) {
156 $this->startSession();
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);
166 $_SESSION['LAST_ACTIVITY'] = $time;
170 public function logout() {
171 $this->log("logout", 4);
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']);
180 if ('' != session_id()) {
185 $this->settings
= array();
188 public function isAdmin() {
191 $this->log("isAdmin", 4);
193 if (isset($this->settings
['admin'])) {
194 $admin = $this->settings
['admin'];
200 public function login($user, $pw) {
204 $this->log("login", 4);
206 if ('' == session_id()) {
207 $this->startSession();
210 $this->settings
['user'] = null;
211 $this->settings
['admin'] = false;
213 $p = explode('@', $user);
214 if (count($p) != 2) {
215 $this->settings
['loginStatus'] = 'Bad username';
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
);
222 @ldap_set_option
($ds, LDAP_OPT_PROTOCOL_VERSION
, 3);
223 $r = @ldap_bind
($ds, $dn, $pw);
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;
230 $this->settings
['loginStatus'] = 'OK';
232 if (isset($info[0]['domainglobaladmin'])) {
233 $admin = $info[0]['domainglobaladmin'][0];
234 $admin = strtoupper($admin);
236 $this->settings
['admin'] = ($admin == 'YES') ?
true : false;
238 $this->settings
['loginStatus'] = 'Login failed';
241 $this->settings
['loginStatus'] = ldap_error($ds);
245 $this->settings
['loginStatus'] = 'Connect to LDAP server failed';
249 $_SESSION['settings'] = $this->settings
;
254 public function getLoginStatus() {
255 $status = 'Not logged in';
257 $this->log("getLoginStatus", 4);
259 if (isset($this->settings
['loginStatus'])) {
260 $status = $this->settings
['loginStatus'];
266 public function isLoggedIn() {
270 $this->log("isLoggedIn[1]: user ".var_export($this->settings
['user'], true), 3);
272 if ('' == session_id()) {
273 $this->startSession();
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);
280 if (isset($this->settings
['user'])) {
281 if ($this->settings
['user'] != null) {
284 if ($CFG->auth_method
== 'HTTP_AUTH') {
285 if (isset($_SERVER['PHP_AUTH_USER'])) {
286 $this->settings
['user'] = $_SERVER['PHP_AUTH_USER'];
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);
298 $_SESSION['settings'] = $this->settings
;
303 public function getUser() {
306 $this->log("getUser", 4);
308 if ($this->isLoggedIn()) {
309 $user = $this->settings
['user'];
315 public function getHeader() {
316 $this->log("getHeader", 4);
318 return $this->header
;
321 public function getFooter() {
322 $this->log("getFooter", 4);
324 return $this->footer
;
327 public function getHeading() {
328 $this->log("getHeading", 4);
330 return $this->heading
;
333 public function setHeading($heading) {
336 $this->log("setHeading", 4);
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
);
345 public function convertContent($code) {
346 $this->log("convertContent", 4);
361 $string = $table[$code];