Override the active application on runtime
authorAlexander Ebert <ebert@woltlab.com>
Tue, 23 Jul 2019 15:36:34 +0000 (17:36 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Tue, 23 Jul 2019 15:36:34 +0000 (17:36 +0200)
Fixes #2934

wcfsetup/install/files/lib/system/application/AbstractApplication.class.php
wcfsetup/install/files/lib/system/application/ApplicationHandler.class.php
wcfsetup/install/files/lib/system/request/RequestHandler.class.php

index 8bc8e144f950b62c61b3d96bc7dfe7f1a380d855..37ba6dee08e114bf5203d893a992c2099928a85c 100644 (file)
@@ -69,10 +69,7 @@ abstract class AbstractApplication extends SingletonFactory implements IApplicat
                
                $this->packageID = $application->packageID;
                
-               // check if current application is active (directly invoked, not dependent)
-               if ($application->packageID == ApplicationHandler::getInstance()->getActiveApplication()->packageID) {
-                       $this->isActiveApplication = true;
-               }
+               $this->rebuildActiveApplication();
        }
        
        /**
@@ -119,6 +116,13 @@ abstract class AbstractApplication extends SingletonFactory implements IApplicat
                return $this->primaryController;
        }
        
+       /**
+        * @since 5.2
+        */
+       public function rebuildActiveApplication() {
+               $this->isActiveApplication = ($this->packageID == ApplicationHandler::getInstance()->getActiveApplication()->packageID);
+       }
+       
        /**
         * @inheritDoc
         */
index 6441b66dc6cb058010d73577bc20e7f38b8ba2c1..675a807fd248dcf3d0f115db2b013c569b33f190 100644 (file)
@@ -6,9 +6,11 @@ use wcf\data\application\ApplicationList;
 use wcf\data\package\Package;
 use wcf\data\package\PackageList;
 use wcf\system\cache\builder\ApplicationCacheBuilder;
+use wcf\system\request\RequestHandler;
 use wcf\system\request\RouteHandler;
 use wcf\system\Regex;
 use wcf\system\SingletonFactory;
+use wcf\system\WCF;
 use wcf\util\FileUtil;
 
 /**
@@ -118,10 +120,18 @@ class ApplicationHandler extends SingletonFactory {
                        return new Application(null, [
                                'domainName' => $host,
                                'domainPath' => $path,
-                               'cookieDomain' => $host
+                               'cookieDomain' => $host,
                        ]);
                }
-               else if (isset($this->cache['application'][PACKAGE_ID])) {
+               
+               $request = RequestHandler::getInstance()->getActiveRequest();
+               if ($request !== null) {
+                       $abbreviation = substr($request->getClassName(), 0, mb_strpos($request->getClassName(), '\\'));
+                       
+                       return $this->getApplication($abbreviation);
+               }
+               
+               if (isset($this->cache['application'][PACKAGE_ID])) {
                        return $this->cache['application'][PACKAGE_ID];
                }
                
@@ -230,6 +240,23 @@ class ApplicationHandler extends SingletonFactory {
                return $this->isMultiDomain;
        }
        
+       /**
+        * @since 5.2
+        */
+       public function rebuildActiveApplication() {
+               /** @var AbstractApplication $application */
+               foreach ($this->cache['application'] as $application) {
+                       if ($application->getPackage()->package === 'com.woltlab.wcf') {
+                               continue;
+                       }
+                       
+                       $appObject = WCF::getApplicationObject($application);
+                       if ($appObject instanceof AbstractApplication) {
+                               $appObject->rebuildActiveApplication();
+                       }
+               }
+       }
+       
        /**
         * Rebuilds cookie domain/path for all applications.
         */
index bace28189e23a31b370d3e18f6526e5a0315fdc7..5fbac25b7769513256e0e2881c2afa6c452f5f3f 100644 (file)
@@ -224,6 +224,8 @@ class RequestHandler extends SingletonFactory {
                                        $this->activeRequest->setIsLandingPage();
                                }
                        }
+                       
+                       ApplicationHandler::getInstance()->rebuildActiveApplication();
                }
                catch (SystemException $e) {
                        if (defined('ENABLE_DEBUG_MODE') && ENABLE_DEBUG_MODE && defined('ENABLE_DEVELOPER_TOOLS') && ENABLE_DEVELOPER_TOOLS) {