bbd4c7aae58ed308a88848d2036d2e51f26a4462
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Configuration / TypoScript / ConditionMatching / AbstractConditionMatcher.php
1 <?php
2 namespace TYPO3\CMS\Core\Configuration\TypoScript\ConditionMatching;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2009-2013 Oliver Hader <oliver@typo3.org>
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 use TYPO3\CMS\Core\Utility\GeneralUtility;
31
32 /**
33 * Matching TypoScript conditions
34 *
35 * Used with the TypoScript parser.
36 * Matches browserinfo, IPnumbers for use with templates
37 *
38 * @author Oliver Hader <oliver@typo3.org>
39 */
40 abstract class AbstractConditionMatcher {
41
42 /**
43 * Id of the current page.
44 *
45 * @var integer
46 */
47 protected $pageId;
48
49 /**
50 * The rootline for the current page.
51 *
52 * @var array
53 */
54 protected $rootline;
55
56 /**
57 * Whether to simulate the behaviour and match all conditions
58 * (used in TypoScript object browser).
59 *
60 * @var boolean
61 */
62 protected $simulateMatchResult = FALSE;
63
64 /**
65 * Whether to simulat the behaviour and match specific conditions
66 * (used in TypoScript object browser).
67 *
68 * @var array
69 */
70 protected $simulateMatchConditions = array();
71
72 /**
73 * Sets the id of the page to evaluate conditions for.
74 *
75 * @param integer $pageId Id of the page (must be positive)
76 * @return void
77 */
78 public function setPageId($pageId) {
79 if (is_integer($pageId) && $pageId > 0) {
80 $this->pageId = $pageId;
81 }
82 }
83
84 /**
85 * Gets the id of the page to evaluate conditions for.
86 *
87 * @return integer Id of the page
88 */
89 public function getPageId() {
90 return $this->pageId;
91 }
92
93 /**
94 * Sets the rootline.
95 *
96 * @param array $rootline The rootline to be used for matching (must have elements)
97 * @return void
98 */
99 public function setRootline(array $rootline) {
100 if (count($rootline)) {
101 $this->rootline = $rootline;
102 }
103 }
104
105 /**
106 * Gets the rootline.
107 *
108 * @return array The rootline to be used for matching
109 */
110 public function getRootline() {
111 return $this->rootline;
112 }
113
114 /**
115 * Sets whether to simulate the behaviour and match all conditions.
116 *
117 * @param boolean $simulateMatchResult Whether to simulate positive matches
118 * @return void
119 */
120 public function setSimulateMatchResult($simulateMatchResult) {
121 if (is_bool($simulateMatchResult)) {
122 $this->simulateMatchResult = $simulateMatchResult;
123 }
124 }
125
126 /**
127 * Sets whether to simulate the behaviour and match specific conditions.
128 *
129 * @param array $simulateMatchConditions Conditions to simulate a match for
130 * @return void
131 */
132 public function setSimulateMatchConditions(array $simulateMatchConditions) {
133 $this->simulateMatchConditions = $simulateMatchConditions;
134 }
135
136 /**
137 * Normalizes an expression and removes the first and last square bracket.
138 * + OR normalization: "...]OR[...", "...]||[...", "...][..." --> "...]||[..."
139 * + AND normalization: "...]AND[...", "...]&&[..." --> "...]&&[..."
140 *
141 * @param string $expression The expression to be normalized (e.g. "[A] && [B] OR [C]")
142 * @return string The normalized expression (e.g. "[A]&&[B]||[C]")
143 */
144 protected function normalizeExpression($expression) {
145 $normalizedExpression = preg_replace(array(
146 '/\\]\\s*(OR|\\|\\|)?\\s*\\[/i',
147 '/\\]\\s*(AND|&&)\\s*\\[/i'
148 ), array(
149 ']||[',
150 ']&&['
151 ), trim($expression));
152 return $normalizedExpression;
153 }
154
155 /**
156 * Matches a TypoScript condition expression.
157 *
158 * @param string $expression The expression to match
159 * @return boolean Whether the expression matched
160 */
161 public function match($expression) {
162 // Return directly if result should be simulated:
163 if ($this->simulateMatchResult) {
164 return $this->simulateMatchResult;
165 }
166 // Return directly if matching for specific condition is simulated only:
167 if (count($this->simulateMatchConditions)) {
168 return in_array($expression, $this->simulateMatchConditions);
169 }
170 // Sets the current pageId if not defined yet:
171 if (!isset($this->pageId)) {
172 $this->pageId = $this->determinePageId();
173 }
174 // Sets the rootline if not defined yet:
175 if (!isset($this->rootline)) {
176 $this->rootline = $this->determineRootline();
177 }
178 $result = FALSE;
179 $normalizedExpression = $this->normalizeExpression($expression);
180 // First and last character must be square brackets (e.g. "[A]&&[B]":
181 if (substr($normalizedExpression, 0, 1) === '[' && substr($normalizedExpression, -1) === ']') {
182 $innerExpression = substr($normalizedExpression, 1, -1);
183 $orParts = explode(']||[', $innerExpression);
184 foreach ($orParts as $orPart) {
185 $andParts = explode(']&&[', $orPart);
186 foreach ($andParts as $andPart) {
187 $result = $this->evaluateCondition($andPart);
188 // If condition in AND context fails, the whole block is FALSE:
189 if ($result === FALSE) {
190 break;
191 }
192 }
193 // If condition in OR context succeeds, the whole expression is TRUE:
194 if ($result === TRUE) {
195 break;
196 }
197 }
198 }
199 return $result;
200 }
201
202 /**
203 * Evaluates a TypoScript condition given as input, eg. "[browser=net][...(other conditions)...]"
204 *
205 * @param string $key The condition to match against its criterias.
206 * @param string $value
207 * @return mixed Returns TRUE or FALSE based on the evaluation
208 */
209 protected function evaluateConditionCommon($key, $value) {
210 if (GeneralUtility::inList('browser,version,system,useragent', strtolower($key))) {
211 $browserInfo = $this->getBrowserInfo(GeneralUtility::getIndpEnv('HTTP_USER_AGENT'));
212 }
213 $keyParts = GeneralUtility::trimExplode('|', $key);
214 switch ($keyParts[0]) {
215 case 'browser':
216 $values = GeneralUtility::trimExplode(',', $value, TRUE);
217 // take all identified browsers into account, eg chrome deliver
218 // webkit=>532.5, chrome=>4.1, safari=>532.5
219 // so comparing string will be
220 // "webkit532.5 chrome4.1 safari532.5"
221 $all = '';
222 foreach ($browserInfo['all'] as $key => $value) {
223 $all .= $key . $value . ' ';
224 }
225 foreach ($values as $test) {
226 if (stripos($all, $test) !== FALSE) {
227 return TRUE;
228 }
229 }
230 break;
231 case 'version':
232 $values = GeneralUtility::trimExplode(',', $value, TRUE);
233 foreach ($values as $test) {
234 if (strcspn($test, '=<>') == 0) {
235 switch (substr($test, 0, 1)) {
236 case '=':
237 if (doubleval(substr($test, 1)) == $browserInfo['version']) {
238 return TRUE;
239 }
240 break;
241 case '<':
242 if (doubleval(substr($test, 1)) > $browserInfo['version']) {
243 return TRUE;
244 }
245 break;
246 case '>':
247 if (doubleval(substr($test, 1)) < $browserInfo['version']) {
248 return TRUE;
249 }
250 break;
251 }
252 } elseif (strpos(' ' . $browserInfo['version'], $test) == 1) {
253 return TRUE;
254 }
255 }
256 break;
257 case 'system':
258 $values = GeneralUtility::trimExplode(',', $value, TRUE);
259 // Take all identified systems into account, e.g. mac for iOS, Linux
260 // for android and Windows NT for Windows XP
261 $allSystems .= ' ' . implode(' ', $browserInfo['all_systems']);
262 foreach ($values as $test) {
263 if (stripos($allSystems, $test) !== FALSE) {
264 return TRUE;
265 }
266 }
267 break;
268 case 'device':
269 if (!isset($this->deviceInfo)) {
270 $this->deviceInfo = $this->getDeviceType(GeneralUtility::getIndpEnv('HTTP_USER_AGENT'));
271 }
272 $values = GeneralUtility::trimExplode(',', $value, TRUE);
273 foreach ($values as $test) {
274 if ($this->deviceInfo == $test) {
275 return TRUE;
276 }
277 }
278 break;
279 case 'useragent':
280 $test = trim($value);
281 if (strlen($test)) {
282 return $this->searchStringWildcard($browserInfo['useragent'], $test);
283 }
284 break;
285 case 'language':
286 $values = GeneralUtility::trimExplode(',', $value, TRUE);
287 foreach ($values as $test) {
288 if (preg_match('/^\\*.+\\*$/', $test)) {
289 $allLanguages = preg_split('/[,;]/', GeneralUtility::getIndpEnv('HTTP_ACCEPT_LANGUAGE'));
290 if (in_array(substr($test, 1, -1), $allLanguages)) {
291 return TRUE;
292 }
293 } elseif (GeneralUtility::getIndpEnv('HTTP_ACCEPT_LANGUAGE') == $test) {
294 return TRUE;
295 }
296 }
297 break;
298 case 'IP':
299 if (GeneralUtility::cmpIP(GeneralUtility::getIndpEnv('REMOTE_ADDR'), $value)) {
300 return TRUE;
301 }
302 break;
303 case 'hostname':
304 if (GeneralUtility::cmpFQDN(GeneralUtility::getIndpEnv('REMOTE_ADDR'), $value)) {
305 return TRUE;
306 }
307 break;
308 case 'hour':
309
310 case 'minute':
311
312 case 'month':
313
314 case 'year':
315
316 case 'dayofweek':
317
318 case 'dayofmonth':
319
320 case 'dayofyear':
321 // In order to simulate time properly in templates.
322 $theEvalTime = $GLOBALS['SIM_EXEC_TIME'];
323 switch ($key) {
324 case 'hour':
325 $theTestValue = date('H', $theEvalTime);
326 break;
327 case 'minute':
328 $theTestValue = date('i', $theEvalTime);
329 break;
330 case 'month':
331 $theTestValue = date('m', $theEvalTime);
332 break;
333 case 'year':
334 $theTestValue = date('Y', $theEvalTime);
335 break;
336 case 'dayofweek':
337 $theTestValue = date('w', $theEvalTime);
338 break;
339 case 'dayofmonth':
340 $theTestValue = date('d', $theEvalTime);
341 break;
342 case 'dayofyear':
343 $theTestValue = date('z', $theEvalTime);
344 break;
345 }
346 $theTestValue = intval($theTestValue);
347 // comp
348 $values = GeneralUtility::trimExplode(',', $value, TRUE);
349 foreach ($values as $test) {
350 if (\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($test)) {
351 $test = '=' . $test;
352 }
353 if ($this->compareNumber($test, $theTestValue)) {
354 return TRUE;
355 }
356 }
357 break;
358 case 'compatVersion':
359 return GeneralUtility::compat_version($value);
360 break;
361 case 'loginUser':
362 if ($this->isUserLoggedIn()) {
363 $values = GeneralUtility::trimExplode(',', $value, TRUE);
364 foreach ($values as $test) {
365 if ($test == '*' || !strcmp($this->getUserId(), $test)) {
366 return TRUE;
367 }
368 }
369 } elseif ($value === '') {
370 return TRUE;
371 }
372 break;
373 case 'page':
374 if ($keyParts[1]) {
375 $page = $this->getPage();
376 $property = $keyParts[1];
377 if (!empty($page) && isset($page[$property])) {
378 if (strcmp($page[$property], $value) === 0) {
379 return TRUE;
380 }
381 }
382 }
383 break;
384 case 'globalVar':
385 $values = GeneralUtility::trimExplode(',', $value, TRUE);
386 foreach ($values as $test) {
387 $point = strcspn($test, '!=<>');
388 $theVarName = substr($test, 0, $point);
389 $nv = $this->getVariable(trim($theVarName));
390 $testValue = substr($test, $point);
391 if ($this->compareNumber($testValue, $nv)) {
392 return TRUE;
393 }
394 }
395 break;
396 case 'globalString':
397 $values = GeneralUtility::trimExplode(',', $value, TRUE);
398 foreach ($values as $test) {
399 $point = strcspn($test, '=');
400 $theVarName = substr($test, 0, $point);
401 $nv = $this->getVariable(trim($theVarName));
402 $testValue = substr($test, $point + 1);
403 if ($this->searchStringWildcard($nv, trim($testValue))) {
404 return TRUE;
405 }
406 }
407 break;
408 case 'userFunc':
409 $values = preg_split('/\\(|\\)/', $value);
410 $funcName = trim($values[0]);
411 $funcValues = GeneralUtility::trimExplode(',', $values[1]);
412 if (function_exists($funcName) && call_user_func_array($funcName, $funcValues)) {
413 return TRUE;
414 }
415 break;
416 }
417 return NULL;
418 }
419
420 /**
421 * Get variable common
422 *
423 * @param array $vars
424 * @return mixed Whatever value. If none, then NULL.
425 */
426 protected function getVariableCommon(array $vars) {
427 $value = NULL;
428 if (count($vars) == 1) {
429 $value = $this->getGlobal($vars[0]);
430 } else {
431 $splitAgain = explode('|', $vars[1], 2);
432 $k = trim($splitAgain[0]);
433 if ($k) {
434 switch ((string) trim($vars[0])) {
435 case 'GP':
436 $value = GeneralUtility::_GP($k);
437 break;
438 case 'ENV':
439 $value = getenv($k);
440 break;
441 case 'IENV':
442 $value = GeneralUtility::getIndpEnv($k);
443 break;
444 case 'LIT':
445 return trim($vars[1]);
446 break;
447 default:
448 return NULL;
449 }
450 // If array:
451 if (count($splitAgain) > 1) {
452 if (is_array($value) && trim($splitAgain[1])) {
453 $value = $this->getGlobal($splitAgain[1], $value);
454 } else {
455 $value = '';
456 }
457 }
458 }
459 }
460 return $value;
461 }
462
463 /**
464 * Evaluates a $leftValue based on an operator: "<", ">", "<=", ">=", "!=" or "="
465 *
466 * @param string $test The value to compare with on the form [operator][number]. Eg. "< 123
467 * @param integer $leftValue The value on the left side
468 * @return boolean If $value is "50" and $test is "< 123" then it will return TRUE.
469 */
470 protected function compareNumber($test, $leftValue) {
471 if (preg_match('/^(!?=+|<=?|>=?)\\s*([^\\s]*)\\s*$/', $test, $matches)) {
472 $operator = $matches[1];
473 $rightValue = $matches[2];
474 switch ($operator) {
475 case '>=':
476 return $leftValue >= doubleval($rightValue);
477 break;
478 case '<=':
479 return $leftValue <= doubleval($rightValue);
480 break;
481 case '!=':
482 // multiple values may be split with '|'
483 // see if none matches ("not in list")
484 $found = FALSE;
485 $rightValueParts = GeneralUtility::trimExplode('|', $rightValue);
486 foreach ($rightValueParts as $rightValueSingle) {
487 if ($leftValue == doubleval($rightValueSingle)) {
488 $found = TRUE;
489 break;
490 }
491 }
492 return $found === FALSE;
493 break;
494 case '<':
495 return $leftValue < doubleval($rightValue);
496 break;
497 case '>':
498 return $leftValue > doubleval($rightValue);
499 break;
500 default:
501 // nothing valid found except '=', use '='
502 // multiple values may be split with '|'
503 // see if one matches ("in list")
504 $found = FALSE;
505 $rightValueParts = GeneralUtility::trimExplode('|', $rightValue);
506 foreach ($rightValueParts as $rightValueSingle) {
507 if ($leftValue == $rightValueSingle) {
508 $found = TRUE;
509 break;
510 }
511 }
512 return $found;
513 }
514 }
515 return FALSE;
516 }
517
518 /**
519 * Matching two strings against each other, supporting a "*" wildcard or (if wrapped in "/") PCRE regular expressions
520 *
521 * @param string $haystack The string in which to find $needle.
522 * @param string $needle The string to find in $haystack
523 * @return boolean Returns TRUE if $needle matches or is found in (according to wildcards) in $haystack. Eg. if $haystack is "Netscape 6.5" and $needle is "Net*" or "Net*ape" then it returns TRUE.
524 */
525 protected function searchStringWildcard($haystack, $needle) {
526 $result = FALSE;
527 if ($needle) {
528 if (preg_match('/^\\/.+\\/$/', $needle)) {
529 // Regular expression, only "//" is allowed as delimiter
530 $regex = $needle;
531 } else {
532 $needle = str_replace(array('*', '?'), array('###MANY###', '###ONE###'), $needle);
533 $regex = '/^' . preg_quote($needle, '/') . '$/';
534 // Replace the marker with .* to match anything (wildcard)
535 $regex = str_replace(array('###MANY###', '###ONE###'), array('.*', '.'), $regex);
536 }
537 $result = (bool) preg_match($regex, ((string) $haystack));
538 }
539 return $result;
540 }
541
542 /**
543 * Generates an array with abstracted browser information
544 *
545 * @param string $userAgent The useragent string, \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('HTTP_USER_AGENT')
546 * @return array Contains keys "browser", "version", "system
547 */
548 protected function getBrowserInfo($userAgent) {
549 return \TYPO3\CMS\Core\Utility\ClientUtility::getBrowserInfo($userAgent);
550 }
551
552 /**
553 * Gets a code for a browsing device based on the input useragent string.
554 *
555 * @param string $userAgent The useragent string, \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('HTTP_USER_AGENT')
556 * @return string Code for the specific device type
557 */
558 protected function getDeviceType($userAgent) {
559 return \TYPO3\CMS\Core\Utility\ClientUtility::getDeviceType($userAgent);
560 }
561
562 /**
563 * Return global variable where the input string $var defines array keys separated by "|"
564 * Example: $var = "HTTP_SERVER_VARS | something" will return the value $GLOBALS['HTTP_SERVER_VARS']['something'] value
565 *
566 * @param string $var Global var key, eg. "HTTP_GET_VAR" or "HTTP_GET_VARS|id" to get the GET parameter "id" back.
567 * @param array $source Alternative array than $GLOBAL to get variables from.
568 * @return mixed Whatever value. If none, then blank string.
569 */
570 protected function getGlobal($var, $source = NULL) {
571 $vars = explode('|', $var);
572 $c = count($vars);
573 $k = trim($vars[0]);
574 $theVar = isset($source) ? $source[$k] : $GLOBALS[$k];
575 for ($a = 1; $a < $c; $a++) {
576 if (!isset($theVar)) {
577 break;
578 }
579 $key = trim($vars[$a]);
580 if (is_object($theVar)) {
581 $theVar = $theVar->{$key};
582 } elseif (is_array($theVar)) {
583 $theVar = $theVar[$key];
584 } else {
585 return '';
586 }
587 }
588 if (!is_array($theVar) && !is_object($theVar)) {
589 return $theVar;
590 } else {
591 return '';
592 }
593 }
594
595 /**
596 * Evaluates a TypoScript condition given as input, eg. "[browser=net][...(other conditions)...]"
597 *
598 * @param string $string The condition to match against its criterias.
599 * @return boolean Whether the condition matched
600 * @see \TYPO3\CMS\Core\TypoScript\Parser\TypoScriptParser::parse()
601 */
602 abstract protected function evaluateCondition($string);
603
604 /**
605 * Gets the value of a variable.
606 *
607 * Examples of names:
608 * + TSFE:id
609 * + GP:firstLevel|secondLevel
610 * + _GET|firstLevel|secondLevel
611 * + LIT:someLiteralValue
612 *
613 * @param string $name The name of the variable to fetch the value from
614 * @return mixed The value of the given variable (string) or NULL if variable did not exist
615 */
616 abstract protected function getVariable($name);
617
618 /**
619 * Gets the usergroup list of the current user.
620 *
621 * @return string The usergroup list of the current user
622 */
623 abstract protected function getGroupList();
624
625 /**
626 * Determines the current page Id.
627 *
628 * @return integer The current page Id
629 */
630 abstract protected function determinePageId();
631
632 /**
633 * Gets the properties for the current page.
634 *
635 * @return array The properties for the current page.
636 */
637 abstract protected function getPage();
638
639 /**
640 * Determines the rootline for the current page.
641 *
642 * @return array The rootline for the current page.
643 */
644 abstract protected function determineRootline();
645
646 /**
647 * Gets the id of the current user.
648 *
649 * @return integer The id of the current user
650 */
651 abstract protected function getUserId();
652
653 /**
654 * Determines if a user is logged in.
655 *
656 * @return boolean Determines if a user is logged in
657 */
658 abstract protected function isUserLoggedIn();
659
660 /**
661 * Sets a log message.
662 *
663 * @param string $message The log message to set/write
664 * @return void
665 */
666 abstract protected function log($message);
667
668 }
669
670
671 ?>