Documentation TYPO3 par Ameos |
00001 <?php 00002 /*************************************************************** 00003 * Copyright notice 00004 * 00005 * (c) 1999-2004 Kasper Skaarhoj (kasperYYYY@typo3.com) 00006 * All rights reserved 00007 * 00008 * This script is part of the TYPO3 project. The TYPO3 project is 00009 * free software; you can redistribute it and/or modify 00010 * it under the terms of the GNU General Public License as published by 00011 * the Free Software Foundation; either version 2 of the License, or 00012 * (at your option) any later version. 00013 * 00014 * The GNU General Public License can be found at 00015 * http://www.gnu.org/copyleft/gpl.html. 00016 * A copy is found in the textfile GPL.txt and important notices to the license 00017 * from the author is found in LICENSE.txt distributed with these scripts. 00018 * 00019 * 00020 * This script is distributed in the hope that it will be useful, 00021 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00022 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00023 * GNU General Public License for more details. 00024 * 00025 * This copyright notice MUST APPEAR in all copies of the script! 00026 ***************************************************************/ 00077 class t3lib_matchCondition { 00078 var $matchAlternative=array(); // If this array has elements, the matching returns true if a whole "matchline" is found in the array! 00079 var $matchAll=0; // If set all is matched! 00080 00081 var $altRootLine=array(); 00082 00091 function match($string) { 00092 00093 if ($this->matchAll) return true; 00094 if (count($this->matchAlternative)) { 00095 return in_array($string,$this->matchAlternative); 00096 } 00097 00098 if (!$this->browserInfoArray) { 00099 $this->browserInfoArray = $this->browserInfo(t3lib_div::getIndpEnv('HTTP_USER_AGENT')); 00100 } 00101 $browserInfo = $this->browserInfoArray; 00102 $string = trim($string); 00103 $string = substr($string,1,strlen($string)-2); 00104 $parts = explode('][',$string); 00105 reset($parts); 00106 while(list(,$val)=each($parts)) { 00107 $pcs = explode('=',$val,2); 00108 $switchKey = trim($pcs[0]); 00109 switch($switchKey) { 00110 case 'browser': 00111 $values = explode(',',$pcs[1]); 00112 while(list(,$test)=each($values)) { 00113 if (strstr($browserInfo['browser'].$browserInfo['version'],trim($test))) { 00114 return true; 00115 } 00116 } 00117 break; 00118 case 'version': 00119 $values = explode(',',$pcs[1]); 00120 while(list(,$test)=each($values)) { 00121 $test = trim($test); 00122 if ($test) { 00123 if (strcspn($test,'=<>')==0) { 00124 switch(substr($test,0,1)) { 00125 case '=': 00126 if (doubleval(substr($test,1))==$browserInfo['version']) return true; 00127 break; 00128 case '<': 00129 if (doubleval(substr($test,1))>$browserInfo['version']) return true; 00130 break; 00131 case '>': 00132 if (doubleval(substr($test,1))<$browserInfo['version']) return true; 00133 break; 00134 } 00135 } else { 00136 if (strpos(' '.$browserInfo['version'],$test)==1) {return true;} 00137 } 00138 } 00139 } 00140 break; 00141 case 'system': 00142 $values = explode(',',$pcs[1]); 00143 while(list(,$test)=each($values)) { 00144 $test = trim($test); 00145 if ($test) { 00146 if (strpos(' '.$browserInfo['system'],$test)==1) {return true;} 00147 } 00148 } 00149 break; 00150 case 'device': 00151 $values = explode(',',$pcs[1]); 00152 if (!isset($this->deviceInfo)) { 00153 $this->deviceInfo = $this->whichDevice(t3lib_div::getIndpEnv('HTTP_USER_AGENT')); 00154 } 00155 while(list(,$test)=each($values)) { 00156 $test = trim($test); 00157 if ($test) { 00158 if ($this->deviceInfo==$test) {return true;} 00159 } 00160 } 00161 break; 00162 case 'useragent': 00163 $test = trim($pcs[1]); 00164 if ($test) { 00165 return $this->matchWild($browserInfo['useragent'],$test); 00166 } 00167 break; 00168 case 'language': 00169 $values = explode(',',$pcs[1]); 00170 while(list(,$test)=each($values)) { 00171 $test = trim($test); 00172 if ($test) { 00173 if (ereg('^\*.+\*$',$test)) { 00174 $allLanguages = split('[,;]',t3lib_div::getIndpEnv('HTTP_ACCEPT_LANGUAGE')); 00175 if (in_array(substr($test,1,-1), $allLanguages)) {return true;} 00176 } else { 00177 if (t3lib_div::getIndpEnv('HTTP_ACCEPT_LANGUAGE') == $test) {return true;} 00178 } 00179 } 00180 } 00181 break; 00182 case 'IP': 00183 if (t3lib_div::cmpIP(t3lib_div::getIndpEnv('REMOTE_ADDR'), $pcs[1])) {return true;} 00184 break; 00185 case 'hostname': 00186 if (t3lib_div::cmpFQDN(t3lib_div::getIndpEnv('REMOTE_ADDR'), $pcs[1])) {return true;} 00187 break; 00188 // hour, minute, dayofweek, dayofmonth, month 00189 case 'hour': 00190 case 'minute': 00191 case 'dayofweek': 00192 case 'dayofmonth': 00193 case 'month': 00194 $theEvalTime = $GLOBALS['SIM_EXEC_TIME']; // In order to simulate time properly in templates. 00195 switch($switchKey) { 00196 case 'hour': $theTestValue = date('H',$theEvalTime); break; 00197 case 'minute': $theTestValue = date('i',$theEvalTime); break; 00198 case 'dayofweek': $theTestValue = date('w',$theEvalTime); break; 00199 case 'dayofmonth': $theTestValue = date('d',$theEvalTime); break; 00200 case 'month': $theTestValue = date('m',$theEvalTime); break; 00201 } 00202 $theTestValue = intval($theTestValue); 00203 // comp 00204 $values = explode(',',$pcs[1]); 00205 reset($values); 00206 while(list(,$test)=each($values)) { 00207 $test = trim($test); 00208 if (t3lib_div::testInt($test)) {$test='='.$test;} 00209 if ($test) { 00210 if ($this->testNumber($test,$theTestValue)) {return true;} 00211 } 00212 } 00213 break; 00214 case 'usergroup': 00215 if ($GLOBALS['TSFE']->gr_list!='0,-1') { // '0,-1' is the default usergroups when not logged in! 00216 $values = explode(',',$pcs[1]); 00217 while(list(,$test)=each($values)) { 00218 $test = trim($test); 00219 if ($test) { 00220 if ($test=='*' || t3lib_div::inList($GLOBALS['TSFE']->gr_list,$test)) {return true;} 00221 } 00222 } 00223 } 00224 break; 00225 case 'loginUser': 00226 if ($GLOBALS['TSFE']->loginUser) { 00227 $values = explode(',',$pcs[1]); 00228 while(list(,$test)=each($values)) { 00229 $test = trim($test); 00230 if ($test) { 00231 if ($test=='*' || !strcmp($GLOBALS['TSFE']->fe_user->user['uid'],$test)) {return true;} 00232 } 00233 } 00234 } 00235 break; 00236 case 'globalVar': 00237 $values = explode(',',$pcs[1]); 00238 while(list(,$test)=each($values)) { 00239 $test = trim($test); 00240 if ($test) { 00241 $point = strcspn($test,'=<>'); 00242 $theVarName = substr($test,0,$point); 00243 $nv = $this->getGP_ENV_TSFE(trim($theVarName)); 00244 if ($this->testNumber(substr($test,$point) ,$nv)) {return true;} 00245 } 00246 } 00247 break; 00248 case 'globalString': 00249 $values = explode(',',$pcs[1]); 00250 while(list(,$test)=each($values)) { 00251 $test = trim($test); 00252 if ($test) { 00253 $point = strcspn($test,'='); 00254 $theVarName = substr($test,0,$point); 00255 $nv = $this->getGP_ENV_TSFE(trim($theVarName)); 00256 if ($this->matchWild($nv,trim(substr($test,$point+1)))) {return true;} 00257 } 00258 } 00259 break; 00260 case 'treeLevel': 00261 $values = explode(',',$pcs[1]); 00262 $theRootLine = is_array($GLOBALS['TSFE']->tmpl->rootLine) ? $GLOBALS['TSFE']->tmpl->rootLine : $this->altRootLine; 00263 $theRLC = count($theRootLine)-1; 00264 while(list(,$test)=each($values)) { 00265 $test = trim($test); 00266 if ($test==$theRLC) { return true; } 00267 } 00268 break; 00269 case 'PIDupinRootline': 00270 case 'PIDinRootline': 00271 $values = explode(',',$pcs[1]); 00272 if (($switchKey=='PIDinRootline') || (!in_array($GLOBALS['TSFE']->id,$values))) { 00273 $theRootLine = is_array($GLOBALS['TSFE']->tmpl->rootLine) ? $GLOBALS['TSFE']->tmpl->rootLine : $this->altRootLine; 00274 reset($values); 00275 while(list(,$test)=each($values)) { 00276 $test = trim($test); 00277 reset($theRootLine); 00278 while(list($rl_key,$rl_dat)=each($theRootLine)) { 00279 if ($rl_dat['uid']==$test) { return true; } 00280 } 00281 } 00282 } 00283 break; 00284 case 'userFunc': 00285 $values = split('\(|\)',$pcs[1]); 00286 $funcName=trim($values[0]); 00287 $funcValue = t3lib_div::trimExplode(',',$values[1]); 00288 $pre = $GLOBALS['TSFE']->TYPO3_CONF_VARS['FE']['userFuncClassPrefix']; 00289 if ($pre && 00290 !t3lib_div::isFirstPartOfStr(trim($funcName),$pre) && 00291 !t3lib_div::isFirstPartOfStr(trim($funcName),'tx_') 00292 ) { 00293 if (is_object($GLOBALS['TT'])) $GLOBALS['TT']->setTSlogMessage('Match condition: Function "'.$funcName.'" was not prepended with "'.$pre.'"',3); 00294 return false; 00295 } 00296 if (function_exists($funcName) && call_user_func($funcName, $funcValue[0])) { 00297 return true; 00298 } 00299 break; 00300 } 00301 } 00302 } 00303 00311 function testNumber($test,$value) { 00312 $test = trim($test); 00313 switch(substr($test,0,1)) { 00314 case '<': 00315 if (doubleval(substr($test,1))>$value) return true; 00316 break; 00317 case '>': 00318 if (doubleval(substr($test,1))<$value) return true; 00319 break; 00320 default: 00321 if (trim(substr($test,1))==$value) return true; 00322 break; 00323 } 00324 } 00325 00333 function matchWild($haystack,$needle) { 00334 if ($needle && $haystack) { 00335 if (substr($needle,0,1)=='*') {$mode.='before';} 00336 if (substr($needle,-1,1)=='*') {$mode.='after';} 00337 switch($mode) { 00338 case 'before': 00339 $matchStr = substr($needle,1); 00340 if (substr($haystack,-strlen($matchStr))==$matchStr) return true; 00341 break; 00342 case 'after': 00343 if (strpos(' '.$haystack,substr($needle,0,-1))==1) return true; 00344 break; 00345 case 'beforeafter': 00346 if (strstr($haystack,substr($needle,1,-1))) return true; 00347 break; 00348 default: 00349 if ($needle==$haystack) return true; 00350 break; 00351 } 00352 } 00353 } 00354 00363 function whichDevice($useragent) { 00364 $agent=strtolower(trim($useragent)); 00365 // pda 00366 if( strstr($agent, 'avantgo')) { 00367 return 'pda'; 00368 } 00369 00370 // wap 00371 $browser=substr($agent,0,4); 00372 $wapviwer=substr(stristr($agent,'wap'),0,3); 00373 if( $wapviwer=='wap' || 00374 $browser=='noki' || 00375 $browser== 'eric' || 00376 $browser== 'r380' || 00377 $browser== 'up.b' || 00378 $browser== 'winw' || 00379 $browser== 'wapa') { 00380 return 'wap'; 00381 } 00382 00383 // grabber 00384 if( strstr($agent, 'g.r.a.b.') || 00385 strstr($agent, 'utilmind httpget') || 00386 strstr($agent, 'webcapture') || 00387 strstr($agent, 'teleport') || 00388 strstr($agent, 'webcopier')) { 00389 return 'grabber'; 00390 } 00391 00392 // robots 00393 if( strstr($agent, 'crawler') || 00394 strstr($agent, 'spider') || 00395 strstr($agent, 'googlebot') || 00396 strstr($agent, 'searchbot') || 00397 strstr($agent, 'infoseek') || 00398 strstr($agent, 'altavista') || 00399 strstr($agent, 'diibot')) { 00400 return 'robot'; 00401 } 00402 } 00403 00413 function browserInfo($useragent) { 00414 $useragent = trim($useragent); 00415 $browserInfo=Array(); 00416 $browserInfo['useragent']=$useragent; 00417 if ($useragent) { 00418 // browser 00419 if (strstr($useragent,'MSIE')) { 00420 $browserInfo['browser']='msie'; 00421 } elseif(strstr($useragent,'Konqueror')) { 00422 $browserInfo['browser']='konqueror'; 00423 } elseif(strstr($useragent,'Opera')) { 00424 $browserInfo['browser']='opera'; 00425 } elseif(strstr($useragent,'Lynx')) { 00426 $browserInfo['browser']='lynx'; 00427 } elseif(strstr($useragent,'PHP')) { 00428 $browserInfo['browser']='php'; 00429 } elseif(strstr($useragent,'AvantGo')) { 00430 $browserInfo['browser']='avantgo'; 00431 } elseif(strstr($useragent,'WebCapture')) { 00432 $browserInfo['browser']='acrobat'; 00433 } elseif(strstr($useragent,'IBrowse')) { 00434 $browserInfo['browser']='ibrowse'; 00435 } elseif(strstr($useragent,'Teleport')) { 00436 $browserInfo['browser']='teleport'; 00437 } elseif(strstr($useragent,'Mozilla')) { 00438 $browserInfo['browser']='netscape'; 00439 } else { 00440 $browserInfo['browser']='unknown'; 00441 } 00442 // version 00443 switch($browserInfo['browser']) { 00444 case 'netscape': 00445 $browserInfo['version'] = $this->browserInfo_version(substr($useragent,8)); 00446 if (strstr($useragent,'Netscape6')) {$browserInfo['version']=6;} 00447 break; 00448 case 'msie': 00449 $tmp = strstr($useragent,'MSIE'); 00450 $browserInfo['version'] = $this->browserInfo_version(substr($tmp,4)); 00451 break; 00452 case 'opera': 00453 $tmp = strstr($useragent,'Opera'); 00454 $browserInfo['version'] = $this->browserInfo_version(substr($tmp,5)); 00455 break; 00456 case 'lynx': 00457 $tmp = strstr($useragent,'Lynx/'); 00458 $browserInfo['version'] = $this->browserInfo_version(substr($tmp,5)); 00459 break; 00460 case 'php': 00461 $tmp = strstr($useragent,'PHP/'); 00462 $browserInfo['version'] = $this->browserInfo_version(substr($tmp,4)); 00463 break; 00464 case 'avantgo': 00465 $tmp = strstr($useragent,'AvantGo'); 00466 $browserInfo['version'] = $this->browserInfo_version(substr($tmp,7)); 00467 break; 00468 case 'acrobat': 00469 $tmp = strstr($useragent,'WebCapture'); 00470 $browserInfo['version'] = $this->browserInfo_version(substr($tmp,10)); 00471 break; 00472 case 'ibrowse': 00473 $tmp = strstr($useragent,'IBrowse/'); 00474 $browserInfo['version'] = $this->browserInfo_version(substr($tmp,8)); 00475 break; 00476 case 'konqueror': 00477 $tmp = strstr($useragent,'Konqueror/'); 00478 $browserInfo['version'] = $this->browserInfo_version(substr($tmp,10)); 00479 break; 00480 } 00481 // system 00482 $browserInfo['system']=''; 00483 if (strstr($useragent,'Win')) { 00484 // windows 00485 if (strstr($useragent,'Win98') || strstr($useragent,'Windows 98')) { 00486 $browserInfo['system']='win98'; 00487 } elseif (strstr($useragent,'Win95') || strstr($useragent,'Windows 95')) { 00488 $browserInfo['system']='win95'; 00489 } elseif (strstr($useragent,'WinNT') || strstr($useragent,'Windows NT')) { 00490 $browserInfo['system']='winNT'; 00491 } elseif (strstr($useragent,'Win16') || strstr($useragent,'Windows 311')) { 00492 $browserInfo['system']='win311'; 00493 } 00494 } elseif (strstr($useragent,'Mac')) { 00495 $browserInfo['system']='mac'; 00496 // unixes 00497 } elseif (strstr($useragent,'Linux')) { 00498 $browserInfo['system']='linux'; 00499 } elseif (strstr($useragent,'SGI') && strstr($useragent,' IRIX ')) { 00500 $browserInfo['system']='unix_sgi'; 00501 } elseif (strstr($useragent,' SunOS ')) { 00502 $browserInfo['system']='unix_sun'; 00503 } elseif (strstr($useragent,' HP-UX ')) { 00504 $browserInfo['system']='unix_hp'; 00505 } 00506 } 00507 00508 return $browserInfo; 00509 } 00510 00517 function browserInfo_version($tmp) { 00518 return doubleval(ereg_replace('^[^0-9]*','',$tmp)); 00519 } 00520 00529 function getGlobal($var,$inArr='') { 00530 $vars = explode('|',$var); 00531 $c = count($vars); 00532 $k = trim($vars[0]); 00533 $theVar = is_array($inArr) ? $inArr[$k] : $GLOBALS[$k]; 00534 00535 for ($a=1;$a<$c;$a++) { 00536 if (!isset($theVar)) {break;} 00537 $theVar = $theVar[trim($vars[$a])]; 00538 } 00539 if (!is_array($theVar)) { 00540 return $theVar; 00541 } else { 00542 return ''; 00543 } 00544 } 00545 00554 function getGP_ENV_TSFE($var) { 00555 $vars = explode(':',$var,2); 00556 if (count($vars)==1) { 00557 $val = $this->getGlobal($var); 00558 } else { 00559 $splitAgain=explode('|',$vars[1],2); 00560 $k=trim($splitAgain[0]); 00561 if ($k) { 00562 switch((string)trim($vars[0])) { 00563 case 'GP': 00564 $val = t3lib_div::_GP($k); 00565 break; 00566 case 'TSFE': 00567 $val = $GLOBALS['TSFE']->$k; 00568 break; 00569 case 'ENV': 00570 $val = getenv($k); 00571 break; 00572 case 'IENV': 00573 $val = t3lib_div::getIndpEnv($k); 00574 break; 00575 case 'LIT': 00576 return trim($vars[1]); // return litteral value... 00577 break; 00578 } 00579 // If array: 00580 if (count($splitAgain)>1) { 00581 if (is_array($val) && trim($splitAgain[1])) { 00582 $val=$this->getGlobal($splitAgain[1],$val); 00583 } else { 00584 $val=''; 00585 } 00586 } 00587 } 00588 } 00589 return $val; 00590 } 00591 } 00592 00593 00594 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_matchcondition.php']) { 00595 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_matchcondition.php']); 00596 } 00597 ?>