TYPO3  7.6
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 {
27  protected $cachedParametersWhiteList = array();
28 
32  protected $excludedParameters = array();
33 
38 
42  protected $excludedParametersIfEmpty = array();
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 
74  public function generateForParameters($queryString)
75  {
76  $cacheHashParams = $this->getRelevantParameters($queryString);
77  return $this->calculateCacheHash($cacheHashParams);
78  }
79 
86  public function doParametersRequireCacheHash($queryString)
87  {
88  if (empty($this->requireCacheHashPresenceParameters)) {
89  return false;
90  }
91  $hasRequiredParameter = false;
92  $parameterNames = array_keys($this->splitQueryStringToArray($queryString));
93  foreach ($parameterNames as $parameterName) {
94  if (in_array($parameterName, $this->requireCacheHashPresenceParameters)) {
95  $hasRequiredParameter = true;
96  }
97  }
98  return $hasRequiredParameter;
99  }
100 
109  public function getRelevantParameters($queryString)
110  {
111  $parameters = $this->splitQueryStringToArray($queryString);
112  $relevantParameters = array();
113  foreach ($parameters as $parameterName => $parameterValue) {
114  if ($this->isAdminPanelParameter($parameterName) || $this->isExcludedParameter($parameterName) || $this->isCoreParameter($parameterName)) {
115  continue;
116  }
117  if ($this->hasCachedParametersWhiteList() && !$this->isInCachedParametersWhiteList($parameterName)) {
118  continue;
119  }
120  if ((is_null($parameterValue) || $parameterValue === '') && !$this->isAllowedWithEmptyValue($parameterName)) {
121  continue;
122  }
123  $relevantParameters[$parameterName] = $parameterValue;
124  }
125  if (!empty($relevantParameters)) {
126  // Finish and sort parameters array by keys:
127  $relevantParameters['encryptionKey'] = $GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'];
128  ksort($relevantParameters);
129  }
130  return $relevantParameters;
131  }
132 
142  protected function splitQueryStringToArray($queryString)
143  {
144  $parameters = array_filter(explode('&', ltrim($queryString, '?')));
145  $parameterArray = array();
146  foreach ($parameters as $parameter) {
147  list($parameterName, $parameterValue) = explode('=', $parameter);
148  $parameterArray[rawurldecode($parameterName)] = rawurldecode($parameterValue);
149  }
150  return $parameterArray;
151  }
152 
160  protected function isAdminPanelParameter($key)
161  {
162  return stristr($key, 'TSFE_ADMIN_PANEL') !== false && preg_match('/TSFE_ADMIN_PANEL\\[.*?\\]/', $key);
163  }
164 
171  protected function isCoreParameter($key)
172  {
173  return GeneralUtility::inList('id,type,no_cache,cHash,MP,ftu', $key);
174  }
175 
182  protected function isExcludedParameter($key)
183  {
184  return in_array($key, $this->excludedParameters);
185  }
186 
193  protected function isInCachedParametersWhiteList($key)
194  {
195  return in_array($key, $this->cachedParametersWhiteList);
196  }
197 
203  protected function hasCachedParametersWhiteList()
204  {
205  return !empty($this->cachedParametersWhiteList);
206  }
207 
213  protected function isAllowedWithEmptyValue($key)
214  {
215  return !($this->excludeAllEmptyParameters || in_array($key, $this->excludedParametersIfEmpty));
216  }
217 
224  public function setConfiguration($configuration)
225  {
226  foreach ($configuration as $name => $value) {
227  $setterName = 'set' . ucfirst($name);
228  if (method_exists($this, $setterName)) {
229  $this->{$setterName}($value);
230  }
231  }
232  }
233 
238  {
239  $this->cachedParametersWhiteList = $cachedParametersWhiteList;
240  }
241 
246  {
247  $this->excludeAllEmptyParameters = $excludeAllEmptyParameters;
248  }
249 
253  protected function setExcludedParameters(array $excludedParameters)
254  {
255  $this->excludedParameters = $excludedParameters;
256  }
257 
262  {
263  $this->excludedParametersIfEmpty = $excludedParametersIfEmpty;
264  }
265 
270  {
271  $this->requireCacheHashPresenceParameters = $requireCacheHashPresenceParameters;
272  }
273 }