2 /* vim: set ts=4 tw=0 sw=4 noet: */
3 require_once $CFG->root
.'config.php';
7 private static $_instance = null;
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 <title>__TITLE__</title>
24 private $footer = '<p class="footer">Powered by <a href="https://qtadmin.datanom.net">
25 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 private function __construct() {
32 $this->server
= $_SERVER;
36 $this->is_admin
= false;
37 $this->loginStatus
= 'Not logged in';
39 if (isset($_SESSION['user'])) {
40 $this->user
= $_SESSION['user'];
41 $this->loginStatus
= 'OK';
42 $this->is_admin
= $_SESSION['is_admin'];
44 if ($CFG->auth_method
== 'HTTP_AUTH') {
45 if (isset($this->server
['PHP_AUTH_USER'])) {
46 $this->user
= $this->server
['PHP_AUTH_USER'];
47 $this->loginStatus
= 'OK';
48 if ($CFG->admin_user
== $this->user
)
49 $this->is_admin
= true;
53 $_SESSION['user'] = $this->user
;
54 $_SESSION['is_admin'] = $this->is_admin
;
57 private function __clone() {}
59 public static function getInstance() {
62 if (!is_object(self
::$_instance)) {
63 self
::$_instance = new Utils();
65 // Session timeout handler
66 if ('' == session_id())
68 if (isset($CFG->session_timeout
)) {
69 $timeout = $CFG->session_timeout
* 60;
74 if (ini_get('session.gc_maxlifetime') != $timeout)
75 ini_set('session.gc_maxlifetime', $timeout);
76 if (ini_get('session.cookie_lifetime') != $timeout)
77 ini_set('session.cookie_lifetime', $timeout);
78 $time = $_SERVER['REQUEST_TIME'];
79 if (isset($_SESSION['LAST_ACTIVITY']) && ($time - $_SESSION['LAST_ACTIVITY']) >= $timeout) {
83 self
::$_instance->user
= null;
84 self
::$_instance->is_admin
= false;
86 $_SESSION['LAST_ACTIVITY'] = $time;
88 return self
::$_instance;
91 public function logout() {
93 if (ini_get('session.use_cookies')) {
94 $params = session_get_cookie_params();
95 setcookie(session_name(), '', time() - 42000,
96 $params['path'], $params['domain'],
97 $params['secure'], $params['httponly']);
102 $this->is_admin
= false;
105 public function isAdmin() {
106 //file_put_contents('/tmp/login.txt', var_export($this, true));
107 return $this->is_admin
;
110 public function login($user, $pw) {
114 unset($_SESSION['user']);
115 unset($_SESSION['is_admin']);
117 $this->is_admin
= false;
119 $p = explode('@', $user);
120 if (count($p) != 2) {
121 $this->loginStatus
= 'Bad username';
125 $dn = "mail=$user,ou=Users,domainName=$domain,$CFG->ldap_base_dn";
126 $filter = "(&(objectclass=mailUser)(accountStatus=active)(mail=$user))";
127 $ds = @ldap_connect
($CFG->ldap_dsn
);
129 @ldap_set_option
($ds, LDAP_OPT_PROTOCOL_VERSION
, 3);
130 $r = @ldap_bind
($ds, $dn, $pw);
132 $sr = @ldap_search
($ds, $CFG->ldap_base_dn
, $filter, array('mail','domainglobaladmin'));
133 $info = @ldap_get_entries
($ds, $sr); // array
134 if ($info['count'] > 0) {
135 $_SESSION['user'] = $user;
138 $this->loginStatus
= 'OK';
140 if (isset($info[0]['domainglobaladmin'])) {
141 $admin = $info[0]['domainglobaladmin'][0];
142 $admin = strtoupper($admin);
144 $this->is_admin
= ($admin == 'YES') ?
true : false;
145 $_SESSION['is_admin'] = $this->is_admin
;
147 $this->loginStatus
= 'Login failed';
150 $this->loginStatus
= ldap_error($ds);
154 $this->loginStatus
= 'Connect to LDAP server failed';
160 public function getLoginStatus() {
161 return $this->loginStatus
;
164 public function isLoggedIn() {
170 } else if (isset($_SESSION['user'])) {
171 $this->user
= $_SESSION['user'];
174 if ($CFG->auth_method
== 'HTTP_AUTH') {
175 if (isset($this->server
['PHP_AUTH_USER'])) {
176 $this->user
= $this->server
['PHP_AUTH_USER'];
185 public function getUser() {
190 public function getHeader() {
191 return $this->header
;
194 public function getFooter() {
195 return $this->footer
;
198 public function getHeading() {
199 return $this->heading
;
202 public function setHeading($heading) {
205 $timeout = $CFG->session_timeout
* 60 * 1000;
206 $this->heading
= str_replace('__TITLE__', $heading, $this->heading
);
207 $this->header
= str_replace('__TITLE__', $heading, $this->header
);
208 $this->header
= str_replace('__ROOT__', $CFG->wwwroot
, $this->header
);
209 $this->header
= str_replace('__TIMEOUT__', $timeout, $this->header
);
212 public function convertContent($code) {
226 $string = $table[$code];