[FEATURE] Add support for PSR-15 HTTP middlewares
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Http / Application.php
index b43ac7f..5ef0ad1 100644 (file)
@@ -15,112 +15,80 @@ namespace TYPO3\CMS\Backend\Http;
  */
 use TYPO3\CMS\Core\Core\ApplicationInterface;
 use TYPO3\CMS\Core\Core\Bootstrap;
-use TYPO3\CMS\Core\Utility\GeneralUtility;
 
 /**
  * Entry point for the TYPO3 Backend (HTTP requests)
  */
-class Application implements ApplicationInterface {
-
-       /**
-        * @var Bootstrap
-        */
-       protected $bootstrap;
-
-       /**
-        * @var string
-        */
-       protected $entryPointPath = 'typo3/';
-
-       /**
-        * @var \Psr\Http\Message\ServerRequestInterface
-        */
-       protected $request;
-
-       /**
-        * All available request handlers that can handle backend requests (non-CLI)
-        * @var array
-        */
-       protected $availableRequestHandlers = array(
-               \TYPO3\CMS\Backend\Http\RequestHandler::class,
-               \TYPO3\CMS\Backend\Http\BackendModuleRequestHandler::class,
-               \TYPO3\CMS\Backend\Http\AjaxRequestHandler::class
-       );
-
-       /**
-        * Constructor setting up legacy constant and register available Request Handlers
-        *
-        * @param \Composer\Autoload\ClassLoader|\Helhum\ClassAliasLoader\Composer\ClassAliasLoader $classLoader an instance of the class loader
-        */
-       public function __construct($classLoader) {
-               $this->defineLegacyConstants();
-
-               $this->bootstrap = Bootstrap::getInstance()
-                       ->initializeClassLoader($classLoader)
-                       ->baseSetup($this->entryPointPath);
-
-               // can be done here after the base setup is done
-               $this->defineAdditionalEntryPointRelatedConstants();
-
-               // Redirect to install tool if base configuration is not found
-               if (!$this->bootstrap->checkIfEssentialConfigurationExists()) {
-                       $this->bootstrap->redirectToInstallTool($this->entryPointPath);
-               }
-
-               foreach ($this->availableRequestHandlers as $requestHandler) {
-                       $this->bootstrap->registerRequestHandlerImplementation($requestHandler);
-               }
-
-               $this->request = \TYPO3\CMS\Core\Http\ServerRequestFactory::fromGlobals();
-               // see below when this option is set
-               if ($GLOBALS['TYPO3_AJAX']) {
-                       $this->request = $this->request->withAttribute('isAjaxRequest', TRUE);
-               } elseif (isset($this->request->getQueryParams()['M'])) {
-                       $this->request = $this->request->withAttribute('isModuleRequest', TRUE);
-               }
-
-               $this->bootstrap->configure();
-       }
-
-       /**
-        * Set up the application and shut it down afterwards
-        *
-        * @param callable $execute
-        * @return void
-        */
-       public function run(callable $execute = NULL) {
-               $this->bootstrap->handleRequest($this->request);
-
-               if ($execute !== NULL) {
-                       if ($execute instanceof \Closure) {
-                               $execute->bindTo($this);
-                       }
-                       call_user_func($execute);
-               }
-
-               $this->bootstrap->shutdown();
-       }
-
-       /**
-        * Define constants and variables
-        */
-       protected function defineLegacyConstants() {
-               define('TYPO3_MODE', 'BE');
-       }
-
-       /**
-        * Define values that are based on the current script
-        */
-       protected function defineAdditionalEntryPointRelatedConstants() {
-               $currentScript = GeneralUtility::getIndpEnv('SCRIPT_NAME');
-
-               // Activate "AJAX" handler when called with the GET variable ajaxID
-               if (!empty(GeneralUtility::_GET('ajaxID'))) {
-                       $GLOBALS['TYPO3_AJAX'] = TRUE;
-               // The following check is security relevant! DO NOT REMOVE!
-               } elseif (empty(GeneralUtility::_GET('M')) && substr($currentScript, -16) === '/typo3/index.php') {
-                       // Allow backend login to work, disallow module access without authenticated backend user
-                       define('TYPO3_PROCEED_IF_NO_USER', 1);
-               }
-       }
+class Application implements ApplicationInterface
+{
+    /**
+     * @var Bootstrap
+     */
+    protected $bootstrap;
+
+    /**
+     * Number of subdirectories where the entry script is located, relative to PATH_site
+     * Usually this is equal to PATH_site = 0
+     * @var int
+     */
+    protected $entryPointLevel = 1;
+
+    /**
+     * All available request handlers that can handle backend requests (non-CLI)
+     * @var array
+     */
+    protected $availableRequestHandlers = [
+        \TYPO3\CMS\Backend\Http\RequestHandler::class,
+        \TYPO3\CMS\Backend\Http\AjaxRequestHandler::class
+    ];
+
+    /**
+     * Constructor setting up legacy constant and register available Request Handlers
+     *
+     * @param \Composer\Autoload\ClassLoader $classLoader an instance of the class loader
+     */
+    public function __construct($classLoader)
+    {
+        $this->defineLegacyConstants();
+
+        $this->bootstrap = Bootstrap::getInstance()
+            ->initializeClassLoader($classLoader)
+            ->setRequestType(TYPO3_REQUESTTYPE_BE | (isset($_REQUEST['route']) && strpos($_REQUEST['route'], '/ajax/') === 0 ? TYPO3_REQUESTTYPE_AJAX : 0))
+            ->baseSetup($this->entryPointLevel);
+
+        // Redirect to install tool if base configuration is not found
+        if (!$this->bootstrap->checkIfEssentialConfigurationExists()) {
+            $this->bootstrap->redirectToInstallTool($this->entryPointLevel);
+        }
+
+        foreach ($this->availableRequestHandlers as $requestHandler) {
+            $this->bootstrap->registerRequestHandlerImplementation($requestHandler);
+        }
+
+        $this->bootstrap->configure();
+    }
+
+    /**
+     * Set up the application and shut it down afterwards
+     *
+     * @param callable $execute
+     */
+    public function run(callable $execute = null)
+    {
+        $this->bootstrap->handleRequest(\TYPO3\CMS\Core\Http\ServerRequestFactory::fromGlobals(), 'backend');
+
+        if ($execute !== null) {
+            call_user_func($execute);
+        }
+
+        $this->bootstrap->shutdown();
+    }
+
+    /**
+     * Define constants and variables
+     */
+    protected function defineLegacyConstants()
+    {
+        define('TYPO3_MODE', 'BE');
+    }
 }