TYPO3 CMS  TYPO3_7-6
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 
21 {
26 
30  protected $excludedParameters = [];
31 
36 
41 
45  protected $excludeAllEmptyParameters = false;
46 
50  protected $includePageId = false;
51 
55  public function __construct()
56  {
57  $this->setConfiguration($GLOBALS['TYPO3_CONF_VARS']['FE']['cacheHash']);
58  }
59 
66  public function calculateCacheHash(array $params)
67  {
68  return !empty($params) ? md5(serialize($params)) : '';
69  }
70 
77  public function generateForParameters($queryString)
78  {
79  $cacheHashParams = $this->getRelevantParameters($queryString);
80  return $this->calculateCacheHash($cacheHashParams);
81  }
82 
89  public function doParametersRequireCacheHash($queryString)
90  {
91  if (empty($this->requireCacheHashPresenceParameters)) {
92  return false;
93  }
94  $hasRequiredParameter = false;
95  $parameterNames = array_keys($this->splitQueryStringToArray($queryString));
96  foreach ($parameterNames as $parameterName) {
97  if (in_array($parameterName, $this->requireCacheHashPresenceParameters)) {
98  $hasRequiredParameter = true;
99  }
100  }
101  return $hasRequiredParameter;
102  }
103 
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 ((is_null($parameterValue) || $parameterValue === '') && !$this->isAllowedWithEmptyValue($parameterName)) {
124  continue;
125  }
126  $relevantParameters[$parameterName] = $parameterValue;
127  }
128  if (!empty($relevantParameters)) {
129  if ($this->includePageId) {
130  if (empty($parameters['id'])) {
131  throw new \RuntimeException('ID parameter needs to be passed for the cHash calculation! As a temporary not recommended workaround, you can set $GLOBALS[\'TYPO3_CONF_VARS\'][\'FE\'][\'cHashIncludePageId\'] to false to avoid this error.', 1467983513);
132  }
133  $relevantParameters['id'] = $parameters['id'];
134  }
135  // Finish and sort parameters array by keys:
136  $relevantParameters['encryptionKey'] = $GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'];
137  ksort($relevantParameters);
138  }
139  return $relevantParameters;
140  }
141 
151  protected function splitQueryStringToArray($queryString)
152  {
153  $parameters = array_filter(explode('&', ltrim($queryString, '?')));
154  $parameterArray = [];
155  foreach ($parameters as $parameter) {
156  list($parameterName, $parameterValue) = explode('=', $parameter);
157  if (trim($parameterName) === '') {
158  // This parameter cannot appear in $_GET in PHP even if its value is not empty, so it should be ignored!
159  continue;
160  }
161  $parameterArray[rawurldecode($parameterName)] = rawurldecode($parameterValue);
162  }
163  return $parameterArray;
164  }
165 
173  protected function isAdminPanelParameter($key)
174  {
175  return $key === 'ADMCMD_noBeUser' || $key === 'ADMCMD_view' || $key === 'ADMCMD_editIcons'
176  || $key === 'ADMCMD_simUser' || $key === 'ADMCMD_simTime' || $key === 'ADMCMD_previewWS'
177  || stripos($key, 'TSFE_ADMIN_PANEL') !== false && preg_match('/TSFE_ADMIN_PANEL\\[.*?\\]/', $key);
178  }
179 
186  protected function isCoreParameter($key)
187  {
188  return $key === 'id' || $key === 'type' || $key === 'no_cache' || $key === 'cHash' || $key === 'MP' || $key === 'ftu';
189  }
190 
197  protected function isExcludedParameter($key)
198  {
199  return in_array($key, $this->excludedParameters);
200  }
201 
208  protected function isInCachedParametersWhiteList($key)
209  {
210  return in_array($key, $this->cachedParametersWhiteList);
211  }
212 
218  protected function hasCachedParametersWhiteList()
219  {
220  return !empty($this->cachedParametersWhiteList);
221  }
222 
229  protected function isAllowedWithEmptyValue($key)
230  {
231  return !($this->excludeAllEmptyParameters || in_array($key, $this->excludedParametersIfEmpty));
232  }
233 
240  public function setConfiguration(array $configuration)
241  {
242  foreach ($configuration as $name => $value) {
243  $setterName = 'set' . ucfirst($name);
244  if (method_exists($this, $setterName)) {
245  $this->{$setterName}($value);
246  }
247  }
248  }
249 
254  {
255  $this->cachedParametersWhiteList = $cachedParametersWhiteList;
256  }
257 
261  protected function setIncludePageId($includePageId)
262  {
263  $this->includePageId = $includePageId;
264  }
265 
270  {
271  $this->excludeAllEmptyParameters = $excludeAllEmptyParameters;
272  }
273 
277  protected function setExcludedParameters(array $excludedParameters)
278  {
279  $this->excludedParameters = $excludedParameters;
280  }
281 
286  {
287  $this->excludedParametersIfEmpty = $excludedParametersIfEmpty;
288  }
289 
294  {
295  $this->requireCacheHashPresenceParameters = $requireCacheHashPresenceParameters;
296  }
297 }
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)