[TASK] Remove superfluous parenthesis in sysext frontend
[Packages/TYPO3.CMS.git] / typo3 / sysext / frontend / Classes / Page / CacheHashCalculator.php
1 <?php
2 namespace TYPO3\CMS\Frontend\Page;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2011 Daniel Pötzinger (poetzinger@aoemedia.de)
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 * A copy is found in the textfile GPL.txt and important notices to the license
19 * from the author is found in LICENSE.txt distributed with these scripts.
20 *
21 *
22 * This script is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * This copyright notice MUST APPEAR in all copies of the script!
28 ***************************************************************/
29 /**
30 * Logic for cHash calculation
31 *
32 * @author Daniel Pötzinger <poetzinger@aoemedia.de>
33 * @coauthor Tolleiv Nietsch <typo3@tolleiv.de>
34 */
35 class CacheHashCalculator implements \TYPO3\CMS\Core\SingletonInterface {
36
37 /**
38 * @var array Parameters that are relevant for cacheHash calculation. Optional.
39 */
40 protected $cachedParametersWhiteList = array();
41
42 /**
43 * @var array Parameters that are not relevant for cacheHash calculation.
44 */
45 protected $excludedParameters = array();
46
47 /**
48 * @var array Parameters that forces a presence of a valid cacheHash.
49 */
50 protected $requireCacheHashPresenceParameters = array();
51
52 /**
53 * @var array Parameters that need a value to be relevant for cacheHash calculation
54 */
55 protected $excludedParametersIfEmpty = array();
56
57 /**
58 * @var bool Whether to exclude all empty parameters for cacheHash calculation
59 */
60 protected $excludeAllEmptyParameters = FALSE;
61
62 /**
63 * Initialise class properties by using the relevant TYPO3 configuration
64 */
65 public function __construct() {
66 $this->setConfiguration($GLOBALS['TYPO3_CONF_VARS']['FE']['cacheHash']);
67 }
68
69 /**
70 * Calculates the cHash based on the provided parameters
71 *
72 * @param array $params Array of cHash key-value pairs
73 * @return string Hash of all the values
74 */
75 public function calculateCacheHash(array $params) {
76 return !empty($params) ? md5(serialize($params)) : '';
77 }
78
79 /**
80 * Returns the cHash based on provided query parameters and added values from internal call
81 *
82 * @param string $queryString Query-parameters: "&xxx=yyy&zzz=uuu
83 * @return string Hash of all the values
84 * @see t3lib_div::cHashParams(), t3lib_div::calculateCHash()
85 */
86 public function generateForParameters($queryString) {
87 $cacheHashParams = $this->getRelevantParameters($queryString);
88 return $this->calculateCacheHash($cacheHashParams);
89 }
90
91 /**
92 * Checks whether a parameter of the given $queryString requires cHash calculation
93 *
94 * @param string $queryString
95 * @return boolean
96 */
97 public function doParametersRequireCacheHash($queryString) {
98 if (empty($this->requireCacheHashPresenceParameters)) {
99 return FALSE;
100 }
101 $hasRequiredParameter = FALSE;
102 $parameterNames = array_keys($this->splitQueryStringToArray($queryString));
103 foreach ($parameterNames as $parameterName) {
104 if (in_array($parameterName, $this->requireCacheHashPresenceParameters)) {
105 $hasRequiredParameter = TRUE;
106 }
107 }
108 return $hasRequiredParameter;
109 }
110
111 /**
112 * Splits the input query-parameters into an array with certain parameters filtered out.
113 * Used to create the cHash value
114 *
115 * @param string $queryString Query-parameters: "&xxx=yyy&zzz=uuu
116 * @return array Array with key/value pairs of query-parameters WITHOUT a certain list of
117 * @see tslib_fe::makeCacheHash(), tslib_cObj::typoLink(), t3lib_div::calculateCHash()
118 */
119 public function getRelevantParameters($queryString) {
120 $parameters = $this->splitQueryStringToArray($queryString);
121 $relevantParameters = array();
122 foreach ($parameters as $parameterName => $parameterValue) {
123 if ($this->isAdminPanelParameter($parameterName) || $this->isExcludedParameter($parameterName) || $this->isCoreParameter($parameterName)) {
124 continue;
125 }
126 if ($this->hasCachedParametersWhiteList() && !$this->isInCachedParametersWhiteList($parameterName)) {
127 continue;
128 }
129 if ((is_null($parameterValue) || $parameterValue === '') && !$this->isAllowedWithEmptyValue($parameterName)) {
130 continue;
131 }
132 $relevantParameters[$parameterName] = $parameterValue;
133 }
134 if (!empty($relevantParameters)) {
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
142 /**
143 * Parses the query string and converts it to an array.
144 * Unlike parse_str it only creates an array with one level.
145 *
146 * e.g. foo[bar]=baz will be array('foo[bar]' => 'baz')
147 *
148 * @param $queryString
149 * @return array
150 */
151 protected function splitQueryStringToArray($queryString) {
152 $parameters = array_filter(explode('&', ltrim($queryString, '?')));
153 $parameterArray = array();
154 foreach ($parameters as $parameter) {
155 list($parameterName, $parameterValue) = explode('=', $parameter);
156 $parameterArray[rawurldecode($parameterName)] = rawurldecode($parameterValue);
157 }
158 return $parameterArray;
159 }
160
161 /**
162 * Checks whether the given parameter starts with TSFE_ADMIN_PANEL
163 * stristr check added to avoid bad performance
164 *
165 * @param string $key
166 * @return boolean
167 */
168 protected function isAdminPanelParameter($key) {
169 return stristr($key, 'TSFE_ADMIN_PANEL') !== FALSE && preg_match('/TSFE_ADMIN_PANEL\\[.*?\\]/', $key);
170 }
171
172 /**
173 * Checks whether the given parameter is a core parameter
174 *
175 * @param string $key
176 * @return boolean
177 */
178 protected function isCoreParameter($key) {
179 return \TYPO3\CMS\Core\Utility\GeneralUtility::inList('id,type,no_cache,cHash,MP,ftu', $key);
180 }
181
182 /**
183 * Checks whether the given parameter should be exluded from cHash calculation
184 *
185 * @param string $key
186 * @return boolean
187 */
188 protected function isExcludedParameter($key) {
189 return in_array($key, $this->excludedParameters);
190 }
191
192 /**
193 * Checks whether the given parameter is an exclusive parameter for cHash calculation
194 *
195 * @param string $key
196 * @return boolean
197 */
198 protected function isInCachedParametersWhiteList($key) {
199 return in_array($key, $this->cachedParametersWhiteList);
200 }
201
202 /**
203 * Checks whether cachedParametersWhiteList parameters are configured
204 *
205 * @return boolean
206 */
207 protected function hasCachedParametersWhiteList() {
208 return !empty($this->cachedParametersWhiteList);
209 }
210
211 /**
212 * Check whether the given parameter may be used even with an empty value
213 *
214 * @param $key
215 */
216 protected function isAllowedWithEmptyValue($key) {
217 return !($this->excludeAllEmptyParameters || in_array($key, $this->excludedParametersIfEmpty));
218 }
219
220 /**
221 * Loops through the configuration array and calls the accordant
222 * getters with the value.
223 *
224 * @param $configuration
225 */
226 public function setConfiguration($configuration) {
227 foreach ($configuration as $name => $value) {
228 $setterName = 'set' . ucfirst($name);
229 if (method_exists($this, $setterName)) {
230 $this->{$setterName}($value);
231 }
232 }
233 }
234
235 /**
236 * @param array $cachedParametersWhiteList
237 */
238 protected function setCachedParametersWhiteList(array $cachedParametersWhiteList) {
239 $this->cachedParametersWhiteList = $cachedParametersWhiteList;
240 }
241
242 /**
243 * @param boolean $excludeAllEmptyParameters
244 */
245 protected function setExcludeAllEmptyParameters($excludeAllEmptyParameters) {
246 $this->excludeAllEmptyParameters = $excludeAllEmptyParameters;
247 }
248
249 /**
250 * @param array $excludedParameters
251 */
252 protected function setExcludedParameters(array $excludedParameters) {
253 $this->excludedParameters = $excludedParameters;
254 }
255
256 /**
257 * @param array $excludedParametersIfEmpty
258 */
259 protected function setExcludedParametersIfEmpty(array $excludedParametersIfEmpty) {
260 $this->excludedParametersIfEmpty = $excludedParametersIfEmpty;
261 }
262
263 /**
264 * @param array $requireCacheHashPresenceParameters
265 */
266 protected function setRequireCacheHashPresenceParameters(array $requireCacheHashPresenceParameters) {
267 $this->requireCacheHashPresenceParameters = $requireCacheHashPresenceParameters;
268 }
269
270 }
271
272
273 ?>