* Changed the layout of the security warning box which is displayed in alt_intro.php
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_matchcondition.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 1999-2005 Kasper Skaarhoj (kasperYYYY@typo3.com)
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 * Contains class for Matching TypoScript conditions
29 *
30 * $Id$
31 * Revised for TYPO3 3.6 July/2003 by Kasper Skaarhoj
32 *
33 * @author Kasper Skaarhoj <kasperYYYY@typo3.com>
34 */
35 /**
36 * [CLASS/FUNCTION INDEX of SCRIPT]
37 *
38 *
39 *
40 * 77: class t3lib_matchCondition
41 * 91: function match($string)
42 * 311: function testNumber($test,$value)
43 * 333: function matchWild($haystack,$needle)
44 * 363: function whichDevice($useragent)
45 * 413: function browserInfo($useragent)
46 * 517: function browserInfo_version($tmp)
47 * 529: function getGlobal($var,$inArr='')
48 * 554: function getGP_ENV_TSFE($var)
49 *
50 * TOTAL FUNCTIONS: 8
51 * (This index is automatically created/updated by the extension "extdeveval")
52 *
53 */
54
55
56
57
58
59
60
61
62
63
64
65
66 /**
67 * Matching TypoScript conditions
68 *
69 * Used with the TypoScript parser.
70 * Matches browserinfo, IPnumbers for use with templates
71 *
72 * @author Kasper Skaarhoj <kasperYYYY@typo3.com>
73 * @package TYPO3
74 * @subpackage t3lib
75 * @see t3lib_TStemplate::matching(), t3lib_TStemplate::generateConfig()
76 */
77 class t3lib_matchCondition {
78 var $matchAlternative=array(); // If this array has elements, the matching returns true if a whole "matchline" is found in the array!
79 var $matchAll=0; // If set all is matched!
80
81 var $altRootLine=array();
82
83 /**
84 * Evaluates a TypoScript condition given as input, eg. "[browser=net][...(other conditions)...]"
85 *
86 * @param string The condition to match against its criterias.
87 * @return boolean Returns true or false based on the evaluation.
88 * @see t3lib_tsparser::parse()
89 * @link http://typo3.org/doc.0.html?&tx_extrepmgm_pi1[extUid]=270&tx_extrepmgm_pi1[tocEl]=292&cHash=c6c7d43d2f
90 */
91 function match($string) {
92
93 if ($this->matchAll) return true;
94 if (count($this->matchAlternative)) {
95 return in_array($string,$this->matchAlternative);
96 }
97
98 if (!$this->browserInfoArray) {
99 $this->browserInfoArray = $this->browserInfo(t3lib_div::getIndpEnv('HTTP_USER_AGENT'));
100 }
101 $browserInfo = $this->browserInfoArray;
102 $string = trim($string);
103 $string = substr($string,1,strlen($string)-2);
104 $parts = explode('][',$string);
105 reset($parts);
106 while(list(,$val)=each($parts)) {
107 $pcs = explode('=',$val,2);
108 $switchKey = trim($pcs[0]);
109 switch($switchKey) {
110 case 'browser':
111 $values = explode(',',$pcs[1]);
112 while(list(,$test)=each($values)) {
113 if (strstr($browserInfo['browser'].$browserInfo['version'],trim($test))) {
114 return true;
115 }
116 }
117 break;
118 case 'version':
119 $values = explode(',',$pcs[1]);
120 while(list(,$test)=each($values)) {
121 $test = trim($test);
122 if ($test) {
123 if (strcspn($test,'=<>')==0) {
124 switch(substr($test,0,1)) {
125 case '=':
126 if (doubleval(substr($test,1))==$browserInfo['version']) return true;
127 break;
128 case '<':
129 if (doubleval(substr($test,1))>$browserInfo['version']) return true;
130 break;
131 case '>':
132 if (doubleval(substr($test,1))<$browserInfo['version']) return true;
133 break;
134 }
135 } else {
136 if (strpos(' '.$browserInfo['version'],$test)==1) {return true;}
137 }
138 }
139 }
140 break;
141 case 'system':
142 $values = explode(',',$pcs[1]);
143 while(list(,$test)=each($values)) {
144 $test = trim($test);
145 if ($test) {
146 if (strpos(' '.$browserInfo['system'],$test)==1) {return true;}
147 }
148 }
149 break;
150 case 'device':
151 $values = explode(',',$pcs[1]);
152 if (!isset($this->deviceInfo)) {
153 $this->deviceInfo = $this->whichDevice(t3lib_div::getIndpEnv('HTTP_USER_AGENT'));
154 }
155 while(list(,$test)=each($values)) {
156 $test = trim($test);
157 if ($test) {
158 if ($this->deviceInfo==$test) {return true;}
159 }
160 }
161 break;
162 case 'useragent':
163 $test = trim($pcs[1]);
164 if ($test) {
165 return $this->matchWild($browserInfo['useragent'],$test);
166 }
167 break;
168 case 'language':
169 $values = explode(',',$pcs[1]);
170 while(list(,$test)=each($values)) {
171 $test = trim($test);
172 if ($test) {
173 if (ereg('^\*.+\*$',$test)) {
174 $allLanguages = split('[,;]',t3lib_div::getIndpEnv('HTTP_ACCEPT_LANGUAGE'));
175 if (in_array(substr($test,1,-1), $allLanguages)) {return true;}
176 } else {
177 if (t3lib_div::getIndpEnv('HTTP_ACCEPT_LANGUAGE') == $test) {return true;}
178 }
179 }
180 }
181 break;
182 case 'IP':
183 if (t3lib_div::cmpIP(t3lib_div::getIndpEnv('REMOTE_ADDR'), $pcs[1])) {return true;}
184 break;
185 case 'hostname':
186 if (t3lib_div::cmpFQDN(t3lib_div::getIndpEnv('REMOTE_ADDR'), $pcs[1])) {return true;}
187 break;
188 // hour, minute, dayofweek, dayofmonth, month
189 case 'hour':
190 case 'minute':
191 case 'dayofweek':
192 case 'dayofmonth':
193 case 'month':
194 $theEvalTime = $GLOBALS['SIM_EXEC_TIME']; // In order to simulate time properly in templates.
195 switch($switchKey) {
196 case 'hour': $theTestValue = date('H',$theEvalTime); break;
197 case 'minute': $theTestValue = date('i',$theEvalTime); break;
198 case 'dayofweek': $theTestValue = date('w',$theEvalTime); break;
199 case 'dayofmonth': $theTestValue = date('d',$theEvalTime); break;
200 case 'month': $theTestValue = date('m',$theEvalTime); break;
201 }
202 $theTestValue = intval($theTestValue);
203 // comp
204 $values = explode(',',$pcs[1]);
205 reset($values);
206 while(list(,$test)=each($values)) {
207 $test = trim($test);
208 if (t3lib_div::testInt($test)) {$test='='.$test;}
209 if ($test) {
210 if ($this->testNumber($test,$theTestValue)) {return true;}
211 }
212 }
213 break;
214 case 'usergroup':
215 if ($GLOBALS['TSFE']->gr_list!='0,-1') { // '0,-1' is the default usergroups when not logged in!
216 $values = explode(',',$pcs[1]);
217 while(list(,$test)=each($values)) {
218 $test = trim($test);
219 if ($test) {
220 if ($test=='*' || t3lib_div::inList($GLOBALS['TSFE']->gr_list,$test)) {return true;}
221 }
222 }
223 }
224 break;
225 case 'loginUser':
226 if ($GLOBALS['TSFE']->loginUser) {
227 $values = explode(',',$pcs[1]);
228 while(list(,$test)=each($values)) {
229 $test = trim($test);
230 if ($test) {
231 if ($test=='*' || !strcmp($GLOBALS['TSFE']->fe_user->user['uid'],$test)) {return true;}
232 }
233 }
234 }
235 break;
236 case 'globalVar':
237 $values = explode(',',$pcs[1]);
238 while(list(,$test)=each($values)) {
239 $test = trim($test);
240 if ($test) {
241 $point = strcspn($test,'=<>');
242 $theVarName = substr($test,0,$point);
243 $nv = $this->getGP_ENV_TSFE(trim($theVarName));
244 if ($this->testNumber(substr($test,$point) ,$nv)) {return true;}
245 }
246 }
247 break;
248 case 'globalString':
249 $values = explode(',',$pcs[1]);
250 while(list(,$test)=each($values)) {
251 $test = trim($test);
252 if ($test) {
253 $point = strcspn($test,'=');
254 $theVarName = substr($test,0,$point);
255 $nv = $this->getGP_ENV_TSFE(trim($theVarName));
256 if ($this->matchWild($nv,trim(substr($test,$point+1)))) {return true;}
257 }
258 }
259 break;
260 case 'treeLevel':
261 $values = explode(',',$pcs[1]);
262 $theRootLine = is_array($GLOBALS['TSFE']->tmpl->rootLine) ? $GLOBALS['TSFE']->tmpl->rootLine : $this->altRootLine;
263 $theRLC = count($theRootLine)-1;
264 while(list(,$test)=each($values)) {
265 $test = trim($test);
266 if ($test==$theRLC) { return true; }
267 }
268 break;
269 case 'PIDupinRootline':
270 case 'PIDinRootline':
271 $values = explode(',',$pcs[1]);
272 if (($switchKey=='PIDinRootline') || (!in_array($GLOBALS['TSFE']->id,$values))) {
273 $theRootLine = is_array($GLOBALS['TSFE']->tmpl->rootLine) ? $GLOBALS['TSFE']->tmpl->rootLine : $this->altRootLine;
274 reset($values);
275 while(list(,$test)=each($values)) {
276 $test = trim($test);
277 reset($theRootLine);
278 while(list($rl_key,$rl_dat)=each($theRootLine)) {
279 if ($rl_dat['uid']==$test) { return true; }
280 }
281 }
282 }
283 break;
284 case 'userFunc':
285 $values = split('\(|\)',$pcs[1]);
286 $funcName=trim($values[0]);
287 $funcValue = t3lib_div::trimExplode(',',$values[1]);
288 $pre = $GLOBALS['TSFE']->TYPO3_CONF_VARS['FE']['userFuncClassPrefix'];
289 if ($pre &&
290 !t3lib_div::isFirstPartOfStr(trim($funcName),$pre) &&
291 !t3lib_div::isFirstPartOfStr(trim($funcName),'tx_')
292 ) {
293 if (is_object($GLOBALS['TT'])) $GLOBALS['TT']->setTSlogMessage('Match condition: Function "'.$funcName.'" was not prepended with "'.$pre.'"',3);
294 return false;
295 }
296 if (function_exists($funcName) && call_user_func($funcName, $funcValue[0])) {
297 return true;
298 }
299 break;
300 }
301 }
302 }
303
304 /**
305 * Will evaluate a $value based on an operator: "<", ">" or "=" (default)
306 *
307 * @param string The value to compare with on the form [operator][number]. Eg. "< 123"
308 * @param integer The number
309 * @return boolean If $value is "50" and $test is "< 123" then it will return true.
310 */
311 function testNumber($test,$value) {
312 $test = trim($test);
313 switch(substr($test,0,1)) {
314 case '<':
315 if (doubleval(substr($test,1))>$value) return true;
316 break;
317 case '>':
318 if (doubleval(substr($test,1))<$value) return true;
319 break;
320 default:
321 if (trim(substr($test,1))==$value) return true;
322 break;
323 }
324 }
325
326 /**
327 * Matching two strings against each other, supporting a "*" wildcard in either end of the $needle
328 *
329 * @param string The string in which to find $needle.
330 * @param string The string to find in $haystack
331 * @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 "Netscape*" then it returns true.
332 */
333 function matchWild($haystack,$needle) {
334 if ($needle && $haystack) {
335 if (substr($needle,0,1)=='*') {$mode.='before';}
336 if (substr($needle,-1,1)=='*') {$mode.='after';}
337 switch($mode) {
338 case 'before':
339 $matchStr = substr($needle,1);
340 if (substr($haystack,-strlen($matchStr))==$matchStr) return true;
341 break;
342 case 'after':
343 if (strpos(' '.$haystack,substr($needle,0,-1))==1) return true;
344 break;
345 case 'beforeafter':
346 if (strstr($haystack,substr($needle,1,-1))) return true;
347 break;
348 default:
349 if ($needle==$haystack) return true;
350 break;
351 }
352 }
353 }
354
355 /**
356 * Returns a code for a browsing device based on the input useragent string
357 *
358 * @param string User agent string from browser, t3lib_div::getIndpEnv('HTTP_USER_AGENT')
359 * @return string A code. See link.
360 * @access private
361 * @link http://typo3.org/doc.0.html?&tx_extrepmgm_pi1[extUid]=270&tx_extrepmgm_pi1[tocEl]=296&cHash=a8ae66c7d6
362 */
363 function whichDevice($useragent) {
364 $agent=strtolower(trim($useragent));
365 // pda
366 if( strstr($agent, 'avantgo')) {
367 return 'pda';
368 }
369
370 // wap
371 $browser=substr($agent,0,4);
372 $wapviwer=substr(stristr($agent,'wap'),0,3);
373 if( $wapviwer=='wap' ||
374 $browser=='noki' ||
375 $browser== 'eric' ||
376 $browser== 'r380' ||
377 $browser== 'up.b' ||
378 $browser== 'winw' ||
379 $browser== 'wapa') {
380 return 'wap';
381 }
382
383 // grabber
384 if( strstr($agent, 'g.r.a.b.') ||
385 strstr($agent, 'utilmind httpget') ||
386 strstr($agent, 'webcapture') ||
387 strstr($agent, 'teleport') ||
388 strstr($agent, 'webcopier')) {
389 return 'grabber';
390 }
391
392 // robots
393 if( strstr($agent, 'crawler') ||
394 strstr($agent, 'spider') ||
395 strstr($agent, 'googlebot') ||
396 strstr($agent, 'searchbot') ||
397 strstr($agent, 'infoseek') ||
398 strstr($agent, 'altavista') ||
399 strstr($agent, 'diibot')) {
400 return 'robot';
401 }
402
403 // Hook for extending device recognition capabilities:
404 if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_matchcondition.php']['devices_class'])) {
405 foreach($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_matchcondition.php']['devices_class'] as $_classRef) {
406 $_procObj = &t3lib_div::getUserObj($_classRef);
407 return $_procObj->whichDevice_ext($useragent);
408 }
409 }
410
411 }
412
413 /**
414 * Generates an array with abstracted browser information
415 * In the function match() this method is called and the result stored in $this->browserInfoArray
416 *
417 * @param string The useragent string, t3lib_div::getIndpEnv('HTTP_USER_AGENT')
418 * @return array Contains keys "browser", "version", "system"
419 * @access private
420 * @see match()
421 */
422 function browserInfo($useragent) {
423 $useragent = trim($useragent);
424 $browserInfo=Array();
425 $browserInfo['useragent']=$useragent;
426 if ($useragent) {
427 // browser
428 if (strstr($useragent,'MSIE')) {
429 $browserInfo['browser']='msie';
430 } elseif(strstr($useragent,'Konqueror')) {
431 $browserInfo['browser']='konqueror';
432 } elseif(strstr($useragent,'Opera')) {
433 $browserInfo['browser']='opera';
434 } elseif(strstr($useragent,'Lynx')) {
435 $browserInfo['browser']='lynx';
436 } elseif(strstr($useragent,'PHP')) {
437 $browserInfo['browser']='php';
438 } elseif(strstr($useragent,'AvantGo')) {
439 $browserInfo['browser']='avantgo';
440 } elseif(strstr($useragent,'WebCapture')) {
441 $browserInfo['browser']='acrobat';
442 } elseif(strstr($useragent,'IBrowse')) {
443 $browserInfo['browser']='ibrowse';
444 } elseif(strstr($useragent,'Teleport')) {
445 $browserInfo['browser']='teleport';
446 } elseif(strstr($useragent,'Mozilla')) {
447 $browserInfo['browser']='netscape';
448 } else {
449 $browserInfo['browser']='unknown';
450 }
451 // version
452 switch($browserInfo['browser']) {
453 case 'netscape':
454 $browserInfo['version'] = $this->browserInfo_version(substr($useragent,8));
455 if (strstr($useragent,'Netscape6')) {$browserInfo['version']=6;}
456 break;
457 case 'msie':
458 $tmp = strstr($useragent,'MSIE');
459 $browserInfo['version'] = $this->browserInfo_version(substr($tmp,4));
460 break;
461 case 'opera':
462 $tmp = strstr($useragent,'Opera');
463 $browserInfo['version'] = $this->browserInfo_version(substr($tmp,5));
464 break;
465 case 'lynx':
466 $tmp = strstr($useragent,'Lynx/');
467 $browserInfo['version'] = $this->browserInfo_version(substr($tmp,5));
468 break;
469 case 'php':
470 $tmp = strstr($useragent,'PHP/');
471 $browserInfo['version'] = $this->browserInfo_version(substr($tmp,4));
472 break;
473 case 'avantgo':
474 $tmp = strstr($useragent,'AvantGo');
475 $browserInfo['version'] = $this->browserInfo_version(substr($tmp,7));
476 break;
477 case 'acrobat':
478 $tmp = strstr($useragent,'WebCapture');
479 $browserInfo['version'] = $this->browserInfo_version(substr($tmp,10));
480 break;
481 case 'ibrowse':
482 $tmp = strstr($useragent,'IBrowse/');
483 $browserInfo['version'] = $this->browserInfo_version(substr($tmp,8));
484 break;
485 case 'konqueror':
486 $tmp = strstr($useragent,'Konqueror/');
487 $browserInfo['version'] = $this->browserInfo_version(substr($tmp,10));
488 break;
489 }
490 // system
491 $browserInfo['system']='';
492 if (strstr($useragent,'Win')) {
493 // windows
494 if (strstr($useragent,'Win98') || strstr($useragent,'Windows 98')) {
495 $browserInfo['system']='win98';
496 } elseif (strstr($useragent,'Win95') || strstr($useragent,'Windows 95')) {
497 $browserInfo['system']='win95';
498 } elseif (strstr($useragent,'WinNT') || strstr($useragent,'Windows NT')) {
499 $browserInfo['system']='winNT';
500 } elseif (strstr($useragent,'Win16') || strstr($useragent,'Windows 311')) {
501 $browserInfo['system']='win311';
502 }
503 } elseif (strstr($useragent,'Mac')) {
504 $browserInfo['system']='mac';
505 // unixes
506 } elseif (strstr($useragent,'Linux')) {
507 $browserInfo['system']='linux';
508 } elseif (strstr($useragent,'SGI') && strstr($useragent,' IRIX ')) {
509 $browserInfo['system']='unix_sgi';
510 } elseif (strstr($useragent,' SunOS ')) {
511 $browserInfo['system']='unix_sun';
512 } elseif (strstr($useragent,' HP-UX ')) {
513 $browserInfo['system']='unix_hp';
514 }
515 }
516
517 return $browserInfo;
518 }
519
520 /**
521 * Returns the version of a browser; Basically getting doubleval() of the input string, stripping of any non-numeric values in the beginning of the string first.
522 *
523 * @param string A string with version number, eg. "/7.32 blablabla"
524 * @return double Returns double value, eg. "7.32"
525 */
526 function browserInfo_version($tmp) {
527 return doubleval(ereg_replace('^[^0-9]*','',$tmp));
528 }
529
530 /**
531 * Return global variable where the input string $var defines array keys separated by "|"
532 *
533 * @param string Global var key, eg. "HTTP_GET_VAR" or "HTTP_GET_VARS|id" to get the id GET parameter back.
534 * @param array Alternative array than $GLOBAL to get variables from.
535 * @return mixed Whatever value. If none, then blank string.
536 * @access private
537 */
538 function getGlobal($var,$inArr='') {
539 $vars = explode('|',$var);
540 $c = count($vars);
541 $k = trim($vars[0]);
542 $theVar = is_array($inArr) ? $inArr[$k] : $GLOBALS[$k];
543
544 for ($a=1;$a<$c;$a++) {
545 if (!isset($theVar)) {break;}
546 $theVar = $theVar[trim($vars[$a])];
547 }
548 if (!is_array($theVar)) {
549 return $theVar;
550 } else {
551 return '';
552 }
553 }
554
555 /**
556 * Returns GP / ENV / TSFE vars
557 *
558 * @param string Identifier
559 * @return mixed The value of the variable pointed to.
560 * @access private
561 * @link http://typo3.org/doc.0.html?&tx_extrepmgm_pi1[extUid]=270&tx_extrepmgm_pi1[tocEl]=311&cHash=487cbd5cdf
562 */
563 function getGP_ENV_TSFE($var) {
564 $vars = explode(':',$var,2);
565 if (count($vars)==1) {
566 $val = $this->getGlobal($var);
567 } else {
568 $splitAgain=explode('|',$vars[1],2);
569 $k=trim($splitAgain[0]);
570 if ($k) {
571 switch((string)trim($vars[0])) {
572 case 'GP':
573 $val = t3lib_div::_GP($k);
574 break;
575 case 'TSFE':
576 $val = $GLOBALS['TSFE']->$k;
577 break;
578 case 'ENV':
579 $val = getenv($k);
580 break;
581 case 'IENV':
582 $val = t3lib_div::getIndpEnv($k);
583 break;
584 case 'LIT':
585 return trim($vars[1]); // return litteral value...
586 break;
587 }
588 // If array:
589 if (count($splitAgain)>1) {
590 if (is_array($val) && trim($splitAgain[1])) {
591 $val=$this->getGlobal($splitAgain[1],$val);
592 } else {
593 $val='';
594 }
595 }
596 }
597 }
598 return $val;
599 }
600 }
601
602
603 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_matchcondition.php']) {
604 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_matchcondition.php']);
605 }
606 ?>