[BUGFIX] Unused TDParams in ColumnsContentObject()
[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 text file 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 NULL|boolean Result of the evaluation; NULL if condition could not be evaluated
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 'applicationContext':
216 $values = GeneralUtility::trimExplode(',', $value, TRUE);
217 $currentApplicationContext = GeneralUtility::getApplicationContext();
218 foreach ($values as $applicationContext) {
219 if ($this->searchStringWildcard($currentApplicationContext, $applicationContext)) {
220 return TRUE;
221 }
222 }
223 break;
224 case 'browser':
225 $values = GeneralUtility::trimExplode(',', $value, TRUE);
226 // take all identified browsers into account, eg chrome deliver
227 // webkit=>532.5, chrome=>4.1, safari=>532.5
228 // so comparing string will be
229 // "webkit532.5 chrome4.1 safari532.5"
230 $all = '';
231 foreach ($browserInfo['all'] as $key => $value) {
232 $all .= $key . $value . ' ';
233 }
234 foreach ($values as $test) {
235 if (stripos($all, $test) !== FALSE) {
236 return TRUE;
237 }
238 }
239 break;
240 case 'version':
241 $values = GeneralUtility::trimExplode(',', $value, TRUE);
242 foreach ($values as $test) {
243 if (strcspn($test, '=<>') == 0) {
244 switch (substr($test, 0, 1)) {
245 case '=':
246 if (doubleval(substr($test, 1)) == $browserInfo['version']) {
247 return TRUE;
248 }
249 break;
250 case '<':
251 if (doubleval(substr($test, 1)) > $browserInfo['version']) {
252 return TRUE;
253 }
254 break;
255 case '>':
256 if (doubleval(substr($test, 1)) < $browserInfo['version']) {
257 return TRUE;
258 }
259 break;
260 }
261 } elseif (strpos(' ' . $browserInfo['version'], $test) == 1) {
262 return TRUE;
263 }
264 }
265 break;
266 case 'system':
267 $values = GeneralUtility::trimExplode(',', $value, TRUE);
268 // Take all identified systems into account, e.g. mac for iOS, Linux
269 // for android and Windows NT for Windows XP
270 $allSystems = ' ' . implode(' ', $browserInfo['all_systems']);
271 foreach ($values as $test) {
272 if (stripos($allSystems, $test) !== FALSE) {
273 return TRUE;
274 }
275 }
276 break;
277 case 'device':
278 if (!isset($this->deviceInfo)) {
279 $this->deviceInfo = $this->getDeviceType(GeneralUtility::getIndpEnv('HTTP_USER_AGENT'));
280 }
281 $values = GeneralUtility::trimExplode(',', $value, TRUE);
282 foreach ($values as $test) {
283 if ($this->deviceInfo == $test) {
284 return TRUE;
285 }
286 }
287 break;
288 case 'useragent':
289 $test = trim($value);
290 if (strlen($test)) {
291 return $this->searchStringWildcard($browserInfo['useragent'], $test);
292 }
293 break;
294 case 'language':
295 $values = GeneralUtility::trimExplode(',', $value, TRUE);
296 foreach ($values as $test) {
297 if (preg_match('/^\\*.+\\*$/', $test)) {
298 $allLanguages = preg_split('/[,;]/', GeneralUtility::getIndpEnv('HTTP_ACCEPT_LANGUAGE'));
299 if (in_array(substr($test, 1, -1), $allLanguages)) {
300 return TRUE;
301 }
302 } elseif (GeneralUtility::getIndpEnv('HTTP_ACCEPT_LANGUAGE') == $test) {
303 return TRUE;
304 }
305 }
306 break;
307 case 'IP':
308 if ($value === 'devIP') {
309 $value = trim($GLOBALS['TYPO3_CONF_VARS']['SYS']['devIPmask']);
310 }
311
312 if (GeneralUtility::cmpIP(GeneralUtility::getIndpEnv('REMOTE_ADDR'), $value)) {
313 return TRUE;
314 }
315 break;
316 case 'hostname':
317 if (GeneralUtility::cmpFQDN(GeneralUtility::getIndpEnv('REMOTE_ADDR'), $value)) {
318 return TRUE;
319 }
320 break;
321 case 'hour':
322
323 case 'minute':
324
325 case 'month':
326
327 case 'year':
328
329 case 'dayofweek':
330
331 case 'dayofmonth':
332
333 case 'dayofyear':
334 // In order to simulate time properly in templates.
335 $theEvalTime = $GLOBALS['SIM_EXEC_TIME'];
336 switch ($key) {
337 case 'hour':
338 $theTestValue = date('H', $theEvalTime);
339 break;
340 case 'minute':
341 $theTestValue = date('i', $theEvalTime);
342 break;
343 case 'month':
344 $theTestValue = date('m', $theEvalTime);
345 break;
346 case 'year':
347 $theTestValue = date('Y', $theEvalTime);
348 break;
349 case 'dayofweek':
350 $theTestValue = date('w', $theEvalTime);
351 break;
352 case 'dayofmonth':
353 $theTestValue = date('d', $theEvalTime);
354 break;
355 case 'dayofyear':
356 $theTestValue = date('z', $theEvalTime);
357 break;
358 }
359 $theTestValue = (int)$theTestValue;
360 // comp
361 $values = GeneralUtility::trimExplode(',', $value, TRUE);
362 foreach ($values as $test) {
363 if (\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($test)) {
364 $test = '=' . $test;
365 }
366 if ($this->compareNumber($test, $theTestValue)) {
367 return TRUE;
368 }
369 }
370 break;
371 case 'compatVersion':
372 return GeneralUtility::compat_version($value);
373 break;
374 case 'loginUser':
375 if ($this->isUserLoggedIn()) {
376 $values = GeneralUtility::trimExplode(',', $value, TRUE);
377 foreach ($values as $test) {
378 if ($test == '*' || (string)$this->getUserId() === (string)$test) {
379 return TRUE;
380 }
381 }
382 } elseif ($value === '') {
383 return TRUE;
384 }
385 break;
386 case 'page':
387 if ($keyParts[1]) {
388 $page = $this->getPage();
389 $property = $keyParts[1];
390 if (!empty($page) && isset($page[$property]) && (string)$page[$property] === (string)$value) {
391 return TRUE;
392 }
393 }
394 break;
395 case 'globalVar':
396 $values = GeneralUtility::trimExplode(',', $value, TRUE);
397 foreach ($values as $test) {
398 $point = strcspn($test, '!=<>');
399 $theVarName = substr($test, 0, $point);
400 $nv = $this->getVariable(trim($theVarName));
401 $testValue = substr($test, $point);
402 if ($this->compareNumber($testValue, $nv)) {
403 return TRUE;
404 }
405 }
406 break;
407 case 'globalString':
408 $values = GeneralUtility::trimExplode(',', $value, TRUE);
409 foreach ($values as $test) {
410 $point = strcspn($test, '=');
411 $theVarName = substr($test, 0, $point);
412 $nv = $this->getVariable(trim($theVarName));
413 $testValue = substr($test, $point + 1);
414 if ($this->searchStringWildcard($nv, trim($testValue))) {
415 return TRUE;
416 }
417 }
418 break;
419 case 'userFunc':
420 $matches = array();
421 preg_match_all('/^\s*([^\(\s]+)\s*(?:\((.*)\))?\s*$/', $value, $matches);
422 $funcName = $matches[1][0];
423 $funcValues = $matches[2][0] ? $this->parseUserFuncArguments($matches[2][0]) : array();
424 if (function_exists($funcName) && call_user_func_array($funcName, $funcValues)) {
425 return TRUE;
426 }
427 break;
428 }
429 return NULL;
430 }
431
432 /**
433 * Parses arguments to the userFunc.
434 *
435 * @param string $arguments
436 * @return array
437 */
438 protected function parseUserFuncArguments($arguments) {
439 $result = array();
440 $arguments = trim($arguments);
441 while ($arguments) {
442 if ($arguments{0} == ',') {
443 $result[] = '';
444 $arguments = substr($arguments, 1);
445 } else {
446 $pos = strcspn($arguments, ',\'"');
447 if ($pos == 0) {
448 // We hit a quote of some kind
449 $quote = $arguments{0};
450 $segment = preg_replace('/^(.*?[^\\\])' . $quote . '.*$/', '\1', substr($arguments, 1));
451 $segment = str_replace('\\' . $quote, $quote, $segment);
452 $result[] = $segment;
453 $offset = strpos($arguments, ',', strlen($segment) + 2);
454 if ($offset === FALSE) {
455 $offset = strlen($arguments);
456 }
457 $arguments = substr($arguments, $offset);
458 } else {
459 $result[] = trim(substr($arguments, 0, $pos));
460 $arguments = substr($arguments, $pos + 1);
461 }
462 }
463 $arguments = trim($arguments);
464 };
465 return $result;
466 }
467
468 /**
469 * Get variable common
470 *
471 * @param array $vars
472 * @return mixed Whatever value. If none, then NULL.
473 */
474 protected function getVariableCommon(array $vars) {
475 $value = NULL;
476 if (count($vars) == 1) {
477 $value = $this->getGlobal($vars[0]);
478 } else {
479 $splitAgain = explode('|', $vars[1], 2);
480 $k = trim($splitAgain[0]);
481 if ($k) {
482 switch ((string) trim($vars[0])) {
483 case 'GP':
484 $value = GeneralUtility::_GP($k);
485 break;
486 case 'ENV':
487 $value = getenv($k);
488 break;
489 case 'IENV':
490 $value = GeneralUtility::getIndpEnv($k);
491 break;
492 case 'LIT':
493 return trim($vars[1]);
494 break;
495 default:
496 return NULL;
497 }
498 // If array:
499 if (count($splitAgain) > 1) {
500 if (is_array($value) && trim($splitAgain[1])) {
501 $value = $this->getGlobal($splitAgain[1], $value);
502 } else {
503 $value = '';
504 }
505 }
506 }
507 }
508 return $value;
509 }
510
511 /**
512 * Evaluates a $leftValue based on an operator: "<", ">", "<=", ">=", "!=" or "="
513 *
514 * @param string $test The value to compare with on the form [operator][number]. Eg. "< 123
515 * @param integer $leftValue The value on the left side
516 * @return boolean If $value is "50" and $test is "< 123" then it will return TRUE.
517 */
518 protected function compareNumber($test, $leftValue) {
519 if (preg_match('/^(!?=+|<=?|>=?)\\s*([^\\s]*)\\s*$/', $test, $matches)) {
520 $operator = $matches[1];
521 $rightValue = $matches[2];
522 switch ($operator) {
523 case '>=':
524 return $leftValue >= doubleval($rightValue);
525 break;
526 case '<=':
527 return $leftValue <= doubleval($rightValue);
528 break;
529 case '!=':
530 // multiple values may be split with '|'
531 // see if none matches ("not in list")
532 $found = FALSE;
533 $rightValueParts = GeneralUtility::trimExplode('|', $rightValue);
534 foreach ($rightValueParts as $rightValueSingle) {
535 if ($leftValue == doubleval($rightValueSingle)) {
536 $found = TRUE;
537 break;
538 }
539 }
540 return $found === FALSE;
541 break;
542 case '<':
543 return $leftValue < doubleval($rightValue);
544 break;
545 case '>':
546 return $leftValue > doubleval($rightValue);
547 break;
548 default:
549 // nothing valid found except '=', use '='
550 // multiple values may be split with '|'
551 // see if one matches ("in list")
552 $found = FALSE;
553 $rightValueParts = GeneralUtility::trimExplode('|', $rightValue);
554 foreach ($rightValueParts as $rightValueSingle) {
555 if ($leftValue == $rightValueSingle) {
556 $found = TRUE;
557 break;
558 }
559 }
560 return $found;
561 }
562 }
563 return FALSE;
564 }
565
566 /**
567 * Matching two strings against each other, supporting a "*" wildcard or (if wrapped in "/") PCRE regular expressions
568 *
569 * @param string $haystack The string in which to find $needle.
570 * @param string $needle The string to find in $haystack
571 * @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.
572 */
573 protected function searchStringWildcard($haystack, $needle) {
574 $result = FALSE;
575 if ($haystack === $needle) {
576 $result = TRUE;
577 } elseif ($needle) {
578 if (preg_match('/^\\/.+\\/$/', $needle)) {
579 // Regular expression, only "//" is allowed as delimiter
580 $regex = $needle;
581 } else {
582 $needle = str_replace(array('*', '?'), array('###MANY###', '###ONE###'), $needle);
583 $regex = '/^' . preg_quote($needle, '/') . '$/';
584 // Replace the marker with .* to match anything (wildcard)
585 $regex = str_replace(array('###MANY###', '###ONE###'), array('.*', '.'), $regex);
586 }
587 $result = (bool) preg_match($regex, ((string) $haystack));
588 }
589 return $result;
590 }
591
592 /**
593 * Generates an array with abstracted browser information
594 *
595 * @param string $userAgent The useragent string, \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('HTTP_USER_AGENT')
596 * @return array Contains keys "browser", "version", "system
597 */
598 protected function getBrowserInfo($userAgent) {
599 return \TYPO3\CMS\Core\Utility\ClientUtility::getBrowserInfo($userAgent);
600 }
601
602 /**
603 * Gets a code for a browsing device based on the input useragent string.
604 *
605 * @param string $userAgent The useragent string, \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('HTTP_USER_AGENT')
606 * @return string Code for the specific device type
607 */
608 protected function getDeviceType($userAgent) {
609 return \TYPO3\CMS\Core\Utility\ClientUtility::getDeviceType($userAgent);
610 }
611
612 /**
613 * Return global variable where the input string $var defines array keys separated by "|"
614 * Example: $var = "HTTP_SERVER_VARS | something" will return the value $GLOBALS['HTTP_SERVER_VARS']['something'] value
615 *
616 * @param string $var Global var key, eg. "HTTP_GET_VAR" or "HTTP_GET_VARS|id" to get the GET parameter "id" back.
617 * @param array $source Alternative array than $GLOBAL to get variables from.
618 * @return mixed Whatever value. If none, then blank string.
619 */
620 protected function getGlobal($var, $source = NULL) {
621 $vars = explode('|', $var);
622 $c = count($vars);
623 $k = trim($vars[0]);
624 $theVar = isset($source) ? $source[$k] : $GLOBALS[$k];
625 for ($a = 1; $a < $c; $a++) {
626 if (!isset($theVar)) {
627 break;
628 }
629 $key = trim($vars[$a]);
630 if (is_object($theVar)) {
631 $theVar = $theVar->{$key};
632 } elseif (is_array($theVar)) {
633 $theVar = $theVar[$key];
634 } else {
635 return '';
636 }
637 }
638 if (!is_array($theVar) && !is_object($theVar)) {
639 return $theVar;
640 } else {
641 return '';
642 }
643 }
644
645 /**
646 * Evaluates a TypoScript condition given as input, eg. "[browser=net][...(other conditions)...]"
647 *
648 * @param string $string The condition to match against its criterias.
649 * @return boolean Whether the condition matched
650 * @see \TYPO3\CMS\Core\TypoScript\Parser\TypoScriptParser::parse()
651 */
652 abstract protected function evaluateCondition($string);
653
654 /**
655 * Gets the value of a variable.
656 *
657 * Examples of names:
658 * + TSFE:id
659 * + GP:firstLevel|secondLevel
660 * + _GET|firstLevel|secondLevel
661 * + LIT:someLiteralValue
662 *
663 * @param string $name The name of the variable to fetch the value from
664 * @return mixed The value of the given variable (string) or NULL if variable did not exist
665 */
666 abstract protected function getVariable($name);
667
668 /**
669 * Gets the usergroup list of the current user.
670 *
671 * @return string The usergroup list of the current user
672 */
673 abstract protected function getGroupList();
674
675 /**
676 * Determines the current page Id.
677 *
678 * @return integer The current page Id
679 */
680 abstract protected function determinePageId();
681
682 /**
683 * Gets the properties for the current page.
684 *
685 * @return array The properties for the current page.
686 */
687 abstract protected function getPage();
688
689 /**
690 * Determines the rootline for the current page.
691 *
692 * @return array The rootline for the current page.
693 */
694 abstract protected function determineRootline();
695
696 /**
697 * Gets the id of the current user.
698 *
699 * @return integer The id of the current user
700 */
701 abstract protected function getUserId();
702
703 /**
704 * Determines if a user is logged in.
705 *
706 * @return boolean Determines if a user is logged in
707 */
708 abstract protected function isUserLoggedIn();
709
710 /**
711 * Sets a log message.
712 *
713 * @param string $message The log message to set/write
714 * @return void
715 */
716 abstract protected function log($message);
717
718 }