[TYPO3-core] RFC: #4155 / userFunc in conditions / t3lib_div::callUserFunction
Wolfgang Klinger
wolfgang at stufenlos.net
Wed Sep 6 22:00:01 CEST 2006
*hiya!*
This is a patch request (trunk).
http://bugs.typo3.org/view.php?id=4155
The attached patch uses t3lib_div::callUserFunction instead of
reinventing the wheel here.
While working on that I also changed 'true' to 'TRUE' and all those
single line if statements, sorry for that inconvinience, but simply
skip to the bottom of the patch file to find the important stuff
("userFunc") ;-)
I changed t3lib_div too to return FALSE explicitly in
callUserFunction.
tia, bye
Wolfgang
-------------- next part --------------
Index: class.t3lib_matchcondition.php
===================================================================
--- class.t3lib_matchcondition.php (revision 1722)
+++ class.t3lib_matchcondition.php (working copy)
@@ -78,7 +78,7 @@
* @see t3lib_TStemplate::matching(), t3lib_TStemplate::generateConfig()
*/
class t3lib_matchCondition {
- var $matchAlternative=array(); // If this array has elements, the matching returns true if a whole "matchline" is found in the array!
+ var $matchAlternative=array(); // If this array has elements, the matching returns TRUE if a whole "matchline" is found in the array!
var $matchAll=0; // If set all is matched!
var $altRootLine=array();
@@ -119,7 +119,7 @@
*/
function match($condition_line) {
if ($this->matchAll) {
- return true;
+ return TRUE;
}
if (count($this->matchAlternative)) {
return in_array($condition_line, $this->matchAlternative);
@@ -134,19 +134,19 @@
$orParts = preg_split('/\]\s*(\|\|){0,1}\s*\[/',$insideSqrBrackets);
foreach ($orParts as $partString) {
- $matches = false;
+ $matches = FALSE;
// Splits by the "&&" (AND) operator:
$andParts = preg_split('/\]\s*&&\s*\[/',$partString);
foreach ($andParts as $condStr) {
$matches = $this->evalConditionStr($condStr);
- if ($matches===false) {
- break; // only true AND true = true, so we have to break here
+ if ($matches===FALSE) {
+ break; // only TRUE AND TRUE = TRUE, so we have to break here
}
}
- if ($matches===true) {
- break; // true OR false = true, so we break if we have a positive result
+ if ($matches===TRUE) {
+ break; // TRUE OR FALSE = TRUE, so we break if we have a positive result
}
}
@@ -158,7 +158,7 @@
* Evaluates a TypoScript condition given as input, eg. "[browser=net][...(other conditions)...]"
*
* @param string The condition to match against its criterias.
- * @return boolean Returns true or false based on the evaluation.
+ * @return boolean Returns TRUE or FALSE based on the evaluation.
* @see t3lib_tsparser::parse()
* @link http://typo3.org/doc.0.html?&tx_extrepmgm_pi1[extUid]=270&tx_extrepmgm_pi1[tocEl]=292&cHash=c6c7d43d2f
*/
@@ -177,7 +177,7 @@
$values = explode(',',$value);
while(list(,$test)=each($values)) {
if (strstr($browserInfo['browser'].$browserInfo['version'],trim($test))) {
- return true;
+ return TRUE;
}
}
break;
@@ -189,17 +189,25 @@
if (strcspn($test,'=<>')==0) {
switch(substr($test,0,1)) {
case '=':
- if (doubleval(substr($test,1))==$browserInfo['version']) return true;
+ if (doubleval(substr($test,1))==$browserInfo['version']) {
+ return TRUE;
+ }
break;
case '<':
- if (doubleval(substr($test,1))>$browserInfo['version']) return true;
+ if (doubleval(substr($test,1))>$browserInfo['version']) {
+ return TRUE;
+ }
break;
case '>':
- if (doubleval(substr($test,1))<$browserInfo['version']) return true;
+ if (doubleval(substr($test,1))<$browserInfo['version']) {
+ return TRUE;
+ }
break;
}
} else {
- if (strpos(' '.$browserInfo['version'],$test)==1) {return true;}
+ if (strpos(' '.$browserInfo['version'],$test)==1) {
+ return TRUE;
+ }
}
}
}
@@ -209,7 +217,9 @@
while(list(,$test)=each($values)) {
$test = trim($test);
if (strlen($test)) {
- if (strpos(' '.$browserInfo['system'],$test)==1) {return true;}
+ if (strpos(' '.$browserInfo['system'],$test)==1) {
+ return TRUE;
+ }
}
}
break;
@@ -221,7 +231,9 @@
while(list(,$test)=each($values)) {
$test = trim($test);
if (strlen($test)) {
- if ($this->deviceInfo==$test) {return true;}
+ if ($this->deviceInfo==$test) {
+ return TRUE;
+ }
}
}
break;
@@ -238,18 +250,26 @@
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;}
+ if (in_array(substr($test,1,-1), $allLanguages)) {
+ return TRUE;
+ }
} else {
- if (t3lib_div::getIndpEnv('HTTP_ACCEPT_LANGUAGE') == $test) {return true;}
+ 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
case 'hour':
@@ -273,7 +293,9 @@
$test = trim($test);
if (t3lib_div::testInt($test)) {$test='='.$test;}
if (strlen($test)) {
- if ($this->testNumber($test,$theTestValue)) {return true;}
+ if ($this->testNumber($test,$theTestValue)) {
+ return TRUE;
+ }
}
}
break;
@@ -283,7 +305,9 @@
while(list(,$test)=each($values)) {
$test = trim($test);
if (strlen($test)) {
- if ($test=='*' || t3lib_div::inList($GLOBALS['TSFE']->gr_list,$test)) {return true;}
+ if ($test=='*' || t3lib_div::inList($GLOBALS['TSFE']->gr_list,$test)) {
+ return TRUE;
+ }
}
}
}
@@ -294,7 +318,9 @@
while(list(,$test)=each($values)) {
$test = trim($test);
if (strlen($test)) {
- if ($test=='*' || !strcmp($GLOBALS['TSFE']->fe_user->user['uid'],$test)) {return true;}
+ if ($test=='*' || !strcmp($GLOBALS['TSFE']->fe_user->user['uid'],$test)) {
+ return TRUE;
+ }
}
}
}
@@ -309,7 +335,9 @@
$nv = $this->getGP_ENV_TSFE(trim($theVarName));
$testValue = substr($test,$point);
- if ($this->testNumber($testValue,$nv)) {return true;}
+ if ($this->testNumber($testValue,$nv)) {
+ return TRUE;
+ }
}
}
break;
@@ -323,7 +351,9 @@
$nv = $this->getGP_ENV_TSFE(trim($theVarName));
$testValue = substr($test,$point+1);
- if ($this->matchWild($nv,trim($testValue))) {return true;}
+ if ($this->matchWild($nv,trim($testValue))) {
+ return TRUE;
+ }
}
}
break;
@@ -333,7 +363,9 @@
$theRLC = count($theRootLine)-1;
while(list(,$test)=each($values)) {
$test = trim($test);
- if ($test==$theRLC) { return true; }
+ if ($test==$theRLC) {
+ return TRUE;
+ }
}
break;
case 'PIDupinRootline':
@@ -346,7 +378,9 @@
$test = trim($test);
reset($theRootLine);
while(list($rl_key,$rl_dat)=each($theRootLine)) {
- if ($rl_dat['uid']==$test) { return true; }
+ if ($rl_dat['uid']==$test) {
+ return TRUE;
+ }
}
}
}
@@ -357,23 +391,19 @@
case 'userFunc':
$values = 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) &&
- !t3lib_div::isFirstPartOfStr(trim($funcName),'tx_')
- ) {
- if (is_object($GLOBALS['TT'])) $GLOBALS['TT']->setTSlogMessage('Match condition: Function "'.$funcName.'" was not prepended with "'.$pre.'"',3);
- return false;
+ $funcValues = t3lib_div::trimExplode(',', $values[1]);
+ // backwards compatible
+ if (count($funcValues) == 1) {
+ list($funcValues) = $funcValues;
}
- if (function_exists($funcName) && call_user_func($funcName, $funcValue[0])) {
- return true;
+ if (t3lib_div::callUserFunction($funcName, $funcValues, $this, $GLOBALS['TSFE']->TYPO3_CONF_VARS['FE']['userFuncClassPrefix'])) {
+ return TRUE;
}
break;
}
- return false;
+ return FALSE;
}
/**
@@ -381,23 +411,29 @@
*
* @param string The value to compare with on the form [operator][number]. Eg. "< 123"
* @param integer The number
- * @return boolean If $value is "50" and $test is "< 123" then it will return true.
+ * @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;
+ if (doubleval(substr($test,1))>$value) {
+ return TRUE;
+ }
break;
case '>':
- if (doubleval(substr($test,1))<$value) return true;
+ if (doubleval(substr($test,1))<$value) {
+ return TRUE;
+ }
break;
default:
- if (trim(substr($test,1))==$value) return true;
+ if (trim(substr($test,1))==$value) {
+ return TRUE;
+ }
break;
}
- return false;
+ return FALSE;
}
/**
@@ -405,7 +441,7 @@
*
* @param string The string in which to find $needle.
* @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.
+ * @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) {
@@ -417,10 +453,12 @@
$regex = str_replace(array('###MANY###','###ONE###'), array('.*','.'), $regex); // Replace the marker with .* to match anything (wildcard)
}
- if (preg_match($regex, $haystack)) return true;
+ if (preg_match($regex, $haystack)) {
+ return TRUE;
+ }
}
- return false;
+ return FALSE;
}
/**
-------------- next part --------------
Index: class.t3lib_div.php
===================================================================
--- class.t3lib_div.php (revision 1722)
+++ class.t3lib_div.php (working copy)
@@ -3827,6 +3827,7 @@
*/
function callUserFunction($funcName,&$params,&$ref,$checkPrefix='user_',$silent=0) {
global $TYPO3_CONF_VARS;
+ $content = '';
// Check persistent object and if found, call directly and exit.
if (is_array($GLOBALS['T3_VAR']['callUserFunction'][$funcName])) {
@@ -3896,15 +3897,18 @@
);
} else {
if (!$silent) debug("<strong>ERROR:</strong> No method name '".$parts[1]."' in class ".$parts[0],1);
+ return FALSE;
}
} else {
if (!$silent) debug("<strong>ERROR:</strong> No class named: ".$parts[0],1);
+ return FALSE;
}
} else { // Function
if (function_exists($funcRef)) {
$content = call_user_func_array($funcRef, array(&$params, &$ref));
} else {
if (!$silent) debug("<strong>ERROR:</strong> No function named: ".$funcRef,1);
+ return FALSE;
}
}
return $content;
More information about the TYPO3-team-core
mailing list