Move cookie handling into SessionHandler
authorTim Düsterhus <duesterhus@woltlab.com>
Tue, 13 Oct 2020 08:40:35 +0000 (10:40 +0200)
committerTim Düsterhus <duesterhus@woltlab.com>
Thu, 15 Oct 2020 14:13:53 +0000 (16:13 +0200)
wcfsetup/install/files/lib/system/WCF.class.php
wcfsetup/install/files/lib/system/WCFACP.class.php
wcfsetup/install/files/lib/system/session/ACPSessionFactory.class.php
wcfsetup/install/files/lib/system/session/SessionHandler.class.php

index 51f75f1f07c91688a945a11546090da10d3033e2..db9326cd6078d1987f661e586ce7e05539130745 100644 (file)
@@ -438,7 +438,6 @@ class WCF {
                $factory->load();
                
                self::$sessionObj = SessionHandler::getInstance();
-               self::$sessionObj->setHasValidCookie($factory->hasValidCookie());
        }
        
        /**
index d944832daef147c19f8f422a6c564991f08e09a1..9aabff0f0f0304ea37406aa7214c2111fcab1bab 100644 (file)
@@ -191,8 +191,6 @@ class WCFACP extends WCF {
                
                $factory = new ACPSessionFactory();
                $factory->load();
-               
-               self::$sessionObj->setHasValidCookie($factory->hasValidCookie());
        }
        
        /**
index ea5a72f81f2de1d1c802e64bfea139f4457a560b..3eb87e951e01e18bdb95b16ea4f2cf19994bc5c7 100644 (file)
@@ -1,8 +1,8 @@
 <?php
 namespace wcf\system\session;
 use wcf\data\acp\session\ACPSessionEditor;
+use wcf\data\session\Session;
 use wcf\system\event\EventHandler;
-use wcf\util\HeaderUtil;
 
 /**
  * Handles the ACP session of the active user.
@@ -14,14 +14,12 @@ use wcf\util\HeaderUtil;
  */
 class ACPSessionFactory {
        /**
-        * suffix used to tell ACP and frontend cookies apart
-        * @var string
+        * @deprecated 5.4 - This property is not read any longer.
         */
        protected $cookieSuffix = 'acp_';
        
        /**
-        * session editor class name
-        * @var string
+        * @deprecated 5.4 - This property is not read any longer.
         */
        protected $sessionEditor = ACPSessionEditor::class;
        
@@ -29,9 +27,7 @@ class ACPSessionFactory {
         * Loads the object of the active session.
         */
        public function load() {
-               // get session
-               $sessionID = $this->readSessionID();
-               SessionHandler::getInstance()->load($this->sessionEditor, $sessionID);
+               SessionHandler::getInstance()->loadFromCookie();
                
                // call beforeInit event
                if (!defined('NO_IMPORTS')) {
@@ -47,19 +43,10 @@ class ACPSessionFactory {
        }
        
        /**
-        * Returns true if session was based upon a valid cookie.
-        * 
-        * @return      boolean
-        * @since       3.0
+        * @deprecated 5.4 - Sessions are fully managed by SessionHandler.
         */
        public function hasValidCookie() {
-               if (isset($_COOKIE[COOKIE_PREFIX.$this->cookieSuffix.'session'])) {
-                       if ($_COOKIE[COOKIE_PREFIX.$this->cookieSuffix.'session'] == SessionHandler::getInstance()->sessionID) {
-                               return true;
-                       }
-               }
-               
-               return false;
+               return SessionHandler::getInstance()->hasValidCookie();
        }
        
        /**
@@ -70,10 +57,7 @@ class ACPSessionFactory {
        }
        
        /**
-        * Returns the session id from cookie. Returns an empty string,
-        * if no session cookie was provided.
-        * 
-        * @return      string
+        * @deprecated 5.4 - Sessions are fully managed by SessionHandler.
         */
        protected function readSessionID() {
                // get sessionID from cookie
index b725329c307ec4c4132e74cb66734628e070ef3d..7664a91957f3101e47dbb454bc8d20211886826d 100644 (file)
@@ -65,12 +65,6 @@ final class SessionHandler extends SingletonFactory {
         */
        protected $groupData = null;
        
-       /**
-        * true if client provided a valid session cookie
-        * @var boolean
-        */
-       protected $hasValidCookie = false;
-       
        /**
         * true if within ACP or WCFSetup
         * @var boolean
@@ -105,18 +99,6 @@ final class SessionHandler extends SingletonFactory {
         */
        protected $legacySession = null;
        
-       /**
-        * session class name
-        * @var string
-        */
-       protected $sessionClassName = '';
-       
-       /**
-        * session editor class name
-        * @var string
-        */
-       protected $sessionEditorClassName = '';
-       
        /**
         * style id
         * @var integer
@@ -193,14 +175,9 @@ final class SessionHandler extends SingletonFactory {
        public function setCookieSuffix() { }
        
        /**
-        * Sets a boolean value to determine if the client provided a valid session cookie.
-        * 
-        * @param       boolean         $hasValidCookie
-        * @since       3.0
+        * @deprecated 5.4 - This method is a noop. Cookie handling works automatically.
         */
-       public function setHasValidCookie($hasValidCookie) {
-               $this->hasValidCookie = $hasValidCookie;
-       }
+       public function setHasValidCookie($hasValidCookie) { }
        
        /**
         * Returns true if client provided a valid session cookie.
@@ -208,20 +185,17 @@ final class SessionHandler extends SingletonFactory {
         * @return      boolean
         * @since       3.0
         */
-       public function hasValidCookie() {
-               return $this->hasValidCookie;
+       public function hasValidCookie(): bool {
+               $cookieName = COOKIE_PREFIX.($this->isACP ? 'acp' : 'user')."_session";
+               $sessionID = $_COOKIE[$cookieName] ?? null;
+               
+               return $sessionID === $this->sessionID;
        }
        
        /**
-        * Loads an existing session or creates a new one.
-        * 
-        * @param       string          $sessionEditorClassName
-        * @param       string          $sessionID
+        * @deprecated 5.4 - Sessions are managed automatically. Use loadFromCookie().
         */
        public function load($sessionEditorClassName, $sessionID) {
-               $this->sessionEditorClassName = $sessionEditorClassName;
-               $this->sessionClassName = call_user_func([$sessionEditorClassName, 'getBaseClass']);
-               
                $hasSession = false;
                if (!empty($sessionID)) {
                        $hasSession = $this->getExistingSession($sessionID);
@@ -233,6 +207,24 @@ final class SessionHandler extends SingletonFactory {
                }
        }
        
+       /**
+        * Loads the session matching the session cookie.
+        */
+       public function loadFromCookie() {
+               $cookieName = COOKIE_PREFIX.($this->isACP ? 'acp' : 'user')."_session";
+               $sessionID = $_COOKIE[$cookieName] ?? null;
+               
+               $hasSession = false;
+               if ($sessionID) {
+                       $hasSession = $this->getExistingSession($sessionID);
+               }
+               
+               // create new session
+               if (!$hasSession) {
+                       $this->create();
+               }
+       }
+       
        /**
         * Initializes session system.
         */