* Fixed some browser detection bugs in t3lib_matchcondition and t3lib_div. Not necess...
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_matchcondition.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 1999-2004 Kasper Skaarhoj (kasper@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 <kasper@typo3.com>
34 */
35 /**
36 * [CLASS/FUNCTION INDEX of SCRIPT]
37 *
38 *
39 *
40 * 77: class t3lib_matchCondition
41 * 91: function match($string)
42 * 303: function testNumber($test,$value)
43 * 325: function matchWild($haystack,$needle)
44 * 355: function whichDevice($useragent)
45 * 405: function browserInfo($useragent)
46 * 503: function browserInfo_version($tmp)
47 * 515: function getGlobal($var,$inArr='')
48 * 540: 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 <kasper@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 (t3lib_div::getIndpEnv('HTTP_ACCEPT_LANGUAGE') == $test) {return true;}
174 }
175 }
176 break;
177 case 'IP':
178 if (t3lib_div::cmpIP(t3lib_div::getIndpEnv('REMOTE_ADDR'), $pcs[1])) {return true;}
179 break;
180 // hour, minute, dayofweek, dayofmonth, month
181 case 'hour':
182 case 'minute':
183 case 'dayofweek':
184 case 'dayofmonth':
185 case 'month':
186 $theEvalTime = $GLOBALS['SIM_EXEC_TIME']; // In order to simulate time properly in templates.
187 switch($switchKey) {
188 case 'hour': $theTestValue = date('H',$theEvalTime); break;
189 case 'minute': $theTestValue = date('i',$theEvalTime); break;
190 case 'dayofweek': $theTestValue = date('w',$theEvalTime); break;
191 case 'dayofmonth': $theTestValue = date('d',$theEvalTime); break;
192 case 'month': $theTestValue = date('m',$theEvalTime); break;
193 }
194 $theTestValue = intval($theTestValue);
195 // comp
196 $values = explode(',',$pcs[1]);
197 reset($values);
198 while(list(,$test)=each($values)) {
199 $test = trim($test);
200 if (t3lib_div::testInt($test)) {$test='='.$test;}
201 if ($test) {
202 if ($this->testNumber($test,$theTestValue)) {return true;}
203 }
204 }
205 break;
206 case 'usergroup':
207 if ($GLOBALS['TSFE']->gr_list!='0,-1') { // '0,-1' is the default usergroups when not logged in!
208 $values = explode(',',$pcs[1]);
209 while(list(,$test)=each($values)) {
210 $test = trim($test);
211 if ($test) {
212 if ($test=='*' || t3lib_div::inList($GLOBALS['TSFE']->gr_list,$test)) {return true;}
213 }
214 }
215 }
216 break;
217 case 'loginUser':
218 if ($GLOBALS['TSFE']->loginUser) {
219 $values = explode(',',$pcs[1]);
220 while(list(,$test)=each($values)) {
221 $test = trim($test);
222 if ($test) {
223 if ($test=='*' || !strcmp($GLOBALS['TSFE']->fe_user->user['uid'],$test)) {return true;}
224 }
225 }
226 }
227 break;
228 case 'globalVar':
229 $values = explode(',',$pcs[1]);
230 while(list(,$test)=each($values)) {
231 $test = trim($test);
232 if ($test) {
233 $point = strcspn($test,'=<>');
234 $theVarName = substr($test,0,$point);
235 $nv = $this->getGP_ENV_TSFE(trim($theVarName));
236 if ($this->testNumber(substr($test,$point) ,$nv)) {return true;}
237 }
238 }
239 break;
240 case 'globalString':
241 $values = explode(',',$pcs[1]);
242 while(list(,$test)=each($values)) {
243 $test = trim($test);
244 if ($test) {
245 $point = strcspn($test,'=');
246 $theVarName = substr($test,0,$point);
247 $nv = $this->getGP_ENV_TSFE(trim($theVarName));
248 if ($this->matchWild($nv,trim(substr($test,$point+1)))) {return true;}
249 }
250 }
251 break;
252 case 'treeLevel':
253 $values = explode(',',$pcs[1]);
254 $theRootLine = is_array($GLOBALS['TSFE']->tmpl->rootLine) ? $GLOBALS['TSFE']->tmpl->rootLine : $this->altRootLine;
255 $theRLC = count($theRootLine)-1;
256 while(list(,$test)=each($values)) {
257 $test = trim($test);
258 if ($test==$theRLC) { return true; }
259 }
260 break;
261 case 'PIDupinRootline':
262 case 'PIDinRootline':
263 $values = explode(',',$pcs[1]);
264 if (($switchKey=='PIDinRootline') || (!in_array($GLOBALS['TSFE']->id,$values))) {
265 $theRootLine = is_array($GLOBALS['TSFE']->tmpl->rootLine) ? $GLOBALS['TSFE']->tmpl->rootLine : $this->altRootLine;
266 reset($values);
267 while(list(,$test)=each($values)) {
268 $test = trim($test);
269 reset($theRootLine);
270 while(list($rl_key,$rl_dat)=each($theRootLine)) {
271 if ($rl_dat['uid']==$test) { return true; }
272 }
273 }
274 }
275 break;
276 case 'userFunc':
277 $values = split('\(|\)',$pcs[1]);
278 $funcName=trim($values[0]);
279 $funcValue = t3lib_div::trimExplode(',',$values[1]);
280 $pre = $GLOBALS['TSFE']->TYPO3_CONF_VARS['FE']['userFuncClassPrefix'];
281 if ($pre &&
282 !t3lib_div::isFirstPartOfStr(trim($funcName),$pre) &&
283 !t3lib_div::isFirstPartOfStr(trim($funcName),'tx_')
284 ) {
285 if (is_object($GLOBALS['TT'])) $GLOBALS['TT']->setTSlogMessage('Match condition: Function "'.$funcName.'" was not prepended with "'.$pre.'"',3);
286 return false;
287 }
288 if (function_exists($funcName) && call_user_func($funcName, $funcValue[0])) {
289 return true;
290 }
291 break;
292 }
293 }
294 }
295
296 /**
297 * Will evaluate a $value based on an operator: "<", ">" or "=" (default)
298 *
299 * @param string The value to compare with on the form [operator][number]. Eg. "< 123"
300 * @param integer The number
301 * @return boolean If $value is "50" and $test is "< 123" then it will return true.
302 */
303 function testNumber($test,$value) {
304 $test = trim($test);
305 switch(substr($test,0,1)) {
306 case '<':
307 if (doubleval(substr($test,1))>$value) return true;
308 break;
309 case '>':
310 if (doubleval(substr($test,1))<$value) return true;
311 break;
312 default:
313 if (trim(substr($test,1))==$value) return true;
314 break;
315 }
316 }
317
318 /**
319 * Matching two strings against each other, supporting a "*" wildcard in either end of the $needle
320 *
321 * @param string The string in which to find $needle.
322 * @param string The string to find in $haystack
323 * @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.
324 */
325 function matchWild($haystack,$needle) {
326 if ($needle && $haystack) {
327 if (substr($needle,0,1)=='*') {$mode.='before';}
328 if (substr($needle,-1,1)=='*') {$mode.='after';}
329 switch($mode) {
330 case 'before':
331 $matchStr = substr($needle,1);
332 if (substr($haystack,-strlen($matchStr))==$matchStr) return true;
333 break;
334 case 'after':
335 if (strpos(' '.$haystack,substr($needle,0,-1))==1) return true;
336 break;
337 case 'beforeafter':
338 if (strstr($haystack,substr($needle,1,-1))) return true;
339 break;
340 default:
341 if ($needle==$haystack) return true;
342 break;
343 }
344 }
345 }
346
347 /**
348 * Returns a code for a browsing device based on the input useragent string
349 *
350 * @param string User agent string from browser, t3lib_div::getIndpEnv('HTTP_USER_AGENT')
351 * @return string A code. See link.
352 * @access private
353 * @link http://typo3.org/doc.0.html?&tx_extrepmgm_pi1[extUid]=270&tx_extrepmgm_pi1[tocEl]=296&cHash=a8ae66c7d6
354 */
355 function whichDevice($useragent) {
356 $agent=strtolower(trim($useragent));
357 // pda
358 if( strstr($agent, 'avantgo')) {
359 return 'pda';
360 }
361
362 // wap
363 $browser=substr($agent,0,4);
364 $wapviwer=substr(stristr($agent,'wap'),0,3);
365 if( $wapviwer=='wap' ||
366 $browser=='noki' ||
367 $browser== 'eric' ||
368 $browser== 'r380' ||
369 $browser== 'up.b' ||
370 $browser== 'winw' ||
371 $browser== 'wapa') {
372 return 'wap';
373 }
374
375 // grabber
376 if( strstr($agent, 'g.r.a.b.') ||
377 strstr($agent, 'utilmind httpget') ||
378 strstr($agent, 'webcapture') ||
379 strstr($agent, 'teleport') ||
380 strstr($agent, 'webcopier')) {
381 return 'grabber';
382 }
383
384 // robots
385 if( strstr($agent, 'crawler') ||
386 strstr($agent, 'spider') ||
387 strstr($agent, 'googlebot') ||
388 strstr($agent, 'searchbot') ||
389 strstr($agent, 'infoseek') ||
390 strstr($agent, 'altavista') ||
391 strstr($agent, 'diibot')) {
392 return 'robot';
393 }
394 }
395
396 /**
397 * Generates an array with abstracted browser information
398 * In the function match() this method is called and the result stored in $this->browserInfoArray
399 *
400 * @param string The useragent string, t3lib_div::getIndpEnv('HTTP_USER_AGENT')
401 * @return array Contains keys "browser", "version", "system"
402 * @access private
403 * @see match()
404 */
405 function browserInfo($useragent) {
406 $useragent = trim($useragent);
407 $browserInfo=Array();
408 $browserInfo['useragent']=$useragent;
409 if ($useragent) {
410 // browser
411 if(strstr($useragent,'Opera')) {
412 $browserInfo['browser']='opera';
413 } elseif(strstr($useragent,'Lynx')) {
414 $browserInfo['browser']='lynx';
415 } elseif(strstr($useragent,'PHP')) {
416 $browserInfo['browser']='php';
417 } elseif(strstr($useragent,'AvantGo')) {
418 $browserInfo['browser']='avantgo';
419 } elseif(strstr($useragent,'WebCapture')) {
420 $browserInfo['browser']='acrobat';
421 } elseif(strstr($useragent,'IBrowse')) {
422 $browserInfo['browser']='ibrowse';
423 } elseif(strstr($useragent,'Teleport')) {
424 $browserInfo['browser']='teleport';
425 } elseif(strstr($useragent,'Mozilla')) {
426 $browserInfo['browser']='netscape';
427 } elseif (strstr($useragent,'MSIE')) {
428 $browserInfo['browser']='msie';
429 } else {
430 $browserInfo['browser']='unknown';
431 }
432 // version
433 switch($browserInfo['browser']) {
434 case 'netscape':
435 $browserInfo['version'] = $this->browserInfo_version(substr($useragent,8));
436 if (strstr($useragent,'Netscape6')) {$browserInfo['version']=6;}
437 break;
438 case 'msie':
439 $tmp = strstr($useragent,'MSIE');
440 $browserInfo['version'] = $this->browserInfo_version(substr($tmp,4));
441 break;
442 case 'opera':
443 $tmp = strstr($useragent,'Opera');
444 $browserInfo['version'] = $this->browserInfo_version(substr($tmp,5));
445 break;
446 case 'lynx':
447 $tmp = strstr($useragent,'Lynx/');
448 $browserInfo['version'] = $this->browserInfo_version(substr($tmp,5));
449 break;
450 case 'php':
451 $tmp = strstr($useragent,'PHP/');
452 $browserInfo['version'] = $this->browserInfo_version(substr($tmp,4));
453 break;
454 case 'avantgo':
455 $tmp = strstr($useragent,'AvantGo');
456 $browserInfo['version'] = $this->browserInfo_version(substr($tmp,5));
457 break;
458 case 'acrobat':
459 $tmp = strstr($useragent,'WebCapture');
460 $browserInfo['version'] = $this->browserInfo_version(substr($tmp,5));
461 break;
462 case 'ibrowse':
463 $tmp = strstr($useragent,'IBrowse/');
464 $browserInfo['version'] = $this->browserInfo_version(substr($tmp,5));
465 break;
466 }
467 // system
468 $browserInfo['system']='';
469 if (strstr($useragent,'Win')) {
470 // windows
471 if (strstr($useragent,'Win98') || strstr($useragent,'Windows 98')) {
472 $browserInfo['system']='win98';
473 } elseif (strstr($useragent,'Win95') || strstr($useragent,'Windows 95')) {
474 $browserInfo['system']='win95';
475 } elseif (strstr($useragent,'WinNT') || strstr($useragent,'Windows NT')) {
476 $browserInfo['system']='winNT';
477 } elseif (strstr($useragent,'Win16') || strstr($useragent,'Windows 311')) {
478 $browserInfo['system']='win311';
479 }
480 } elseif (strstr($useragent,'Mac')) {
481 $browserInfo['system']='mac';
482 // unixes
483 } elseif (strstr($useragent,'Linux')) {
484 $browserInfo['system']='linux';
485 } elseif (strstr($useragent,'SGI') && strstr($useragent,' IRIX ')) {
486 $browserInfo['system']='unix_sgi';
487 } elseif (strstr($useragent,' SunOS ')) {
488 $browserInfo['system']='unix_sun';
489 } elseif (strstr($useragent,' HP-UX ')) {
490 $browserInfo['system']='unix_hp';
491 }
492 }
493
494 return $browserInfo;
495 }
496
497 /**
498 * 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.
499 *
500 * @param string A string with version number, eg. "/7.32 blablabla"
501 * @return double Returns double value, eg. "7.32"
502 */
503 function browserInfo_version($tmp) {
504 return doubleval(ereg_replace('^[^0-9]*','',$tmp));
505 }
506
507 /**
508 * Return global variable where the input string $var defines array keys separated by "|"
509 *
510 * @param string Global var key, eg. "HTTP_GET_VAR" or "HTTP_GET_VARS|id" to get the id GET parameter back.
511 * @param array Alternative array than $GLOBAL to get variables from.
512 * @return mixed Whatever value. If none, then blank string.
513 * @access private
514 */
515 function getGlobal($var,$inArr='') {
516 $vars = explode('|',$var);
517 $c = count($vars);
518 $k = trim($vars[0]);
519 $theVar = is_array($inArr) ? $inArr[$k] : $GLOBALS[$k];
520
521 for ($a=1;$a<$c;$a++) {
522 if (!isset($theVar)) {break;}
523 $theVar = $theVar[trim($vars[$a])];
524 }
525 if (!is_array($theVar)) {
526 return $theVar;
527 } else {
528 return '';
529 }
530 }
531
532 /**
533 * Returns GP / ENV / TSFE vars
534 *
535 * @param string Identifier
536 * @return mixed The value of the variable pointed to.
537 * @access private
538 * @link http://typo3.org/doc.0.html?&tx_extrepmgm_pi1[extUid]=270&tx_extrepmgm_pi1[tocEl]=311&cHash=487cbd5cdf
539 */
540 function getGP_ENV_TSFE($var) {
541 $vars = explode(':',$var,2);
542 if (count($vars)==1) {
543 $val = $this->getGlobal($var);
544 } else {
545 $splitAgain=explode('|',$vars[1],2);
546 $k=trim($splitAgain[0]);
547 if ($k) {
548 switch((string)trim($vars[0])) {
549 case 'GP':
550 $val = t3lib_div::_GP($k);
551 break;
552 case 'TSFE':
553 $val = $GLOBALS['TSFE']->$k;
554 break;
555 case 'ENV':
556 $val = getenv($k);
557 break;
558 case 'IENV':
559 $val = t3lib_div::getIndpEnv($k);
560 break;
561 case 'LIT':
562 return trim($vars[1]); // return litteral value...
563 break;
564 }
565 // If array:
566 if (count($splitAgain)>1) {
567 if (is_array($val) && trim($splitAgain[1])) {
568 $val=$this->getGlobal($splitAgain[1],$val);
569 } else {
570 $val='';
571 }
572 }
573 }
574 }
575 return $val;
576 }
577 }
578
579
580 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_matchcondition.php']) {
581 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_matchcondition.php']);
582 }
583 ?>