[TASK] Move "pageUnavailable_force" into a PSR-15 middleware 21/55721/5
authorBenni Mack <benni@typo3.org>
Wed, 14 Feb 2018 20:31:35 +0000 (21:31 +0100)
committerBenni Mack <benni@typo3.org>
Thu, 15 Feb 2018 18:05:57 +0000 (19:05 +0100)
The check if the frontend is in "maintenance mode",
set by $GLOBALS['TYPO3_CONF_VARS']['FE']['pageUnavailable_force'],
is moved into a custom PSR-15 based middleware,
effectively decoupling this logic from TSFE object.

Resolves: #83917
Releases: master
Change-Id: I38c42069b82ca2403df9aa4b0e66410a3dfe6404
Reviewed-on: https://review.typo3.org/55721
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Susanne Moog <susanne.moog@typo3.org>
Tested-by: Susanne Moog <susanne.moog@typo3.org>
Reviewed-by: Mathias Schreiber <mathias.schreiber@typo3.com>
Tested-by: Mathias Schreiber <mathias.schreiber@typo3.com>
Reviewed-by: Benni Mack <benni@typo3.org>
Tested-by: Benni Mack <benni@typo3.org>
typo3/sysext/frontend/Classes/Http/RequestHandler.php
typo3/sysext/frontend/Classes/Middleware/MaintenanceMode.php [new file with mode: 0644]
typo3/sysext/frontend/Configuration/RequestMiddlewares.php

index 8d8d7ef..fe9dc43 100644 (file)
@@ -26,7 +26,6 @@ use TYPO3\CMS\Core\Http\RequestHandlerInterface;
 use TYPO3\CMS\Core\TimeTracker\TimeTracker;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
 use TYPO3\CMS\Core\Utility\MathUtility;
-use TYPO3\CMS\Frontend\Controller\ErrorController;
 use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
 use TYPO3\CMS\Frontend\Page\PageGenerator;
 use TYPO3\CMS\Frontend\Utility\CompressionUtility;
@@ -101,16 +100,6 @@ class RequestHandler implements RequestHandlerInterface, PsrRequestHandlerInterf
         // Fetch the initialized time tracker object
         $this->timeTracker = GeneralUtility::makeInstance(TimeTracker::class);
         $this->initializeController();
-
-        if ($GLOBALS['TYPO3_CONF_VARS']['FE']['pageUnavailable_force']
-            && !GeneralUtility::cmpIP(
-                GeneralUtility::getIndpEnv('REMOTE_ADDR'),
-                $GLOBALS['TYPO3_CONF_VARS']['SYS']['devIPmask']
-            )
-        ) {
-            return GeneralUtility::makeInstance(ErrorController::class)->unavailableAction('This page is temporarily unavailable.');
-        }
-
         $this->controller->connectToDB();
 
         // Output compression
diff --git a/typo3/sysext/frontend/Classes/Middleware/MaintenanceMode.php b/typo3/sysext/frontend/Classes/Middleware/MaintenanceMode.php
new file mode 100644 (file)
index 0000000..3709350
--- /dev/null
@@ -0,0 +1,55 @@
+<?php
+declare(strict_types = 1);
+namespace TYPO3\CMS\Frontend\Middleware;
+
+/*
+ * This file is part of the TYPO3 CMS project.
+ *
+ * It is free software; you can redistribute it and/or modify it under
+ * the terms of the GNU General Public License, either version 2
+ * of the License, or any later version.
+ *
+ * For the full copyright and license information, please read the
+ * LICENSE.txt file that was distributed with this source code.
+ *
+ * The TYPO3 project - inspiring people to share!
+ */
+
+use Psr\Http\Message\ResponseInterface;
+use Psr\Http\Message\ServerRequestInterface;
+use Psr\Http\Server\MiddlewareInterface;
+use Psr\Http\Server\RequestHandlerInterface;
+use TYPO3\CMS\Core\Utility\GeneralUtility;
+use TYPO3\CMS\Frontend\Controller\ErrorController;
+
+/**
+ * Checks whether the whole Frontend should be put into "Page unavailable mode"
+ * unless the devIPMask matches the current visitor's IP.
+ *
+ * The global setting $GLOBALS['TYPO3_CONF_VARS']['FE']['pageUnavailable_force']
+ * is used for turning on the maintenance mode.
+ */
+class MaintenanceMode implements MiddlewareInterface
+{
+    /**
+     * Calls the "unavailableAction" of the error controller if the system is in maintenance mode.
+     * This only applies if the REMOTE_ADDR does not match the devIpMask
+     *
+     * @param ServerRequestInterface $request
+     * @param RequestHandlerInterface $handler
+     * @return ResponseInterface
+     */
+    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
+    {
+        if ($GLOBALS['TYPO3_CONF_VARS']['FE']['pageUnavailable_force']
+            && !GeneralUtility::cmpIP(
+                $request->getAttribute('normalizedParams')->getRemoteAddress(),
+                $GLOBALS['TYPO3_CONF_VARS']['SYS']['devIPmask']
+            )
+        ) {
+            return GeneralUtility::makeInstance(ErrorController::class)->unavailableAction('This page is temporarily unavailable.');
+        }
+        // Continue the regular stack if no maintenance mode is active
+        return $handler->handle($request);
+    }
+}
index 537b568..ef53e0b 100644 (file)
@@ -38,10 +38,17 @@ return [
                 'typo3/cms-frontend/preprocessing'
             ]
         ],
+        'typo3/cms-frontend/maintenance-mode' => [
+            'target' => \TYPO3\CMS\Frontend\Middleware\MaintenanceMode::class,
+            'after' => [
+                'typo3/cms-core/normalized-params-attribute',
+                'typo3/cms-frontend/eid'
+            ]
+        ],
         'typo3/cms-frontend/content-length-headers' => [
             'target' => \TYPO3\CMS\Frontend\Middleware\ContentLengthResponseHeader::class,
             'after' => [
-                'typo3/cms-frontend/eid'
+                'typo3/cms-frontend/maintenance-mode'
             ]
         ],
     ]