2 /* vim: set ts=4 tw=0 sw=4 noet: */
3 require_once $CFG->root
.'config.php';
5 class Utils
implements Serializable
{
7 private static $_instance = null;
13 private $header = '<!DOCTYPE html>
16 <meta charset="utf-8">
17 <link rel="stylesheet" href="css/styles.css">
19 var timeout = __TIMEOUT__;
21 <script src="__ROOT__js/timer.js"></script>
22 <script src="__ROOT__js/checkbox.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 private function __construct() {
34 $this->server
= $_SERVER;
37 $this->is_admin
= false;
38 $this->loginStatus
= 'Not logged in';
40 $this->startSession();
42 if (isset($_SESSION['user'])) {
43 $this->user
= $_SESSION['user'];
44 $this->loginStatus
= 'OK';
45 $this->is_admin
= $_SESSION['is_admin'];
47 if ($CFG->auth_method
== 'HTTP_AUTH') {
48 if (isset($this->server
['PHP_AUTH_USER'])) {
49 $this->user
= $this->server
['PHP_AUTH_USER'];
50 $this->loginStatus
= 'OK';
51 if ($CFG->admin_user
== $this->user
)
52 $this->is_admin
= true;
56 $_SESSION['user'] = $this->user
;
57 $_SESSION['is_admin'] = $this->is_admin
;
58 $_SESSION['Utils'] = serialize($this);
61 private function __clone() {}
63 public function serialize() {
64 file_put_contents('/tmp/dump', 'Serialize called: '.var_export($this, true), FILE_APPEND
);
65 return serialize(get_object_vars($this));
68 public function unserialize($data) {
69 $values = unserialize($data);
70 foreach ($values as $key=>$value) {
75 private function startSession() {
78 if (isset($CFG->session_timeout
)) {
79 $this->timeout
= $CFG->session_timeout
* 60;
81 $this->timeout
= 20 * 60;
84 if (ini_get('session.gc_maxlifetime') != $this->timeout
)
85 ini_set('session.gc_maxlifetime', $this->timeout
);
86 if (ini_get('session.cookie_lifetime') != $this->timeout
)
87 ini_set('session.cookie_lifetime', $this->timeout
);
91 //echo ini_get('session.gc_maxlifetime').':'.ini_get('session.cookie_lifetime');
94 public static function getInstance() {
97 if (!is_object(self
::$_instance)) {
98 if (isset($_SESSION['Utils'])) {
99 self
::$_instance = unserialize($_SESSION['Utils']);
100 file_put_contents('/tmp/dump', 'Unserialize called: '.var_export(self
::$_instance, true), FILE_APPEND
);
102 self
::$_instance = new Utils();
106 $time = $_SERVER['REQUEST_TIME'];
107 if (isset($_SESSION['LAST_ACTIVITY']) &&
108 ($time - $_SESSION['LAST_ACTIVITY']) >= self
::$_instance->timeout
) {
109 echo 'R_TIME: '.date('c', $time).' L_ACT: '.date('c', $_SESSION['LAST_ACTIVITY']);
113 self
::$_instance->user
= null;
114 self
::$_instance->is_admin
= false;
115 $_SESSION['Utils'] = serialize(self
::$_instance);
117 $_SESSION['LAST_ACTIVITY'] = $time;
120 return self
::$_instance;
123 public function logout() {
125 if (ini_get('session.use_cookies')) {
126 $params = session_get_cookie_params();
127 setcookie(session_name(), '', time() - 42000,
128 $params['path'], $params['domain'],
129 $params['secure'], $params['httponly']);
134 $this->is_admin
= false;
137 public function isAdmin() {
138 //file_put_contents('/tmp/login.txt', var_export($this, true));
139 return $this->is_admin
;
142 public function login($user, $pw) {
146 unset($_SESSION['user']);
147 unset($_SESSION['is_admin']);
149 $this->is_admin
= false;
151 $p = explode('@', $user);
152 if (count($p) != 2) {
153 $this->loginStatus
= 'Bad username';
157 $dn = "mail=$user,ou=Users,domainName=$domain,$CFG->ldap_base_dn";
158 $filter = "(&(objectclass=mailUser)(accountStatus=active)(mail=$user))";
159 $ds = @ldap_connect
($CFG->ldap_dsn
);
161 @ldap_set_option
($ds, LDAP_OPT_PROTOCOL_VERSION
, 3);
162 $r = @ldap_bind
($ds, $dn, $pw);
164 $sr = @ldap_search
($ds, $CFG->ldap_base_dn
, $filter, array('mail','domainglobaladmin'));
165 $info = @ldap_get_entries
($ds, $sr); // array
166 if ($info['count'] > 0) {
167 $_SESSION['user'] = $user;
170 $this->loginStatus
= 'OK';
172 if (isset($info[0]['domainglobaladmin'])) {
173 $admin = $info[0]['domainglobaladmin'][0];
174 $admin = strtoupper($admin);
176 $this->is_admin
= ($admin == 'YES') ?
true : false;
177 $_SESSION['is_admin'] = $this->is_admin
;
179 $this->loginStatus
= 'Login failed';
182 $this->loginStatus
= ldap_error($ds);
186 $this->loginStatus
= 'Connect to LDAP server failed';
189 $_SESSION['Utils'] = serialize($this);
194 public function getLoginStatus() {
195 return $this->loginStatus
;
198 public function isLoggedIn() {
204 } else if (isset($_SESSION['user'])) {
205 $this->user
= $_SESSION['user'];
208 if ($CFG->auth_method
== 'HTTP_AUTH') {
209 if (isset($this->server
['PHP_AUTH_USER'])) {
210 $this->user
= $this->server
['PHP_AUTH_USER'];
216 if ($loggedIn == false) {
217 echo '$this->user: '.$this->user
.' $_SESSION[\'user\']: '.$_SESSION['user'];
218 echo 'R_TIME: '.date('c', $_SERVER['REQUEST_TIME']).' L_ACT: '.date('c', $_SESSION['LAST_ACTIVITY']);
222 $_SESSION['Utils'] = serialize($this);
227 public function getUser() {
232 public function getHeader() {
233 return $this->header
;
236 public function getFooter() {
237 return $this->footer
;
240 public function getHeading() {
241 return $this->heading
;
244 public function setHeading($heading) {
247 $timeout = $CFG->session_timeout
* 60 * 1000;
248 $this->heading
= str_replace('__TITLE__', $heading, $this->heading
);
249 $this->header
= str_replace('__TITLE__', $heading, $this->header
);
250 $this->header
= str_replace('__ROOT__', $CFG->wwwroot
, $this->header
);
251 $this->header
= str_replace('__TIMEOUT__', $timeout, $this->header
);
253 $_SESSION['Utils'] = serialize($this);
256 public function convertContent($code) {
270 $string = $table[$code];