[BUGFIX] Make encoding of parameters for cHash independent of PHP settings
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Mvc / Web / CacheHashEnforcer.php
1 <?php
2 declare(strict_types=1);
3 namespace TYPO3\CMS\Extbase\Mvc\Web;
4
5 /*
6 * This file is part of the TYPO3 CMS project.
7 *
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
11 *
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
14 *
15 * The TYPO3 project - inspiring people to share!
16 */
17
18 use TYPO3\CMS\Core\SingletonInterface;
19 use TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController;
20 use TYPO3\CMS\Frontend\Page\CacheHashCalculator;
21
22 /**
23 * Enforces cHash argument if it is required for a given request
24 */
25 class CacheHashEnforcer implements SingletonInterface
26 {
27 /**
28 * @var CacheHashCalculator
29 */
30 protected $cacheHashCalculator;
31
32 /**
33 * @var TypoScriptFrontendController
34 */
35 protected $typoScriptFrontendController;
36
37 /**
38 * CacheHashEnforcer constructor.
39 *
40 * @param CacheHashCalculator $cacheHashCalculator
41 * @param TypoScriptFrontendController|null $typoScriptFrontendController
42 */
43 public function __construct(
44 CacheHashCalculator $cacheHashCalculator,
45 TypoScriptFrontendController $typoScriptFrontendController = null
46 ) {
47 $this->cacheHashCalculator = $cacheHashCalculator;
48 $this->typoScriptFrontendController = $typoScriptFrontendController ?: $GLOBALS['TSFE'];
49 }
50
51 /**
52 * Checks if cHash is required for the current request and calls
53 * TypoScriptFrontendController::reqCHash() if so.
54 * This call will trigger a PageNotFoundException if arguments are required and cHash is not present.
55 *
56 * @param Request $request
57 * @param string $pluginNamespace
58 */
59 public function enforceForRequest(Request $request, string $pluginNamespace)
60 {
61 $arguments = $request->getArguments();
62 if (is_array($arguments) && count($arguments) > 0) {
63 $parameters = [$pluginNamespace => $arguments];
64 $parameters['id'] = $this->typoScriptFrontendController->id;
65 $relevantParameters = $this->cacheHashCalculator->getRelevantParameters(
66 http_build_query($parameters, '', '&', PHP_QUERY_RFC3986)
67 );
68 if (count($relevantParameters) > 0) {
69 $this->typoScriptFrontendController->reqCHash();
70 }
71 }
72 }
73 }