Fixed bug #10735: Wrong returnUrl with clipboard actions
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_matchcondition.php
index eb32fb2..8278e7d 100644 (file)
@@ -2,7 +2,7 @@
 /***************************************************************
 *  Copyright notice
 *
-*  (c) 1999-2006 Kasper Skaarhoj (kasperYYYY@typo3.com)
+*  (c) 1999-2009 Kasper Skaarhoj (kasperYYYY@typo3.com)
 *  All rights reserved
 *
 *  This script is part of the TYPO3 project. The TYPO3 project is
@@ -168,196 +168,202 @@ class t3lib_matchCondition {
                }
                list($key, $value) = explode('=', $string, 2);
                $key = trim($key);
-               if (stristr(',browser,version,system,useragent,', ",$key,")) {
+               $value = trim($value);
+               if (t3lib_div::inList('browser,version,system,useragent', strtolower($key))) {
                        $browserInfo = $this->browserInfo(t3lib_div::getIndpEnv('HTTP_USER_AGENT'));
                }
-               $value = trim($value);
                switch ($key) {
                        case 'browser':
-                               $values = explode(',',$value);
-                               while(list(,$test)=each($values))       {
-                                       if (strstr($browserInfo['browser'].$browserInfo['version'],trim($test)))        {
+                               $values = t3lib_div::trimExplode(',', $value, true);
+                               foreach ($values as $test) {
+                                       if (strpos($browserInfo['browser'] . $browserInfo['version'], $test) !== false) {
                                                return true;
                                        }
                                }
                        break;
                        case 'version':
-                               $values = explode(',',$value);
-                               while(list(,$test)=each($values))       {
-                                       $test = trim($test);
-                                       if (strlen($test)) {
-                                               if (strcspn($test,'=<>')==0)    {
-                                                       switch(substr($test,0,1))       {
-                                                               case '=':
-                                                                       if (doubleval(substr($test,1))==$browserInfo['version']) return true;
-                                                               break;
-                                                               case '<':
-                                                                       if (doubleval(substr($test,1))>$browserInfo['version']) return true;
-                                                               break;
-                                                               case '>':
-                                                                       if (doubleval(substr($test,1))<$browserInfo['version']) return true;
-                                                               break;
-                                                       }
-                                               } else {
-                                                       if (strpos(' '.$browserInfo['version'],$test)==1)       {return true;}
+                               $values = t3lib_div::trimExplode(',', $value, true);
+                               foreach ($values as $test) {
+                                       if (strcspn($test, '=<>') == 0) {
+                                               switch (substr($test, 0, 1)) {
+                                                       case '=':
+                                                               if (doubleval(substr($test, 1)) == $browserInfo['version']) {
+                                                                       return true;
+                                                               }
+                                                       break;
+                                                       case '<':
+                                                               if (doubleval(substr($test, 1)) > $browserInfo['version']) {
+                                                                       return true;
+                                                               }
+                                                       break;
+                                                       case '>':
+                                                               if (doubleval(substr($test, 1)) < $browserInfo['version']) {
+                                                                       return true;
+                                                               }
+                                                       break;
+                                               }
+                                       } else {
+                                               if (strpos(' ' . $browserInfo['version'], $test) == 1) {
+                                                       return true;
                                                }
                                        }
                                }
                        break;
                        case 'system':
-                               $values = explode(',',$value);
-                               while(list(,$test)=each($values))       {
-                                       $test = trim($test);
-                                       if (strlen($test)) {
-                                               if (strpos(' '.$browserInfo['system'],$test)==1)        {return true;}
+                               $values = t3lib_div::trimExplode(',', $value, true);
+                               foreach ($values as $test) {
+                                       if (strpos(' ' . $browserInfo['system'], $test) == 1) {
+                                               return true;
                                        }
                                }
                        break;
                        case 'device':
-                               $values = explode(',',$value);
-                               if (!isset($this->deviceInfo))  {
+                               if (!isset($this->deviceInfo)) {
                                        $this->deviceInfo = $this->whichDevice(t3lib_div::getIndpEnv('HTTP_USER_AGENT'));
                                }
-                               while(list(,$test)=each($values))       {
-                                       $test = trim($test);
-                                       if (strlen($test)) {
-                                               if ($this->deviceInfo==$test)   {return true;}
+                               $values = t3lib_div::trimExplode(',', $value, true);
+                               foreach ($values as $test) {
+                                       if ($this->deviceInfo == $test) {
+                                               return true;
                                        }
                                }
                        break;
                        case 'useragent':
                                $test = trim($value);
                                if (strlen($test)) {
-                                       return $this->matchWild($browserInfo['useragent'],$test);
+                                       return $this->matchWild($browserInfo['useragent'], $test);
                                }
                        break;
                        case 'language':
-                               $values = explode(',',$value);
-                               while(list(,$test)=each($values))       {
-                                       $test = trim($test);
-                                       if (strlen($test)) {
-                                               if (preg_match('/^\*.+\*$/',$test))     {
-                                                       $allLanguages = split('[,;]',t3lib_div::getIndpEnv('HTTP_ACCEPT_LANGUAGE'));
-                                                       if (in_array(substr($test,1,-1), $allLanguages))        {return true;}
-                                               } else {
-                                                       if (t3lib_div::getIndpEnv('HTTP_ACCEPT_LANGUAGE') == $test)     {return true;}
+                               $values = t3lib_div::trimExplode(',', $value, true);
+                               foreach ($values as $test) {
+                                       if (preg_match('/^\*.+\*$/', $test)) {
+                                               $allLanguages = preg_split('/[,;]/', t3lib_div::getIndpEnv('HTTP_ACCEPT_LANGUAGE'));
+                                               if (in_array(substr($test, 1, -1), $allLanguages)) {
+                                                       return true;
                                                }
+                                       } else if (t3lib_div::getIndpEnv('HTTP_ACCEPT_LANGUAGE') == $test) {
+                                               return true;
                                        }
                                }
                        break;
                        case 'IP':
-                               if (t3lib_div::cmpIP(t3lib_div::getIndpEnv('REMOTE_ADDR'), $value))     {return true;}
+                               if (t3lib_div::cmpIP(t3lib_div::getIndpEnv('REMOTE_ADDR'), $value)) {
+                                       return true;
+                               }
                        break;
                        case 'hostname':
-                               if (t3lib_div::cmpFQDN(t3lib_div::getIndpEnv('REMOTE_ADDR'), $value))  {return true;}
+                               if (t3lib_div::cmpFQDN(t3lib_div::getIndpEnv('REMOTE_ADDR'), $value)) {
+                                       return true;
+                               }
                        break;
-                               // hour, minute, dayofweek, dayofmonth, month
+                               // hour, minute, dayofweek, dayofmonth, month, year, julianday
                        case 'hour':
                        case 'minute':
+                       case 'month':
+                       case 'year':
                        case 'dayofweek':
                        case 'dayofmonth':
-                       case 'month':
+                       case 'dayofyear':
                                $theEvalTime = $GLOBALS['SIM_EXEC_TIME'];       // In order to simulate time properly in templates.
                                switch($key) {
-                                       case 'hour':            $theTestValue = date('H',$theEvalTime); break;
-                                       case 'minute':          $theTestValue = date('i',$theEvalTime); break;
-                                       case 'dayofweek':       $theTestValue = date('w',$theEvalTime); break;
-                                       case 'dayofmonth':      $theTestValue = date('d',$theEvalTime); break;
-                                       case 'month':           $theTestValue = date('m',$theEvalTime); break;
+                                       case 'hour':            $theTestValue = date('H', $theEvalTime);        break;
+                                       case 'minute':          $theTestValue = date('i', $theEvalTime);        break;
+                                       case 'month':           $theTestValue = date('m', $theEvalTime);        break;
+                                       case 'year':            $theTestValue = date('Y', $theEvalTime);        break;
+                                       case 'dayofweek':       $theTestValue = date('w', $theEvalTime);        break;
+                                       case 'dayofmonth':      $theTestValue = date('d', $theEvalTime);        break;
+                                       case 'dayofyear':       $theTestValue = date('z', $theEvalTime);        break;
                                }
                                $theTestValue = intval($theTestValue);
                                        // comp
-                               $values = explode(',',$value);
-                               reset($values);
-                               while(list(,$test)=each($values))       {
-                                       $test = trim($test);
-                                       if (t3lib_div::testInt($test))  {$test='='.$test;}
-                                       if (strlen($test)) {
-                                               if ($this->testNumber($test,$theTestValue)) {return true;}
+                               $values = t3lib_div::trimExplode(',', $value, true);
+                               foreach ($values as $test) {
+                                       if (t3lib_div::testInt($test)) {
+                                               $test = '=' . $test;
+                                       }
+                                       if ($this->testNumber($test, $theTestValue)) {
+                                               return true;
                                        }
                                }
                        break;
                        case 'usergroup':
-                               if ($GLOBALS['TSFE']->gr_list!='0,-1')  {               // '0,-1' is the default usergroups when not logged in!
-                                       $values = explode(',',$value);
-                                       while(list(,$test)=each($values))       {
-                                               $test = trim($test);
-                                               if (strlen($test)) {
-                                                       if ($test=='*' || t3lib_div::inList($GLOBALS['TSFE']->gr_list,$test))   {return true;}
+                               if ($GLOBALS['TSFE']->gr_list != '0,-1') {              // '0,-1' is the default usergroups when not logged in!
+                                       $values = t3lib_div::trimExplode(',', $value, true);
+                                       foreach ($values as $test) {
+                                               if ($test == '*' || t3lib_div::inList($GLOBALS['TSFE']->gr_list, $test)) {
+                                                       return true;
                                                }
                                        }
                                }
                        break;
                        case 'loginUser':
-                               if ($GLOBALS['TSFE']->loginUser)        {
-                                       $values = explode(',',$value);
-                                       while(list(,$test)=each($values))       {
-                                               $test = trim($test);
-                                               if (strlen($test)) {
-                                                       if ($test=='*' || !strcmp($GLOBALS['TSFE']->fe_user->user['uid'],$test))        {return true;}
+                               if ($GLOBALS['TSFE']->loginUser) {
+                                       $values = t3lib_div::trimExplode(',', $value, true);
+                                       foreach ($values as $test) {
+                                               if ($test == '*' || !strcmp($GLOBALS['TSFE']->fe_user->user['uid'], $test)) {
+                                                       return true;
                                                }
                                        }
                                }
                        break;
                        case 'globalVar':
-                               $values = explode(',',$value);
-                               while(list(,$test)=each($values))       {
-                                       $test = trim($test);
-                                       if (strlen($test)) {
-                                               $point = strcspn($test,'=<>');
-                                               $theVarName = substr($test,0,$point);
-                                               $nv = $this->getGP_ENV_TSFE(trim($theVarName));
-                                               $testValue = substr($test,$point);
-
-                                               if ($this->testNumber($testValue,$nv)) {return true;}
+                               $values = t3lib_div::trimExplode(',', $value, true);
+                               foreach ($values as $test) {
+                                       $point = strcspn($test, '!=<>');
+                                       $theVarName = substr($test, 0, $point);
+                                       $nv = $this->getGP_ENV_TSFE(trim($theVarName));
+                                       $testValue = substr($test, $point);
+
+                                       if ($this->testNumber($testValue, $nv)) {
+                                               return true;
                                        }
                                }
                        break;
                        case 'globalString':
-                               $values = explode(',',$value);
-                               while(list(,$test)=each($values))       {
-                                       $test = trim($test);
-                                       if (strlen($test)) {
-                                               $point = strcspn($test,'=');
-                                               $theVarName = substr($test,0,$point);
-                                               $nv = $this->getGP_ENV_TSFE(trim($theVarName));
-                                               $testValue = substr($test,$point+1);
-
-                                               if ($this->matchWild($nv,trim($testValue))) {return true;}
+                               $values = t3lib_div::trimExplode(',', $value, true);
+                               foreach ($values as $test) {
+                                       $point = strcspn($test, '=');
+                                       $theVarName = substr($test, 0, $point);
+                                       $nv = $this->getGP_ENV_TSFE(trim($theVarName));
+                                       $testValue = substr($test, $point+1);
+
+                                       if ($this->matchWild($nv, trim($testValue))) {
+                                               return true;
                                        }
                                }
                        break;
                        case 'treeLevel':
-                               $values = explode(',',$value);
+                               $values = t3lib_div::trimExplode(',', $value, true);
                                $theRootLine = is_array($GLOBALS['TSFE']->tmpl->rootLine) ? $GLOBALS['TSFE']->tmpl->rootLine : $this->altRootLine;
-                               $theRLC = count($theRootLine)-1;
-                               while(list(,$test)=each($values))       {
-                                       $test = trim($test);
-                                       if ($test==$theRLC)     {       return true;    }
+                               $treeLevel = count($theRootLine)-1;
+                               foreach ($values as $test) {
+                                       if ($test == $treeLevel) {
+                                               return true;
+                                       }
                                }
                        break;
                        case 'PIDupinRootline':
                        case 'PIDinRootline':
-                               $values = explode(',',$value);
-                               if (($key=='PIDinRootline') || (!in_array($GLOBALS['TSFE']->id,$values))) {
+                               $values = t3lib_div::trimExplode(',', $value, true);
+                               if (($key=='PIDinRootline') || (!in_array($GLOBALS['TSFE']->id, $values))) {
                                        $theRootLine = is_array($GLOBALS['TSFE']->tmpl->rootLine) ? $GLOBALS['TSFE']->tmpl->rootLine : $this->altRootLine;
-                                       reset($values);
-                                       while(list(,$test)=each($values))       {
-                                               $test = trim($test);
-                                               reset($theRootLine);
-                                               while(list($rl_key,$rl_dat)=each($theRootLine)) {
-                                                       if ($rl_dat['uid']==$test)      {       return true;    }
+                                       foreach ($values as $test) {
+                                               foreach ($theRootLine as $rl_dat) {
+                                                       if ($rl_dat['uid'] == $test) {
+                                                               return true;
+                                                       }
                                                }
                                        }
                                }
                        break;
                        case 'compatVersion':
-                               { return t3lib_div::compat_version($value); }
+                               return t3lib_div::compat_version($value);
                        break;
                        case 'userFunc':
-                               $values = split('\(|\)',$value);
-                               $funcName=trim($values[0]);
-                               $funcValue = t3lib_div::trimExplode(',',$values[1]);
+                               $values = preg_split('/[\(\)]/', $value);
+                               $funcName = trim($values[0]);
+                               $funcValue = t3lib_div::trimExplode(',', $values[1]);
                                $pre = $GLOBALS['TSFE']->TYPO3_CONF_VARS['FE']['userFuncClassPrefix'];
                                if ($pre &&
                                        !t3lib_div::isFirstPartOfStr(trim($funcName),$pre) &&
@@ -377,24 +383,38 @@ class t3lib_matchCondition {
        }
 
        /**
-        * Will evaluate a $value based on an operator: "<", ">" or "=" (default)
+        * Evaluates a $leftValue based on an operator: "<", ">", "<=", ">=", "!=" or "="
         *
-        * @param       string          The value to compare with on the form [operator][number]. Eg. "< 123"
-        * @param       integer         The number
+        * @param       string          $test: The value to compare with on the form [operator][number]. Eg. "< 123"
+        * @param       integer         $leftValue: The value on the left side
         * @return      boolean         If $value is "50" and $test is "< 123" then it will return true.
         */
