[Jaws-commits] r13519 - in branches/0.8/html/include/Jaws: . Session

afz at jaws-project.com afz at jaws-project.com
Tue Aug 28 11:39:14 UTC 2012


Author: afz
Date: 2012-08-28 11:39:14 +0000 (Tue, 28 Aug 2012)
New Revision: 13519

Removed:
   branches/0.8/html/include/Jaws/Session/Cache.php
Modified:
   branches/0.8/html/include/Jaws/Session.php
   branches/0.8/html/include/Jaws/User.php
Log:
integrated Jaws_Session_Cache with Jaws_Session

Deleted: branches/0.8/html/include/Jaws/Session/Cache.php
===================================================================
--- branches/0.8/html/include/Jaws/Session/Cache.php	2012-08-28 09:25:26 UTC (rev 13518)
+++ branches/0.8/html/include/Jaws/Session/Cache.php	2012-08-28 11:39:14 UTC (rev 13519)
@@ -1,231 +0,0 @@
-<?php
-/**
- * Session data cache class
- *
- * @category   Session
- * @package    Jaws_Session_Cache_SQL
- * @author     Helgi Þormar Þorbjörnsson <dufuz at php.net>
- * @author     Ali Fazelzadeh <afz at php.net>
- * @copyright  2007-2012 Jaws Development Group
- * @license    http://www.gnu.org/copyleft/lesser.html
- */
-class Jaws_Session_Cache
-{
-    /**
-     * Synchronize current session with DB
-     *
-     * @param   integer $sid    Session ID
-     * @return  mixed   Session ID if success, false otherwise
-     */
-    function Synchronize($sid = '')
-    {
-        // agent
-        $xss = $GLOBALS['app']->loadClass('XSS', 'Jaws_XSS');
-        $agent = $xss->filter($_SERVER['HTTP_USER_AGENT']);
-        // ip
-        $ip = 0;
-        if (preg_match('/\b(?:\d{1,3}\.){3}\d{1,3}\b/', $_SERVER['REMOTE_ADDR'])) {
-            $ip = ip2long($_SERVER['REMOTE_ADDR']);
-            $ip = ($ip < 0)? ($ip + 0xffffffff + 1) : $ip;
-        }
-        // referrer
-        $referrer = @parse_url($_SERVER['HTTP_REFERER']);
-        if ($referrer && isset($referrer['host']) && ($referrer['host'] != $_SERVER['HTTP_HOST'])) {
-            $referrer = $referrer['host'];
-        } else {
-            $referrer = '';
-        }
-
-        if (!empty($sid)) {
-            // Now we sync with a previous session only if has changed
-            if ($GLOBALS['app']->Session->_HasChanged) {
-                $params = array();
-                $serialized = serialize($GLOBALS['app']->Session->_Attributes);
-                $params['sid']        = $sid;
-                $params['data']       = $serialized;
-                $params['user']       = $GLOBALS['app']->Session->GetAttribute('user');
-                $params['longevity']  = $GLOBALS['app']->Session->GetAttribute('longevity');
-                $params['referrer']   = md5($referrer);
-                $params['checksum']   = md5($params['user'] . $serialized);
-                $params['ip']         = $ip;
-                $params['agent']      = $agent;
-                $params['updatetime'] = time();
-
-                $sql = '
-                    UPDATE [[session]] SET
-                        [user]       = {user},
-                        [data]       = {data},
-                        [longevity]  = {longevity},
-                        [referrer]   = {referrer},
-                        [checksum]   = {checksum},
-                        [ip]         = {ip},
-                        [agent]      = {agent},
-                        [updatetime] = {updatetime}
-                    WHERE [sid] = {sid}';
-
-                $result = $GLOBALS['db']->query($sql, $params);
-                if (!Jaws_Error::IsError($result)) {
-                    $GLOBALS['log']->Log(JAWS_LOG_DEBUG, 'Session synchronized succesfully');
-                    return $sid;
-                }
-            } else {
-                $params = array();
-                $params['sid']        = $sid;
-                $params['updatetime'] = time();
-                $sql = '
-                    UPDATE [[session]] SET
-                        [updatetime] = {updatetime}
-                    WHERE [sid] = {sid}';
-                $result = $GLOBALS['db']->query($sql, $params);
-                if (!Jaws_Error::IsError($result)) {
-                    $GLOBALS['log']->Log(JAWS_LOG_DEBUG, 'Session synchronized succesfully(only modification time)');
-                    return $sid;
-                }
-            }
-        } else {
-            //A new session, we insert it to the DB
-            $updatetime = time();
-            $GLOBALS['app']->Session->SetAttribute('groups', array());
-            $serialized = serialize($GLOBALS['app']->Session->_Attributes);
-
-            $params = array();
-            $params['data']       = $serialized;
-            $params['longevity']  = $GLOBALS['app']->Session->GetAttribute('longevity');
-            $params['app_type']   = APP_TYPE;
-            $params['user']       = $GLOBALS['app']->Session->GetAttribute('user');
-            $params['referrer']   = md5($referrer);
-            $params['checksum']   = md5($params['user'] . $serialized);
-            $params['ip']         = $ip;
-            $params['agent']      = $agent;
-            $params['updatetime'] = $updatetime;
-            $params['createtime'] = $updatetime;
-
-            $sql = '
-                INSERT INTO [[session]]
-                    ([user], [type], [longevity], [data], [referrer], [checksum],
-                     [ip], [agent], [createtime], [updatetime])
-                VALUES
-                    ({user}, {app_type}, {longevity}, {data}, {referrer}, {checksum},
-                     {ip}, {agent}, {createtime}, {updatetime})';
-
-            $result = $GLOBALS['db']->query($sql, $params);
-            if (!Jaws_Error::IsError($result)) {
-                $result = $GLOBALS['db']->lastInsertID('session', 'sid');
-                if (!Jaws_Error::IsError($result) && !empty($result)) {
-                    return $result;
-                }
-            }
-        }
-
-        return false;
-    }
-
-    /**
-     * Delete a session
-     *
-     * @param   integer  $sid  Session ID
-     * @return  boolean Success/Failure
-     */
-    function Delete($sid)
-    {
-        $sql = 'DELETE FROM [[session]] WHERE [sid] = {sid}';
-        $result = $GLOBALS['db']->query($sql, array('sid' => $sid));
-        if (Jaws_Error::IsError($result)) {
-            return false;
-        }
-
-        return true;
-    }
-
-    /**
-     * Deletes all sessions of an user
-     *
-     * @param   string  $user   User's ID
-     * @return  boolean Success/Failure
-     */
-    function DeleteUserSessions($user)
-    {
-        //Get the sessions ID of the user
-        $sql = 'DELETE FROM [[session]] WHERE [user] = {user}';
-        $sessions = $GLOBALS['db']->queryAll($sql, array('user' => (string)$user));
-        if (Jaws_Error::IsError($sessions)) {
-            return false;
-        }
-
-        return true;
-    }
-
-    /**
-     * Delete expired sessions
-     */
-    function DeleteExpiredSessions()
-    {
-        $params = array();
-        $params['expired'] = time() - ($GLOBALS['app']->Registry->Get('/policy/session_idle_timeout') * 60);
-        $sql = "DELETE FROM [[session]] WHERE [updatetime] < ({expired} - [longevity])";
-        $result = $GLOBALS['db']->queryAll($sql, $params);
-        if (Jaws_Error::IsError($result)) {
-            return false;
-        }
-
-        return true;
-    }
-
-    /**
-     * Returns all users's sessions count
-     *
-     * @access  public
-     * @param   integer $user   User ID
-     * @return  mixed   Session count if exist, false otherwise
-     */
-    function GetUserSessions($user, $onlyOnline = false)
-    {
-        $params = array();
-        $params['user'] = (string)$user;
-        $params['expired'] = time() - ($GLOBALS['app']->Registry->Get('/policy/session_idle_timeout') * 60);
-        $sql = '
-            SELECT COUNT([user])
-            FROM [[session]]
-            WHERE [user] = {user}';
-
-        if ($onlyOnline) {
-            $sql.= ' AND [updatetime] >= {expired}';
-        }
-
-        $count = $GLOBALS['db']->queryOne($sql, $params);
-        if (Jaws_Error::isError($count)) {
-            return false;
-        }
-
-        return (int) $count;
-    }
-
-    /**
-     * Returns the session values
-     *
-     * @access  private
-     * @param   string  $sid  Session ID
-     * @return  mixed   Session values if exist, false otherwise
-     */
-    function GetSession($sid)
-    {
-        $params = array();
-        $params['sid'] = $sid;
-
-        $sql = '
-            SELECT
-                [sid], [user], [data], [referrer], [checksum], [ip], [agent],
-                [updatetime], [longevity]
-            FROM [[session]]
-            WHERE
-                [sid] = {sid}';
-
-        $result = $GLOBALS['db']->queryRow($sql, $params);
-        if (!Jaws_Error::isError($result) && isset($result['sid'])) {
-            return $result;
-        }
-
-        return false;
-    }
-
-}
\ No newline at end of file

