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