Main Page | Directories | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members | Related Pages | Examples

class.t3lib_matchcondition.php

Go to the documentation of this file.
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,'Opera'))   {
00422             $browserInfo['browser']='opera';
00423          } elseif(strstr($useragent,'Lynx')) {
00424             $browserInfo['browser']='lynx';
00425          } elseif(strstr($useragent,'PHP'))  {
00426             $browserInfo['browser']='php';
00427          } elseif(strstr($useragent,'AvantGo')) {
00428             $browserInfo['browser']='avantgo';
00429          } elseif(strstr($useragent,'WebCapture')) {
00430             $browserInfo['browser']='acrobat';
00431          } elseif(strstr($useragent,'IBrowse')) {
00432             $browserInfo['browser']='ibrowse';
00433          } elseif(strstr($useragent,'Teleport'))   {
00434             $browserInfo['browser']='teleport';
00435          } elseif(strstr($useragent,'Mozilla')) {
00436             $browserInfo['browser']='netscape';
00437          } else {
00438             $browserInfo['browser']='unknown';
00439          }
00440          // version
00441          switch($browserInfo['browser'])  {
00442             case 'netscape':
00443                $browserInfo['version'] = $this->browserInfo_version(substr($useragent,8));
00444                if (strstr($useragent,'Netscape6')) {$browserInfo['version']=6;}
00445             break;
00446             case 'msie':
00447                $tmp = strstr($useragent,'MSIE');
00448                $browserInfo['version'] = $this->browserInfo_version(substr($tmp,4));
00449             break;
00450             case 'opera':
00451                $tmp = strstr($useragent,'Opera');
00452                $browserInfo['version'] = $this->browserInfo_version(substr($tmp,5));
00453             break;
00454             case 'lynx':
00455                $tmp = strstr($useragent,'Lynx/');
00456                $browserInfo['version'] = $this->browserInfo_version(substr($tmp,5));
00457             break;
00458             case 'php':
00459                $tmp = strstr($useragent,'PHP/');
00460                $browserInfo['version'] = $this->browserInfo_version(substr($tmp,4));
00461             break;
00462             case 'avantgo':
00463                $tmp = strstr($useragent,'AvantGo');
00464                $browserInfo['version'] = $this->browserInfo_version(substr($tmp,5));
00465             break;
00466             case 'acrobat':
00467                $tmp = strstr($useragent,'WebCapture');
00468                $browserInfo['version'] = $this->browserInfo_version(substr($tmp,5));
00469             break;
00470             case 'ibrowse':
00471                $tmp = strstr($useragent,'IBrowse/');
00472                $browserInfo['version'] = $this->browserInfo_version(substr($tmp,5));
00473             break;
00474          }
00475          // system
00476          $browserInfo['system']='';
00477          if (strstr($useragent,'Win')) {
00478             // windows
00479             if (strstr($useragent,'Win98') || strstr($useragent,'Windows 98'))   {
00480                $browserInfo['system']='win98';
00481             } elseif (strstr($useragent,'Win95') || strstr($useragent,'Windows 95'))   {
00482                $browserInfo['system']='win95';
00483             } elseif (strstr($useragent,'WinNT') || strstr($useragent,'Windows NT'))   {
00484                $browserInfo['system']='winNT';
00485             } elseif (strstr($useragent,'Win16') || strstr($useragent,'Windows 311'))  {
00486                $browserInfo['system']='win311';
00487             }
00488          } elseif (strstr($useragent,'Mac')) {
00489             $browserInfo['system']='mac';
00490             // unixes
00491          } elseif (strstr($useragent,'Linux'))  {
00492             $browserInfo['system']='linux';
00493          } elseif (strstr($useragent,'SGI') && strstr($useragent,' IRIX '))   {
00494             $browserInfo['system']='unix_sgi';
00495          } elseif (strstr($useragent,' SunOS '))   {
00496             $browserInfo['system']='unix_sun';
00497          } elseif (strstr($useragent,' HP-UX '))   {
00498             $browserInfo['system']='unix_hp';
00499          }
00500       }
00501 
00502       return $browserInfo;
00503    }
00504 
00511    function browserInfo_version($tmp)  {
00512       return doubleval(ereg_replace('^[^0-9]*','',$tmp));
00513    }
00514 
00523    function getGlobal($var,$inArr='') {
00524       $vars = explode('|',$var);
00525       $c = count($vars);
00526       $k = trim($vars[0]);
00527       $theVar = is_array($inArr) ? $inArr[$k] : $GLOBALS[$k];
00528 
00529       for ($a=1;$a<$c;$a++) {
00530          if (!isset($theVar)) {break;}
00531          $theVar = $theVar[trim($vars[$a])];
00532       }
00533       if (!is_array($theVar)) {
00534          return $theVar;
00535       } else {
00536          return '';
00537       }
00538    }
00539 
00548    function getGP_ENV_TSFE($var) {
00549       $vars = explode(':',$var,2);
00550       if (count($vars)==1) {
00551          $val = $this->getGlobal($var);
00552       } else {
00553          $splitAgain=explode('|',$vars[1],2);
00554          $k=trim($splitAgain[0]);
00555          if ($k)  {
00556             switch((string)trim($vars[0]))   {
00557                case 'GP':
00558                   $val = t3lib_div::_GP($k);
00559                break;
00560                case 'TSFE':
00561                   $val = $GLOBALS['TSFE']->$k;
00562                break;
00563                case 'ENV':
00564                   $val = getenv($k);
00565                break;
00566                case 'IENV':
00567                   $val = t3lib_div::getIndpEnv($k);
00568                break;
00569                case 'LIT':
00570                   return trim($vars[1]);  // return litteral value...
00571                break;
00572             }
00573                // If array:
00574             if (count($splitAgain)>1)  {
00575                if (is_array($val) && trim($splitAgain[1]))  {
00576                   $val=$this->getGlobal($splitAgain[1],$val);
00577                } else {
00578                   $val='';
00579                }
00580             }
00581          }
00582       }
00583       return $val;
00584    }
00585 }
00586 
00587 
00588 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_matchcondition.php'])   {
00589    include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_matchcondition.php']);
00590 }
00591 ?>

Generated on Sun Oct 3 01:05:48 2004 for TYPO3core 3.7.0 dev by  doxygen 1.3.8-20040913