Index: autoload/ezp_kernel.php
===================================================================
--- autoload/ezp_kernel.php	(revision 22332)
+++ autoload/ezp_kernel.php	(working copy)
@@ -358,6 +358,7 @@
       'eZSelectionType'                    => 'kernel/classes/datatypes/ezselection/ezselectiontype.php',
       'eZSendmailTransport'                => 'lib/ezutils/classes/ezsendmailtransport.php',
       'eZSerializedObjectNameList'         => 'kernel/classes/ezserializedobjectnamelist.php',
+      'eZSession'                          => 'lib/ezutils/classes/ezsession.php',
       'eZSetupFunctionCollection'          => 'kernel/setup/ezsetupfunctioncollection.php',
       'eZSetupSummary'                     => 'kernel/setup/ezsetup_summary.php',
       'eZShippingManager'                  => 'kernel/classes/ezshippingmanager.php',
Index: cronjobs/workflow.php
===================================================================
--- cronjobs/workflow.php	(revision 22332)
+++ cronjobs/workflow.php	(working copy)
@@ -43,7 +43,7 @@
 //include_once( "kernel/classes/datatypes/ezuser/ezuser.php" );
 //include_once( "lib/ezutils/classes/ezoperationmemento.php" );
 //include_once( "lib/ezutils/classes/ezoperationhandler.php" );
-require_once( "lib/ezutils/classes/ezsession.php" );
+//require_once( "lib/ezutils/classes/ezsession.php" );
 
 require_once( "lib/ezutils/classes/ezdebug.php" );
 //include_once( "lib/ezutils/classes/ezini.php" );
Index: index.php
===================================================================
--- index.php	(revision 22332)
+++ index.php	(working copy)
@@ -363,8 +363,7 @@
 */
 
 // include ezsession override implementation
-//Needs to be added no class definition
-require_once( "lib/ezutils/classes/ezsession.php" );
+//require_once( "lib/ezutils/classes/ezsession.php" );
 
 
 // Check for extension
@@ -415,16 +414,16 @@
         eZBasket::cleanupExpired( $time );
     }
 
-    function eZSessionBasketEmpty( $db )
+    function eZSessionBasketCleanup( $db )
     {
         ////include_once( 'kernel/classes/ezbasket.php' );
         eZBasket::cleanup();
     }
 
     // Fill in hooks
-    $GLOBALS['eZSessionFunctions']['destroy_pre'][] = 'eZSessionBasketDestroy';
-    $GLOBALS['eZSessionFunctions']['gc_pre'][] = 'eZSessionBasketGarbageCollector';
-    $GLOBALS['eZSessionFunctions']['empty_pre'][] = 'eZSessionBasketEmpty';
+    eZSession::addCallback( 'destroy_pre', 'eZSessionBasketDestroy');
+    eZSession::addCallback( 'gc_pre', 'eZSessionBasketGarbageCollector');
+    eZSession::addCallback( 'cleanup_pre', 'eZSessionBasketCleanup');
 }
 
 // Initialize module loading
@@ -449,7 +448,7 @@
     if ( $sessionRequired and
          $db->isConnected() )
     {
-        eZSessionStart();
+        eZSession::start();
     }
 
     if ( !$db->isConnected() )
Index: index_treemenu.php
===================================================================
--- index_treemenu.php	(revision 22332)
+++ index_treemenu.php	(working copy)
@@ -47,7 +47,7 @@
 //require_once( 'lib/ezutils/classes/ezdebug.php' );
 ////include_once( 'lib/ezutils/classes/ezini.php' );
 ////include_once( 'lib/ezutils/classes/ezuri.php' );
-require_once( 'lib/ezutils/classes/ezsession.php' );
+//require_once( 'lib/ezutils/classes/ezsession.php' );
 ////include_once( 'lib/ezutils/classes/ezextension.php' );
 require_once( 'kernel/common/ezincludefunctions.php' );
 ////include_once( 'lib/ezutils/classes/ezmodule.php' );