Modified: branches/0.8/html/include/Jaws/Session.php
===================================================================
--- branches/0.8/html/include/Jaws/Session.php	2012-08-28 09:25:26 UTC (rev 13518)
+++ branches/0.8/html/include/Jaws/Session.php	2012-08-28 11:39:14 UTC (rev 13519)
@@ -110,13 +110,9 @@
         // Try to restore session...
         $this->_HasChanged = false;
 
-        // Load cache
-        include_once JAWS_PATH . 'include/Jaws/Session/Cache.php';
-        $this->_cache = new Jaws_Session_Cache;
-
         // Delete expired sessions
         if (mt_rand(1, 32) == mt_rand(1, 32)) {
-            $this->_cache->DeleteExpiredSessions();
+            $this->DeleteExpiredSessions();
         }
     }
 
@@ -165,7 +161,7 @@
                 if (!Jaws_Error::isError($result)) {
                     $existSessions = 0;
                     if (!empty($result['concurrent_logins'])) {
-                        $existSessions = $this->_cache->GetUserSessions($result['id'], true);
+                        $existSessions = $this->GetUserSessions($result['id'], true);
                     }
 
                     if (empty($existSessions) || $result['concurrent_logins'] > $existSessions)
@@ -230,7 +226,7 @@
         $GLOBALS['log']->Log(JAWS_LOG_DEBUG, 'Loading session');
         $this->_SessionID = '';
         @list($sid, $salt) = explode('-', $sid);
-        $session = $this->_cache->GetSession((int)$sid);
+        $session = $this->GetSession((int)$sid);
         if (is_array($session)) {
             $checksum = md5($session['user'] . $session['data']);
             $expTime = time() - 60 * (int) $GLOBALS['app']->Registry->Get('/policy/session_idle_timeout');
@@ -266,7 +262,7 @@
             // concurrent logins
             if ($session['updatetime'] < $expTime) {
                 $logins = $this->GetAttribute('concurrent_logins');
-                $existSessions = $this->_cache->GetUserSessions($this->GetAttribute('user'), true);
+                $existSessions = $this->GetUserSessions($this->GetAttribute('user'), true);
                 if (!empty($existSessions) && !empty($logins) && $existSessions >= $logins) {
                     $GLOBALS['app']->Session->Logout();
                     Jaws_Error::Fatal('Maximum number of concurrent logins reached');
@@ -538,11 +534,216 @@
      */
     function Synchronize()
     {
-        $GLOBALS['log']->Log(JAWS_LOG_DEBUG, 'Synchronizing session');
-        return $this->_cache->Synchronize($this->_SessionID);
+        // agent
+        $xss = $GLOBALS['app']->loadClass('XSS', 'Jaws_XSS');
+        $agent = $xss->filter($_SERVER['HTTP_USER_AGENT']);
+        // ip
+        $ip = 0;
+        if (preg_match('/\b(?:\d{1,3}\.){3}\d{1,3}\b/', $_SERVER['REMOTE_ADDR'])) {
+            $ip = ip2long($_SERVER['REMOTE_ADDR']);
+            $ip = ($ip < 0)? ($ip + 0xffffffff + 1) : $ip;
+        }
+        // referrer
+        $referrer = @parse_url($_SERVER['HTTP_REFERER']);
+        if ($referrer && isset($referrer['host']) && ($referrer['host'] != $_SERVER['HTTP_HOST'])) {
+            $referrer = $referrer['host'];
+        } else {
+            $referrer = '';
+        }
+
+        if (!empty($this->_SessionID)) {
+            // Now we sync with a previous session only if has changed
+            if ($GLOBALS['app']->Session->_HasChanged) {
+                $params = array();
+                $serialized = serialize($GLOBALS['app']->Session->_Attributes);
+                $params['sid']        = $this->_SessionID;
+                $params['data']       = $serialized;
+                $params['user']       = $GLOBALS['app']->Session->GetAttribute('user');
+                $params['longevity']  = $GLOBALS['app']->Session->GetAttribute('longevity');
+                $params['referrer']   = md5($referrer);
+                $params['checksum']   = md5($params['user'] . $serialized);
+                $params['ip']         = $ip;
+                $params['agent']      = $agent;
+                $params['updatetime'] = time();
+
+                $sql = '
+                    UPDATE [[session]] SET
+                        [user]       = {user},
+                        [data]       = {data},
+                        [longevity]  = {longevity},
+                        [referrer]   = {referrer},
+                        [checksum]   = {checksum},
+                        [ip]         = {ip},
+                        [agent]      = {agent},
+                        [updatetime] = {updatetime}
+                    WHERE [sid] = {sid}';
+
+                $result = $GLOBALS['db']->query($sql, $params);
+                if (!Jaws_Error::IsError($result)) {
+                    $GLOBALS['log']->Log(JAWS_LOG_DEBUG, 'Session synchronized succesfully');
+                    return $this->_SessionID;
+                }
+            } else {
+                $params = array();
+                $params['sid']        = $this->_SessionID;
+                $params['updatetime'] = time();
+                $sql = '
+                    UPDATE [[session]] SET
+                        [updatetime] = {updatetime}
+                    WHERE [sid] = {sid}';
+                $result = $GLOBALS['db']->query($sql, $params);
+                if (!Jaws_Error::IsError($result)) {
+                    $GLOBALS['log']->Log(JAWS_LOG_DEBUG, 'Session synchronized succesfully(only modification time)');
+                    return $this->_SessionID;
+                }
+            }
+        } else {
+            //A new session, we insert it to the DB
+            $updatetime = time();
+            $GLOBALS['app']->Session->SetAttribute('groups', array());
+            $serialized = serialize($GLOBALS['app']->Session->_Attributes);
+
+            $params = array();
+            $params['data']       = $serialized;
+            $params['longevity']  = $GLOBALS['app']->Session->GetAttribute('longevity');
+            $params['app_type']   = APP_TYPE;
+            $params['user']       = $GLOBALS['app']->Session->GetAttribute('user');
+            $params['referrer']   = md5($referrer);
+            $params['checksum']   = md5($params['user'] . $serialized);
+            $params['ip']         = $ip;
+            $params['agent']      = $agent;
+            $params['updatetime'] = $updatetime;
+            $params['createtime'] = $updatetime;
+
+            $sql = '
+                INSERT INTO [[session]]
+                    ([user], [type], [longevity], [data], [referrer], [checksum],
+                     [ip], [agent], [createtime], [updatetime])
+                VALUES
+                    ({user}, {app_type}, {longevity}, {data}, {referrer}, {checksum},
+                     {ip}, {agent}, {createtime}, {updatetime})';
+
+            $result = $GLOBALS['db']->query($sql, $params);
+            if (!Jaws_Error::IsError($result)) {
+                $result = $GLOBALS['db']->lastInsertID('session', 'sid');
+                if (!Jaws_Error::IsError($result) && !empty($result)) {
+                    return $result;
+                }
+            }
+        }
+
+        return false;
     }
 
     /**
+     * Delete a session
+     *
+     * @param   integer  $sid  Session ID
+     * @return  boolean Success/Failure
+     */
+    function Delete($sid)
+    {
+        $sql = 'DELETE FROM [[session]] WHERE [sid] = {sid}';
+        $result = $GLOBALS['db']->query($sql, array('sid' => $sid));
+        if (Jaws_Error::IsError($result)) {
+            return false;
+        }
+
+        return true;
+    }
+
+    /**
+     * Deletes all sessions of an user
+     *
+     * @param   string  $user   User's ID
+     * @return  boolean Success/Failure
+     */
+    function DeleteUserSessions($user)
+    {
+        //Get the sessions ID of the user
+        $sql = 'DELETE FROM [[session]] WHERE [user] = {user}';
+        $result = $GLOBALS['db']->query($sql, array('user' => (string)$user));
+        if (Jaws_Error::IsError($result)) {
+            return false;
+        }
+
+        return true;
+    }
+
+    /**
+     * Delete expired sessions
+     */
+    function DeleteExpiredSessions()
+    {
+        $params = array();
+        $params['expired'] = time() - ($GLOBALS['app']->Registry->Get('/policy/session_idle_timeout') * 60);
+        $sql = "DELETE FROM [[session]] WHERE [updatetime] < ({expired} - [longevity])";
+        $result = $GLOBALS['db']->query($sql, $params);
+        if (Jaws_Error::IsError($result)) {
+            return false;
+        }
+
+        return true;
+    }
+
+    /**
+     * Returns all users's sessions count
+     *
+     * @access  public
+     * @param   integer $user   User ID
+     * @return  mixed   Session count if exist, false otherwise
+     */
+    function GetUserSessions($user, $onlyOnline = false)
+    {
+        $params = array();
+        $params['user'] = (string)$user;
+        $params['expired'] = time() - ($GLOBALS['app']->Registry->Get('/policy/session_idle_timeout') * 60);
+        $sql = '
+            SELECT COUNT([user])
+            FROM [[session]]
+            WHERE [user] = {user}';
+
+        if ($onlyOnline) {
+            $sql.= ' AND [updatetime] >= {expired}';
+        }
+
+        $count = $GLOBALS['db']->queryOne($sql, $params);
+        if (Jaws_Error::isError($count)) {
+            return false;
+        }
+
+        return (int) $count;
+    }
+
+    /**
+     * Returns the session values
+     *
+     * @access  private
+     * @param   string  $sid  Session ID
+     * @return  mixed   Session values if exist, false otherwise
+     */
+    function GetSession($sid)
+    {
+        $params = array();
+        $params['sid'] = $sid;
+
+        $sql = '
+            SELECT
+                [sid], [user], [data], [referrer], [checksum], [ip], [agent],
+                [updatetime], [longevity]
+            FROM [[session]]
+            WHERE
+                [sid] = {sid}';
+
+        $result = $GLOBALS['db']->queryRow($sql, $params);
+        if (!Jaws_Error::isError($result) && isset($result['sid'])) {
+            return $result;
+        }
+
+        return false;
+    }
+
+    /**
      * Push a simple response (no CSS and special data)
      *
      * @access  public

Modified: branches/0.8/html/include/Jaws/User.php
===================================================================
--- branches/0.8/html/include/Jaws/User.php	2012-08-28 09:25:26 UTC (rev 13518)
+++ branches/0.8/html/include/Jaws/User.php	2012-08-28 11:39:14 UTC (rev 13519)
@@ -1044,7 +1044,7 @@
         $GLOBALS['app']->ACL->DeleteUserACL($user['username']);
 
         if (isset($GLOBALS['app']->Session)) {
-            $res = $GLOBALS['app']->Session->_cache->DeleteUserSessions($id);
+            $res = $GLOBALS['app']->Session->DeleteUserSessions($id);
             if (!$res) {
                 return false;
             }



More information about the Jaws-commits mailing list