Renamed function so it might be more understandable
[mailer.git] / inc / expression-functions.php
index b1e95c2ef6932df503a5dc581664dd78c02e3fa6..dd533a27a7cbcbc53ec6874c9eb99b024ae8c3d9 100644 (file)
@@ -14,8 +14,6 @@
  * $Date::                                                            $ *
  * $Tag:: 0.2.1-FINAL                                                 $ *
  * $Author::                                                          $ *
- * Needs to be in all Files and every File needs "svn propset           *
- * svn:keywords Date Revision" (autoprobset!) at least!!!!!!            *
  * -------------------------------------------------------------------- *
  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
  * Copyright (c) 2009, 2010 by Mailer Developer Team                    *
@@ -42,10 +40,67 @@ if (!defined('__SECURITY')) {
        die();
 } // END - if
 
+// Private function to replace the code
+function replaceExpressionCode ($data, $replacer) {
+       // Replace the code
+       // @TODO is escapeQuotes() enougth for strings with single/double quotes?
+       return str_replace($data['matches'][0][$data['key']], $replacer, escapeQuotes($data['code']));
+}
+
+// Private function to determine wether we have a special expression function avaible
+// (mostly located in wrapper-functions.php)
+function isExpressionFunctionAvaiable ($data) {
+       // Get the enty we need
+       $entry = $data['matches'][4][$data['key']];
+
+       // Do we have cache?
+       if (!isset($GLOBALS['expression_function_available'][$entry])) {
+               // Init function name
+               $functionName = 'get';
+
+               // Explode it in an array
+               foreach (explode('_', $entry) as $piece) {
+                       // Add non-empty parts
+                       if (!empty($piece)) {
+                               // Add it
+                               $functionName .= capitalizeUnderscoreString($piece);
+                       } // END - if
+               } // END - foreach
+
+               // Is that function there?
+               if (function_exists($functionName)) {
+                       // Cache it all
+                       $GLOBALS['expression_function_name'][$entry] = $functionName;
+                       $GLOBALS['expression_function_available'][$entry] = true;
+               } else {
+                       // Not avaiable
+                       logDebugMessage(__FUNCTION__, __LINE__, 'Expression function ' . $functionName . ' not found. Please consider adding it to improve execution speed.');
+
+                       // And cache it
+                       $GLOBALS['expression_function_available'][$entry] = false;
+               }
+       } elseif ($GLOBALS['expression_function_available'][$entry] == false) {
+               // Debug message
+               logDebugMessage(__FUNCTION__, __LINE__, 'Expression function for entry ' . $entry . ' requested but not found.');
+       }
+
+       // Return cache
+       return $GLOBALS['expression_function_available'][$entry];
+}
+
+// Getter for above expression function
+function getExpressionFunction ($data) {
+       // Get the enty we need
+       $entry = $data['matches'][4][$data['key']];
+
+       // Return it
+       return $GLOBALS['expression_function_name'][$entry];
+}
+
 // Expression call-back function for getCode() calls
 function doExpressionCode ($data) {
        // Replace the code
-       $code = str_replace($data['matches'][0][$data['key']], "\" . getCode('" . $data['matches'][4][$data['key']] . "') . \"", $data['code']);
+       $code = str_replace($data['matches'][0][$data['key']], "{DQUOTE} . getCode('" . $data['matches'][4][$data['key']] . "') . {DQUOTE}", $data['code']);
 
        // Return replaced code
        return $code;
@@ -57,10 +112,10 @@ function doExpressionUrl ($data) {
        if ($data['callback'] == 'js') $data['mode'] = 1;
 
        // Handle an URL here
-       $replacer = "\" . encodeUrl(\"" . $data['matches'][4][$data['key']] . "\", " . $data['mode'] . ") . \"";
+       $replacer = "{DQUOTE} . encodeUrl('" . $data['matches'][4][$data['key']] . "', " . $data['mode'] . ') . {DQUOTE}';
 
        // Replace it
-       $code = str_replace($data['matches'][0][$data['key']], $replacer, $data['code']);
+       $code = replaceExpressionCode($data, $replacer);
 
        // Return replaced code
        return $code;
@@ -71,41 +126,11 @@ function doExpressionServer ($data) {
        // This will make 'foo_bar' to detectFooBar()
        $functionName = "'detect' . implode('', array_map('ucfirst', explode('_', '" . $data['callback'] . "')))";
 
-       // Replace it
-       $code = str_replace($data['matches'][0][$data['key']], "\" . call_user_func(" . $functionName . ") . \"", $data['code']);
-
-       // Return replaced code
-       return $code;
-}
-
-// Expression call-back function for fetching user data
-function doExpressionUser ($data) {
-       // Use current userid by default
-       $functionName = 'getMemberId()';
-
-       // User-related data, so is there a userid?
-       if (!empty($data['matches'][4][$data['key']])) {
-               // Do we have a userid or $userid?
-               if ($data['matches'][4][$data['key']] == '$userid') {
-                       // Use dynamic call
-                       $functionName = "getFetchedUserData('userid', \$userid, '" . $data['callback'] . "')";
-               } elseif ($data['matches'][4][$data['key']] > 0) {
-                       // User data found
-                       $functionName = "getFetchedUserData('userid', " . $data['matches'][4][$data['key']] . ", " . $data['callback'] . "')";
-               }
-       } elseif ((!empty($data['callback'])) && (isUserDataValid())) {
-               // "Call-back" alias column for current logged in user's data
-               $functionName = "getUserData('" . $data['callback'] . "')";
-       }
+       // Generate replacer
+       $replacer = '{DQUOTE} . call_user_func(' . $functionName . ') . {DQUOTE}';
 
-       // Do we have another function to run (e.g. translations)
-       if (!empty($data['extra_func'])) {
-               // Surround the original function call with it
-               $functionName = $data['extra_func'] . '(' . $functionName . ')';
-       } // END - if
-
-       // Now replace the code
-       $code = str_replace($data['matches'][0][$data['key']], "\" . " . $functionName . " . \"", $data['code']);
+       // Replace it
+       $code = replaceExpressionCode($data, $replacer);
 
        // Return replaced code
        return $code;
@@ -119,14 +144,17 @@ function doExpressionExt ($data) {
        // Is the extension installed?
        if (isExtensionInstalled($data['matches'][4][$data['key']])) {
                // Construct call-back function name
-               $functionName = 'getExtension' . ucfirst(strtolower($data['callback']));
+               $functionName = 'getExtension' . capitalizeUnderscoreString($data['callback']);
 
                // Construct call of the function
-               $replacer = "\" . call_user_func_array('" . $functionName . "', array('" . $data['matches'][4][$data['key']] . "', true)) . \"";
+               $replacer = "{DQUOTE} . call_user_func_array('" . $functionName . "', array('" . $data['matches'][4][$data['key']] . "', true)) . {DQUOTE}";
        } // END - if
 
+       // Generate replacer
+       $replacer = sprintf("&ext=%s&ver=%s&rev={?CURRENT_REPOSITORY_REVISION?}", $data['matches'][4][$data['key']], $replacer);
+
        // Replace it and insert parameter for GET request
-       $code = str_replace($data['matches'][0][$data['key']], sprintf("&ext=%s&ver=%s&rev=\" . getConfig('CURR_SVN_REVISION') . \"", $data['matches'][4][$data['key']], $replacer), $data['code']);
+       $code = replaceExpressionCode($data, $replacer);
 
        // Return replaced code
        return $code;
@@ -135,17 +163,43 @@ function doExpressionExt ($data) {
 // Expression call-back function for getting configuration data
 // @TODO FILTER_COMPILE_CONFIG does not handle call-back functions so we handle it here again
 function doExpressionConfig ($data) {
-       // Read configuration
-       $configValue = getConfig($data['matches'][4][$data['key']]);
+       // Do we have a special expression function for it?
+       if (isExpressionFunctionAvaiable($data)) {
+               // Then use it
+               $replacer = '{DQUOTE}  . ' . $data['callback'] . '(' . getExpressionFunction($data) . '()) . {DQUOTE}';
+       } else {
+               // Default replacer is the config value itself
+               $replacer = '{DQUOTE}  . ' . $data['callback'] . '(getConfig(' . "'" . $data['matches'][4][$data['key']] . "'" . ')) . {DQUOTE}';
+       }
+
+       // Replace the config entry
+       $code = replaceExpressionCode($data, $replacer);
+
+       // Return replaced code
+       return $code;
+}
+
+// Expression call-back function for piping data through functions
+function doExpressionPipe ($data) {
+       // We need callback and extra_func: callback is really the call-back function, extra_func is our value
+       $replacer = $data['extra_func'];
 
-       // Do we have a call-back?
+       // Do we have a call-back? Should always be there!
        if (!empty($data['callback'])) {
-               // Parse it through this function
-               $configValue = call_user_func_array($data['callback'], array($configValue));
+               //if ($data['callback'] == 'getMemberId') die('<pre>'.encodeEntities(print_r($data, true)).'</pre>');
+               // If the value is empty, we don't add it
+               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'value[' . gettype($data['value']) . ']=' . $data['value']);
+               if ((empty($data['value'])) && ($data['value'] != '0')) {
+                       // No value is set
+                       $replacer = '{DQUOTE} . ' . $data['extra_func2'] . '(' . $data['extra_func'] . '(' . $data['callback'] . '())) . {DQUOTE}';
+               } else {
+                       // Some value is set
+                       $replacer = '{DQUOTE} . ' . $data['extra_func2'] . '(' . $data['extra_func'] . '(' . $data['callback'] . "('" . $data['value'] . "'))) . {DQUOTE}";
+               }
        } // END - if
 
        // Replace the config entry
-       $code = str_replace($data['matches'][0][$data['key']], $configValue, $data['code']);
+       $code = replaceExpressionCode($data, $replacer);
 
        // Return replaced code
        return $code;
@@ -154,10 +208,10 @@ function doExpressionConfig ($data) {
 // Expression call-back function for calling filters
 function doExpressionFilter ($data) {
        // Construct replacement
-       $replacer = "\" . runFilterChain('" . $data['matches'][4][$data['key']] . "') . \"";
+       $replacer = "{DQUOTE} . runFilterChain('" . $data['matches'][4][$data['key']] . "') . {DQUOTE}";
 
        // Run the filter and insert result
-       $code = str_replace($data['matches'][0][$data['key']], $replacer, $data['code']);
+       $code = replaceExpressionCode($data, $replacer);
 
        // Return replaced code
        return $code;
@@ -178,7 +232,102 @@ function doExpressionValidatorLinks ($data) {
        } // END - if
 
        // Replace the code
-       $code = str_replace($data['matches'][0][$data['key']], $replacer, $code);
+       $code = replaceExpressionCode($data, $replacer);
+
+       // Return the (maybe) replaced code
+       return $code;
+}
+
+// Expression call-back for dynamic messages
+function doExpressionMessage ($data) {
+       // Debug message
+       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'callback=' . $data['callback'] . ',extra_func=' . $data['extra_func'] . ',value=' . $data['value']);
+
+       // Message string replacement depends on if message is masked
+       if ((isMessageMasked($data['callback'])) && ((!empty($data['extra_func'])) || ($data['extra_func'] == '0'))) {
+               // Message should be masked
+               $replacer = "{DQUOTE} . getMaskedMessage('" . $data['callback'] . "', '" . $data['extra_func'] . "') . {DQUOTE}";
+       } elseif (!empty($data['value'])) {
+               // value is set, so it is masked message
+               $replacer = "{DQUOTE} . getMaskedMessage('" . $data['callback'] . "', '" . $data['value'] . "') . {DQUOTE}";
+       } else {
+               // Regular message
+               $replacer = "{DQUOTE} . getMessage('" . $data['callback'] . "') . {DQUOTE}";
+       }
+
+       // Replace the code
+       $code = replaceExpressionCode($data, $replacer);
+
+       // Return the (maybe) replaced code
+       return $code;
+}
+
+// Expression call-back for template functions
+function doExpressionTemplate ($data) {
+       // Construct call-back function name
+       $callback = 'doTemplate' . $data['callback'];
+
+       // Init replacer
+       $replacer = '<!-- ['.__FUNCTION__.':'.__LINE__.'] Call-back function ' . $callback  . ' does not exist. //-->';
+
+       // Is the function there?
+       if (function_exists($callback)) {
+               // Do the replacement
+               //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'template='.$GLOBALS['current_template']);
+               $replacer = '{DQUOTE} . ' . $callback . "('" . $GLOBALS['current_template'] . "', true";
+
+               // Is 'value' set?
+               if (!empty($data['value'])) {
+                       // Then include it as well
+                       $replacer .= ", '" . $data['value'] . "'";
+               } // END - if
+
+               // Replacer is ready
+               $replacer .= ') . {DQUOTE}';
+       } // END - if
+
+       // Replace the code
+       $code = replaceExpressionCode($data, $replacer);
+
+       // Return the (maybe) replaced code
+       return $code;
+}
+
+// Expression call-back for math functions
+function doExpressionMath ($data) {
+       // Do the replacement
+       //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'template='.$GLOBALS['current_template']);
+       $replacer = '{DQUOTE} . doCalculate' . $data['callback'] . '(' . $data['value'] . ') . {DQUOTE}';
+
+       // Replace the code
+       $code = replaceExpressionCode($data, $replacer);
+
+       // Load include once
+       loadIncludeOnce('inc/math-functions.php');
+
+       // Return the (maybe) replaced code
+       return $code;
+}
+
+// Expression call-back for GET request
+function doExpressionGet ($data) {
+       // Construct the replacer
+       $replacer = '{%pipe,getRequestParameter=' . $data['value'] . '%}';
+
+       // Replace the code
+       $code = replaceExpressionCode($data, $replacer);
+
+       // Return the (maybe) replaced code
+       return $code;
+}
+
+// Expression call-back for POST request
+function doExpressionPost ($data) {
+       // Construct the replacer
+       $replacer = '{%pipe,postRequestParameter=' . $data['value'] . '%}';
+
+       // Replace the code
+       $code = replaceExpressionCode($data, $replacer);
 
        // Return the (maybe) replaced code
        return $code;