Documentation TYPO3 par Ameos

class.t3lib_matchcondition.php

00001 <?php
00002 /***************************************************************
00003 *  Copyright notice
00004 *
00005 *  (c) 1999-2005 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                         // Hook for extending device recognition capabilities:
00404                 if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_matchcondition.php']['devices_class']))       {
00405                         foreach($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_matchcondition.php']['devices_class'] as $_classRef)       {
00406                                 $_procObj = &t3lib_div::getUserObj($_classRef);
00407                                 return $_procObj->whichDevice_ext($useragent);
00408                         }
00409                 }
00410 
00411         }
00412 
00422         function browserInfo($useragent)        {
00423                 $useragent = trim($useragent);
00424                 $browserInfo=Array();
00425                 $browserInfo['useragent']=$useragent;
00426                 if ($useragent) {
00427                         // browser
00428                         if (strstr($useragent,'MSIE'))  {
00429                                 $browserInfo['browser']='msie';
00430                         } elseif(strstr($useragent,'Konqueror'))        {
00431                                 $browserInfo['browser']='konqueror';
00432                         } elseif(strstr($useragent,'Opera'))    {
00433                                 $browserInfo['browser']='opera';
00434                         } elseif(strstr($useragent,'Lynx'))     {
00435                                 $browserInfo['browser']='lynx';
00436                         } elseif(strstr($useragent,'PHP'))      {
00437                                 $browserInfo['browser']='php';
00438                         } elseif(strstr($useragent,'AvantGo'))  {
00439                                 $browserInfo['browser']='avantgo';
00440                         } elseif(strstr($useragent,'WebCapture'))       {
00441                                 $browserInfo['browser']='acrobat';
00442                         } elseif(strstr($useragent,'IBrowse'))  {
00443                                 $browserInfo['browser']='ibrowse';
00444                         } elseif(strstr($useragent,'Teleport')) {
00445                                 $browserInfo['browser']='teleport';
00446                         } elseif(strstr($useragent,'Mozilla'))  {
00447                                 $browserInfo['browser']='netscape';
00448                         } else {
00449                                 $browserInfo['browser']='unknown';
00450                         }
00451                         // version
00452                         switch($browserInfo['browser']) {
00453                                 case 'netscape':
00454                                         $browserInfo['version'] = $this->browserInfo_version(substr($useragent,8));
00455                                         if (strstr($useragent,'Netscape6')) {$browserInfo['version']=6;}
00456                                 break;
00457                                 case 'msie':
00458                                         $tmp = strstr($useragent,'MSIE');
00459                                         $browserInfo['version'] = $this->browserInfo_version(substr($tmp,4));
00460                                 break;
00461                                 case 'opera':
00462                                         $tmp = strstr($useragent,'Opera');
00463                                         $browserInfo['version'] = $this->browserInfo_version(substr($tmp,5));
00464                                 break;
00465                                 case 'lynx':
00466                                         $tmp = strstr($useragent,'Lynx/');
00467                                         $browserInfo['version'] = $this->browserInfo_version(substr($tmp,5));
00468                                 break;
00469                                 case 'php':
00470                                         $tmp = strstr($useragent,'PHP/');
00471                                         $browserInfo['version'] = $this->browserInfo_version(substr($tmp,4));
00472                                 break;
00473                                 case 'avantgo':
00474                                         $tmp = strstr($useragent,'AvantGo');
00475                                         $browserInfo['version'] = $this->browserInfo_version(substr($tmp,7));
00476                                 break;
00477                                 case 'acrobat':
00478                                         $tmp = strstr($useragent,'WebCapture');
00479                                         $browserInfo['version'] = $this->browserInfo_version(substr($tmp,10));
00480                                 break;
00481                                 case 'ibrowse':
00482                                         $tmp = strstr($useragent,'IBrowse/');
00483                                         $browserInfo['version'] = $this->browserInfo_version(substr($tmp,8));
00484                                 break;
00485                                 case 'konqueror':
00486                                         $tmp = strstr($useragent,'Konqueror/');
00487                                         $browserInfo['version'] = $this->browserInfo_version(substr($tmp,10));
00488                                 break;
00489                         }
00490                         // system
00491                         $browserInfo['system']='';
00492                         if (strstr($useragent,'Win'))   {
00493                                 // windows
00494                                 if (strstr($useragent,'Win98') || strstr($useragent,'Windows 98'))      {
00495                                         $browserInfo['system']='win98';
00496                                 } elseif (strstr($useragent,'Win95') || strstr($useragent,'Windows 95'))        {
00497                                         $browserInfo['system']='win95';
00498                                 } elseif (strstr($useragent,'WinNT') || strstr($useragent,'Windows NT'))        {
00499                                         $browserInfo['system']='winNT';
00500                                 } elseif (strstr($useragent,'Win16') || strstr($useragent,'Windows 311'))       {
00501                                         $browserInfo['system']='win311';
00502                                 }
00503                         } elseif (strstr($useragent,'Mac'))     {
00504                                 $browserInfo['system']='mac';
00505                                 // unixes
00506                         } elseif (strstr($useragent,'Linux'))   {
00507                                 $browserInfo['system']='linux';
00508                         } elseif (strstr($useragent,'SGI') && strstr($useragent,' IRIX '))      {
00509                                 $browserInfo['system']='unix_sgi';
00510                         } elseif (strstr($useragent,' SunOS ')) {
00511                                 $browserInfo['system']='unix_sun';
00512                         } elseif (strstr($useragent,' HP-UX ')) {
00513                                 $browserInfo['system']='unix_hp';
00514                         }
00515                 }
00516 
00517                 return $browserInfo;
00518         }
00519 
00526         function browserInfo_version($tmp)      {
00527                 return doubleval(ereg_replace('^[^0-9]*','',$tmp));
00528         }
00529 
00538         function getGlobal($var,$inArr='') {
00539                 $vars = explode('|',$var);
00540                 $c = count($vars);
00541                 $k = trim($vars[0]);
00542                 $theVar = is_array($inArr) ? $inArr[$k] : $GLOBALS[$k];
00543 
00544                 for ($a=1;$a<$c;$a++) {
00545                         if (!isset($theVar))    {break;}
00546                         $theVar = $theVar[trim($vars[$a])];
00547                 }
00548                 if (!is_array($theVar)) {
00549                         return $theVar;
00550                 } else {
00551                         return '';
00552                 }
00553         }
00554 
00563         function getGP_ENV_TSFE($var) {
00564                 $vars = explode(':',$var,2);
00565                 if (count($vars)==1)    {
00566                         $val = $this->getGlobal($var);
00567                 } else {
00568                         $splitAgain=explode('|',$vars[1],2);
00569                         $k=trim($splitAgain[0]);
00570                         if ($k) {
00571                                 switch((string)trim($vars[0]))  {
00572                                         case 'GP':
00573                                                 $val = t3lib_div::_GP($k);
00574                                         break;
00575                                         case 'TSFE':
00576                                                 $val = $GLOBALS['TSFE']->$k;
00577                                         break;
00578                                         case 'ENV':
00579                                                 $val = getenv($k);
00580                                         break;
00581                                         case 'IENV':
00582                                                 $val = t3lib_div::getIndpEnv($k);
00583                                         break;
00584                                         case 'LIT':
00585                                                 return trim($vars[1]);  // return litteral value...
00586                                         break;
00587                                 }
00588                                         // If array:
00589                                 if (count($splitAgain)>1)       {
00590                                         if (is_array($val) && trim($splitAgain[1]))     {
00591                                                 $val=$this->getGlobal($splitAgain[1],$val);
00592                                         } else {
00593                                                 $val='';
00594                                         }
00595                                 }
00596                         }
00597                 }
00598                 return $val;
00599         }
00600 }
00601 
00602 
00603 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_matchcondition.php'])    {
00604         include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_matchcondition.php']);
00605 }
00606 ?>


Généré par TYPO3 Ameos avec  doxygen 1.4.6