2 /* vim: set ts=4 tw=4 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 <script src="__ROOT__js/forms.js"></script>
23 <title>__TITLE__</title>
26 private $footer = '<p class="footer">Powered by <a href="https://qtadmin.datanom.net"
27 title="Goto QtAdmin homepage">QtAdmin</a>. © 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>';
31 public function __construct() {
34 if (isset($CFG->log_level
)) {
35 $this->log_level
= $CFG->log_level
;
40 if (isset($CFG->log_method
)) {
41 $this->log_method
= $CFG->log_method
;
43 $this->log_level
= 'syslog';
46 $this->log("Init Utils", 4);
48 $this->log("__construct[1]: user ".var_export($this->settings
['user'], true), 3);
49 $this->startSession();
50 $this->log("__construct[2]: user ".var_export($this->settings
['user'], true), 3);
52 if (! isset($_SESSION['settings'])) {
53 $this->initSettings();
55 $this->log("__construct[3]: user ".var_export($this->settings
['user'], true), 3);
56 $this->settings
= $_SESSION['settings'];
57 $this->log("__construct[4]: user ".var_export($this->settings
['user'], true), 3);
59 if ($CFG->auth_method
== 'HTTP_AUTH') {
60 if (isset($_SERVER['PHP_AUTH_USER'])) {
61 $this->settings
['user'] = $_SERVER['PHP_AUTH_USER'];
62 $this->settings
['loginStatus'] = 'OK';
63 if ($CFG->admin_user
== $this->settings
['user'])
64 $this->settings
['admin'] = true;
69 private function log($message, $level = 1) {
72 if ($level > $this->log_level
)
79 case 1: $priority = LOG_ERR
; break;
80 case 2: $priority = LOG_WARNING
; break;
81 case 3: $priority = LOG_INFO
; break;
82 case 4: $priority = LOG_DEBUG
; break;
85 switch ($this->log_method
) {
87 if (isset($CFG->log_file
)) {
88 if ($CFG->log_file
[0] == '/') {
89 $file = $CFG->log_file
;
91 $file = $CFG->root
.$CFG->log_file
;
94 $file = $CFG->root
.'qtadmin.log';
96 file_put_contents($file, "[$time]: $message\n", FILE_APPEND | LOCK_EX
);
100 file_put_contents('php://stderr', "[$time]: $message\n");
103 syslog($priority, $message);
108 private function initSettings() {
109 $this->log("InitSettings", 4);
111 if ('' == session_id()) {
112 $this->startSession();
115 if (false !== $this->timeout
) {
116 $timeout = $this->timeout
;
121 $this->settings
= array(
124 'loginStatus' => 'Not logged in',
125 'timeout' => $timeout
128 $_SESSION['settings'] = $this->settings
;
131 private function startSession() {
134 $this->log("startSession", 4);
136 if (isset($CFG->session_timeout
)) {
137 $this->timeout
= $CFG->session_timeout
* 60;
139 $this->timeout
= 20 * 60;
142 if (ini_get('session.gc_maxlifetime') != $this->timeout
)
143 ini_set('session.gc_maxlifetime', $this->timeout
);
144 //if (ini_get('session.cookie_lifetime') != $this->timeout)
145 // ini_set('session.cookie_lifetime', $this->timeout);
146 ini_set('session.cookie_lifetime', 0);
151 private function checkSession() {
154 $this->log("checkSession", 4);
156 if ('' == session_id()) {
157 $this->startSession();
160 $time = $_SERVER['REQUEST_TIME'];
161 if (isset($_SESSION['LAST_ACTIVITY']) &&
162 ($time - $_SESSION['LAST_ACTIVITY']) >= $this->settings
['timeout']) {
163 $this->log('R_TIME: '.date('c', $time).' L_ACT: '.date('c', $_SESSION['LAST_ACTIVITY'].
164 'Test: '.($time - $_SESSION['LAST_ACTIVITY'])).' >= '.$this->settings
['timeout'], 3);
167 $_SESSION['LAST_ACTIVITY'] = $time;
171 public function logout() {
172 $this->log("logout", 4);
174 if (ini_get('session.use_cookies')) {
175 $params = session_get_cookie_params();
176 setcookie(session_name(), '', time() - 42000,
177 $params['path'], $params['domain'],
178 $params['secure'], $params['httponly']);
181 if ('' != session_id()) {
186 $this->settings
= array();
189 public function isAdmin() {
192 $this->log("isAdmin", 4);
194 if (isset($this->settings
['admin'])) {
195 $admin = $this->settings
['admin'];
201 public function login($user, $pw) {
205 $this->log("login", 4);
207 if ('' == session_id()) {
208 $this->startSession();
211 $this->settings
['user'] = null;
212 $this->settings
['admin'] = false;
214 $p = explode('@', $user);
215 if (count($p) != 2) {
216 $this->settings
['loginStatus'] = 'Bad username';
219 $dn = "mail=$user,ou=Users,domainName=$domain,$CFG->ldap_base_dn";
220 $filter = "(&(objectclass=mailUser)(accountStatus=active)(mail=$user))";
221 $ds = @ldap_connect
($CFG->ldap_dsn
);
223 @ldap_set_option
($ds, LDAP_OPT_PROTOCOL_VERSION
, 3);
224 $r = @ldap_bind
($ds, $dn, $pw);
226 $sr = @ldap_search
($ds, $CFG->ldap_base_dn
, $filter, array('mail','domainglobaladmin'));
227 $info = @ldap_get_entries
($ds, $sr); // array
228 if ($info['count'] > 0) {
229 $this->settings
['user'] = $user;
231 $this->settings
['loginStatus'] = 'OK';
233 if (isset($info[0]['domainglobaladmin'])) {
234 $admin = $info[0]['domainglobaladmin'][0];
235 $admin = strtoupper($admin);
237 $this->settings
['admin'] = ($admin == 'YES') ?
true : false;
239 $this->settings
['loginStatus'] = 'Login failed';
242 $this->settings
['loginStatus'] = ldap_error($ds);
246 $this->settings
['loginStatus'] = 'Connect to LDAP server failed';
250 $_SESSION['settings'] = $this->settings
;
255 public function getLoginStatus() {
256 $status = 'Not logged in';
258 $this->log("getLoginStatus", 4);
260 if (isset($this->settings
['loginStatus'])) {
261 $status = $this->settings
['loginStatus'];
267 public function isLoggedIn() {
271 $this->log("isLoggedIn[1]: user ".var_export($this->settings
['user'], true), 3);
273 if ('' == session_id()) {
274 $this->startSession();
277 $this->log("isLoggedIn[2]: user ".var_export($this->settings
['user'], true), 3);
278 $this->checkSession();
279 $this->log("isLoggedIn[3]: user ".var_export($this->settings
['user'], true), 3);
281 if (isset($this->settings
['user'])) {
282 if ($this->settings
['user'] != null) {
285 if ($CFG->auth_method
== 'HTTP_AUTH') {
286 if (isset($_SERVER['PHP_AUTH_USER'])) {
287 $this->settings
['user'] = $_SERVER['PHP_AUTH_USER'];
294 if ($loggedIn == false) {
295 $this->log('$this->settings: '.var_export($this->settings
, true), 3);
296 $this->log('R_TIME: '.date('c', $_SERVER['REQUEST_TIME']).' L_ACT: '.date('c', $_SESSION['LAST_ACTIVITY']), 3);
299 $_SESSION['settings'] = $this->settings
;
304 public function getUser() {
307 $this->log("getUser", 4);
309 if ($this->isLoggedIn()) {
310 $user = $this->settings
['user'];
316 public function getHeader() {
317 $this->log("getHeader", 4);
319 return $this->header
;
322 public function getFooter() {
323 $this->log("getFooter", 4);
325 return $this->footer
;
328 public function getHeading() {
329 $this->log("getHeading", 4);
331 return $this->heading
;
334 public function setHeading($heading) {
337 $this->log("setHeading", 4);
339 $timeout = $CFG->session_timeout
* 60 * 1000;
340 $this->heading
= str_replace('__TITLE__', $heading, $this->heading
);
341 $this->header
= str_replace('__TITLE__', $heading, $this->header
);
342 $this->header
= str_replace('__ROOT__', $CFG->wwwroot
, $this->header
);
343 $this->header
= str_replace('__TIMEOUT__', $timeout, $this->header
);
346 public function convertContent($code) {
347 $this->log("convertContent", 4);
362 $string = $table[$code];