@@ -118,7 +118,7 @@
 $db = eZDB::instance();
 if ( $db->isConnected() )
 {
-    eZSessionStart();
+    eZSession::start();
 }
 else
 {
Index: kernel/class/edit.php
===================================================================
--- kernel/class/edit.php	(revision 22332)
+++ kernel/class/edit.php	(working copy)
@@ -183,7 +183,7 @@
 
 if ( $http->hasPostVariable( 'DiscardButton' ) )
 {
-    eZSessionDestroy( $http->sessionVariable( 'CanStoreTicket' ) );
+    eZSession::destroy( $http->sessionVariable( 'CanStoreTicket' ) );
     $http->removeSessionVariable( 'CanStoreTicket' );
     $class->setVersion( eZContentClass::VERSION_STATUS_TEMPORARY );
     $class->remove( true, eZContentClass::VERSION_STATUS_TEMPORARY );
@@ -644,12 +644,12 @@
     }
     else
     {
-        $firstStoreAttempt = eZSessionRead( $http->sessionVariable( 'CanStoreTicket' ) );
+        $firstStoreAttempt = eZSession::read( $http->sessionVariable( 'CanStoreTicket' ) );
         if ( !$firstStoreAttempt )
         {
             return $Module->redirectToView( 'view', array( $ClassID ), array( 'Language' => $EditLanguage ) );
         }
-        eZSessionDestroy( $http->sessionVariable( 'CanStoreTicket' ) );
+        eZSession::destroy( $http->sessionVariable( 'CanStoreTicket' ) );
 
         // Class cleanup, update existing class objects according to new changes
         //include_once( 'kernel/classes/ezcontentobject.php' );
@@ -769,7 +769,7 @@
 if ( !$http->hasSessionVariable( 'CanStoreTicket' ) )
 {
     $http->setSessionVariable( 'CanStoreTicket', md5( (string)rand() ) );
-    eZSessionWrite( $http->sessionVariable( 'CanStoreTicket' ), 1 );
+    eZSession::write( $http->sessionVariable( 'CanStoreTicket' ), 1 );
 }
 
 // Fetch updated attributes
Index: kernel/classes/datatypes/ezuser/ezuser.php
===================================================================
--- kernel/classes/datatypes/ezuser/ezuser.php	(revision 22332)
+++ kernel/classes/datatypes/ezuser/ezuser.php	(working copy)
@@ -957,9 +957,9 @@
         // instance() when there is no ID passed to the function.
         $GLOBALS["eZUserGlobalInstance_"] = $user;
         $http->setSessionVariable( 'eZUserLoggedInID', $userID );
-        eZSessionRegenerate();
+        eZSession::regenerate();
         $user->cleanup();
-        eZSessionSetUserID( $userID );
+        eZSession::setUserID( $userID );
     }
 
     /*!
@@ -1020,7 +1020,7 @@
         $contentObjectID = $http->sessionVariable( "eZUserLoggedInID" );
         $newUserID = self::anonymousId();
         $http->setSessionVariable( 'eZUserLoggedInID', $newUserID );
-        eZSessionSetUserID( $newUserID );
+        eZSession::setUserID( $newUserID );
         // Clear current basket if necessary
         $db = eZDB::instance();
         $db->begin();
@@ -1058,7 +1058,7 @@
             {
                 $id = self::anonymousId();
                 $http->setSessionVariable( 'eZUserLoggedInID', $id );
-                eZSessionSetUserID( $id );
+                eZSession::setUserID( $id );
             }
         }
 
@@ -1163,7 +1163,7 @@
                     $http->setSessionVariable( 'eZUserInfoCache', $userInfo );
                     $http->setSessionVariable( 'eZUserInfoCache_Timestamp', time() );
                     $http->setSessionVariable( 'eZUserLoggedInID', $id );
-                    eZSessionSetUserID( $currentUser->attribute( 'contentobject_id' ) );
+                    eZSession::setUserID( $currentUser->attribute( 'contentobject_id' ) );
 
                     eZUser::updateLastVisit( $currentUser->attribute( 'contentobject_id' ) );
                     eZUser::setCurrentlyLoggedInUser( $currentUser, $currentUser->attribute( 'contentobject_id' ) );
Index: kernel/classes/ezscript.php
===================================================================
--- kernel/classes/ezscript.php	(revision 22332)
+++ kernel/classes/ezscript.php	(working copy)
@@ -280,12 +280,12 @@
         if ( $this->UseSession )
         {
             // include ezsession override implementation
-            require_once( "lib/ezutils/classes/ezsession.php" );
+            //require_once( "lib/ezutils/classes/ezsession.php" );
             //include_once( 'lib/ezdb/classes/ezdb.php' );
             $db = eZDB::instance();
             if ( $db->isConnected() )
             {
-                eZSessionStart();
+                eZSession::start();
             }
             else
             {
@@ -363,7 +363,7 @@
             {
                 //include_once( 'kernel/classes/datatypes/ezuser/ezuser.php' );
                 eZUser::logoutCurrent();
-                eZSessionRemove();
+                eZSession::remove();
             }
         }
 
Index: kernel/setup/session.php
===================================================================
--- kernel/setup/session.php	(revision 22332)
+++ kernel/setup/session.php	(working copy)
@@ -27,7 +27,7 @@
 //
 require_once( 'kernel/common/template.php' );
 //include_once( 'lib/ezutils/classes/ezhttptool.php' );
-require_once( 'lib/ezutils/classes/ezsession.php' );
+//require_once( 'lib/ezutils/classes/ezsession.php' );
 
 $tpl = templateInit();
 $sessionsRemoved = false;
@@ -71,12 +71,12 @@
 }
 else if ( $module->isCurrentAction( 'RemoveAllSessions' ) )
 {
-    eZSessionEmpty();
+    eZSession::cleanup();
     $sessionsRemoved = true;
 }
 else if ( $module->isCurrentAction( 'RemoveTimedOutSessions' ) )
 {
-    eZSessionGarbageCollector();
+    eZSession::garbageCollector();
     $sessionsRemoved = true;
 }
 else if ( $module->isCurrentAction( 'RemoveSelectedSessions' ) )
@@ -88,7 +88,7 @@
             $sessionKeyArray = $http->postVariable( 'SessionKeyArray' );
             foreach ( $sessionKeyArray as $sessionKeyItem )
             {
-                eZSessionDestroy( $sessionKeyItem );
+                eZSession::destroy( $sessionKeyItem );
             }
         }
     }
@@ -105,7 +105,7 @@
                 $rows = $db->arrayQuery( "SELECT session_key FROM ezsession WHERE user_id IN ( " . $userIDArrayString . " )" );
                 foreach ( $rows as $row )
                 {
-                    eZSessionDestroy( $row['session_key'] );
+                    eZSession::destroy( $row['session_key'] );
                 }
             }
         }
@@ -347,7 +347,7 @@
 $param['user_id'] = $userID;
 if ( isset( $viewParameters['sortby'] ) )
     $param['sortby'] = $viewParameters['sortby'];
-$sessionsActive = eZSessionCountActive( $param );
+$sessionsActive = eZSession::countActive( $param );
 $sessionsCount = eZFetchActiveSessionCount( $param );
 $sessionsList = eZFetchActiveSessions( $param );
 
Index: kernel/user/login.php
===================================================================
--- kernel/user/login.php	(revision 22332)
+++ kernel/user/login.php	(working copy)
@@ -293,8 +293,8 @@
             if ( $rememberMeTimeout )
             {
                 $GLOBALS['RememberMeTimeout'] = $rememberMeTimeout;
-                eZSessionStop();
-                eZSessionStart();
+                eZSession::stop();
+                eZSession::start();
                 unset( $GLOBALS['RememberMeTimeout'] );
             }
 
Index: lib/ezutils/classes/ezhttptool.php
===================================================================
--- lib/ezutils/classes/ezhttptool.php	(revision 22332)
+++ lib/ezutils/classes/ezhttptool.php	(working copy)
@@ -41,7 +41,7 @@
 */
 
 require_once( "lib/ezutils/classes/ezdebug.php" );
-require_once( "lib/ezutils/classes/ezsession.php" );
+//require_once( "lib/ezutils/classes/ezsession.php" );
 //include_once( "lib/ezutils/classes/ezsys.php" );
 
 class eZHTTPTool
@@ -204,7 +204,7 @@
         {
             $GLOBALS["eZHTTPToolInstance"] = new eZHTTPTool();
             $GLOBALS["eZHTTPToolInstance"]->createPostVarsFromImageButtons();
-            eZSessionStart();
+            eZSession::start();
         }
 
         return $GLOBALS["eZHTTPToolInstance"];
Index: lib/ezutils/classes/ezsession.php
===================================================================
--- lib/ezutils/classes/ezsession.php	(revision 22332)
+++ lib/ezutils/classes/ezsession.php	(working copy)
@@ -34,20 +34,18 @@
   The session system has a hook system which allows external code to perform
   extra tasks before and after a certain action is executed. For instance to
   cleanup a table when sessions are removed.
-  To create a hook function the global variable \c eZSessionFunctions must be
-  filled in. This contains an associative array with hook points, e.g.
-  \c destroy_pre which is checked by the session system.
-  Each hook point must contain an array with function names to execute, if the
-  hook point does not exist the session handler will not handle the hooks.
+  This can be used by adding a callback with the eZSession::addCallback function,
+  first param is type and second is callback (called with call_user_func_array)
 
   \code
   function cleanupStuff( $db, $key, $escKey )
   {
       // Do cleanup here
   }
-  if ( !isset( $GLOBALS['eZSessionFunctions']['destroy_pre'] ) )
-      $GLOBALS['eZSessionFunctions']['destroy_pre'] = array();
-  $GLOBALS['eZSessionFunctions']['destroy_pre'][] = 'cleanupstuff';
+
+  eZSession::addCallback( 'destroy_pre', 'cleanupstuff');
+  // Or if it was a class function:
+  // eZSession::addCallback( 'destroy_pre', array('myClass', 'myCleanupStuff') );
   \endcode
 
   When new data is inserted to the database it will call the \c update_pre and
@@ -67,8 +65,8 @@
   function gcollect( $db, $expiredTime )
 
   When all sessionss are removed from the database it will call the
-  \c empty_pre and \c empty_post hooks. The signature of the function is
-  function empty( $db )
+  \c cleanup_pre and \c cleanup_post hooks. The signature of the function is
+  function cleanup( $db )
 
   \param $db The database object used by the session manager.
   \param $key The session key which are being worked on, see also \a $escapedKey
@@ -81,386 +79,355 @@
                       lower expiration times will be removed.
 */
 
-function eZSessionOpen( )
-{
-    // do nothing eZDB will open connection when needed.
-}
 
-function eZSessionClose( )
+class eZSession
 {
-    // eZDB will handle closing the database
-}
+    // name of session handler, change if you override class with autoload
+    const HANDLER_NAME = 'db';
 
-function eZSessionRead( $key )
-{
-    //include_once( 'lib/ezdb/classes/ezdb.php' );
-    $db = eZDB::instance();
+    // user id
+    static private $userID = 0;
 
-    $key = $db->escapeString( $key );
+    // session started flag
+    static private $hasStarted = false;
 
-    $sessionRes = $db->arrayQuery( "SELECT data, user_id, expiration_time FROM ezsession WHERE session_key='$key'" );
+    // list of actions to call on misc events (insert / update / destroy)
+    static private $callbackFunctions = array(); 
 
-    if ( $sessionRes !== false and count( $sessionRes ) == 1 )
+    private function eZSession()
     {
-        $ini = eZINI::instance();
-
-        $sessionUpdatesTime = $sessionRes[0]['expiration_time'] - $ini->variable( 'Session', 'SessionTimeout' );
-        $sessionIdle = time() - $sessionUpdatesTime;
-
-        $GLOBALS['eZSessionUserID'] = $sessionRes[0]['user_id'];
-        $GLOBALS['eZSessionIdleTime'] = $sessionIdle;
-
-        /*
-         * The line below is needed to correctly load list of content classes saved
-         * in the CanInstantiateClassList session variable.
-         * Without this we get incomplete classes loaded (__PHP_Incomplete_Class).
-         */
-        require_once( 'kernel/classes/ezcontentclass.php' );
+        
+    }
 
-        return $sessionRes[0]['data'];
-    }
-    else
+    static public function open()
     {
-        return false;
+        // do nothing eZDB will open connection when needed.
     }
-}
-
-/*!
-  Will write the session information to database.
-*/
-function eZSessionWrite( $key, $value )
-{
-//    //include_once( 'lib/ezdb/classes/ezdb.php' );
 
-    if ( isset( $GLOBALS["eZRequestError"] ) && $GLOBALS["eZRequestError"] )
+    static public function close()
     {
-        return;
+        // eZDB will handle closing the database
     }
 
-    $db = eZDB::instance();
-    $ini = eZINI::instance();
-    $expirationTime = time() + $ini->variable( 'Session', 'SessionTimeout' );
-
-    if ( $db->bindingType() != eZDBInterface::BINDING_NO )
+    static public function read( $key )
     {
-        $value = $db->bindVariable( $value, array( 'name' => 'data' ) );
-    }
-    else
-    {
-        $value = '\'' . $db->escapeString( $value ) . '\'';
+        //include_once( 'lib/ezdb/classes/ezdb.php' );
+        $db = eZDB::instance();
+        $key = $db->escapeString( $key );
 
-    }
-//    $value = $db->escapeString( $value );
-    $escKey = $db->escapeString( $key );
-    // check if session already exists
+        $sessionRes = $db->arrayQuery( "SELECT data, user_id, expiration_time FROM ezsession WHERE session_key='$key'" );
+
+        if ( $sessionRes !== false and count( $sessionRes ) == 1 )
+        {
+            $ini = eZINI::instance();
 
-    $userID = 0;
-    if ( isset( $GLOBALS['eZSessionUserID'] ) )
-        $userID = $GLOBALS['eZSessionUserID'];
-    $userID = $db->escapeString( $userID );
+            $sessionUpdatesTime = $sessionRes[0]['expiration_time'] - $ini->variable( 'Session', 'SessionTimeout' );
+            $sessionIdle = time() - $sessionUpdatesTime;
 
-    $sessionRes = $db->arrayQuery( "SELECT session_key FROM ezsession WHERE session_key='$escKey'" );
+            self::$userID = $sessionRes[0]['user_id'];
+            $GLOBALS['eZSessionIdleTime'] = $sessionIdle;
 
-    if ( count( $sessionRes ) == 1 )
-    {
-        if ( isset( $GLOBALS['eZSessionFunctions']['update_pre'] ) )
+            return $sessionRes[0]['data'];
+        }
+        else
         {
-            foreach ( $GLOBALS['eZSessionFunctions']['update_pre'] as $func )
-            {
-                $func( $db, $key, $escKey, $expirationTime, $userID, $value );
-            }
+            return false;
         }
+    }
 
-        $updateQuery = "UPDATE ezsession
-                    SET expiration_time='$expirationTime', data=$value, user_id='$userID'
-                    WHERE session_key='$escKey'";
+    /*!
+      Will write the session information to database.
+    */
+    static public function write( $key, $value )
+    {
+    //    //include_once( 'lib/ezdb/classes/ezdb.php' );
+        if ( isset( $GLOBALS["eZRequestError"] ) && $GLOBALS["eZRequestError"] )
+        {
+            return;
+        }
 
-        $ret = $db->query( $updateQuery );
+        $db = eZDB::instance();
+        $ini = eZINI::instance();
+        $expirationTime = time() + $ini->variable( 'Session', 'SessionTimeout' );
 
-        if ( isset( $GLOBALS['eZSessionFunctions']['update_post'] ) )
+        if ( $db->bindingType() != eZDBInterface::BINDING_NO )
         {
-            foreach ( $GLOBALS['eZSessionFunctions']['update_post'] as $func )
-            {
-                $func( $db, $key, $escKey, $expirationTime, $userID, $value );
-            }
+            $value = $db->bindVariable( $value, array( 'name' => 'data' ) );
         }
-    }
-    else
-    {
-        if ( isset( $GLOBALS['eZSessionFunctions']['insert_pre'] ) )
+        else
         {
-            foreach ( $GLOBALS['eZSessionFunctions']['insert_pre'] as $func )
-            {
-                $func( $db, $key, $escKey, $expirationTime, $userID, $value );
-            }
+            $value = '\'' . $db->escapeString( $value ) . '\'';
         }
+    //    $value = $db->escapeString( $value );
+        $escKey = $db->escapeString( $key );
+        // check if session already exists
 
-        $insertQuery = "INSERT INTO ezsession
-                    ( session_key, expiration_time, data, user_id )
-                    VALUES ( '$escKey', '$expirationTime', $value, '$userID' )";
+        $userID = $db->escapeString( self::$userID );
 
-        $ret = $db->query( $insertQuery );
+        $sessionRes = $db->arrayQuery( "SELECT session_key FROM ezsession WHERE session_key='$escKey'" );
 
-        if ( isset( $GLOBALS['eZSessionFunctions']['insert_post'] ) )
+        if ( count( $sessionRes ) == 1 )
         {
-            foreach ( $GLOBALS['eZSessionFunctions']['insert_post'] as $func )
-            {
-                $func( $db, $key, $escKey, $expirationTime, $userID, $value );
-            }
+            self::triggerCallback( 'update_pre', array( $db, $key, $escKey, $expirationTime, $userID, $value ) );
+
+            $updateQuery = "UPDATE ezsession
+                        SET expiration_time='$expirationTime', data=$value, user_id='$userID'
+                        WHERE session_key='$escKey'";
+            $ret = $db->query( $updateQuery );
+
+            self::triggerCallback( 'update_post', array( $db, $key, $escKey, $expirationTime, $userID, $value ) );
         }
-    }
-}
+        else
+        {
+            self::triggerCallback( 'insert_pre', array( $db, $key, $escKey, $expirationTime, $userID, $value ) );
 
-/*!
-  Will remove a session from the database.
-*/
-function eZSessionDestroy( $key )
-{
-    //include_once( 'lib/ezdb/classes/ezdb.php' );
-    $db = eZDB::instance();
+            $insertQuery = "INSERT INTO ezsession
+                        ( session_key, expiration_time, data, user_id )
+                        VALUES ( '$escKey', '$expirationTime', $value, '$userID' )";
+            $ret = $db->query( $insertQuery );
 
-    $escKey = $db->escapeString( $key );
-    if ( isset( $GLOBALS['eZSessionFunctions']['destroy_pre'] ) )
-    {
-        foreach ( $GLOBALS['eZSessionFunctions']['destroy_pre'] as $func )
-        {
-            $func( $db, $key, $escKey );
+            self::triggerCallback( 'insert_post', array( $db, $key, $escKey, $expirationTime, $userID, $value ) );
         }
     }
 
-    $query = "DELETE FROM ezsession WHERE session_key='$escKey'";
-    $db->query( $query );
+    /*!
+      Will remove a session from the database.
+    */
+    static public function destroy( $key )
+    {
+        //include_once( 'lib/ezdb/classes/ezdb.php' );
+        $db = eZDB::instance();
+        $escKey = $db->escapeString( $key );
 
-    if ( isset( $GLOBALS['eZSessionFunctions']['destroy_post'] ) )
+        self::triggerCallback( 'destroy_pre', array( $db, $key, $escKey ) );
+
+        $query = "DELETE FROM ezsession WHERE session_key='$escKey'";
+        $db->query( $query );
+
+        self::triggerCallback( 'destroy_post', array( $db, $key, $escKey ) );
+    }
+
+    /*!
+      Handles session cleanup. Will delete timed out sessions from the database.
+    */
+    static public function garbageCollector()
     {
-        foreach ( $GLOBALS['eZSessionFunctions']['destroy_post'] as $func )
-        {
-            $func( $db, $key, $escKey );
-        }
+        //include_once( 'lib/ezdb/classes/ezdb.php' );
+        $db = eZDB::instance();
+        $time = time();
+
+        self::triggerCallback( 'gc_pre', array( $db, $time ) );
+
+        $query = "DELETE FROM ezsession WHERE expiration_time < " . $time;
+        $db->query( $query );
+
+        self::triggerCallback( 'gc_post', array( $db, $time ) );
     }
-}
 
-/*!
-  Handles session cleanup. Will delete timed out sessions from the database.
-*/
-function eZSessionGarbageCollector()
-{
-    //include_once( 'lib/ezdb/classes/ezdb.php' );
-    $db = eZDB::instance();
-    $time = time();
+    /*!
+      Removes all entries from session.
+      BC: eZSessionEmpty
+    */
+    static public function cleanup()
+    {
+        //include_once( 'lib/ezdb/classes/ezdb.php' );
+        $db = eZDB::instance();
 
-    if ( isset( $GLOBALS['eZSessionFunctions']['gc_pre'] ) )
-    {
-        foreach ( $GLOBALS['eZSessionFunctions']['gc_pre'] as $func )
-        {
-            $func( $db, $time );
-        }
+        self::triggerCallback( 'cleanup_pre', array( $db ) );
+
+        $query = "TRUNCATE TABLE ezsession";
+        $db->query( $query );
+
+        self::triggerCallback( 'cleanup_post', array( $db ) );
     }
 
-    $query = "DELETE FROM ezsession WHERE expiration_time < " . $time;
+    /*!
+      Counts the number of active session and returns it.
+    */
+    static public function countActive()
+    {
+        //include_once( 'lib/ezdb/classes/ezdb.php' );
+        $db = eZDB::instance();
+        $query = "SELECT count( * ) AS count FROM ezsession";
 
-    $db->query( $query );
+        $rows = $db->arrayQuery( $query );
+        return $rows[0]['count'];
+    }
 
-    if ( isset( $GLOBALS['eZSessionFunctions']['gc_post'] ) )
+    /*!
+     Register the needed session functions.
+     Call this only once.
+     BC: eZRegisterSessionFunctions
+    */
+    static private function registerFunctions()
     {
-        foreach ( $GLOBALS['eZSessionFunctions']['gc_post'] as $func )
+        session_module_name( 'user' );
+        $ini = eZINI::instance();
+        if ( $ini->variable( 'Session', 'SessionNameHandler' ) == 'custom' )
         {
-            $func( $db, $time );
+            $sessionName = $ini->variable( 'Session', 'SessionNamePrefix' );
+            if ( $ini->variable( 'Session', 'SessionNamePerSiteAccess' ) == 'enabled' )
+            {
+                $access = $GLOBALS['eZCurrentAccess'];
+                $sessionName .=  $access['name'];
+            }
+            session_name( $sessionName );
         }
+        session_set_save_handler(
+            array('eZSession', 'open'),
+            array('eZSession', 'close'),
+            array('eZSession', 'read'),
+            array('eZSession', 'write'),
+            array('eZSession', 'destroy'),
+            array('eZSession', 'garbageCollector')
+            );
     }
-}
 
-/*!
-  Removes all entries from session.
-*/
-function eZSessionEmpty()
-{
-    //include_once( 'lib/ezdb/classes/ezdb.php' );
-    $db = eZDB::instance();
+    /*!
+     Makes sure that the session is started properly.
+     Multiple calls will just be ignored.
+    */
+    static public function start()
+    {
+        // Check if we are allowed to use sessions
+        if ( isset( $GLOBALS['eZSiteBasics'] ) &&
+             isset( $GLOBALS['eZSiteBasics']['session-required'] ) &&
+             !$GLOBALS['eZSiteBasics']['session-required'] )
+        {
+            return false;
+        }
+        if ( self::$hasStarted )
+        {
+             return false;
+        }
+        //include_once( 'lib/ezdb/classes/ezdb.php' );
+        $db = eZDB::instance();
+        if ( !$db->isConnected() )
+        {
+            return false;
+        }
+        self::registerFunctions();
+        $ini = eZINI::instance();
+        $cookieTimeout = isset( $GLOBALS['RememberMeTimeout'] ) ? $GLOBALS['RememberMeTimeout'] : $ini->variable( 'Session', 'CookieTimeout' );
 
-    if ( isset( $GLOBALS['eZSessionFunctions']['empty_pre'] ) )
-    {
-        foreach ( $GLOBALS['eZSessionFunctions']['empty_pre'] as $func )
+        if ( is_numeric( $cookieTimeout ) )
         {
-            $func( $db );
+            session_set_cookie_params( (int)$cookieTimeout );
         }
+        session_start();
+        return self::$hasStarted = true;
     }
 
-    $query = "TRUNCATE TABLE ezsession";
-
-    $db->query( $query );
-
-    if ( isset( $GLOBALS['eZSessionFunctions']['empty_post'] ) )
+    /*!
+     Makes sure session data is stored in the session and stops the session.
+    */
+    static public function stop()
     {
-        foreach ( $GLOBALS['eZSessionFunctions']['empty_post'] as $func )
+        if ( !self::$hasStarted )
         {
-            $func( $db );
+             return false;
         }
+        //include_once( 'lib/ezdb/classes/ezdb.php' );
+        $db = eZDB::instance();
+        if ( !$db->isConnected() )
+        {
+            return false;
+        }
+        session_write_close();
+        self::$hasStarted = false;
+        return true;
     }
-}
-
-/*!
-  Counts the number of active session and returns it.
-*/
-function eZSessionCountActive()
-{
-    //include_once( 'lib/ezdb/classes/ezdb.php' );
-    $db = eZDB::instance();
-    $query = "SELECT count( * ) AS count FROM ezsession";
 
-    $rows = $db->arrayQuery( $query );
-    return $rows[0]['count'];
-}
-
-/*!
- Register the needed session functions.
- Call this only once.
-*/
-function eZRegisterSessionFunctions()
-{
-    session_module_name( 'user' );
-    $ini = eZINI::instance();
-    if ( $ini->variable( 'Session', 'SessionNameHandler' ) == 'custom' )
+    /*!
+     Will make sure the user gets a new session ID while keepin the session data.
+     This is useful to call on logins, to avoid sessions theft from users.
+     \note This requires PHP 4.3.2 and higher which has the session_regenerate_id
+     \return \c true if succesful
+    */
+    static public function regenerate()
     {
-        $sessionName = $ini->variable( 'Session', 'SessionNamePrefix' );
-        if ( $ini->variable( 'Session', 'SessionNamePerSiteAccess' ) == 'enabled' )
+        if ( !self::$hasStarted )
         {
-            $access = $GLOBALS['eZCurrentAccess'];
-            $sessionName .=  $access['name'];
+             return false;
         }
-        session_name( $sessionName );
+        if ( !function_exists( 'session_regenerate_id' ) )
+        {
+            return false;
+        }
+        // This doesn't seem to work as expected
+    //     session_regenerate_id();
+        return true;
     }
-    session_set_save_handler(
-        'ezsessionopen',
-        'ezsessionclose',
-        'ezsessionread',
-        'ezsessionwrite',
-        'ezsessiondestroy',
-        'ezsessiongarbagecollector' );
-}
 
-/*!
- Makes sure that the session is started properly.
- Multiple calls will just be ignored.
-*/
-function eZSessionStart()
-{
-    // Check if we are allowed to use sessions
-    if ( isset( $GLOBALS['eZSiteBasics'] ) &&
-         isset( $GLOBALS['eZSiteBasics']['session-required'] ) &&
-         !$GLOBALS['eZSiteBasics']['session-required'] )
+    /*!
+     Removes the current session and resets session variables.
+    */
+    static public function remove()
     {
-        return false;
-    }
-    if ( isset( $GLOBALS['eZSessionIsStarted'] ) &&
-         $GLOBALS['eZSessionIsStarted'] )
-    {
-         return false;
+        if ( !self::$hasStarted )
+        {
+             return false;
+        }
+        //include_once( 'lib/ezdb/classes/ezdb.php' );
+        $db = eZDB::instance();
+        if ( !$db->isConnected() )
+        {
+            return false;
+        }
+        $_SESSION = array();
+        session_destroy();
+        self::$hasStarted = false;
+        return true;
     }
-    //include_once( 'lib/ezdb/classes/ezdb.php' );
-    $db = eZDB::instance();
-    if ( !$db->isConnected() )
-    {
-        return false;
-    }
-    eZRegisterSessionFunctions();
-    $ini = eZINI::instance();
-    $cookieTimeout = isset( $GLOBALS['RememberMeTimeout'] ) ? $GLOBALS['RememberMeTimeout'] : $ini->variable( 'Session', 'CookieTimeout' );
 
-    if ( is_numeric( $cookieTimeout ) )
+    /*!
+     Sets the current user ID to \a $userID,
+     this ID will be written to the session table field user_id
+     when the page is done.
+     \sa eZSessionUserID
+    */
+    static public function setUserID( $userID )
     {
-        session_set_cookie_params( (int)$cookieTimeout );
+        self::$userID = $userID;
     }
-    session_start();
-    return $GLOBALS['eZSessionIsStarted'] = true;
-}
 
-/*!
- Makes sure session data is stored in the session and stops the session.
-*/
-function eZSessionStop()
-{
-    if ( isset( $GLOBALS['eZSessionIsStarted'] ) &&
-         !$GLOBALS['eZSessionIsStarted'] )
+    /*!
+     Returns the current session ID.
+     The session handler will not care about value of the ID,
+     it's entirely up to the clients of the session handler to use and update this value.
+    */
+    static public function userID()
     {
-         return false;
+        return self::$userID;
     }
-    //include_once( 'lib/ezdb/classes/ezdb.php' );
-    $db = eZDB::instance();
-    if ( !$db->isConnected() )
-    {
-        return false;
-    }
-    session_write_close();
-    $GLOBALS['eZSessionIsStarted'] = false;
-    return true;
-}
 
-/*!
- Will make sure the user gets a new session ID while keepin the session data.
- This is useful to call on logins, to avoid sessions theft from users.
- \note This requires PHP 4.3.2 and higher which has the session_regenerate_id
- \return \c true if succesful
-*/
-function eZSessionRegenerate()
-{
-    if ( isset( $GLOBALS['eZSessionIsStarted'] ) &&
-         !$GLOBALS['eZSessionIsStarted'] )
+    static public function addCallback( $type, $callback )
     {
-         return false;
-    }
-    if ( !function_exists( 'session_regenerate_id' ) )
-    {
-        return false;
+        if ( !isset( self::$callbackFunctions[$type] ) )
+        {
+            self::$callbackFunctions[$type] = array();
+        }
+        self::$callbackFunctions[$type][] = $callback;
     }
-    // This doesn't seem to work as expected
-//     session_regenerate_id();
-    return true;
-}
 
-/*!
- Removes the current session and resets session variables.
-*/
-function eZSessionRemove()
-{
-    if ( isset( $GLOBALS['eZSessionIsStarted'] ) &&
-         !$GLOBALS['eZSessionIsStarted'] )
+    static public function triggerCallback( $type, $params )
     {
-         return false;
-    }
-    //include_once( 'lib/ezdb/classes/ezdb.php' );
-    $db = eZDB::instance();
-    if ( !$db->isConnected() )
-    {
+        if ( isset( self::$callbackFunctions[$type] ) )
+        {
+            foreach( self::$callbackFunctions[$type] as $callback )
+            {
+                call_user_func_array( $callback, $params );
+            }
+            return true;
+        }
         return false;
     }
-    $_SESSION = array();
-    session_destroy();
-    $GLOBALS['eZSessionIsStarted'] = false;
-    return true;
 }
 
-/*!
- Sets the current user ID to \a $userID,
- this ID will be written to the session table field user_id
- when the page is done.
- \sa eZSessionUserID
-*/
-function eZSessionSetUserID( $userID )
-{
-    $GLOBALS['eZSessionUserID'] = $userID;
-}
-
-/*!
- Returns the current session ID.
- The session handler will not care about value of the ID,
- it's entirely up to the clients of the session handler to use and update this value.
-*/
-function eZSessionUserID()
+// For BC
+function eZSessionStart()
 {
-    if ( isset( $GLOBALS['eZSessionUserID'] ) )
-        return $GLOBALS['eZSessionUserID'];
-    return 0;
+    eZSession::start();
 }
 
-?>
+?>
\ No newline at end of file
Index: soap.php
===================================================================
--- soap.php	(revision 22332)
+++ soap.php	(working copy)
@@ -72,7 +72,7 @@
 
 
 // include ezsession override implementation
-require_once( "lib/ezutils/classes/ezsession.php" );
+//require_once( "lib/ezutils/classes/ezsession.php" );
 
 // Check for extension
 //include_once( 'lib/ezutils/classes/ezextension.php' );
Index: update/common/scripts/cleanup.php
===================================================================
--- update/common/scripts/cleanup.php	(revision 22332)
+++ update/common/scripts/cleanup.php	(working copy)
@@ -166,18 +166,18 @@
 
 //include_once( 'kernel/classes/ezpersistentobject.php' );
 
-require_once( 'lib/ezutils/classes/ezsession.php' );
+//require_once( 'lib/ezutils/classes/ezsession.php' );
 if ( $clean['session'] )
 {
     $cli->output( "Removing all sessions" );
-    eZSessionEmpty();
+    eZSession::cleanup();
 }
 
 if ( $clean['expired_session'] )
 {
     $cli->output( "Removing expired sessions,", false );
-    eZSessionGarbageCollector();
-    $activeCount = eZSessionCountActive();
+    eZSession::garbageCollector();
+    $activeCount = eZSession::countActive();
     $cli->output( " " . $cli->stylize( 'emphasize', $activeCount ) . " left" );
 }
 
Index: webdav.php
===================================================================
--- webdav.php	(revision 22332)
+++ webdav.php	(working copy)
@@ -136,7 +136,7 @@
     }
     //include_once( "lib/ezutils/classes/ezmodule.php" );
     require_once( 'lib/ezutils/classes/ezexecution.php' );
-    require_once( "lib/ezutils/classes/ezsession.php" );
+    //require_once( "lib/ezutils/classes/ezsession.php" );
     require_once( "access.php" );
     require_once( "kernel/common/i18n.php" );
 
