[FEATURE] Add TypoScript condition for ApplicationContext
[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 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 (GeneralUtility::cmpIP(GeneralUtility::getIndpEnv('REMOTE_ADDR'), $value)) {
309 return TRUE;
310 }
311 break;
312 case 'hostname':
313 if (GeneralUtility::cmpFQDN(GeneralUtility::getIndpEnv('REMOTE_ADDR'), $value)) {
314 return TRUE;
315 }
316 break;
317 case 'hour':
318
319 case 'minute':
320
321 case 'month':
322
323 case 'year':
324
325 case 'dayofweek':
326
327 case 'dayofmonth':
328
329 case 'dayofyear':
330 // In order to simulate time properly in templates.
331 $theEvalTime = $GLOBALS['SIM_EXEC_TIME'];
332 switch ($key) {
333 case 'hour':
334 $theTestValue = date('H', $theEvalTime);
335 break;
336 case 'minute':
337 $theTestValue = date('i', $theEvalTime);
338 break;
339 case 'month':
340 $theTestValue = date('m', $theEvalTime);
341 break;
342 case 'year':
343 $theTestValue = date('Y', $theEvalTime);
344 break;
345 case 'dayofweek':
346 $theTestValue = date('w', $theEvalTime);
347 break;
348 case 'dayofmonth':
349 $theTestValue = date('d', $theEvalTime);
350 break;
351 case 'dayofyear':
352 $theTestValue = date('z', $theEvalTime);
353 break;
354 }
355 $theTestValue = intval($theTestValue);
356 // comp
357 $values = GeneralUtility::trimExplode(',', $value, TRUE);
358 foreach ($values as $test) {
359 if (\TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($test)) {
360 $test = '=' . $test;
361 }
362 if ($this->compareNumber($test, $theTestValue)) {
363 return TRUE;
364 }
365 }
366 break;
367 case 'compatVersion':
368 return GeneralUtility::compat_version($value);
369 break;
370 case 'loginUser':
371 if ($this->isUserLoggedIn()) {
372 $values = GeneralUtility::trimExplode(',', $value, TRUE);
373 foreach ($values as $test) {
374 if ($test == '*' || !strcmp($this->getUserId(), $test)) {
375 return TRUE;
376 }
377 }
378 } elseif ($value === '') {
379 return TRUE;
380 }
381 break;
382 case 'page':
383 if ($keyParts[1]) {
384 $page = $this->getPage();
385 $property = $keyParts[1];
386 if (!empty($page) && isset($page[$property])) {
387 if (strcmp($page[$property], $value) === 0) {
388 return TRUE;
389 }
390 }
391 }
392 break;
393 case 'globalVar':
394 $values = GeneralUtility::trimExplode(',', $value, TRUE);
395 foreach ($values as $test) {
396 $point = strcspn($test, '!=<>');
397 $theVarName = substr($test, 0, $point);
398 $nv = $this->getVariable(trim($theVarName));
399 $testValue = substr($test, $point);
400 if ($this->compareNumber($testValue, $nv)) {
401 return TRUE;
402 }
403 }
404 break;
405 case 'globalString':
406 $values = GeneralUtility::trimExplode(',', $value, TRUE);
407 foreach ($values as $test) {
408 $point = strcspn($test, '=');
409 $theVarName = substr($test, 0, $point);
410 $nv = $this->getVariable(trim($theVarName));
411 $testValue = substr($test, $point + 1);
412 if ($this->searchStringWildcard($nv, trim($testValue))) {
413 return TRUE;
414 }
415 }
416 break;
417 case 'userFunc':
418 $values = preg_split('/\\(|\\)/', $value);
419 $funcName = trim($values[0]);
420 $funcValues = GeneralUtility::trimExplode(',', $values[1]);
421 if (function_exists($funcName) && call_user_func_array($funcName, $funcValues)) {
422 return TRUE;
423 }
424 break;
425 }
426 return NULL;
427 }
428
429 /**
430 * Get variable common
431 *
432 * @param array $vars
433 * @return mixed Whatever value. If none, then NULL.
434 */
435 protected function getVariableCommon(array $vars) {
436 $value = NULL;
437 if (count($vars) == 1) {
438 $value = $this->getGlobal($vars[0]);
439 } else {
440 $splitAgain = explode('|', $vars[1], 2);
441 $k = trim($splitAgain[0]);
442 if ($k) {
443 switch ((string) trim($vars[0])) {
444 case 'GP':
445 $value = GeneralUtility::_GP($k);
446 break;
447 case 'ENV':
448 $value = getenv($k);
449 break;
450 case 'IENV':
451 $value = GeneralUtility::getIndpEnv($k);
452 break;
453 case 'LIT':
454 return trim($vars[1]);
455 break;
456 default:
457 return NULL;
458 }
459 // If array:
460 if (count($splitAgain) > 1) {
461 if (is_array($value) && trim($splitAgain[1])) {
462 $value = $this->getGlobal($splitAgain[1], $value);
463 } else {
464 $value = '';
465 }
466 }
467 }
468 }
469 return $value;
470 }
471
472 /**
473 * Evaluates a $leftValue based on an operator: "<", ">", "<=", ">=", "!=" or "="
474 *
475 * @param string $test The value to compare with on the form [operator][number]. Eg. "< 123
476 * @param integer $leftValue The value on the left side
477 * @return boolean If $value is "50" and $test is "< 123" then it will return TRUE.
478 */
479 protected function compareNumber($test, $leftValue) {
480 if (preg_match('/^(!?=+|<=?|>=?)\\s*([^\\s]*)\\s*$/', $test, $matches)) {
481 $operator = $matches[1];
482 $rightValue = $matches[2];
483 switch ($operator) {
484 case '>=':
485 return $leftValue >= doubleval($rightValue);
486 break;
487 case '<=':
488 return $leftValue <= doubleval($rightValue);
489 break;
490 case '!=':
491 // multiple values may be split with '|'
492 // see if none matches ("not in list")
493 $found = FALSE;
494 $rightValueParts = GeneralUtility::trimExplode('|', $rightValue);
495 foreach ($rightValueParts as $rightValueSingle) {
496 if ($leftValue == doubleval($rightValueSingle)) {
497 $found = TRUE;
498 break;
499 }
500 }
501 return $found === FALSE;
502 break;
503 case '<':
504 return $leftValue < doubleval($rightValue);
505 break;
506 case '>':
507 return $leftValue > doubleval($rightValue);
508 break;
509 default:
510 // nothing valid found except '=', use '='
511 // multiple values may be split with '|'
512 // see if one matches ("in list")
513 $found = FALSE;
514 $rightValueParts = GeneralUtility::trimExplode('|', $rightValue);
515 foreach ($rightValueParts as $rightValueSingle) {
516 if ($leftValue == $rightValueSingle) {
517 $found = TRUE;
518 break;
519 }
520 }
521 return $found;
522 }
523 }
524 return FALSE;
525 }
526
527 /**
528 * Matching two strings against each other, supporting a "*" wildcard or (if wrapped in "/") PCRE regular expressions
529 *
530 * @param string $haystack The string in which to find $needle.
531 * @param string $needle The string to find in $haystack
532 * @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.
533 */
534 protected function searchStringWildcard($haystack, $needle) {
535 $result = FALSE;
536 if ($needle) {
537 if (preg_match('/^\\/.+\\/$/', $needle)) {
538 // Regular expression, only "//" is allowed as delimiter
539 $regex = $needle;
540 } else {
541 $needle = str_replace(array('*', '?'), array('###MANY###', '###ONE###'), $needle);
542 $regex = '/^' . preg_quote($needle, '/') . '$/';
543 // Replace the marker with .* to match anything (wildcard)
544 $regex = str_replace(array('###MANY###', '###ONE###'), array('.*', '.'), $regex);
545 }
546 $result = (bool) preg_match($regex, ((string) $haystack));
547 }
548 return $result;
549 }
550
551 /**
552 * Generates an array with abstracted browser information
553 *
554 * @param string $userAgent The useragent string, \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('HTTP_USER_AGENT')
555 * @return array Contains keys "browser", "version", "system
556 */
557 protected function getBrowserInfo($userAgent) {
558 return \TYPO3\CMS\Core\Utility\ClientUtility::getBrowserInfo($userAgent);
559 }
560
561 /**
562 * Gets a code for a browsing device based on the input useragent string.
563 *
564 * @param string $userAgent The useragent string, \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('HTTP_USER_AGENT')
565 * @return string Code for the specific device type
566 */
567 protected function getDeviceType($userAgent) {
568 return \TYPO3\CMS\Core\Utility\ClientUtility::getDeviceType($userAgent);
569 }
570
571 /**
572 * Return global variable where the input string $var defines array keys separated by "|"
573 * Example: $var = "HTTP_SERVER_VARS | something" will return the value $GLOBALS['HTTP_SERVER_VARS']['something'] value
574 *
575 * @param string $var Global var key, eg. "HTTP_GET_VAR" or "HTTP_GET_VARS|id" to get the GET parameter "id" back.
576 * @param array $source Alternative array than $GLOBAL to get variables from.
577 * @return mixed Whatever value. If none, then blank string.
578 */
579 protected function getGlobal($var, $source = NULL) {
580 $vars = explode('|', $var);
581 $c = count($vars);
582 $k = trim($vars[0]);
583 $theVar = isset($source) ? $source[$k] : $GLOBALS[$k];
584 for ($a = 1; $a < $c; $a++) {
585 if (!isset($theVar)) {
586 break;
587 }
588 $key = trim($vars[$a]);
589 if (is_object($theVar)) {
590 $theVar = $theVar->{$key};
591 } elseif (is_array($theVar)) {
592 $theVar = $theVar[$key];
593 } else {
594 return '';
595 }
596 }
597 if (!is_array($theVar) && !is_object($theVar)) {
598 return $theVar;
599 } else {
600 return '';
601 }
602 }
603
604 /**
605 * Evaluates a TypoScript condition given as input, eg. "[browser=net][...(other conditions)...]"
606 *
607 * @param string $string The condition to match against its criterias.
608 * @return boolean Whether the condition matched
609 * @see \TYPO3\CMS\Core\TypoScript\Parser\TypoScriptParser::parse()
610 */
611 abstract protected function evaluateCondition($string);
612
613 /**
614 * Gets the value of a variable.
615 *
616 * Examples of names:
617 * + TSFE:id
618 * + GP:firstLevel|secondLevel
619 * + _GET|firstLevel|secondLevel
620 * + LIT:someLiteralValue
621 *
622 * @param string $name The name of the variable to fetch the value from
623 * @return mixed The value of the given variable (string) or NULL if variable did not exist
624 */
625 abstract protected function getVariable($name);
626
627 /**
628 * Gets the usergroup list of the current user.
629 *
630 * @return string The usergroup list of the current user
631 */
632 abstract protected function getGroupList();
633
634 /**
635 * Determines the current page Id.
636 *
637 * @return integer The current page Id
638 */
639 abstract protected function determinePageId();
640
641 /**
642 * Gets the properties for the current page.
643 *
644 * @return array The properties for the current page.
645 */
646 abstract protected function getPage();
647
648 /**
649 * Determines the rootline for the current page.
650 *
651 * @return array The rootline for the current page.
652 */
653 abstract protected function determineRootline();
654
655 /**
656 * Gets the id of the current user.
657 *
658 * @return integer The id of the current user
659 */
660 abstract protected function getUserId();
661
662 /**
663 * Determines if a user is logged in.
664 *
665 * @return boolean Determines if a user is logged in
666 */
667 abstract protected function isUserLoggedIn();
668
669 /**
670 * Sets a log message.
671 *
672 * @param string $message The log message to set/write
673 * @return void
674 */
675 abstract protected function log($message);
676
677 }