From b4aa8b7f902c51b76b60d24cca6dc40adfda82ba Mon Sep 17 00:00:00 2001 From: Benni Mack <benni@typo3.org> Date: Wed, 14 Feb 2018 21:31:35 +0100 Subject: [PATCH] [TASK] Move "pageUnavailable_force" into a PSR-15 middleware 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> --- .../frontend/Classes/Http/RequestHandler.php | 11 ---- .../Classes/Middleware/MaintenanceMode.php | 55 +++++++++++++++++++ .../Configuration/RequestMiddlewares.php | 9 ++- 3 files changed, 63 insertions(+), 12 deletions(-) create mode 100644 typo3/sysext/frontend/Classes/Middleware/MaintenanceMode.php diff --git a/typo3/sysext/frontend/Classes/Http/RequestHandler.php b/typo3/sysext/frontend/Classes/Http/RequestHandler.php index 8d8d7efbdbf3..fe9dc43091df 100644 --- a/typo3/sysext/frontend/Classes/Http/RequestHandler.php +++ b/typo3/sysext/frontend/Classes/Http/RequestHandler.php @@ -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 index 000000000000..3709350dbd28 --- /dev/null +++ b/typo3/sysext/frontend/Classes/Middleware/MaintenanceMode.php @@ -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); + } +} diff --git a/typo3/sysext/frontend/Configuration/RequestMiddlewares.php b/typo3/sysext/frontend/Configuration/RequestMiddlewares.php index 537b568ec663..ef53e0b00952 100644 --- a/typo3/sysext/frontend/Configuration/RequestMiddlewares.php +++ b/typo3/sysext/frontend/Configuration/RequestMiddlewares.php @@ -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' ] ], ] -- GitLab