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 <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 private function __construct() {
33 $this->server
= $_SERVER;
37 $this->is_admin
= false;
38 $this->loginStatus
= 'Not logged in';
40 if (isset($_SESSION['user'])) {
41 $this->user
= $_SESSION['user'];
42 $this->loginStatus
= 'OK';
43 $this->is_admin
= $_SESSION['is_admin'];
45 if ($CFG->auth_method
== 'HTTP_AUTH') {
46 if (isset($this->server
['PHP_AUTH_USER'])) {
47 $this->user
= $this->server
['PHP_AUTH_USER'];
48 $this->loginStatus
= 'OK';
49 if ($CFG->admin_user
== $this->user
)
50 $this->is_admin
= true;
54 $_SESSION['user'] = $this->user
;
55 $_SESSION['is_admin'] = $this->is_admin
;
58 private function __clone() {}
60 public static function getInstance() {
63 if (!is_object(self
::$_instance)) {
64 self
::$_instance = new Utils();
66 // Session timeout handler
67 if ('' == session_id())
69 if (isset($CFG->session_timeout
)) {
70 $timeout = $CFG->session_timeout
* 60;
75 if (ini_get('session.gc_maxlifetime') != $timeout)
76 ini_set('session.gc_maxlifetime', $timeout);
77 if (ini_get('session.cookie_lifetime') != $timeout)
78 ini_set('session.cookie_lifetime', $timeout);
79 $time = $_SERVER['REQUEST_TIME'];
80 if (isset($_SESSION['LAST_ACTIVITY']) && ($time - $_SESSION['LAST_ACTIVITY']) >= $timeout) {
84 self
::$_instance->user
= null;
85 self
::$_instance->is_admin
= false;
87 $_SESSION['LAST_ACTIVITY'] = $time;
89 return self
::$_instance;
92 public function logout() {
94 if (ini_get('session.use_cookies')) {
95 $params = session_get_cookie_params();
96 setcookie(session_name(), '', time() - 42000,
97 $params['path'], $params['domain'],
98 $params['secure'], $params['httponly']);
103 $this->is_admin
= false;
106 public function isAdmin() {
107 //file_put_contents('/tmp/login.txt', var_export($this, true));
108 return $this->is_admin
;
111 public function login($user, $pw) {
115 unset($_SESSION['user']);
116 unset($_SESSION['is_admin']);
118 $this->is_admin
= false;
120 $p = explode('@', $user);
121 if (count($p) != 2) {
122 $this->loginStatus
= 'Bad username';
126 $dn = "mail=$user,ou=Users,domainName=$domain,$CFG->ldap_base_dn";
127 $filter = "(&(objectclass=mailUser)(accountStatus=active)(mail=$user))";
128 $ds = @ldap_connect
($CFG->ldap_dsn
);
130 @ldap_set_option
($ds, LDAP_OPT_PROTOCOL_VERSION
, 3);
131 $r = @ldap_bind
($ds, $dn, $pw);
133 $sr = @ldap_search
($ds, $CFG->ldap_base_dn
, $filter, array('mail','domainglobaladmin'));
134 $info = @ldap_get_entries
($ds, $sr); // array
135 if ($info['count'] > 0) {
136 $_SESSION['user'] = $user;
139 $this->loginStatus
= 'OK';
141 if (isset($info[0]['domainglobaladmin'])) {
142 $admin = $info[0]['domainglobaladmin'][0];
143 $admin = strtoupper($admin);
145 $this->is_admin
= ($admin == 'YES') ?
true : false;
146 $_SESSION['is_admin'] = $this->is_admin
;
148 $this->loginStatus
= 'Login failed';
151 $this->loginStatus
= ldap_error($ds);
155 $this->loginStatus
= 'Connect to LDAP server failed';
161 public function getLoginStatus() {
162 return $this->loginStatus
;
165 public function isLoggedIn() {
171 } else if (isset($_SESSION['user'])) {
172 $this->user
= $_SESSION['user'];
175 if ($CFG->auth_method
== 'HTTP_AUTH') {
176 if (isset($this->server
['PHP_AUTH_USER'])) {
177 $this->user
= $this->server
['PHP_AUTH_USER'];
186 public function getUser() {
191 public function getHeader() {
192 return $this->header
;
195 public function getFooter() {
196 return $this->footer
;
199 public function getHeading() {
200 return $this->heading
;
203 public function setHeading($heading) {
206 $timeout = $CFG->session_timeout
* 60 * 1000;
207 $this->heading
= str_replace('__TITLE__', $heading, $this->heading
);
208 $this->header
= str_replace('__TITLE__', $heading, $this->header
);
209 $this->header
= str_replace('__ROOT__', $CFG->wwwroot
, $this->header
);
210 $this->header
= str_replace('__TIMEOUT__', $timeout, $this->header
);
213 public function convertContent($code) {
227 $string = $table[$code];