-       function testNumber($test,$value) {
-               $test = trim($test);
-               switch(substr($test,0,1))       {
-                       case '<':
-                               if (doubleval(substr($test,1))>$value)  return true;
-                       break;
-                       case '>':
-                               if (doubleval(substr($test,1))<$value)  return true;
-                       break;
-                       default:
-                               if (trim(substr($test,1))==$value)      return true;
-                       break;
+       function testNumber($test, $leftValue) {
+               if (preg_match('/^(!?=+|<=?|>=?)\s*([^\s]*)\s*$/', $test, $matches)) {
+                       $operator = $matches[1];
+                       $rightValue = $matches[2];
+
+                       switch ($operator) {
+                               case '>=':
+                                       return ($leftValue >= doubleval($rightValue));
+                                       break;
+                               case '<=':
+                                       return ($leftValue <= doubleval($rightValue));
+                                       break;
+                               case '!=':
+                                       return ($leftValue != doubleval($rightValue));
+                                       break;
+                               case '<':
+                                       return ($leftValue < doubleval($rightValue));
+                                       break;
+                               case '>':
+                                       return ($leftValue > doubleval($rightValue));
+                                       break;
+                               default:
+                                       // nothing valid found except '=', use '='
+                                       return ($leftValue == trim($rightValue));
+                                       break;
+                       }
                }
 
                return false;
@@ -407,20 +427,24 @@ class t3lib_matchCondition {
         * @param       string          The string to find in $haystack
         * @return      boolean         Returns true if $needle matches or is found in (according to wildcards) in $haystack. Eg. if $haystack is "Netscape 6.5" and $needle is "Net*" or "Net*ape" then it returns true.
         */
-       function matchWild($haystack,$needle)   {
-               if ($needle && $haystack)       {
-                       if (preg_match('/^\/.+\/$/', $needle))  {       // Looks like a regular expression
+       function matchWild($haystack, $needle) {
+               $result = false;
+
+               if ($needle) {
+                       if (preg_match('/^\/.+\/$/', $needle)) {
+                               // Regular expression, only "//" is allowed as delimiter
                                $regex = $needle;
                        } else {
-                               $needle = str_replace(array('*','?'), array('###MANY###','###ONE###'), $needle);
-                               $regex = '/^'.preg_quote($needle,'/').'$/';
-                               $regex = str_replace(array('###MANY###','###ONE###'), array('.*','.'), $regex); // Replace the marker with .* to match anything (wildcard)
+                               $needle = str_replace(array('*', '?'), array('###MANY###', '###ONE###'), $needle);
+                               $regex = '/^' . preg_quote($needle, '/') . '$/';
+                               // Replace the marker with .* to match anything (wildcard)
+                               $regex = str_replace(array('###MANY###', '###ONE###'), array('.*' , '.'), $regex);
                        }
 
-                       if (preg_match($regex, $haystack)) return true;
+                       $result = (boolean)preg_match($regex, (string)$haystack);
                }
 
-               return false;
+               return $result;
        }
 
        /**
@@ -704,4 +728,5 @@ class t3lib_matchCondition {
 if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_matchcondition.php'])   {
        include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_matchcondition.php']);
 }
-?>
+
+?>
\ No newline at end of file