2 /* vim: set ts=4 tw=0 sw=4 noet: */
3 require_once $CFG->root
.'config.php';
7 private $timeout = false;
11 private $header = '<!DOCTYPE html>
14 <meta charset="utf-8">
15 <link rel="stylesheet" href="css/styles.css">
17 var timeout = __TIMEOUT__;
19 <script src="__ROOT__js/timer.js"></script>
20 <script src="__ROOT__js/checkbox.js"></script>
21 <title>__TITLE__</title>
24 private $footer = '<p class="footer">Powered by <a href="https://qtadmin.datanom.net"
25 title="Goto QtAdmin homepage">QtAdmin</a>. © 2015 by Michael Rasmussen</p></body></html>';
26 private $heading = '<p id="time" class="time">Session timeout:
27 <span id="timer"></span></p><h1 class="h1">__TITLE__</h1>';
29 public function __construct() {
32 if (isset($CFG->log_level
)) {
33 $this->log_level
= $CFG->log_level
;
38 if (isset($CFG->log_method
)) {
39 $this->log_method
= $CFG->log_method
;
41 $this->log_level
= 'syslog';
44 $this->log("Init Utils", 4);
46 $this->log("__construct[1]: user ".var_export($this->settings
['user'], true), 3);
47 $this->startSession();
48 $this->log("__construct[2]: user ".var_export($this->settings
['user'], true), 3);
50 if (! isset($_SESSION['settings'])) {
51 $this->initSettings();
53 $this->log("__construct[3]: user ".var_export($this->settings
['user'], true), 3);
54 $this->settings
= $_SESSION['settings'];
55 $this->log("__construct[4]: user ".var_export($this->settings
['user'], true), 3);
57 if ($CFG->auth_method
== 'HTTP_AUTH') {
58 if (isset($_SERVER['PHP_AUTH_USER'])) {
59 $this->settings
['user'] = $_SERVER['PHP_AUTH_USER'];
60 $this->settings
['loginStatus'] = 'OK';
61 if ($CFG->admin_user
== $this->settings
['user'])
62 $this->settings
['admin'] = true;
67 private function log($message, $level = 1) {
70 if ($level > $this->log_level
)
77 case 1: $priority = LOG_ERR
; break;
78 case 2: $priority = LOG_WARNING
; break;
79 case 3: $priority = LOG_INFO
; break;
80 case 4: $priority = LOG_DEBUG
; break;
83 switch ($this->log_method
) {
85 if (isset($CFG->log_file
)) {
86 if ($CFG->log_file
[0] == '/') {
87 $file = $CFG->log_file
;
89 $file = $CFG->root
.$CFG->log_file
;
92 $file = $CFG->root
.'qtadmin.log';
94 file_put_contents($file, "[$time]: $message\n", FILE_APPEND | LOCK_EX
);
98 file_put_contents('php://stderr', "[$time]: $message\n");
101 syslog($priority, $message);
106 private function initSettings() {
107 $this->log("InitSettings", 4);
109 if ('' == session_id()) {
110 $this->startSession();
113 if (false !== $this->timeout
) {
114 $timeout = $this->timeout
;
119 $this->settings
= array(
122 'loginStatus' => 'Not logged in',
123 'timeout' => $timeout
126 $_SESSION['settings'] = $this->settings
;
129 private function startSession() {
132 $this->log("startSession", 4);
134 if (isset($CFG->session_timeout
)) {
135 $this->timeout
= $CFG->session_timeout
* 60;
137 $this->timeout
= 20 * 60;
140 if (ini_get('session.gc_maxlifetime') != $this->timeout
)
141 ini_set('session.gc_maxlifetime', $this->timeout
);
142 //if (ini_get('session.cookie_lifetime') != $this->timeout)
143 // ini_set('session.cookie_lifetime', $this->timeout);
144 ini_set('session.cookie_lifetime', 0);
149 private function checkSession() {
152 $this->log("checkSession", 4);
154 if ('' == session_id()) {
155 $this->startSession();
158 $time = $_SERVER['REQUEST_TIME'];
159 if (isset($_SESSION['LAST_ACTIVITY']) &&
160 ($time - $_SESSION['LAST_ACTIVITY']) >= $this->settings
['timeout']) {
161 $this->log('R_TIME: '.date('c', $time).' L_ACT: '.date('c', $_SESSION['LAST_ACTIVITY'].
162 'Test: '.($time - $_SESSION['LAST_ACTIVITY'])).' >= '.$this->settings
['timeout'], 3);
165 $_SESSION['LAST_ACTIVITY'] = $time;
169 public function logout() {
170 $this->log("logout", 4);
172 if (ini_get('session.use_cookies')) {
173 $params = session_get_cookie_params();
174 setcookie(session_name(), '', time() - 42000,
175 $params['path'], $params['domain'],
176 $params['secure'], $params['httponly']);
179 if ('' != session_id()) {
184 $this->settings
= array();
187 public function isAdmin() {
190 $this->log("isAdmin", 4);
192 if (isset($this->settings
['admin'])) {
193 $admin = $this->settings
['admin'];
199 public function login($user, $pw) {
203 $this->log("login", 4);
205 if ('' == session_id()) {
206 $this->startSession();
209 $this->settings
['user'] = null;
210 $this->settings
['admin'] = false;
212 $p = explode('@', $user);
213 if (count($p) != 2) {
214 $this->settings
['loginStatus'] = 'Bad username';
217 $dn = "mail=$user,ou=Users,domainName=$domain,$CFG->ldap_base_dn";
218 $filter = "(&(objectclass=mailUser)(accountStatus=active)(mail=$user))";
219 $ds = @ldap_connect
($CFG->ldap_dsn
);
221 @ldap_set_option
($ds, LDAP_OPT_PROTOCOL_VERSION
, 3);
222 $r = @ldap_bind
($ds, $dn, $pw);
224 $sr = @ldap_search
($ds, $CFG->ldap_base_dn
, $filter, array('mail','domainglobaladmin'));
225 $info = @ldap_get_entries
($ds, $sr); // array
226 if ($info['count'] > 0) {
227 $this->settings
['user'] = $user;
229 $this->settings
['loginStatus'] = 'OK';
231 if (isset($info[0]['domainglobaladmin'])) {
232 $admin = $info[0]['domainglobaladmin'][0];
233 $admin = strtoupper($admin);
235 $this->settings
['admin'] = ($admin == 'YES') ?
true : false;
237 $this->settings
['loginStatus'] = 'Login failed';
240 $this->settings
['loginStatus'] = ldap_error($ds);
244 $this->settings
['loginStatus'] = 'Connect to LDAP server failed';
248 $_SESSION['settings'] = $this->settings
;
253 public function getLoginStatus() {
254 $status = 'Not logged in';
256 $this->log("getLoginStatus", 4);
258 if (isset($this->settings
['loginStatus'])) {
259 $status = $this->settings
['loginStatus'];
265 public function isLoggedIn() {
269 $this->log("isLoggedIn[1]: user ".var_export($this->settings
['user'], true), 3);
271 if ('' == session_id()) {
272 $this->startSession();
275 $this->log("isLoggedIn[2]: user ".var_export($this->settings
['user'], true), 3);
276 $this->checkSession();
277 $this->log("isLoggedIn[3]: user ".var_export($this->settings
['user'], true), 3);
279 if (isset($this->settings
['user'])) {
280 if ($this->settings
['user'] != null) {
283 if ($CFG->auth_method
== 'HTTP_AUTH') {
284 if (isset($_SERVER['PHP_AUTH_USER'])) {
285 $this->settings
['user'] = $_SERVER['PHP_AUTH_USER'];
292 if ($loggedIn == false) {
293 $this->log('$this->settings: '.var_export($this->settings
, true), 3);
294 $this->log('R_TIME: '.date('c', $_SERVER['REQUEST_TIME']).' L_ACT: '.date('c', $_SESSION['LAST_ACTIVITY']), 3);
297 $_SESSION['settings'] = $this->settings
;
302 public function getUser() {
305 $this->log("getUser", 4);
307 if ($this->isLoggedIn()) {
308 $user = $this->settings
['user'];
314 public function getHeader() {
315 $this->log("getHeader", 4);
317 return $this->header
;
320 public function getFooter() {
321 $this->log("getFooter", 4);
323 return $this->footer
;
326 public function getHeading() {
327 $this->log("getHeading", 4);
329 return $this->heading
;
332 public function setHeading($heading) {
335 $this->log("setHeading", 4);
337 $timeout = $CFG->session_timeout
* 60 * 1000;
338 $this->heading
= str_replace('__TITLE__', $heading, $this->heading
);
339 $this->header
= str_replace('__TITLE__', $heading, $this->header
);
340 $this->header
= str_replace('__ROOT__', $CFG->wwwroot
, $this->header
);
341 $this->header
= str_replace('__TIMEOUT__', $timeout, $this->header
);
344 public function convertContent($code) {
345 $this->log("convertContent", 4);
360 $string = $table[$code];