2 /* vim: set ts=4 tw=0 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>
25 <body><div id="container">';
26 private $footer = '</div><div id="footer"><p>Powered by <a href="https://qtadmin.datanom.net"
27 title="Goto QtAdmin homepage">QtAdmin</a>. © 2015 by Michael Rasmussen</p>
28 </div></body></html>';
29 private $heading = '<p id="time" class="time">Session timeout:
30 <span id="timer"></span></p><h1 class="h1">__TITLE__</h1>';
32 public function __construct() {
35 if (isset($CFG->log_level
)) {
36 $this->log_level
= $CFG->log_level
;
41 if (isset($CFG->log_method
)) {
42 $this->log_method
= $CFG->log_method
;
44 $this->log_level
= 'syslog';
47 $this->log("Init Utils", 4);
49 $this->log("__construct[1]: user ".var_export($this->settings
['user'], true), 3);
50 $this->startSession();
51 $this->log("__construct[2]: user ".var_export($this->settings
['user'], true), 3);
53 if (! isset($_SESSION['settings'])) {
54 $this->initSettings();
56 $this->log("__construct[3]: user ".var_export($this->settings
['user'], true), 3);
57 $this->settings
= $_SESSION['settings'];
58 $this->log("__construct[4]: user ".var_export($this->settings
['user'], true), 3);
60 if ($CFG->auth_method
== 'HTTP_AUTH') {
61 if (isset($_SERVER['PHP_AUTH_USER'])) {
62 $this->settings
['user'] = $_SERVER['PHP_AUTH_USER'];
63 $this->settings
['loginStatus'] = 'OK';
64 if ($CFG->admin_user
== $this->settings
['user'])
65 $this->settings
['admin'] = true;
70 private function log($message, $level = 1) {
73 if ($level > $this->log_level
)
80 case 1: $priority = LOG_ERR
; break;
81 case 2: $priority = LOG_WARNING
; break;
82 case 3: $priority = LOG_INFO
; break;
83 case 4: $priority = LOG_DEBUG
; break;
86 switch ($this->log_method
) {
88 if (isset($CFG->log_file
)) {
89 if ($CFG->log_file
[0] == '/') {
90 $file = $CFG->log_file
;
92 $file = $CFG->root
.$CFG->log_file
;
95 $file = $CFG->root
.'qtadmin.log';
97 file_put_contents($file, "[$time]: $message\n", FILE_APPEND | LOCK_EX
);
101 file_put_contents('php://stderr', "[$time]: $message\n");
104 syslog($priority, $message);
109 private function initSettings() {
110 $this->log("InitSettings", 4);
112 if ('' == session_id()) {
113 $this->startSession();
116 if (false !== $this->timeout
) {
117 $timeout = $this->timeout
;
122 $this->settings
= array(
125 'loginStatus' => 'Not logged in',
126 'timeout' => $timeout
129 $_SESSION['settings'] = $this->settings
;
132 private function startSession() {
135 $this->log("startSession", 4);
137 if (isset($CFG->session_timeout
)) {
138 $this->timeout
= $CFG->session_timeout
* 60;
140 $this->timeout
= 20 * 60;
143 if (ini_get('session.gc_maxlifetime') != $this->timeout
)
144 ini_set('session.gc_maxlifetime', $this->timeout
);
145 //if (ini_get('session.cookie_lifetime') != $this->timeout)
146 // ini_set('session.cookie_lifetime', $this->timeout);
147 ini_set('session.cookie_lifetime', 0);
152 private function checkSession() {
155 $this->log("checkSession", 4);
157 if ('' == session_id()) {
158 $this->startSession();
161 $time = $_SERVER['REQUEST_TIME'];
162 if (isset($_SESSION['LAST_ACTIVITY']) &&
163 ($time - $_SESSION['LAST_ACTIVITY']) >= $this->settings
['timeout']) {
164 $this->log('R_TIME: '.date('c', $time).' L_ACT: '.date('c', $_SESSION['LAST_ACTIVITY'].
165 'Test: '.($time - $_SESSION['LAST_ACTIVITY'])).' >= '.$this->settings
['timeout'], 3);
168 $_SESSION['LAST_ACTIVITY'] = $time;
172 private function getCSRFPreventionToken($ticket) {
173 return array('CSRFPreventionToken: ' . $ticket->CSRFPreventionToken
);
176 private function getRestTicket($username, $password) {
180 $url = $CFG->wblistadm_url
. '/ticket';
182 $data = "username=$username&password=$password";
183 $response = $this->RESTCall($url, $data, $cookiesIn = '');
184 if ($response['http_code'] >= 200 && $response['http_code'] <= 204) {
185 $data = json_decode($response['content']);
186 $_SESSION['ticket'] = $data->data
;
187 $_SESSION['cookies'] = $response['cookies'];
194 public function makeRestCall($method, $data = null) {
199 $url = $CFG->wblistadm_url
. "$method";
200 $token = $this->getCSRFPreventionToken($_SESSION['ticket']);
201 $response = $this->RESTCall($url, $data, $_SESSION['cookies'], $token);
203 if ($response['http_code'] >= 200 && $response['http_code'] <= 204) {
209 $data = json_decode($response['content']);
210 $result = $data->data
;
213 $result = ($data) ?
false : array();
219 private function RESTCall($url, $data = null, $cookiesIn = '', $headers = null) {
221 CURLOPT_RETURNTRANSFER
=> true, // return web page
222 CURLOPT_HEADER
=> true, //return headers in addition to content
223 CURLOPT_FOLLOWLOCATION
=> true, // follow redirects
224 CURLOPT_ENCODING
=> "", // handle all encodings
225 CURLOPT_AUTOREFERER
=> true, // set referer on redirect
226 CURLOPT_CONNECTTIMEOUT
=> 120, // timeout on connect
227 CURLOPT_TIMEOUT
=> 120, // timeout on response
228 CURLOPT_MAXREDIRS
=> 10, // stop after 10 redirects
229 CURLINFO_HEADER_OUT
=> true,
230 CURLOPT_SSL_VERIFYPEER
=> false, // Disabled SSL Cert checks
231 CURLOPT_HTTP_VERSION
=> CURL_HTTP_VERSION_1_1
,
232 CURLOPT_COOKIE
=> $cookiesIn
236 $options[CURLOPT_POST
] = 1;
237 $options[CURLOPT_POSTFIELDS
] = $data;
241 $options[CURLOPT_HTTPHEADER
] = $headers;
244 $ch = curl_init($url);
245 curl_setopt_array($ch, $options);
246 $rough_content = curl_exec($ch);
247 $err = curl_errno($ch);
248 $errmsg = curl_error($ch);
249 $header = curl_getinfo($ch);
252 $header_content = substr($rough_content, 0, $header['header_size']);
253 $body_content = trim(str_replace($header_content, '', $rough_content));
254 $pattern = "#Set-Cookie:\\s+(?<cookie>[^=]+=[^;]+)#m";
255 preg_match_all($pattern, $header_content, $matches);
256 $cookiesOut = implode("; ", $matches['cookie']);
258 $header['errno'] = $err;
259 $header['errmsg'] = $errmsg;
260 $header['headers'] = $header_content;
261 $header['content'] = $body_content;
262 $header['cookies'] = $cookiesOut;
267 public function logout() {
268 $this->log("logout", 4);
270 if (ini_get('session.use_cookies')) {
271 $params = session_get_cookie_params();
272 setcookie(session_name(), '', time() - 42000,
273 $params['path'], $params['domain'],
274 $params['secure'], $params['httponly']);
277 if ('' != session_id()) {
282 $this->settings
= array();
285 public function isAdmin() {
288 $this->log("isAdmin", 4);
290 if (isset($this->settings
['admin'])) {
291 $admin = $this->settings
['admin'];
297 public function login($user, $pw) {
301 $this->log("login", 4);
303 if ('' == session_id()) {
304 $this->startSession();
307 $this->settings
['user'] = null;
308 $this->settings
['admin'] = false;
310 $p = explode('@', $user);
311 if (count($p) != 2) {
312 $this->settings
['loginStatus'] = 'Bad username';
315 $dn = "mail=$user,ou=Users,domainName=$domain,$CFG->ldap_base_dn";
316 $filter = "(&(objectclass=mailUser)(accountStatus=active)(mail=$user))";
317 $ds = @ldap_connect
($CFG->ldap_dsn
);
319 @ldap_set_option
($ds, LDAP_OPT_PROTOCOL_VERSION
, 3);
320 $r = @ldap_bind
($ds, $dn, $pw);
322 $sr = @ldap_search
($ds, $CFG->ldap_base_dn
, $filter, array('mail','domainglobaladmin'));
323 $info = @ldap_get_entries
($ds, $sr); // array
324 if ($info['count'] > 0) {
325 // Log in to wblistadm server and get CSRFPreventionToken
326 if ($this->getRestTicket($user, $pw)) {
327 $this->settings
['user'] = $user;
329 $this->settings
['loginStatus'] = 'OK';
331 if (isset($info[0]['domainglobaladmin'])) {
332 $admin = $info[0]['domainglobaladmin'][0];
333 $admin = strtoupper($admin);
335 $this->settings
['admin'] = ($admin == 'YES') ?
true : false;
337 $this->settings
['loginStatus'] = 'Login failed';
340 $this->settings
['loginStatus'] = 'Login failed';
343 $this->settings
['loginStatus'] = ldap_error($ds);
347 $this->settings
['loginStatus'] = 'Connect to LDAP server failed';
351 $_SESSION['settings'] = $this->settings
;
356 public function getLoginStatus() {
357 $status = 'Not logged in';
359 $this->log("getLoginStatus", 4);
361 if (isset($this->settings
['loginStatus'])) {
362 $status = $this->settings
['loginStatus'];
368 public function isLoggedIn() {
372 $this->log("isLoggedIn[1]: user ".var_export($this->settings
['user'], true), 3);
374 if ('' == session_id()) {
375 $this->startSession();
378 $this->log("isLoggedIn[2]: user ".var_export($this->settings
['user'], true), 3);
379 $this->checkSession();
380 $this->log("isLoggedIn[3]: user ".var_export($this->settings
['user'], true), 3);
382 if (isset($this->settings
['user'])) {
383 if ($this->settings
['user'] != null) {
386 if ($CFG->auth_method
== 'HTTP_AUTH') {
387 if (isset($_SERVER['PHP_AUTH_USER'])) {
388 $this->settings
['user'] = $_SERVER['PHP_AUTH_USER'];
395 if ($loggedIn == false) {
396 $this->log('$this->settings: '.var_export($this->settings
, true), 3);
397 $this->log('R_TIME: '.date('c', $_SERVER['REQUEST_TIME']).' L_ACT: '.date('c', $_SESSION['LAST_ACTIVITY']), 3);
400 $_SESSION['settings'] = $this->settings
;
405 public function getUser() {
408 $this->log("getUser", 4);
410 if ($this->isLoggedIn()) {
411 $user = $this->settings
['user'];
417 public function authorized($recipient) {
420 $this->log("authorized '$recipient'", 3);
422 if ($this->isAdmin() ||
$this->getUser() == $recipient) {
425 $msg = ($authorized) ?
'authorize' : 'not authorize';
426 $this->log("$msg '".$this->getUser()."' rcpt '$recipient'", 3);
431 public function getHeader() {
432 $this->log("getHeader", 4);
434 return $this->header
;
437 public function getFooter() {
438 $this->log("getFooter", 4);
440 return $this->footer
;
443 public function getHeading() {
444 $this->log("getHeading", 4);
446 return $this->heading
;
449 public function setHeading($heading) {
452 $this->log("setHeading", 4);
454 $timeout = $CFG->session_timeout
* 60 * 1000;
455 $this->heading
= str_replace('__TITLE__', $heading, $this->heading
);
456 $this->header
= str_replace('__TITLE__', $heading, $this->header
);
457 $this->header
= str_replace('__ROOT__', $CFG->wwwroot
, $this->header
);
458 $this->header
= str_replace('__TIMEOUT__', $timeout, $this->header
);
461 public function convertContent($code) {
462 $this->log("convertContent", 4);
477 $string = $table[$code];