00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00080 class t3lib_matchCondition {
00081 var $matchAlternative=array();
00082 var $matchAll=0;
00083
00084 var $altRootLine=array();
00085 var $hookObjectsArr = array();
00086
00092 function __construct() {
00093 global $TYPO3_CONF_VARS;
00094
00095
00096
00097
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
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
00134 $orParts = preg_split('/\]\s*(\|\|){0,1}\s*\[/',$insideSqrBrackets);
00135
00136 foreach ($orParts as $partString) {
00137 $matches = false;
00138
00139
00140 $andParts = preg_split('/\]\s*&&\s*\[/',$partString);
00141 foreach ($andParts as $condStr) {
00142 $matches = $this->evalConditionStr($condStr);
00143 if ($matches===false) {
00144 break;
00145 }
00146 }
00147
00148 if ($matches===true) {
00149 break;
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
00255 case 'hour':
00256 case 'minute':
00257 case 'month':
00258 case 'year':
00259 case 'dayofweek':
00260 case 'dayofmonth':
00261 case 'dayofyear':
00262 $theEvalTime = $GLOBALS['SIM_EXEC_TIME'];
00263 switch($key) {
00264 case 'hour': $theTestValue = date('H',$theEvalTime); break;
00265 case 'minute': $theTestValue = date('i',$theEvalTime); break;
00266 case 'month': $theTestValue = date('m',$theEvalTime); break;
00267 case 'year': $theTestValue = date('Y',$theEvalTime); break;
00268 case 'dayofweek': $theTestValue = date('w',$theEvalTime); break;
00269 case 'dayofmonth': $theTestValue = date('d',$theEvalTime); break;
00270 case 'dayofyear': $theTestValue = date('z',$theEvalTime); break;
00271 }
00272 $theTestValue = intval($theTestValue);
00273
00274 $values = explode(',',$value);
00275 reset($values);
00276 while(list(,$test)=each($values)) {
00277 $test = trim($test);
00278 if (t3lib_div::testInt($test)) {$test='='.$test;}
00279 if (strlen($test)) {
00280 if ($this->testNumber($test,$theTestValue)) {return true;}
00281 }
00282 }
00283 break;
00284 case 'usergroup':
00285 if ($GLOBALS['TSFE']->gr_list!='0,-1') {
00286 $values = explode(',',$value);
00287 while(list(,$test)=each($values)) {
00288 $test = trim($test);
00289 if (strlen($test)) {
00290 if ($test=='*' || t3lib_div::inList($GLOBALS['TSFE']->gr_list,$test)) {return true;}
00291 }
00292 }
00293 }
00294 break;
00295 case 'loginUser':
00296 if ($GLOBALS['TSFE']->loginUser) {
00297 $values = explode(',',$value);
00298 while(list(,$test)=each($values)) {
00299 $test = trim($test);
00300 if (strlen($test)) {
00301 if ($test=='*' || !strcmp($GLOBALS['TSFE']->fe_user->user['uid'],$test)) {return true;}
00302 }
00303 }
00304 }
00305 break;
00306 case 'globalVar':
00307 $values = explode(',',$value);
00308 while(list(,$test)=each($values)) {
00309 $test = trim($test);
00310 if (strlen($test)) {
00311 $point = strcspn($test,'=<>');
00312 $theVarName = substr($test,0,$point);
00313 $nv = $this->getGP_ENV_TSFE(trim($theVarName));
00314 $testValue = substr($test,$point);
00315
00316 if ($this->testNumber($testValue,$nv)) {return true;}
00317 }
00318 }
00319 break;
00320 case 'globalString':
00321 $values = explode(',',$value);
00322 while(list(,$test)=each($values)) {
00323 $test = trim($test);
00324 if (strlen($test)) {
00325 $point = strcspn($test,'=');
00326 $theVarName = substr($test,0,$point);
00327 $nv = $this->getGP_ENV_TSFE(trim($theVarName));
00328 $testValue = substr($test,$point+1);
00329
00330 if ($this->matchWild($nv,trim($testValue))) {return true;}
00331 }
00332 }
00333 break;
00334 case 'treeLevel':
00335 $values = explode(',',$value);
00336 $theRootLine = is_array($GLOBALS['TSFE']->tmpl->rootLine) ? $GLOBALS['TSFE']->tmpl->rootLine : $this->altRootLine;
00337 $theRLC = count($theRootLine)-1;
00338 while(list(,$test)=each($values)) {
00339 $test = trim($test);
00340 if ($test==$theRLC) { return true; }
00341 }
00342 break;
00343 case 'PIDupinRootline':
00344 case 'PIDinRootline':
00345 $values = explode(',',$value);
00346 if (($key=='PIDinRootline') || (!in_array($GLOBALS['TSFE']->id,$values))) {
00347 $theRootLine = is_array($GLOBALS['TSFE']->tmpl->rootLine) ? $GLOBALS['TSFE']->tmpl->rootLine : $this->altRootLine;
00348 reset($values);
00349 while(list(,$test)=each($values)) {
00350 $test = trim($test);
00351 reset($theRootLine);
00352 while(list($rl_key,$rl_dat)=each($theRootLine)) {
00353 if ($rl_dat['uid']==$test) { return true; }
00354 }
00355 }
00356 }
00357 break;
00358 case 'compatVersion':
00359 { return t3lib_div::compat_version($value); }
00360 break;
00361 case 'userFunc':
00362 $values = split('\(|\)',$value);
00363 $funcName=trim($values[0]);
00364 $funcValue = t3lib_div::trimExplode(',',$values[1]);
00365 $pre = $GLOBALS['TSFE']->TYPO3_CONF_VARS['FE']['userFuncClassPrefix'];
00366 if ($pre &&
00367 !t3lib_div::isFirstPartOfStr(trim($funcName),$pre) &&
00368 !t3lib_div::isFirstPartOfStr(trim($funcName),'tx_')
00369 ) {
00370 if (is_object($GLOBALS['TT'])) $GLOBALS['TT']->setTSlogMessage('Match condition: Function "'.$funcName.'" was not prepended with "'.$pre.'"',3);
00371 return false;
00372 }
00373 if (function_exists($funcName) && call_user_func($funcName, $funcValue[0])) {
00374 return true;
00375 }
00376 break;
00377 }
00378
00379
00380 return false;
00381 }
00382
00390 function testNumber($test,$value) {
00391 $test = trim($test);
00392 switch(substr($test,0,1)) {
00393 case '<':
00394 if (doubleval(substr($test,1))>$value) return true;
00395 break;
00396 case '>':
00397 if (doubleval(substr($test,1))<$value) return true;
00398 break;
00399 default:
00400 if (trim(substr($test,1))==$value) return true;
00401 break;
00402 }
00403
00404 return false;
00405 }
00406
00414 function matchWild($haystack,$needle) {
00415 if ($needle && $haystack) {
00416 if (preg_match('/^\/.+\/$/', $needle)) {
00417 $regex = $needle;
00418 } else {
00419 $needle = str_replace(array('*','?'), array('###MANY###','###ONE###'), $needle);
00420 $regex = '/^'.preg_quote($needle,'/').'$/';
00421 $regex = str_replace(array('###MANY###','###ONE###'), array('.*','.'), $regex);
00422 }
00423
00424 if (preg_match($regex, $haystack)) return true;
00425 }
00426
00427 return false;
00428 }
00429
00438 function whichDevice($useragent) {
00439 foreach($this->hookObjectsArr as $hookObj) {
00440 if (method_exists($hookObj, 'whichDevice')) {
00441 $result = $hookObj->whichDevice($useragent);
00442 if (strlen($result)) {
00443 return $result;
00444 }
00445 }
00446 }
00447
00448
00449 if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_matchcondition.php']['devices_class'])) {
00450 foreach($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_matchcondition.php']['devices_class'] as $_classRef) {
00451 $_procObj = &t3lib_div::getUserObj($_classRef);
00452 return $_procObj->whichDevice_ext($useragent);
00453 }
00454 }
00455
00456
00457 $agent=strtolower(trim($useragent));
00458
00459 if( strstr($agent, 'avantgo')) {
00460 return 'pda';
00461 }
00462
00463
00464 $browser=substr($agent,0,4);
00465 $wapviwer=substr(stristr($agent,'wap'),0,3);
00466 if( $wapviwer=='wap' ||
00467 $browser=='noki' ||
00468 $browser== 'eric' ||
00469 $browser== 'r380' ||
00470 $browser== 'up.b' ||
00471 $browser== 'winw' ||
00472 $browser== 'wapa') {
00473 return 'wap';
00474 }
00475
00476
00477 if( strstr($agent, 'g.r.a.b.') ||
00478 strstr($agent, 'utilmind httpget') ||
00479 strstr($agent, 'webcapture') ||
00480 strstr($agent, 'teleport') ||
00481 strstr($agent, 'webcopier')) {
00482 return 'grabber';
00483 }
00484
00485
00486 if( strstr($agent, 'crawler') ||
00487 strstr($agent, 'spider') ||
00488 strstr($agent, 'googlebot') ||
00489 strstr($agent, 'searchbot') ||
00490 strstr($agent, 'infoseek') ||
00491 strstr($agent, 'altavista') ||
00492 strstr($agent, 'diibot')) {
00493 return 'robot';
00494 }
00495
00496 }
00497
00507 function browserInfo($useragent) {
00508 foreach($this->hookObjectsArr as $hookObj) {
00509 if (method_exists($hookObj, 'browserInfo')) {
00510 $result = $hookObj->browserInfo($useragent);
00511 if (strlen($result)) {
00512 return $result;
00513 }
00514 }
00515 }
00516
00517 $useragent = trim($useragent);
00518 $browserInfo=Array();
00519 $browserInfo['useragent']=$useragent;
00520 if ($useragent) {
00521
00522 if (strstr($useragent,'MSIE')) {
00523 $browserInfo['browser']='msie';
00524 } elseif(strstr($useragent,'Konqueror')) {
00525 $browserInfo['browser']='konqueror';
00526 } elseif(strstr($useragent,'Opera')) {
00527 $browserInfo['browser']='opera';
00528 } elseif(strstr($useragent,'Lynx')) {
00529 $browserInfo['browser']='lynx';
00530 } elseif(strstr($useragent,'PHP')) {
00531 $browserInfo['browser']='php';
00532 } elseif(strstr($useragent,'AvantGo')) {
00533 $browserInfo['browser']='avantgo';
00534 } elseif(strstr($useragent,'WebCapture')) {
00535 $browserInfo['browser']='acrobat';
00536 } elseif(strstr($useragent,'IBrowse')) {
00537 $browserInfo['browser']='ibrowse';
00538 } elseif(strstr($useragent,'Teleport')) {
00539 $browserInfo['browser']='teleport';
00540 } elseif(strstr($useragent,'Mozilla')) {
00541 $browserInfo['browser']='netscape';
00542 } else {
00543 $browserInfo['browser']='unknown';
00544 }
00545
00546 switch($browserInfo['browser']) {
00547 case 'netscape':
00548 $browserInfo['version'] = $this->browserInfo_version(substr($useragent,8));
00549 if (strstr($useragent,'Netscape6')) {$browserInfo['version']=6;}
00550 break;
00551 case 'msie':
00552 $tmp = strstr($useragent,'MSIE');
00553 $browserInfo['version'] = $this->browserInfo_version(substr($tmp,4));
00554 break;
00555 case 'opera':
00556 $tmp = strstr($useragent,'Opera');
00557 $browserInfo['version'] = $this->browserInfo_version(substr($tmp,5));
00558 break;
00559 case 'lynx':
00560 $tmp = strstr($useragent,'Lynx/');
00561 $browserInfo['version'] = $this->browserInfo_version(substr($tmp,5));
00562 break;
00563 case 'php':
00564 $tmp = strstr($useragent,'PHP/');
00565 $browserInfo['version'] = $this->browserInfo_version(substr($tmp,4));
00566 break;
00567 case 'avantgo':
00568 $tmp = strstr($useragent,'AvantGo');
00569 $browserInfo['version'] = $this->browserInfo_version(substr($tmp,7));
00570 break;
00571 case 'acrobat':
00572 $tmp = strstr($useragent,'WebCapture');
00573 $browserInfo['version'] = $this->browserInfo_version(substr($tmp,10));
00574 break;
00575 case 'ibrowse':
00576 $tmp = strstr($useragent,'IBrowse/');
00577 $browserInfo['version'] = $this->browserInfo_version(substr($tmp,8));
00578 break;
00579 case 'konqueror':
00580 $tmp = strstr($useragent,'Konqueror/');
00581 $browserInfo['version'] = $this->browserInfo_version(substr($tmp,10));
00582 break;
00583 }
00584
00585 $browserInfo['system']='';
00586 if (strstr($useragent,'Win')) {
00587
00588 if (strstr($useragent,'Win98') || strstr($useragent,'Windows 98')) {
00589 $browserInfo['system']='win98';
00590 } elseif (strstr($useragent,'Win95') || strstr($useragent,'Windows 95')) {
00591 $browserInfo['system']='win95';
00592 } elseif (strstr($useragent,'WinNT') || strstr($useragent,'Windows NT')) {
00593 $browserInfo['system']='winNT';
00594 } elseif (strstr($useragent,'Win16') || strstr($useragent,'Windows 311')) {
00595 $browserInfo['system']='win311';
00596 }
00597 } elseif (strstr($useragent,'Mac')) {
00598 $browserInfo['system']='mac';
00599
00600 } elseif (strstr($useragent,'Linux')) {
00601 $browserInfo['system']='linux';
00602 } elseif (strstr($useragent,'SGI') && strstr($useragent,' IRIX ')) {
00603 $browserInfo['system']='unix_sgi';
00604 } elseif (strstr($useragent,' SunOS ')) {
00605 $browserInfo['system']='unix_sun';
00606 } elseif (strstr($useragent,' HP-UX ')) {
00607 $browserInfo['system']='unix_hp';
00608 }
00609 }
00610
00611 return $browserInfo;
00612 }
00613
00620 function browserInfo_version($tmp) {
00621 return doubleval(preg_replace('/^[^0-9]*/','',$tmp));
00622 }
00623
00633 function getGlobal($var, $source=NULL) {
00634 $vars = explode('|',$var);
00635 $c = count($vars);
00636 $k = trim($vars[0]);
00637 $theVar = isset($source) ? $source[$k] : $GLOBALS[$k];
00638
00639 for ($a=1;$a<$c;$a++) {
00640 if (!isset($theVar)) { break; }
00641
00642 $key = trim($vars[$a]);
00643 if (is_object($theVar)) {
00644 $theVar = $theVar->$key;
00645 } elseif (is_array($theVar)) {
00646 $theVar = $theVar[$key];
00647 } else {
00648 return '';
00649 }
00650 }
00651
00652 if (!is_array($theVar) && !is_object($theVar)) {
00653 return $theVar;
00654 } else {
00655 return '';
00656 }
00657 }
00658
00667 function getGP_ENV_TSFE($var) {
00668 $vars = explode(':',$var,2);
00669 if (count($vars)==1) {
00670 $val = $this->getGlobal($var);
00671 } else {
00672 $splitAgain=explode('|',$vars[1],2);
00673 $k=trim($splitAgain[0]);
00674 if ($k) {
00675 switch((string)trim($vars[0])) {
00676 case 'GP':
00677 $val = t3lib_div::_GP($k);
00678 break;
00679 case 'TSFE':
00680 $val = $this->getGlobal('TSFE|'.$vars[1]);
00681 $splitAgain=0;
00682 break;
00683 case 'ENV':
00684 $val = getenv($k);
00685 break;
00686 case 'IENV':
00687 $val = t3lib_div::getIndpEnv($k);
00688 break;
00689 case 'LIT':
00690 { return trim($vars[1]); }
00691 break;
00692 }
00693
00694 if (count($splitAgain)>1) {
00695 if (is_array($val) && trim($splitAgain[1])) {
00696 $val=$this->getGlobal($splitAgain[1],$val);
00697 } else {
00698 $val='';
00699 }
00700 }
00701 }
00702 }
00703 return $val;
00704 }
00705 }
00706
00707
00708 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_matchcondition.php']) {
00709 include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_matchcondition.php']);
00710 }
00711 ?>