]> git.datanom.net - qtadmin.git/blobdiff - lib/utils.inc.php
Fix bug in session handler
[qtadmin.git] / lib / utils.inc.php
index dca39a6b2473b760e579aca40da9b863f655adcd..5abc5863eeb71c51ad3a9aea19f93c42b8e8098c 100644 (file)
@@ -2,13 +2,14 @@
 /* vim: set ts=4 tw=0 sw=4 noet: */
 require_once $CFG->root .'config.php';
 
-class Utils {
+class Utils implements Serializable {
 
     private static $_instance = null;
     private $server;
     private $user;
     private $is_admin;
     private $loginStatus;
+    private $timeout;
     private $header = '<!DOCTYPE html>
 <html>
 <head>
@@ -18,6 +19,7 @@ class Utils {
         var timeout = __TIMEOUT__;
     </script>
     <script src="__ROOT__js/timer.js"></script>
+    <script src="__ROOT__js/checkbox.js"></script>
     <title>__TITLE__</title>
 </head>
 <body>';
@@ -30,12 +32,13 @@ class Utils {
         global $CFG;
 
         $this->server = $_SERVER;
-        session_start();
 
         $this->user = null;
         $this->is_admin = false;
         $this->loginStatus = 'Not logged in';
 
+        $this->startSession();
+
         if (isset($_SESSION['user'])) {
             $this->user = $_SESSION['user'];
             $this->loginStatus = 'OK';
@@ -52,38 +55,67 @@ class Utils {
         }
         $_SESSION['user'] = $this->user;
         $_SESSION['is_admin'] = $this->is_admin;
+        $_SESSION['Utils'] = serialize($this);
     }
 
     private function __clone() {}
 
-    public static function getInstance() {
-        global $CFG;
+    public function serialize() {
+        file_put_contents('/tmp/dump', 'Serialize called: '.var_export($this, true), FILE_APPEND);
+        return serialize(get_object_vars($this));
+    }
 
-        if (!is_object(self::$_instance)) {
-            self::$_instance = new Utils();
+    public function unserialize($data) {
+        $values = unserialize($data);
+        foreach ($values as $key=>$value) {
+            $this->$key = $value;
         }
-        // Session timeout handler
-        if ('' == session_id())
-            session_start();
+    }
+
+    private function startSession() {
+        global $CFG;
+
         if (isset($CFG->session_timeout)) {
-            $timeout = $CFG->session_timeout * 60;
+            $this->timeout = $CFG->session_timeout * 60;
         } else {
-            $timeout = 20 * 60;
+            $this->timeout = 20 * 60;
+        }
+
+        if (ini_get('session.gc_maxlifetime') != $this->timeout)
+            ini_set('session.gc_maxlifetime', $this->timeout);
+        if (ini_get('session.cookie_lifetime') != $this->timeout)
+            ini_set('session.cookie_lifetime', $this->timeout);
+
+        session_start();
+
+        //echo ini_get('session.gc_maxlifetime').':'.ini_get('session.cookie_lifetime');
+    }
+
+    public static function getInstance() {
+        global $CFG;
+
+        if (!is_object(self::$_instance)) {
+            if (isset($_SESSION['Utils'])) {
+                self::$_instance = unserialize($_SESSION['Utils']);
+                file_put_contents('/tmp/dump', 'Unserialize called: '.var_export($this, true), FILE_APPEND);
+            } else {
+                self::$_instance = new Utils();
+            }
         }
 
-        if (ini_get('session.gc_maxlifetime') != $timeout)
-            ini_set('session.gc_maxlifetime', $timeout);
-        if (ini_get('session.cookie_lifetime') != $timeout)
-            ini_set('session.cookie_lifetime', $timeout);
         $time = $_SERVER['REQUEST_TIME'];
-        if (isset($_SESSION['LAST_ACTIVITY']) && ($time - $_SESSION['LAST_ACTIVITY']) >= $timeout) {
+        if (isset($_SESSION['LAST_ACTIVITY']) &&
+                ($time - $_SESSION['LAST_ACTIVITY']) >= self::$_instance->timeout) {
+            echo 'R_TIME: '.date('c', $time).' L_ACT: '.date('c', $_SESSION['LAST_ACTIVITY']);
+            exit;
             session_unset();
             session_destroy();
-            session_start();
             self::$_instance->user = null;
             self::$_instance->is_admin = false;
+            $_SESSION['Utils'] = serialize(self::$_instance);
+        } else {
+            $_SESSION['LAST_ACTIVITY'] = $time;
         }
-        $_SESSION['LAST_ACTIVITY'] = $time;
 
         return self::$_instance;
     }
@@ -154,6 +186,8 @@ class Utils {
             $this->loginStatus = 'Connect to LDAP server failed';
         }
 
+        $_SESSION['Utils'] = serialize($this);
+
         return $result;
     }
 
@@ -179,6 +213,14 @@ class Utils {
             }
         }
 
+        if ($loggedIn == false) {
+            echo '$this->user: '.$this->user.' $_SESSION[\'user\']: '.$_SESSION['user'];
+            echo 'R_TIME: '.date('c', $_SERVER['REQUEST_TIME']).' L_ACT: '.date('c', $_SESSION['LAST_ACTIVITY']);
+            exit;
+        }
+
+        $_SESSION['Utils'] = serialize($this);
+
         return $loggedIn;
     }
 
@@ -207,6 +249,8 @@ class Utils {
         $this->header = str_replace('__TITLE__', $heading, $this->header);
         $this->header = str_replace('__ROOT__', $CFG->wwwroot, $this->header);
         $this->header = str_replace('__TIMEOUT__', $timeout, $this->header);
+
+        $_SESSION['Utils'] = serialize($this);
     }
 
     public function convertContent($code) {
This page took 0.034336 seconds and 5 git commands to generate.