Updated years from 2004 to 2005 + all function indexes. NO actual code changes done!
[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
404 /**
405 * Generates an array with abstracted browser information
406 * In the function match() this method is called and the result stored in $this->browserInfoArray
407 *
408 * @param string The useragent string, t3lib_div::getIndpEnv('HTTP_USER_AGENT')
409 * @return array Contains keys "browser", "version", "system"
410 * @access private
411 * @see match()
412 */
413 function browserInfo($useragent) {
414 $useragent = trim($useragent);
415 $browserInfo=Array();
416 $browserInfo['useragent']=$useragent;
417 if ($useragent) {
418 // browser
419 if (strstr($useragent,'MSIE')) {
420 $browserInfo['browser']='msie';
421 } elseif(strstr($useragent,'Konqueror')) {
422 $browserInfo['browser']='konqueror';
423 } elseif(strstr($useragent,'Opera')) {
424 $browserInfo['browser']='opera';
425 } elseif(strstr($useragent,'Lynx')) {
426 $browserInfo['browser']='lynx';
427 } elseif(strstr($useragent,'PHP')) {
428 $browserInfo['browser']='php';
429 } elseif(strstr($useragent,'AvantGo')) {
430 $browserInfo['browser']='avantgo';
431 } elseif(strstr($useragent,'WebCapture')) {
432 $browserInfo['browser']='acrobat';
433 } elseif(strstr($useragent,'IBrowse')) {
434 $browserInfo['browser']='ibrowse';
435 } elseif(strstr($useragent,'Teleport')) {
436 $browserInfo['browser']='teleport';
437 } elseif(strstr($useragent,'Mozilla')) {
438 $browserInfo['browser']='netscape';
439 } else {
440 $browserInfo['browser']='unknown';
441 }
442 // version
443 switch($browserInfo['browser']) {
444 case 'netscape':
445 $browserInfo['version'] = $this->browserInfo_version(substr($useragent,8));
446 if (strstr($useragent,'Netscape6')) {$browserInfo['version']=6;}
447 break;
448 case 'msie':
449 $tmp = strstr($useragent,'MSIE');
450 $browserInfo['version'] = $this->browserInfo_version(substr($tmp,4));
451 break;
452 case 'opera':
453 $tmp = strstr($useragent,'Opera');
454 $browserInfo['version'] = $this->browserInfo_version(substr($tmp,5));
455 break;
456 case 'lynx':
457 $tmp = strstr($useragent,'Lynx/');
458 $browserInfo['version'] = $this->browserInfo_version(substr($tmp,5));
459 break;
460 case 'php':
461 $tmp = strstr($useragent,'PHP/');
462 $browserInfo['version'] = $this->browserInfo_version(substr($tmp,4));
463 break;
464 case 'avantgo':
465 $tmp = strstr($useragent,'AvantGo');
466 $browserInfo['version'] = $this->browserInfo_version(substr($tmp,7));
467 break;
468 case 'acrobat':
469 $tmp = strstr($useragent,'WebCapture');
470 $browserInfo['version'] = $this->browserInfo_version(substr($tmp,10));
471 break;
472 case 'ibrowse':
473 $tmp = strstr($useragent,'IBrowse/');
474 $browserInfo['version'] = $this->browserInfo_version(substr($tmp,8));
475 break;
476 case 'konqueror':
477 $tmp = strstr($useragent,'Konqueror/');
478 $browserInfo['version'] = $this->browserInfo_version(substr($tmp,10));
479 break;
480 }
481 // system
482 $browserInfo['system']='';
483 if (strstr($useragent,'Win')) {
484 // windows
485 if (strstr($useragent,'Win98') || strstr($useragent,'Windows 98')) {
486 $browserInfo['system']='win98';
487 } elseif (strstr($useragent,'Win95') || strstr($useragent,'Windows 95')) {
488 $browserInfo['system']='win95';
489 } elseif (strstr($useragent,'WinNT') || strstr($useragent,'Windows NT')) {
490 $browserInfo['system']='winNT';
491 } elseif (strstr($useragent,'Win16') || strstr($useragent,'Windows 311')) {
492 $browserInfo['system']='win311';
493 }
494 } elseif (strstr($useragent,'Mac')) {
495 $browserInfo['system']='mac';
496 // unixes
497 } elseif (strstr($useragent,'Linux')) {
498 $browserInfo['system']='linux';
499 } elseif (strstr($useragent,'SGI') && strstr($useragent,' IRIX ')) {
500 $browserInfo['system']='unix_sgi';
501 } elseif (strstr($useragent,' SunOS ')) {
502 $browserInfo['system']='unix_sun';
503 } elseif (strstr($useragent,' HP-UX ')) {
504 $browserInfo['system']='unix_hp';
505 }
506 }
507
508 return $browserInfo;
509 }
510
511 /**
512 * 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.
513 *
514 * @param string A string with version number, eg. "/7.32 blablabla"
515 * @return double Returns double value, eg. "7.32"
516 */
517 function browserInfo_version($tmp) {
518 return doubleval(ereg_replace('^[^0-9]*','',$tmp));
519 }
520
521 /**
522 * Return global variable where the input string $var defines array keys separated by "|"
523 *
524 * @param string Global var key, eg. "HTTP_GET_VAR" or "HTTP_GET_VARS|id" to get the id GET parameter back.
525 * @param array Alternative array than $GLOBAL to get variables from.
526 * @return mixed Whatever value. If none, then blank string.
527 * @access private
528 */
529 function getGlobal($var,$inArr='') {
530 $vars = explode('|',$var);
531 $c = count($vars);
532 $k = trim($vars[0]);
533 $theVar = is_array($inArr) ? $inArr[$k] : $GLOBALS[$k];
534
535 for ($a=1;$a<$c;$a++) {
536 if (!isset($theVar)) {break;}
537 $theVar = $theVar[trim($vars[$a])];
538 }
539 if (!is_array($theVar)) {
540 return $theVar;
541 } else {
542 return '';
543 }
544 }
545
546 /**
547 * Returns GP / ENV / TSFE vars
548 *
549 * @param string Identifier
550 * @return mixed The value of the variable pointed to.
551 * @access private
552 * @link http://typo3.org/doc.0.html?&tx_extrepmgm_pi1[extUid]=270&tx_extrepmgm_pi1[tocEl]=311&cHash=487cbd5cdf
553 */
554 function getGP_ENV_TSFE($var) {
555 $vars = explode(':',$var,2);
556 if (count($vars)==1) {
557 $val = $this->getGlobal($var);
558 } else {
559 $splitAgain=explode('|',$vars[1],2);
560 $k=trim($splitAgain[0]);
561 if ($k) {
562 switch((string)trim($vars[0])) {
563 case 'GP':
564 $val = t3lib_div::_GP($k);
565 break;
566 case 'TSFE':
567 $val = $GLOBALS['TSFE']->$k;
568 break;
569 case 'ENV':
570 $val = getenv($k);
571 break;
572 case 'IENV':
573 $val = t3lib_div::getIndpEnv($k);
574 break;
575 case 'LIT':
576 return trim($vars[1]); // return litteral value...
577 break;
578 }
579 // If array:
580 if (count($splitAgain)>1) {
581 if (is_array($val) && trim($splitAgain[1])) {
582 $val=$this->getGlobal($splitAgain[1],$val);
583 } else {
584 $val='';
585 }
586 }
587 }
588 }
589 return $val;
590 }
591 }
592
593
594 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_matchcondition.php']) {
595 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_matchcondition.php']);
596 }
597 ?>