TYPO3 CMS  TYPO3_6-2
CacheHashCalculator.php
Go to the documentation of this file.
1 <?php
3 
23 
27  protected $cachedParametersWhiteList = array();
28 
32  protected $excludedParameters = array();
33 
38 
42  protected $excludedParametersIfEmpty = array();
43 
47  protected $excludeAllEmptyParameters = FALSE;
48 
52  protected $includePageId = FALSE;
53 
57  public function __construct() {
58  $this->setConfiguration($GLOBALS['TYPO3_CONF_VARS']['FE']['cacheHash']);
59  }
60 
67  public function calculateCacheHash(array $params) {
68  return !empty($params) ? md5(serialize($params)) : '';
69  }
70 
77  public function generateForParameters($queryString) {
78  $cacheHashParams = $this->getRelevantParameters($queryString);
79  return $this->calculateCacheHash($cacheHashParams);
80  }
81 
88  public function doParametersRequireCacheHash($queryString) {
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)) {
96  $hasRequiredParameter = TRUE;
97  }
98  }
99  return $hasRequiredParameter;
100  }
101 
110  public function getRelevantParameters($queryString) {
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  if ($this->includePageId) {
127  if (empty($parameters['id'])) {
128  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);
129  }
130  $relevantParameters['id'] = $parameters['id'];
131  }
132  // Finish and sort parameters array by keys:
133  $relevantParameters['encryptionKey'] = $GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'];
134  ksort($relevantParameters);
135  }
136  return $relevantParameters;
137  }
138 
148  protected function splitQueryStringToArray($queryString) {
149  $parameters = array_filter(explode('&', ltrim($queryString, '?')));
150  $parameterArray = array();
151  foreach ($parameters as $parameter) {
152  list($parameterName, $parameterValue) = explode('=', $parameter);
153  if (trim($parameterName) === '') {
154  // This parameter cannot appear in $_GET in PHP even if its value is not empty, so it should be ignored!
155  continue;
156  }
157  $parameterArray[rawurldecode($parameterName)] = rawurldecode($parameterValue);
158  }
159  return $parameterArray;
160  }
161 
169  protected function isAdminPanelParameter($key) {
170  return $key === 'ADMCMD_noBeUser' || $key === 'ADMCMD_view' || $key === 'ADMCMD_editIcons'
171  || $key === 'ADMCMD_simUser' || $key === 'ADMCMD_simTime' || $key === 'ADMCMD_previewWS'
172  || stripos($key, 'TSFE_ADMIN_PANEL') !== FALSE && preg_match('/TSFE_ADMIN_PANEL\\[.*?\\]/', $key);
173  }
174 
181  protected function isCoreParameter($key) {
182  return \TYPO3\CMS\Core\Utility\GeneralUtility::inList('id,type,no_cache,cHash,MP,ftu', $key);
183  }
184 
191  protected function isExcludedParameter($key) {
192  return in_array($key, $this->excludedParameters);
193  }
194 
201  protected function isInCachedParametersWhiteList($key) {
202  return in_array($key, $this->cachedParametersWhiteList);
203  }
204 
210  protected function hasCachedParametersWhiteList() {
211  return !empty($this->cachedParametersWhiteList);
212  }
213 
219  protected function isAllowedWithEmptyValue($key) {
220  return !($this->excludeAllEmptyParameters || in_array($key, $this->excludedParametersIfEmpty));
221  }
222 
229  public function setConfiguration($configuration) {
230  foreach ($configuration as $name => $value) {
231  $setterName = 'set' . ucfirst($name);
232  if (method_exists($this, $setterName)) {
233  $this->{$setterName}($value);
234  }
235  }
236  }
237 
242  $this->cachedParametersWhiteList = $cachedParametersWhiteList;
243  }
244 
248  protected function setIncludePageId($includePageId) {
249  $this->includePageId = $includePageId;
250  }
251 
256  $this->excludeAllEmptyParameters = $excludeAllEmptyParameters;
257  }
258 
262  protected function setExcludedParameters(array $excludedParameters) {
263  $this->excludedParameters = $excludedParameters;
264  }
265 
270  $this->excludedParametersIfEmpty = $excludedParametersIfEmpty;
271  }
272 
277  $this->requireCacheHashPresenceParameters = $requireCacheHashPresenceParameters;
278  }
279 
280 }
$parameters
Definition: FileDumpEID.php:15
setExcludedParametersIfEmpty(array $excludedParametersIfEmpty)
setExcludeAllEmptyParameters($excludeAllEmptyParameters)
if(!defined('TYPO3_MODE')) $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['logoff_pre_processing'][]
setRequireCacheHashPresenceParameters(array $requireCacheHashPresenceParameters)
setCachedParametersWhiteList(array $cachedParametersWhiteList)