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