TYPO3 CMS  TYPO3_8-7
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 
18 
23 {
28 
32  protected $excludedParameters = [];
33 
38 
43 
47  protected $excludeAllEmptyParameters = false;
48 
52  public function __construct()
53  {
54  $this->setConfiguration($GLOBALS['TYPO3_CONF_VARS']['FE']['cacheHash']);
55  }
56 
63  public function calculateCacheHash(array $params)
64  {
65  return !empty($params) ? md5(serialize($params)) : '';
66  }
67 
75  public function generateForParameters($queryString)
76  {
77  $cacheHashParams = $this->getRelevantParameters($queryString);
78  return $this->calculateCacheHash($cacheHashParams);
79  }
80 
87  public function doParametersRequireCacheHash($queryString)
88  {
89  if (empty($this->requireCacheHashPresenceParameters)) {
90  return false;
91  }
92  $hasRequiredParameter = false;
93  $parameterNames = array_keys($this->splitQueryStringToArray($queryString));
94  foreach ($parameterNames as $parameterName) {
95  if (in_array($parameterName, $this->requireCacheHashPresenceParameters, true)) {
96  $hasRequiredParameter = true;
97  break;
98  }
99  }
100  return $hasRequiredParameter;
101  }
102 
112  public function getRelevantParameters($queryString)
113  {
114  $parameters = $this->splitQueryStringToArray($queryString);
115  $relevantParameters = [];
116  foreach ($parameters as $parameterName => $parameterValue) {
117  if ($this->isAdminPanelParameter($parameterName) || $this->isExcludedParameter($parameterName) || $this->isCoreParameter($parameterName)) {
118  continue;
119  }
120  if ($this->hasCachedParametersWhiteList() && !$this->isInCachedParametersWhiteList($parameterName)) {
121  continue;
122  }
123  if (($parameterValue === null || $parameterValue === '') && !$this->isAllowedWithEmptyValue($parameterName)) {
124  continue;
125  }
126  $relevantParameters[$parameterName] = $parameterValue;
127  }
128  if (!empty($relevantParameters)) {
129  if (empty($parameters['id'])) {
130  throw new \RuntimeException('ID parameter needs to be passed for the cHash calculation!', 1467983513);
131  }
132  $relevantParameters['id'] = $parameters['id'];
133  // Finish and sort parameters array by keys:
134  $relevantParameters['encryptionKey'] = $GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'];
135  ksort($relevantParameters);
136  }
137  return $relevantParameters;
138  }
139 
149  protected function splitQueryStringToArray($queryString)
150  {
151  $parameters = array_filter(explode('&', ltrim($queryString, '?')));
152  $parameterArray = [];
153  foreach ($parameters as $parameter) {
154  list($parameterName, $parameterValue) = explode('=', $parameter);
155  if (trim($parameterName) === '') {
156  // This parameter cannot appear in $_GET in PHP even if its value is not empty, so it should be ignored!
157  continue;
158  }
159  $parameterArray[rawurldecode($parameterName)] = rawurldecode($parameterValue);
160  }
161  return $parameterArray;
162  }
163 
171  protected function isAdminPanelParameter($key)
172  {
173  return $key === 'ADMCMD_noBeUser' || $key === 'ADMCMD_view' || $key === 'ADMCMD_editIcons'
174  || $key === 'ADMCMD_simUser' || $key === 'ADMCMD_simTime' || $key === 'ADMCMD_previewWS'
175  || stripos($key, 'TSFE_ADMIN_PANEL') !== false && preg_match('/TSFE_ADMIN_PANEL\\[.*?\\]/', $key);
176  }
177 
184  protected function isCoreParameter($key)
185  {
186  return $key === 'id' || $key === 'type' || $key === 'no_cache' || $key === 'cHash' || $key === 'MP' || $key === 'ftu';
187  }
188 
195  protected function isExcludedParameter($key)
196  {
197  return in_array($key, $this->excludedParameters, true);
198  }
199 
206  protected function isInCachedParametersWhiteList($key)
207  {
208  return in_array($key, $this->cachedParametersWhiteList, true);
209  }
210 
216  protected function hasCachedParametersWhiteList()
217  {
218  return !empty($this->cachedParametersWhiteList);
219  }
220 
227  protected function isAllowedWithEmptyValue($key)
228  {
229  return !($this->excludeAllEmptyParameters || in_array($key, $this->excludedParametersIfEmpty, true));
230  }
231 
238  public function setConfiguration(array $configuration)
239  {
240  foreach ($configuration as $name => $value) {
241  $setterName = 'set' . ucfirst($name);
242  if (method_exists($this, $setterName)) {
243  $this->{$setterName}($value);
244  }
245  }
246  }
247 
252  {
253  $this->cachedParametersWhiteList = $cachedParametersWhiteList;
254  }
255 
260  {
261  $this->excludeAllEmptyParameters = $excludeAllEmptyParameters;
262  }
263 
267  protected function setExcludedParameters(array $excludedParameters)
268  {
269  $this->excludedParameters = $excludedParameters;
270  }
271 
276  {
277  $this->excludedParametersIfEmpty = $excludedParametersIfEmpty;
278  }
279 
284  {
285  $this->requireCacheHashPresenceParameters = $requireCacheHashPresenceParameters;
286  }
287 }
setExcludedParametersIfEmpty(array $excludedParametersIfEmpty)
setExcludeAllEmptyParameters($excludeAllEmptyParameters)
setRequireCacheHashPresenceParameters(array $requireCacheHashPresenceParameters)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']
setCachedParametersWhiteList(array $cachedParametersWhiteList)