TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
CacheHashCalculator.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Frontend\Page;
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  protected $includePageId = false;
53 
57  public function __construct()
58  {
59  $this->setConfiguration($GLOBALS['TYPO3_CONF_VARS']['FE']['cacheHash']);
60  }
61 
68  public function calculateCacheHash(array $params)
69  {
70  return !empty($params) ? md5(serialize($params)) : '';
71  }
72 
80  public function generateForParameters($queryString)
81  {
82  $cacheHashParams = $this->getRelevantParameters($queryString);
83  return $this->calculateCacheHash($cacheHashParams);
84  }
85 
92  public function doParametersRequireCacheHash($queryString)
93  {
94  if (empty($this->requireCacheHashPresenceParameters)) {
95  return false;
96  }
97  $hasRequiredParameter = false;
98  $parameterNames = array_keys($this->splitQueryStringToArray($queryString));
99  foreach ($parameterNames as $parameterName) {
100  if (in_array($parameterName, $this->requireCacheHashPresenceParameters, true)) {
101  $hasRequiredParameter = true;
102  break;
103  }
104  }
105  return $hasRequiredParameter;
106  }
107 
117  public function getRelevantParameters($queryString)
118  {
119  $parameters = $this->splitQueryStringToArray($queryString);
120  $relevantParameters = [];
121  foreach ($parameters as $parameterName => $parameterValue) {
122  if ($this->isAdminPanelParameter($parameterName) || $this->isExcludedParameter($parameterName) || $this->isCoreParameter($parameterName)) {
123  continue;
124  }
125  if ($this->hasCachedParametersWhiteList() && !$this->isInCachedParametersWhiteList($parameterName)) {
126  continue;
127  }
128  if (($parameterValue === null || $parameterValue === '') && !$this->isAllowedWithEmptyValue($parameterName)) {
129  continue;
130  }
131  $relevantParameters[$parameterName] = $parameterValue;
132  }
133  if (!empty($relevantParameters)) {
134  if ($this->includePageId) {
135  if (empty($parameters['id'])) {
136  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);
137  }
138  $relevantParameters['id'] = $parameters['id'];
139  }
140  // Finish and sort parameters array by keys:
141  $relevantParameters['encryptionKey'] = $GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'];
142  ksort($relevantParameters);
143  }
144  return $relevantParameters;
145  }
146 
156  protected function splitQueryStringToArray($queryString)
157  {
158  $parameters = array_filter(explode('&', ltrim($queryString, '?')));
159  $parameterArray = [];
160  foreach ($parameters as $parameter) {
161  list($parameterName, $parameterValue) = explode('=', $parameter);
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 stripos($key, 'TSFE_ADMIN_PANEL') !== false && preg_match('/TSFE_ADMIN_PANEL\\[.*?\\]/', $key);
181  }
182 
189  protected function isCoreParameter($key)
190  {
191  return $key === 'id' || $key === 'type' || $key === 'no_cache' || $key === 'cHash' || $key === 'MP' || $key === 'ftu';
192  }
193 
200  protected function isExcludedParameter($key)
201  {
202  return in_array($key, $this->excludedParameters, true);
203  }
204 
211  protected function isInCachedParametersWhiteList($key)
212  {
213  return in_array($key, $this->cachedParametersWhiteList, true);
214  }
215 
221  protected function hasCachedParametersWhiteList()
222  {
223  return !empty($this->cachedParametersWhiteList);
224  }
225 
232  protected function isAllowedWithEmptyValue($key)
233  {
234  return !($this->excludeAllEmptyParameters || in_array($key, $this->excludedParametersIfEmpty, true));
235  }
236 
243  public function setConfiguration(array $configuration)
244  {
245  foreach ($configuration as $name => $value) {
246  $setterName = 'set' . ucfirst($name);
247  if (method_exists($this, $setterName)) {
248  $this->{$setterName}($value);
249  }
250  }
251  }
252 
257  {
258  $this->cachedParametersWhiteList = $cachedParametersWhiteList;
259  }
260 
264  protected function setIncludePageId($includePageId)
265  {
266  $this->includePageId = $includePageId;
267  }
268 
273  {
274  $this->excludeAllEmptyParameters = $excludeAllEmptyParameters;
275  }
276 
280  protected function setExcludedParameters(array $excludedParameters)
281  {
282  $this->excludedParameters = $excludedParameters;
283  }
284 
289  {
290  $this->excludedParametersIfEmpty = $excludedParametersIfEmpty;
291  }
292 
297  {
298  $this->requireCacheHashPresenceParameters = $requireCacheHashPresenceParameters;
299  }
300 }
setExcludeAllEmptyParameters($excludeAllEmptyParameters)
setRequireCacheHashPresenceParameters(array $requireCacheHashPresenceParameters)
setCachedParametersWhiteList(array $cachedParametersWhiteList)
setExcludedParametersIfEmpty(array $excludedParametersIfEmpty)
if(TYPO3_MODE=== 'BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']