‪TYPO3CMS  10.4
CacheHashCalculator.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
20 
25 {
29  protected ‪$configuration;
30 
37  {
38  $this->configuration = ‪$configuration ?? GeneralUtility::makeInstance(CacheHashConfiguration::class);
39  }
40 
47  public function ‪calculateCacheHash(array $params)
48  {
49  return !empty($params) ? md5(serialize($params)) : '';
50  }
51 
59  public function ‪generateForParameters($queryString)
60  {
61  $cacheHashParams = $this->‪getRelevantParameters($queryString);
62  return $this->‪calculateCacheHash($cacheHashParams);
63  }
64 
71  public function ‪doParametersRequireCacheHash($queryString)
72  {
74  return false;
75  }
76  $parameterNames = array_keys($this->‪splitQueryStringToArray($queryString));
77  foreach ($parameterNames as $parameterName) {
78  $hasRequiredParameter = $this->configuration->applies(
80  $parameterName
81  );
82  if ($hasRequiredParameter) {
83  return true;
84  }
85  }
86  return false;
87  }
88 
98  public function ‪getRelevantParameters($queryString)
99  {
100  $parameters = $this->‪splitQueryStringToArray($queryString);
101  $relevantParameters = [];
102  foreach ($parameters as $parameterName => $parameterValue) {
103  if ($this->‪isAdminPanelParameter($parameterName) || $this->‪isExcludedParameter($parameterName) || $this->‪isCoreParameter($parameterName)) {
104  continue;
105  }
106  if ($this->‪hasCachedParametersWhiteList() && !$this->‪isInCachedParametersWhiteList($parameterName)) {
107  continue;
108  }
109  if (($parameterValue === null || $parameterValue === '') && $this->‪isAllowedWithEmptyValue($parameterName)) {
110  continue;
111  }
112  $relevantParameters[$parameterName] = $parameterValue;
113  }
114  if (!empty($relevantParameters)) {
115  if (empty($parameters['id'])) {
116  throw new \RuntimeException('ID parameter needs to be passed for the cHash calculation!', 1467983513);
117  }
118  $relevantParameters['id'] = $parameters['id'];
119  // Finish and sort parameters array by keys:
120  $relevantParameters['encryptionKey'] = ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'];
121  ksort($relevantParameters);
122  }
123  return $relevantParameters;
124  }
125 
135  protected function ‪splitQueryStringToArray($queryString)
136  {
137  $parameters = array_filter(explode('&', ltrim($queryString, '?')));
138  $parameterArray = [];
139  foreach ($parameters as $parameter) {
140  // should not remove empty values with trimExplode, otherwise cases like &=value, value is used as parameterName.
141  $parts = ‪GeneralUtility::trimExplode('=', $parameter, false);
142  $parameterName = $parts[0];
143  $parameterValue = $parts[1] ?? '';
144  if (trim($parameterName) === '') {
145  // This parameter cannot appear in $_GET in PHP even if its value is not empty, so it should be ignored!
146  continue;
147  }
148  $parameterArray[rawurldecode($parameterName)] = rawurldecode($parameterValue);
149  }
150  return $parameterArray;
151  }
152 
160  protected function ‪isAdminPanelParameter($key)
161  {
162  return $key === 'ADMCMD_simUser' || $key === 'ADMCMD_simTime' || $key === 'ADMCMD_prev';
163  }
164 
171  protected function ‪isCoreParameter($key)
172  {
173  return $key === 'id' || $key === 'type' || $key === 'no_cache' || $key === 'cHash' || $key === 'MP';
174  }
175 
182  protected function ‪isExcludedParameter($key)
183  {
184  return $this->configuration->applies(
186  $key
187  );
188  }
189 
196  protected function ‪isInCachedParametersWhiteList($key)
197  {
198  return $this->configuration->applies(
200  $key
201  );
202  }
203 
209  protected function ‪hasCachedParametersWhiteList()
210  {
211  return $this->configuration->hasData(
213  );
214  }
215 
222  protected function ‪isAllowedWithEmptyValue($key)
223  {
224  return $this->configuration->shallExcludeAllEmptyParameters()
225  || $this->configuration->applies(
227  $key
228  );
229  }
230 
236  public function ‪setConfiguration(array ‪$configuration)
237  {
238  $newConfiguration = GeneralUtility::makeInstance(
239  CacheHashConfiguration::class,
240  ‪$configuration ?? ‪$GLOBALS['TYPO3_CONF_VARS']['FE']['cacheHash'] ?? []
241  );
242  $this->configuration = $this->configuration->with($newConfiguration);
243  }
244 }
‪TYPO3\CMS\Frontend\Page\CacheHashCalculator\isAllowedWithEmptyValue
‪bool isAllowedWithEmptyValue($key)
Definition: CacheHashCalculator.php:221
‪TYPO3\CMS\Frontend\Page\CacheHashCalculator\isInCachedParametersWhiteList
‪bool isInCachedParametersWhiteList($key)
Definition: CacheHashCalculator.php:195
‪TYPO3\CMS\Frontend\Page\CacheHashCalculator\setConfiguration
‪setConfiguration(array $configuration)
Definition: CacheHashCalculator.php:235
‪TYPO3\CMS\Frontend\Page\CacheHashConfiguration\ASPECT_CACHED_PARAMETERS_WHITELIST
‪const ASPECT_CACHED_PARAMETERS_WHITELIST
Definition: CacheHashConfiguration.php:36
‪TYPO3\CMS\Frontend\Page\CacheHashConfiguration\ASPECT_REQUIRED_CACHE_HASH_PRESENCE_PARAMETERS
‪const ASPECT_REQUIRED_CACHE_HASH_PRESENCE_PARAMETERS
Definition: CacheHashConfiguration.php:39
‪TYPO3\CMS\Frontend\Page\CacheHashCalculator\__construct
‪__construct(CacheHashConfiguration $configuration=null)
Definition: CacheHashCalculator.php:35
‪TYPO3\CMS\Frontend\Page\CacheHashCalculator\hasCachedParametersWhiteList
‪bool hasCachedParametersWhiteList()
Definition: CacheHashCalculator.php:208
‪TYPO3\CMS\Frontend\Page\CacheHashCalculator\isAdminPanelParameter
‪bool isAdminPanelParameter($key)
Definition: CacheHashCalculator.php:159
‪TYPO3\CMS\Frontend\Page\CacheHashCalculator\$configuration
‪CacheHashConfiguration $configuration
Definition: CacheHashCalculator.php:28
‪TYPO3\CMS\Frontend\Page\CacheHashConfiguration\ASPECT_EXCLUDED_PARAMETERS
‪const ASPECT_EXCLUDED_PARAMETERS
Definition: CacheHashConfiguration.php:37
‪TYPO3\CMS\Frontend\Page\CacheHashConfiguration
Definition: CacheHashConfiguration.php:35
‪TYPO3\CMS\Frontend\Page
Definition: LegacyClassesForIde.php:20
‪TYPO3\CMS\Core\Utility\GeneralUtility\trimExplode
‪static string[] trimExplode($delim, $string, $removeEmptyValues=false, $limit=0)
Definition: GeneralUtility.php:1059
‪TYPO3\CMS\Core\SingletonInterface
Definition: SingletonInterface.php:23
‪TYPO3\CMS\Frontend\Page\CacheHashCalculator\isExcludedParameter
‪bool isExcludedParameter($key)
Definition: CacheHashCalculator.php:181
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Frontend\Page\CacheHashCalculator
Definition: CacheHashCalculator.php:25
‪TYPO3\CMS\Frontend\Page\CacheHashCalculator\doParametersRequireCacheHash
‪bool doParametersRequireCacheHash($queryString)
Definition: CacheHashCalculator.php:70
‪TYPO3\CMS\Frontend\Page\CacheHashConfiguration\ASPECT_EXCLUDED_PARAMETERS_IF_EMPTY
‪const ASPECT_EXCLUDED_PARAMETERS_IF_EMPTY
Definition: CacheHashConfiguration.php:38
‪TYPO3\CMS\Frontend\Page\CacheHashCalculator\getRelevantParameters
‪array getRelevantParameters($queryString)
Definition: CacheHashCalculator.php:97
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Frontend\Page\CacheHashCalculator\calculateCacheHash
‪string calculateCacheHash(array $params)
Definition: CacheHashCalculator.php:46
‪TYPO3\CMS\Frontend\Page\CacheHashCalculator\isCoreParameter
‪bool isCoreParameter($key)
Definition: CacheHashCalculator.php:170
‪TYPO3\CMS\Frontend\Page\CacheHashCalculator\generateForParameters
‪string generateForParameters($queryString)
Definition: CacheHashCalculator.php:58
‪TYPO3\CMS\Frontend\Page\CacheHashCalculator\splitQueryStringToArray
‪array splitQueryStringToArray($queryString)
Definition: CacheHashCalculator.php:134