‪TYPO3CMS  9.5
CacheHashCalculator.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
19 
24 {
28  protected ‪$cachedParametersWhiteList = [];
29 
33  protected ‪$excludedParameters = [];
34 
39 
44 
48  protected ‪$excludeAllEmptyParameters = false;
49 
55  public function ‪__construct(array $configuration = null)
56  {
57  $configuration = $configuration ?? ‪$GLOBALS['TYPO3_CONF_VARS']['FE']['cacheHash'] ?? [];
58  $this->‪setConfiguration($configuration);
59  }
60 
67  public function ‪calculateCacheHash(array $params)
68  {
69  return !empty($params) ? md5(serialize($params)) : '';
70  }
71 
79  public function ‪generateForParameters($queryString)
80  {
81  $cacheHashParams = $this->‪getRelevantParameters($queryString);
82  return $this->‪calculateCacheHash($cacheHashParams);
83  }
84 
91  public function ‪doParametersRequireCacheHash($queryString)
92  {
93  if (empty($this->requireCacheHashPresenceParameters)) {
94  return false;
95  }
96  $hasRequiredParameter = false;
97  $parameterNames = array_keys($this->‪splitQueryStringToArray($queryString));
98  foreach ($parameterNames as $parameterName) {
99  if (in_array($parameterName, $this->requireCacheHashPresenceParameters, true)) {
100  $hasRequiredParameter = true;
101  break;
102  }
103  }
104  return $hasRequiredParameter;
105  }
106 
116  public function ‪getRelevantParameters($queryString)
117  {
118  $parameters = $this->‪splitQueryStringToArray($queryString);
119  $relevantParameters = [];
120  foreach ($parameters as $parameterName => $parameterValue) {
121  if ($this->‪isAdminPanelParameter($parameterName) || $this->‪isExcludedParameter($parameterName) || $this->‪isCoreParameter($parameterName)) {
122  continue;
123  }
124  if ($this->‪hasCachedParametersWhiteList() && !$this->‪isInCachedParametersWhiteList($parameterName)) {
125  continue;
126  }
127  if (($parameterValue === null || $parameterValue === '') && !$this->‪isAllowedWithEmptyValue($parameterName)) {
128  continue;
129  }
130  $relevantParameters[$parameterName] = $parameterValue;
131  }
132  if (!empty($relevantParameters)) {
133  if (empty($parameters['id'])) {
134  throw new \RuntimeException('ID parameter needs to be passed for the cHash calculation!', 1467983513);
135  }
136  $relevantParameters['id'] = $parameters['id'];
137  // Finish and sort parameters array by keys:
138  $relevantParameters['encryptionKey'] = ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'];
139  ksort($relevantParameters);
140  }
141  return $relevantParameters;
142  }
143 
153  protected function ‪splitQueryStringToArray($queryString)
154  {
155  $parameters = array_filter(explode('&', ltrim($queryString, '?')));
156  $parameterArray = [];
157  foreach ($parameters as $parameter) {
158  // should not remove empty values with trimExplode, otherwise cases like &=value, value is used as parameterName.
159  $parts = GeneralUtility::trimExplode('=', $parameter, false);
160  $parameterName = $parts[0];
161  $parameterValue = $parts[1] ?? '';
162  if (trim($parameterName) === '') {
163  // This parameter cannot appear in $_GET in PHP even if its value is not empty, so it should be ignored!
164  continue;
165  }
166  $parameterArray[rawurldecode($parameterName)] = rawurldecode($parameterValue);
167  }
168  return $parameterArray;
169  }
170 
178  protected function ‪isAdminPanelParameter($key)
179  {
180  return $key === 'ADMCMD_noBeUser' || $key === 'ADMCMD_view' || $key === 'ADMCMD_editIcons'
181  || $key === 'ADMCMD_simUser' || $key === 'ADMCMD_simTime' || $key === 'ADMCMD_prev'
182  || stripos($key, 'TSFE_ADMIN_PANEL') !== false && preg_match('/TSFE_ADMIN_PANEL\\[.*?\\]/', $key);
183  }
184 
191  protected function ‪isCoreParameter($key)
192  {
193  return $key === 'id' || $key === 'type' || $key === 'no_cache' || $key === 'cHash' || $key === 'MP' || $key === 'ftu';
194  }
195 
202  protected function ‪isExcludedParameter($key)
203  {
204  return in_array($key, $this->excludedParameters, true);
205  }
206 
213  protected function ‪isInCachedParametersWhiteList($key)
214  {
215  return in_array($key, $this->cachedParametersWhiteList, true);
216  }
217 
223  protected function ‪hasCachedParametersWhiteList()
224  {
225  return !empty($this->cachedParametersWhiteList);
226  }
227 
234  protected function ‪isAllowedWithEmptyValue($key)
235  {
236  return !($this->excludeAllEmptyParameters || in_array($key, $this->excludedParametersIfEmpty, true));
237  }
238 
245  public function ‪setConfiguration(array $configuration)
246  {
247  foreach ($configuration as $name => $value) {
248  $setterName = 'set' . ucfirst($name);
249  if (method_exists($this, $setterName)) {
250  $this->{$setterName}($value);
251  }
252  }
253  }
254 
259  {
260  $this->cachedParametersWhiteList = ‪$cachedParametersWhiteList;
261  }
262 
267  {
268  $this->excludeAllEmptyParameters = ‪$excludeAllEmptyParameters;
269  }
270 
274  protected function ‪setExcludedParameters(array ‪$excludedParameters)
275  {
276  $this->excludedParameters = ‪$excludedParameters;
277  }
278 
283  {
284  $this->excludedParametersIfEmpty = ‪$excludedParametersIfEmpty;
285  }
286 
291  {
292  $this->requireCacheHashPresenceParameters = ‪$requireCacheHashPresenceParameters;
293  }
294 }
‪TYPO3\CMS\Frontend\Page\CacheHashCalculator\isAllowedWithEmptyValue
‪bool isAllowedWithEmptyValue($key)
Definition: CacheHashCalculator.php:229
‪TYPO3\CMS\Frontend\Page\CacheHashCalculator\isInCachedParametersWhiteList
‪bool isInCachedParametersWhiteList($key)
Definition: CacheHashCalculator.php:208
‪TYPO3\CMS\Frontend\Page\CacheHashCalculator\__construct
‪__construct(array $configuration=null)
Definition: CacheHashCalculator.php:50
‪TYPO3\CMS\Frontend\Page\CacheHashCalculator\setExcludedParametersIfEmpty
‪setExcludedParametersIfEmpty(array $excludedParametersIfEmpty)
Definition: CacheHashCalculator.php:277
‪TYPO3\CMS\Frontend\Page\CacheHashCalculator\setConfiguration
‪setConfiguration(array $configuration)
Definition: CacheHashCalculator.php:240
‪TYPO3\CMS\Frontend\Page\CacheHashCalculator\$excludeAllEmptyParameters
‪bool $excludeAllEmptyParameters
Definition: CacheHashCalculator.php:43
‪TYPO3\CMS\Frontend\Page\CacheHashCalculator\$excludedParametersIfEmpty
‪array $excludedParametersIfEmpty
Definition: CacheHashCalculator.php:39
‪TYPO3\CMS\Frontend\Page\CacheHashCalculator\setCachedParametersWhiteList
‪setCachedParametersWhiteList(array $cachedParametersWhiteList)
Definition: CacheHashCalculator.php:253
‪TYPO3\CMS\Frontend\Page\CacheHashCalculator\setRequireCacheHashPresenceParameters
‪setRequireCacheHashPresenceParameters(array $requireCacheHashPresenceParameters)
Definition: CacheHashCalculator.php:285
‪TYPO3\CMS\Frontend\Page\CacheHashCalculator\$requireCacheHashPresenceParameters
‪array $requireCacheHashPresenceParameters
Definition: CacheHashCalculator.php:35
‪TYPO3\CMS\Frontend\Page\CacheHashCalculator\hasCachedParametersWhiteList
‪bool hasCachedParametersWhiteList()
Definition: CacheHashCalculator.php:218
‪TYPO3\CMS\Frontend\Page\CacheHashCalculator\isAdminPanelParameter
‪bool isAdminPanelParameter($key)
Definition: CacheHashCalculator.php:173
‪TYPO3\CMS\Frontend\Page\CacheHashCalculator\$excludedParameters
‪array $excludedParameters
Definition: CacheHashCalculator.php:31
‪TYPO3\CMS\Frontend\Page\CacheHashCalculator\setExcludeAllEmptyParameters
‪setExcludeAllEmptyParameters($excludeAllEmptyParameters)
Definition: CacheHashCalculator.php:261
‪TYPO3\CMS\Frontend\Page\CacheHashCalculator\$cachedParametersWhiteList
‪array $cachedParametersWhiteList
Definition: CacheHashCalculator.php:27
‪TYPO3\CMS\Frontend\Page
Definition: CacheHashCalculator.php:2
‪TYPO3\CMS\Core\SingletonInterface
Definition: SingletonInterface.php:22
‪TYPO3\CMS\Frontend\Page\CacheHashCalculator\isExcludedParameter
‪bool isExcludedParameter($key)
Definition: CacheHashCalculator.php:197
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Frontend\Page\CacheHashCalculator
Definition: CacheHashCalculator.php:24
‪TYPO3\CMS\Frontend\Page\CacheHashCalculator\doParametersRequireCacheHash
‪bool doParametersRequireCacheHash($queryString)
Definition: CacheHashCalculator.php:86
‪TYPO3\CMS\Frontend\Page\CacheHashCalculator\getRelevantParameters
‪array getRelevantParameters($queryString)
Definition: CacheHashCalculator.php:111
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Frontend\Page\CacheHashCalculator\setExcludedParameters
‪setExcludedParameters(array $excludedParameters)
Definition: CacheHashCalculator.php:269
‪TYPO3\CMS\Frontend\Page\CacheHashCalculator\calculateCacheHash
‪string calculateCacheHash(array $params)
Definition: CacheHashCalculator.php:62
‪TYPO3\CMS\Frontend\Page\CacheHashCalculator\isCoreParameter
‪bool isCoreParameter($key)
Definition: CacheHashCalculator.php:186
‪TYPO3\CMS\Frontend\Page\CacheHashCalculator\generateForParameters
‪string generateForParameters($queryString)
Definition: CacheHashCalculator.php:74
‪TYPO3\CMS\Frontend\Page\CacheHashCalculator\splitQueryStringToArray
‪array splitQueryStringToArray($queryString)
Definition: CacheHashCalculator.php:148