[TASK] Rename PageParameterValidator middleware 46/58446/2
authorBenni Mack <benni@typo3.org>
Fri, 28 Sep 2018 21:42:46 +0000 (23:42 +0200)
committerGeorg Ringer <georg.ringer@gmail.com>
Fri, 28 Sep 2018 21:49:58 +0000 (23:49 +0200)
In order to be in line with the new PageArguments object,
this newly introduced class is now renamed.

Resolves: #86431
Releases: master
Change-Id: I96575338538641fc27a578c49868b21100b61ed3
Reviewed-on: https://review.typo3.org/58446
Reviewed-by: Oliver Hader <oliver.hader@typo3.org>
Tested-by: Oliver Hader <oliver.hader@typo3.org>
Reviewed-by: Georg Ringer <georg.ringer@gmail.com>
Tested-by: Georg Ringer <georg.ringer@gmail.com>
typo3/sysext/frontend/Classes/Middleware/PageArgumentValidator.php [new file with mode: 0644]
typo3/sysext/frontend/Classes/Middleware/PageParameterValidator.php [deleted file]
typo3/sysext/frontend/Classes/Page/CacheHashCalculator.php
typo3/sysext/frontend/Configuration/RequestMiddlewares.php

diff --git a/typo3/sysext/frontend/Classes/Middleware/PageArgumentValidator.php b/typo3/sysext/frontend/Classes/Middleware/PageArgumentValidator.php
new file mode 100644 (file)
index 0000000..1b0ddbe
--- /dev/null
@@ -0,0 +1,124 @@
+<?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\TimeTracker\TimeTracker;
+use TYPO3\CMS\Core\Utility\GeneralUtility;
+use TYPO3\CMS\Frontend\Controller\ErrorController;
+use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
+use TYPO3\CMS\Frontend\Page\CacheHashCalculator;
+use TYPO3\CMS\Frontend\Page\PageAccessFailureReasons;
+
+/**
+ * This middleware validates given request parameters against the common "cHash" functionality.
+ */
+class PageArgumentValidator implements MiddlewareInterface
+{
+
+    /**
+     * The cHash Service class used for cHash related functionality
+     *
+     * @var CacheHashCalculator
+     */
+    protected $cacheHashCalculator;
+
+    /**
+     * @var TypoScriptFrontendController
+     */
+    protected $controller;
+
+    /**
+     * @param TypoScriptFrontendController|null $controller
+     */
+    public function __construct(TypoScriptFrontendController $controller = null)
+    {
+        $this->controller = $controller ?? $GLOBALS['TSFE'];
+        $this->cacheHashCalculator = GeneralUtility::makeInstance(CacheHashCalculator::class);
+    }
+
+    /**
+     * Validates the &cHash parameter against the other $queryParameters / GET parameters
+     *
+     * @param ServerRequestInterface $request
+     * @param RequestHandlerInterface $handler
+     * @return ResponseInterface
+     */
+    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
+    {
+        $pageNotFoundOnValidationError = (bool)($GLOBALS['TYPO3_CONF_VARS']['FE']['pageNotFoundOnCHashError'] ?? true);
+        if ($this->controller->no_cache && !$pageNotFoundOnValidationError) {
+            // No need to test anything if caching was already disabled.
+        } else {
+            // Evaluate the cache hash parameter
+            $queryParams = $request->getQueryParams();
+            if (!empty($queryParams) && !$this->evaluateCacheHashParameter($queryParams, $pageNotFoundOnValidationError)) {
+                return GeneralUtility::makeInstance(ErrorController::class)->pageNotFoundAction(
+                    $request,
+                    'Request parameters could not be validated (&cHash comparison failed)',
+                    ['code' => PageAccessFailureReasons::CACHEHASH_COMPARISON_FAILED]
+                );
+            }
+        }
+        return $handler->handle($request);
+    }
+
+    /**
+     * Calculates a hash string based on additional parameters in the url.
+     *
+     * Calculated hash is stored in $this->controller->cHash_array.
+     * This is used to cache pages with more parameters than just id and type.
+     *
+     * @see TypoScriptFrontendController::reqCHash()
+     * @param array $queryParams GET parameters
+     * @param bool $pageNotFoundOnCacheHashError see $GLOBALS['TYPO3_CONF_VARS']['FE']['pageNotFoundOnCHashError']
+     * @return bool if false, then a PageNotFound response is triggered
+     */
+    protected function evaluateCacheHashParameter(array $queryParams, bool $pageNotFoundOnCacheHashError): bool
+    {
+        if ($this->controller->cHash) {
+            // Make sure we use the page uid and not the page alias
+            $queryParams['id'] = $this->controller->id;
+            $this->controller->cHash_array = $this->cacheHashCalculator->getRelevantParameters(GeneralUtility::implodeArrayForUrl('', $queryParams));
+            $cHash_calc = $this->cacheHashCalculator->calculateCacheHash($this->controller->cHash_array);
+            if (!hash_equals($cHash_calc, $this->controller->cHash)) {
+                // Early return to trigger the error controller
+                if ($pageNotFoundOnCacheHashError) {
+                    return false;
+                }
+                $this->controller->no_cache = true;
+                $this->getTimeTracker()->setTSlogMessage('The incoming cHash "' . $this->controller->cHash . '" and calculated cHash "' . $cHash_calc . '" did not match, so caching was disabled. The fieldlist used was "' . implode(',', array_keys($this->controller->cHash_array)) . '"', 2);
+            }
+            // No cHash is set, check if that is correct
+        } elseif ($this->cacheHashCalculator->doParametersRequireCacheHash(GeneralUtility::implodeArrayForUrl('', $queryParams))) {
+            // Will disable caching
+            $this->controller->reqCHash();
+        }
+        return true;
+    }
+
+    /**
+     * @return TimeTracker
+     */
+    protected function getTimeTracker(): TimeTracker
+    {
+        return GeneralUtility::makeInstance(TimeTracker::class);
+    }
+}
diff --git a/typo3/sysext/frontend/Classes/Middleware/PageParameterValidator.php b/typo3/sysext/frontend/Classes/Middleware/PageParameterValidator.php
deleted file mode 100644 (file)
index d4b7d73..0000000
+++ /dev/null
@@ -1,124 +0,0 @@
-<?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\TimeTracker\TimeTracker;
-use TYPO3\CMS\Core\Utility\GeneralUtility;
-use TYPO3\CMS\Frontend\Controller\ErrorController;
-use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
-use TYPO3\CMS\Frontend\Page\CacheHashCalculator;
-use TYPO3\CMS\Frontend\Page\PageAccessFailureReasons;
-
-/**
- * This middleware validates given request parameters.
- */
-class PageParameterValidator implements MiddlewareInterface
-{
-
-    /**
-     * The cHash Service class used for cHash related functionality
-     *
-     * @var CacheHashCalculator
-     */
-    protected $cacheHashCalculator;
-
-    /**
-     * @var TypoScriptFrontendController
-     */
-    protected $controller;
-
-    /**
-     * @param TypoScriptFrontendController|null $controller
-     */
-    public function __construct(TypoScriptFrontendController $controller = null)
-    {
-        $this->controller = $controller ?? $GLOBALS['TSFE'];
-        $this->cacheHashCalculator = GeneralUtility::makeInstance(CacheHashCalculator::class);
-    }
-
-    /**
-     * Validates the &cHash parameter against the other $queryParameters / GET parameters
-     *
-     * @param ServerRequestInterface $request
-     * @param RequestHandlerInterface $handler
-     * @return ResponseInterface
-     */
-    public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
-    {
-        $pageNotFoundOnValidationError = (bool)($GLOBALS['TYPO3_CONF_VARS']['FE']['pageNotFoundOnCHashError'] ?? true);
-        if ($this->controller->no_cache && !$pageNotFoundOnValidationError) {
-            // No need to test anything if caching was already disabled.
-        } else {
-            // Evaluate the cache hash parameter
-            $queryParams = $request->getQueryParams();
-            if (!empty($queryParams) && !$this->evaluateCacheHashParameter($queryParams, $pageNotFoundOnValidationError)) {
-                return GeneralUtility::makeInstance(ErrorController::class)->pageNotFoundAction(
-                    $request,
-                    'Request parameters could not be validated (&cHash comparison failed)',
-                    ['code' => PageAccessFailureReasons::CACHEHASH_COMPARISON_FAILED]
-                );
-            }
-        }
-        return $handler->handle($request);
-    }
-
-    /**
-     * Calculates a hash string based on additional parameters in the url.
-     *
-     * Calculated hash is stored in $this->controller->cHash_array.
-     * This is used to cache pages with more parameters than just id and type.
-     *
-     * @see TypoScriptFrontendController::reqCHash()
-     * @param array $queryParams GET parameters
-     * @param bool $pageNotFoundOnCacheHashError see $GLOBALS['TYPO3_CONF_VARS']['FE']['pageNotFoundOnCHashError']
-     * @return bool if false, then a PageNotFound response is triggered
-     */
-    protected function evaluateCacheHashParameter(array $queryParams, bool $pageNotFoundOnCacheHashError): bool
-    {
-        if ($this->controller->cHash) {
-            // Make sure we use the page uid and not the page alias
-            $queryParams['id'] = $this->controller->id;
-            $this->controller->cHash_array = $this->cacheHashCalculator->getRelevantParameters(GeneralUtility::implodeArrayForUrl('', $queryParams));
-            $cHash_calc = $this->cacheHashCalculator->calculateCacheHash($this->controller->cHash_array);
-            if (!hash_equals($cHash_calc, $this->controller->cHash)) {
-                // Early return to trigger the error controller
-                if ($pageNotFoundOnCacheHashError) {
-                    return false;
-                }
-                $this->controller->no_cache = true;
-                $this->getTimeTracker()->setTSlogMessage('The incoming cHash "' . $this->controller->cHash . '" and calculated cHash "' . $cHash_calc . '" did not match, so caching was disabled. The fieldlist used was "' . implode(',', array_keys($this->controller->cHash_array)) . '"', 2);
-            }
-            // No cHash is set, check if that is correct
-        } elseif ($this->cacheHashCalculator->doParametersRequireCacheHash(GeneralUtility::implodeArrayForUrl('', $queryParams))) {
-            // Will disable caching
-            $this->controller->reqCHash();
-        }
-        return true;
-    }
-
-    /**
-     * @return TimeTracker
-     */
-    protected function getTimeTracker(): TimeTracker
-    {
-        return GeneralUtility::makeInstance(TimeTracker::class);
-    }
-}
index d3ad34c..be14390 100644 (file)
@@ -52,7 +52,7 @@ class CacheHashCalculator implements SingletonInterface
      */
     public function __construct()
     {
-        $this->setConfiguration($GLOBALS['TYPO3_CONF_VARS']['FE']['cacheHash']);
+        $this->setConfiguration($GLOBALS['TYPO3_CONF_VARS']['FE']['cacheHash'] ?? []);
     }
 
     /**
index a3de66f..68c9931 100644 (file)
@@ -106,8 +106,8 @@ return [
                 'typo3/cms-frontend/site',
             ]
         ],
-        'typo3/cms-frontend/page-parameter-validator' => [
-            'target' => \TYPO3\CMS\Frontend\Middleware\PageParameterValidator::class,
+        'typo3/cms-frontend/page-argument-validator' => [
+            'target' => \TYPO3\CMS\Frontend\Middleware\PageArgumentValidator::class,
             'after' => [
                 'typo3/cms-frontend/page-resolver',
             ],
@@ -119,7 +119,7 @@ return [
         'typo3/cms-frontend/prepare-tsfe-rendering' => [
             'target' => \TYPO3\CMS\Frontend\Middleware\PrepareTypoScriptFrontendRendering::class,
             'after' => [
-                'typo3/cms-frontend/page-parameter-validator',
+                'typo3/cms-frontend/page-argument-validator',
             ]
         ],
         /** internal: do not use or reference this middleware in your own code, as this will be possibly be removed */