Documentation TYPO3 par Ameos |
00001 <?php 00002 /*************************************************************** 00003 * Copyright notice 00004 * 00005 * (c) 1999-2006 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 ***************************************************************/ 00080 class t3lib_matchCondition { 00081 var $matchAlternative=array(); // If this array has elements, the matching returns true if a whole "matchline" is found in the array! 00082 var $matchAll=0; // If set all is matched! 00083 00084 var $altRootLine=array(); 00085 var $hookObjectsArr = array(); 00086 00092 function __construct() { 00093 global $TYPO3_CONF_VARS; 00094 00095 // Usage (ext_localconf.php): 00096 // $TYPO3_CONF_VARS['SC_OPTIONS']['t3lib/class.t3lib_matchcondition.php']['matchConditionClass'] = 00097 // 'EXT:my_ext/class.browserinfo.php:MyBrowserInfoClass'; 00098 if (is_array($TYPO3_CONF_VARS['SC_OPTIONS']['t3lib/class.t3lib_matchcondition.php']['matchConditionClass'])) { 00099 foreach ($TYPO3_CONF_VARS['SC_OPTIONS']['t3lib/class.t3lib_matchcondition.php']['matchConditionClass'] as $classRef) { 00100 $this->hookObjectsArr[] = &t3lib_div::getUserObj($classRef, ''); 00101 } 00102 } 00103 } 00104 00110 function t3lib_matchCondition() { 00111 $this->__construct(); 00112 } 00113 00120 function match($condition_line) { 00121 if ($this->matchAll) { 00122 return true; 00123 } 00124 if (count($this->matchAlternative)) { 00125 return in_array($condition_line, $this->matchAlternative); 00126 } 00127 00128 // Getting the value from inside of the wrapping square brackets of the condition line: 00129 $insideSqrBrackets = substr(trim($condition_line), 1, strlen($condition_line) - 2); 00130 $insideSqrBrackets = preg_replace('/\]\s*OR\s*\[/i', ']||[', $insideSqrBrackets); 00131 $insideSqrBrackets = preg_replace('/\]\s*AND\s*\[/i', ']&&[', $insideSqrBrackets); 00132 00133 // The "weak" operator "||" (OR) takes precedence: backwards compatible, [XYZ][ZYX] does still work as OR 00134 $orParts = preg_split('/\]\s*(\|\|){0,1}\s*\[/',$insideSqrBrackets); 00135 00136 foreach ($orParts as $partString) { 00137 $matches = false; 00138 00139 // Splits by the "&&" (AND) operator: 00140 $andParts = preg_split('/\]\s*&&\s*\[/',$partString); 00141 foreach ($andParts as $condStr) { 00142 $matches = $this->evalConditionStr($condStr); 00143 if ($matches===false) { 00144 break; // only true AND true = true, so we have to break here 00145 } 00146 } 00147 00148 if ($matches===true) { 00149 break; // true OR false = true, so we break if we have a positive result 00150 } 00151 } 00152 00153 return $matches; 00154 } 00155 00156 00165 function evalConditionStr($string) { 00166 if (!is_array($this->altRootLine)) { 00167 $this->altRootLine = array(); 00168 } 00169 list($key, $value) = explode('=', $string, 2); 00170 $key = trim($key); 00171 if (stristr(',browser,version,system,useragent,', ",$key,")) { 00172 $browserInfo = $this->browserInfo(t3lib_div::getIndpEnv('HTTP_USER_AGENT')); 00173 } 00174 $value = trim($value); 00175 switch ($key) { 00176 case 'browser': 00177 $values = explode(',',$value); 00178 while(list(,$test)=each($values)) { 00179 if (strstr($browserInfo['browser'].$browserInfo['version'],trim($test))) { 00180 return true; 00181 } 00182 } 00183 break; 00184 case 'version': 00185 $values = explode(',',$value); 00186 while(list(,$test)=each($values)) { 00187 $test = trim($test); 00188 if (strlen($test)) { 00189 if (strcspn($test,'=<>')==0) { 00190 switch(substr($test,0,1)) { 00191 case '=': 00192 if (doubleval(substr($test,1))==$browserInfo['version']) return true; 00193 break; 00194 case '<': 00195 if (doubleval(substr($test,1))>$browserInfo['version']) return true; 00196 break; 00197 case '>': 00198 if (doubleval(substr($test,1))<$browserInfo['version']) return true; 00199 break; 00200 } 00201 } else { 00202 if (strpos(' '.$browserInfo['version'],$test)==1) {return true;} 00203 } 00204 } 00205 } 00206 break; 00207 case 'system': 00208 $values = explode(',',$value); 00209 while(list(,$test)=each($values)) { 00210 $test = trim($test); 00211 if (strlen($test)) { 00212 if (strpos(' '.$browserInfo['system'],$test)==1) {return true;} 00213 } 00214 } 00215 break; 00216 case 'device': 00217 $values = explode(',',$value); 00218 if (!isset($this->deviceInfo)) { 00219 $this->deviceInfo = $this->whichDevice(t3lib_div::getIndpEnv('HTTP_USER_AGENT')); 00220 } 00221 while(list(,$test)=each($values)) { 00222 $test = trim($test); 00223 if (strlen($test)) { 00224 if ($this->deviceInfo==$test) {return true;} 00225 } 00226 } 00227 break; 00228 case 'useragent': 00229 $test = trim($value); 00230 if (strlen($test)) { 00231 return $this->matchWild($browserInfo['useragent'],$test); 00232 } 00233 break; 00234 case 'language': 00235 $values = explode(',',$value); 00236 while(list(,$test)=each($values)) { 00237 $test = trim($test); 00238 if (strlen($test)) { 00239 if (preg_match('/^\*.+\*$/',$test)) { 00240 $allLanguages = split('[,;]',t3lib_div::getIndpEnv('HTTP_ACCEPT_LANGUAGE')); 00241 if (in_array(substr($test,1,-1), $allLanguages)) {return true;} 00242 } else { 00243 if (t3lib_div::getIndpEnv('HTTP_ACCEPT_LANGUAGE') == $test) {return true;} 00244 } 00245 } 00246 } 00247 break; 00248 case 'IP': 00249 if (t3lib_div::cmpIP(t3lib_div::getIndpEnv('REMOTE_ADDR'), $value)) {return true;} 00250 break; 00251 case 'hostname': 00252 if (t3lib_div::cmpFQDN(t3lib_div::getIndpEnv('REMOTE_ADDR'), $value)) {return true;} 00253 break; 00254 // hour, minute, dayofweek, dayofmonth, month 00255 case 'hour': 00256 case 'minute': 00257 case 'dayofweek': 00258 case 'dayofmonth': 00259 case 'month': 00260 $theEvalTime = $GLOBALS['SIM_EXEC_TIME']; // In order to simulate time properly in templates. 00261 switch($key) { 00262 case 'hour': $theTestValue = date('H',$theEvalTime); break; 00263 case 'minute': $theTestValue = date('i',$theEvalTime); break; 00264 case 'dayofweek': $theTestValue = date('w',$theEvalTime); break; 00265 case 'dayofmonth': $theTestValue = date('d',$theEvalTime); break; 00266 case 'month': $theTestValue = date('m',$theEvalTime); break; 00267 } 00268 $theTestValue = intval($theTestValue); 00269 // comp 00270 $values = explode(',',$value); 00271 reset($values); 00272 while(list(,$test)=each($values)) { 00273 $test = trim($test); 00274 if (t3lib_div::testInt($test)) {$test='='.$test;} 00275 if (strlen($test)) { 00276 if ($this->testNumber($test,$theTestValue)) {return true;} 00277 } 00278 } 00279 break; 00280 case 'usergroup': 00281 if ($GLOBALS['TSFE']->gr_list!='0,-1') { // '0,-1' is the default usergroups when not logged in! 00282 $values = explode(',',$value); 00283 while(list(,$test)=each($values)) { 00284 $test = trim($test); 00285 if (strlen($test)) { 00286 if ($test=='*' || t3lib_div::inList($GLOBALS['TSFE']->gr_list,$test)) {return true;} 00287 } 00288 } 00289 } 00290 break; 00291 case 'loginUser': 00292 if ($GLOBALS['TSFE']->loginUser) { 00293 $values = explode(',',$value); 00294 while(list(,$test)=each($values)) { 00295 $test = trim($test); 00296 if (strlen($test)) { 00297 if ($test=='*' || !strcmp($GLOBALS['TSFE']->fe_user->user['uid'],$test)) {return true;} 00298 } 00299 } 00300 } 00301 break; 00302 case 'globalVar': 00303 $values = explode(',',$value); 00304 while(list(,$test)=each($values)) { 00305 $test = trim($test); 00306 if (strlen($test)) { 00307 $point = strcspn($test,'=<>'); 00308 $theVarName = substr($test,0,$point); 00309 $nv = $this->getGP_ENV_TSFE(trim($theVarName)); 00310 $testValue = substr($test,$point); 00311 00312 if ($this->testNumber($testValue,$nv)) {return true;} 00313 } 00314 } 00315 break; 00316 case 'globalString': 00317 $values = explode(',',$value); 00318 while(list(,$test)=each($values)) { 00319 $test = trim($test); 00320 if (strlen($test)) { 00321 $point = strcspn($test,'='); 00322 $theVarName = substr($test,0,$point); 00323 $nv = $this->getGP_ENV_TSFE(trim($theVarName)); 00324 $testValue = substr($test,$point+1); 00325 00326 if ($this->matchWild($nv,trim($testValue))) {return true;} 00327 } 00328 } 00329 break; 00330 case 'treeLevel': 00331 $values = explode(',',$value); 00332 $theRootLine = is_array($GLOBALS['TSFE']->tmpl->rootLine) ? $GLOBALS['TSFE']->tmpl->rootLine : $this->altRootLine; 00333 $theRLC = count($theRootLine)-1; 00334 while(list(,$test)=each($values)) { 00335 $test = trim($test); 00336 if ($test==$theRLC) { return true; } 00337 } 00338 break; 00339 case 'PIDupinRootline': 00340 case 'PIDinRootline': 00341 $values = explode(',',$value); 00342 if (($key=='PIDinRootline') || (!in_array($GLOBALS['TSFE']->id,$values))) { 00343 $theRootLine = is_array($GLOBALS['TSFE']->tmpl->rootLine) ? $GLOBALS['TSFE']->tmpl->rootLine : $this->altRootLine; 00344 reset($values); 00345 while(list(,$test)=each($values)) { 00346 $test = trim($test); 00347 reset($theRootLine); 00348 while(list($rl_key,$rl_dat)=each($theRootLine)) { 00349 if ($rl_dat['uid']==$test) { return true; } 00350 } 00351 } 00352 } 00353 break; 00354 case 'compatVersion': 00355 { return t3lib_div::compat_version($value); } 00356 break; 00357 case 'userFunc': 00358 $values = split('\(|\)',$value); 00359 $funcName=trim($values[0]); 00360 $funcValue = t3lib_div::trimExplode(',',$values[1]); 00361 $pre = $GLOBALS['TSFE']->TYPO3_CONF_VARS['FE']['userFuncClassPrefix']; 00362 if ($pre && 00363 !t3lib_div::isFirstPartOfStr(trim($funcName),$pre) && 00364 !t3lib_div::isFirstPartOfStr(trim($funcName),'tx_') 00365 ) { 00366 if (is_object($GLOBALS['TT'])) $GLOBALS['TT']->setTSlogMessage('Match condition: Function "'.$funcName.'" was not prepended with "'.$pre.'"',3); 00367 return false; 00368 } 00369 if (function_exists($funcName) && call_user_func($funcName, $funcValue[0])) { 00370 return true; 00371 } 00372 break; 00373 } 00374 00375 00376 return false; 00377 } 00378 00386 function testNumber($test,$value) { 00387 $test = trim($test); 00388 switch(substr($test,0,1)) { 00389 case '<': 00390 if (doubleval(substr($test,1))>$value) return true; 00391 break; 00392 case '>': 00393 if (doubleval(substr($test,1))<$value) return true; 00394 break; 00395 default: 00396 if (trim(substr($test,1))==$value) return true; 00397 break; 00398 } 00399 00400 return false; 00401 } 00402 00410 function matchWild($haystack,$needle) { 00411 if ($needle && $haystack) { 00412 if (preg_match('/^\/.+\/$/', $needle)) { // Looks like a regular expression 00413 $regex = $needle; 00414 } else { 00415 $needle = str_replace(array('*','?'), array('###MANY###','###ONE###'), $needle); 00416 $regex = '/^'.preg_quote($needle,'/').'$/'; 00417 $regex = str_replace(array('###MANY###','###ONE###'), array('.*','.'), $regex); // Replace the marker with .* to match anything (wildcard) 00418 } 00419 00420 if (preg_match($regex, $haystack)) return true; 00421 } 00422 00423 return false; 00424 } 00425 00434 function whichDevice($useragent) { 00435 foreach($this->hookObjectsArr as $hookObj) { 00436 if (method_exists($hookObj, 'whichDevice')) { 00437 $result = $hookObj->whichDevice($useragent); 00438 if (strlen($result)) { 00439 return $result; 00440 } 00441 } 00442 } 00443 00444 // deprecated, see above 00445 if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_matchcondition.php']['devices_class'])) { 00446 foreach($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_matchcondition.php']['devices_class'] as $_classRef) { 00447 $_procObj = &t3lib_div::getUserObj($_classRef); 00448 return $_procObj->whichDevice_ext($useragent); 00449 } 00450 } 00451 // 00452 00453 $agent=strtolower(trim($useragent)); 00454 // pda 00455 if( strstr($agent, 'avantgo')) { 00456 return 'pda'; 00457 } 00458 00459 // wap 00460 $browser=substr($agent,0,4); 00461 $wapviwer=substr(stristr($agent,'wap'),0,3); 00462 if( $wapviwer=='wap' || 00463 $browser=='noki' || 00464 $browser== 'eric' || 00465 $browser== 'r380' || 00466 $browser== 'up.b' || 00467 $browser== 'winw' || 00468 $browser== 'wapa') { 00469 return 'wap'; 00470 } 00471 00472 // grabber 00473 if( strstr($agent, 'g.r.a.b.') || 00474 strstr($agent, 'utilmind httpget') || 00475 strstr($agent, 'webcapture') || 00476 strstr($agent, 'teleport') || 00477 strstr($agent, 'webcopier')) { 00478 return 'grabber'; 00479 } 00480 00481 // robots 00482 if( strstr($agent, 'crawler') || 00483 strstr($agent, 'spider') || 00484 strstr($agent, 'googlebot') || 00485 strstr($agent, 'searchbot') || 00486 strstr($agent, 'infoseek') || 00487 strstr($agent, 'altavista') || 00488 strstr($agent, 'diibot')) { 00489 return 'robot'; 00490 } 00491 00492 } 00493 00503 function browserInfo($useragent) { 00504 foreach($this->hookObjectsArr as $hookObj) { 00505 if (method_exists($hookObj, 'browserInfo')) { 00506 $result = $hookObj->browserInfo($useragent); 00507 if (strlen($result)) { 00508 return $result; 00509 } 00510 } 00511 } 00512 00513 $useragent = trim($useragent); 00514 $browserInfo=Array(); 00515 $browserInfo['useragent']=$useragent; 00516 if ($useragent) { 00517 // browser 00518 if (strstr($useragent,'MSIE')) { 00519 $browserInfo['browser']='msie'; 00520 } elseif(strstr($useragent,'Konqueror')) { 00521 $browserInfo['browser']='konqueror'; 00522 } elseif(strstr($useragent,'Opera')) { 00523 $browserInfo['browser']='opera'; 00524 } elseif(strstr($useragent,'Lynx')) { 00525 $browserInfo['browser']='lynx'; 00526 } elseif(strstr($useragent,'PHP')) { 00527 $browserInfo['browser']='php'; 00528 } elseif(strstr($useragent,'AvantGo')) { 00529 $browserInfo['browser']='avantgo'; 00530 } elseif(strstr($useragent,'WebCapture')) { 00531 $browserInfo['browser']='acrobat'; 00532 } elseif(strstr($useragent,'IBrowse')) { 00533 $browserInfo['browser']='ibrowse'; 00534 } elseif(strstr($useragent,'Teleport')) { 00535 $browserInfo['browser']='teleport'; 00536 } elseif(strstr($useragent,'Mozilla')) { 00537 $browserInfo['browser']='netscape'; 00538 } else { 00539 $browserInfo['browser']='unknown'; 00540 } 00541 // version 00542 switch($browserInfo['browser']) { 00543 case 'netscape': 00544 $browserInfo['version'] = $this->browserInfo_version(substr($useragent,8)); 00545 if (strstr($useragent,'Netscape6')) {$browserInfo['version']=6;} 00546 break; 00547 case 'msie': 00548 $tmp = strstr($useragent,'MSIE'); 00549 $browserInfo['version'] = $this->browserInfo_version(substr($tmp,4)); 00550 break; 00551 case 'opera': 00552 $tmp = strstr($useragent,'Opera'); 00553 $browserInfo['version'] = $this->browserInfo_version(substr($tmp,5)); 00554 break; 00555 case 'lynx': 00556 $tmp = strstr($useragent,'Lynx/'); 00557 $browserInfo['version'] = $this->browserInfo_version(substr($tmp,5)); 00558 break; 00559 case 'php': 00560 $tmp = strstr($useragent,'PHP/'); 00561 $browserInfo['version'] = $this->browserInfo_version(substr($tmp,4)); 00562 break; 00563 case 'avantgo': 00564 $tmp = strstr($useragent,'AvantGo'); 00565 $browserInfo['version'] = $this->browserInfo_version(substr($tmp,7)); 00566 break; 00567 case 'acrobat': 00568 $tmp = strstr($useragent,'WebCapture'); 00569 $browserInfo['version'] = $this->browserInfo_version(substr($tmp,10)); 00570 break; 00571 case 'ibrowse': 00572 $tmp = strstr($useragent,'IBrowse/'); 00573 $browserInfo['version'] = $this->browserInfo_version(substr($tmp,8)); 00574 break; 00575 case 'konqueror': 00576 $tmp = strstr($useragent,'Konqueror/'); 00577 $browserInfo['version'] = $this->browserInfo_version(substr($tmp,10)); 00578 break; 00579 } 00580 // system 00581 $browserInfo['system']=''; 00582 if (strstr($useragent,'Win')) { 00583 // windows 00584 if (strstr($useragent,'Win98') || strstr($useragent,'Windows 98')) { 00585 $browserInfo['system']='win98'; 00586 } elseif (strstr($useragent,'Win95') || strstr($useragent,'Windows 95')) { 00587 $browserInfo['system']='win95'; 00588 } elseif (strstr($useragent,'WinNT') || strstr($useragent,'Windows NT')) { 00589 $browserInfo['system']='winNT'; 00590 } elseif (strstr($useragent,'Win16') || strstr($useragent,'Windows 311')) { 00591 $browserInfo['system']='win311'; 00592 } 00593 } elseif (strstr($useragent,'Mac')) { 00594 $browserInfo['system']='mac'; 00595 // unixes 00596 } elseif (strstr($useragent,'Linux')) { 00597 $browserInfo['system']='linux'; 00598 } elseif (strstr($useragent,'SGI') && strstr($useragent,' IRIX ')) { 00599 $browserInfo['system']='unix_sgi'; 00600 } elseif (strstr($useragent,' SunOS ')) { 00601 $browserInfo['system']='unix_sun'; 00602 } elseif (strstr($useragent,' HP-UX ')) { 00603 $browserInfo['system']='unix_hp'; 00604 } 00605 } 00606 00607 return $browserInfo; 00608 } 00609 00616 function browserInfo_version($tmp) { 00617 return doubleval(preg_replace('/^[^0-9]*/','',$tmp)); 00618 } 00619 00629 function getGlobal($var, $source=NULL) { 00630 $vars = explode('|',$var); 00631 $c = count($vars); 00632 $k = trim($vars[0]); 00633 $theVar = isset($source) ? $source[$k] : $GLOBALS[$k]; 00634 00635 for ($a=1;$a<$c;$a++) { 00636 if (!isset($theVar)) { break; } 00637 00638 $key = trim($vars[$a]); 00639 if (is_object($theVar)) { 00640 $theVar = $theVar->$key; 00641 } elseif (is_array($theVar)) { 00642 $theVar = $theVar[$key]; 00643 } else { 00644 return ''; 00645 } 00646 } 00647 00648 if (!is_array($theVar) && !is_object($theVar)) { 00649 return $theVar; 00650 } else { 00651 return ''; 00652 } 00653 } 00654 00663 function getGP_ENV_TSFE($var) { 00664 $vars = explode(':',$var,2); 00665 if (count($vars)==1) { 00666 $val = $this->getGlobal($var); 00667 } else { 00668 $splitAgain=explode('|',$vars[1],2); 00669 $k=trim($splitAgain[0]); 00670 if ($k) { 00671 switch((string)trim($vars[0])) { 00672 case 'GP': 00673 $val = t3lib_div::_GP($k); 00674 break; 00675 case 'TSFE': 00676 $val = $this->getGlobal('TSFE|'.$vars[1]); 00677 $splitAgain=0; // getGlobal resolves all parts of the key, so no further splitting is needed 00678 break; 00679 case 'ENV': 00680 $val = getenv($k); 00681 break; 00682 case 'IENV': 00683 $val = t3lib_div::getIndpEnv($k); 00684 break; 00685 case 'LIT': 00686 { return trim($vars[1]); } // return litteral value... 00687 break; 00688 } 00689 // If array: 00690 if (count($splitAgain)>1) { 00691 if (is_array($val) && trim($splitAgain[1])) { 00692 $val=$this->getGlobal($splitAgain[1],$val); 00693 } else { 00694 $val=''; 00695 } 00696 } 00697 } 00698 } 00699 return $val; 00700 } 00701 } 00702 00703 00704 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_matchcondition.php']) { 00705 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_matchcondition.php']); 00706 } 00707 ?>