From: Roland Häder Date: Fri, 4 Dec 2009 01:09:18 +0000 (+0000) Subject: Expression call-back functions are now externalized in own PHP script, redirect had... X-Git-Url: https://git.mxchange.org/?p=mailer.git;a=commitdiff_plain;h=c00854616d540e2372c89cc04ab3677544efbd35 Expression call-back functions are now externalized in own PHP script, redirect had a debug-line active :( --- diff --git a/.gitattributes b/.gitattributes index f3c1ca3a6b..ac51f2424b 100644 --- a/.gitattributes +++ b/.gitattributes @@ -113,6 +113,7 @@ inc/debug/client/.htaccess -text inc/debug/relay/.htaccess -text inc/debug/request_ -text inc/debug/server/.htaccess -text +inc/expression-functions.php -text inc/extensions-functions.php -text inc/extensions/.htaccess -text inc/extensions/ext- -text diff --git a/DOCS/TODOs.txt b/DOCS/TODOs.txt index 103df2caf8..f07be0ad56 100644 --- a/DOCS/TODOs.txt +++ b/DOCS/TODOs.txt @@ -32,8 +32,6 @@ ./inc/extensions/ext-yoomedia.php:55:// @TODO Only deprecated when 'network' is ready! setExtensionDeprecated('Y'); ./inc/extensions-functions.php:379:// @TODO Change from ext_id to ext_name (not just even the variable! ;-) ) ./inc/extensions-functions.php:492: // @TODO Extension is loaded, what next? -./inc/filters.php:472: // @TODO This whole switch-block is very static -./inc/filters.php:543: case 'config': // @TODO FILTER_COMPILE_CONFIG does not handle call-back functions so we handle it here again ./inc/functions.php:1010:// @TODO $simple is deprecated ./inc/functions.php:1045: // @TODO Do only use $content and deprecate $GLOBALS and $DATA in templates ./inc/functions.php:2521:// @TODO Please describe this function diff --git a/inc/expression-functions.php b/inc/expression-functions.php new file mode 100644 index 0000000000..485e619585 --- /dev/null +++ b/inc/expression-functions.php @@ -0,0 +1,165 @@ + 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'] . "')"; + } + + // Do we have another function to run (e.g. translations) + if (!empty($extraFunction)) { + // Surround the original function call with it + $functionName = $extraFunction . '(' . $functionName . ')'; + } // END - if + + // Now replace the code + $code = str_replace($data['matches'][0][$data['key']], "\" . " . $functionName . " . \"", $data['code']); + + // Return replaced code + return $code; +} + +// Expression call-back function for getting extension data +function doExpressionExt ($data) { + // Not installed is default + $replacer = 'false'; + + // Is the extension installed? + if (isExtensionInstalled($data['matches'][4][$data['key']])) { + // Construct call-back function name + $functionName = 'getExtension' . ucfirst(strtolower($data['callback'])); + + // Construct call of the function + $replacer = "\" . call_user_func_array('" . $functionName . "', array('" . $data['matches'][4][$data['key']] . "', true)) . \""; + } // END - if + + // 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']); + + // Return replaced code + return $code; +} + +// Expression call-back function for getting configuration data +function doExpressionConfig ($data) { + // Read configuration + $configValue = getConfig($data['matches'][4][$data['key']]); + + // Do we have a call-back? + if (!empty($data['callback'])) { + // Parse it through this function + $configValue = call_user_func_array($data['callback'], array($configValue)); + } // END - if + + // Replace the config entry + $code = str_replace($data['matches'][0][$data['key']], $configValue, $data['code']); + + // Return replaced code + return $code; +} + +// Expression call-back function for calling filters +function doExpressionFilter ($data) { + // Construct replacement + $replacer = "\" . runFilterChain('" . $data['matches'][4][$data['key']] . "') . \""; + + // Run the filter and insert result + $code = str_replace($data['matches'][0][$data['key']], $replacer, $data['code']); + + // Return replaced code + return $code; +} + +// [EOF] +?> diff --git a/inc/filters.php b/inc/filters.php index 35ff120eb7..fd55e5785a 100644 --- a/inc/filters.php +++ b/inc/filters.php @@ -445,7 +445,7 @@ function FILTER_COMPILE_CONFIG ($code, $compiled = false) { // Filter for compiling expression code function FILTER_COMPILE_EXPRESSION_CODE ($code) { - // Compile {%cmd=some_value%} to get expression code snippets + // Compile {%cmd,callback,extraFunction=some_value%} to get expression code snippets // See switch() command below for supported commands preg_match_all('/\{%(([a-zA-Z0-9-_,]+)(=([^\}]+)){0,1})*%\}/', $code, $matches); //* DEBUG: */ print('
'.print_r($matches, true).'
'); @@ -465,108 +465,29 @@ function FILTER_COMPILE_EXPRESSION_CODE ($code) { // Extract command and call-back $cmdArray = explode(',', $cmd); $cmd = $cmdArray[0]; - if (isset($cmdArray[1])) $callback = $cmdArray[1]; + if (isset($cmdArray[1])) $callback = $cmdArray[1]; if (isset($cmdArray[2])) $extraFunction = $cmdArray[2]; - // Is the extension installed or code provided? - // @TODO This whole switch-block is very static - switch ($cmd) { - case 'code': // Code asked for - $code = str_replace($matches[0][$key], "\" . getCode('" . $matches[4][$key] . "') . \"", $code); - break; - - case 'url': - // Do we have JS-mode? - if (substr($cmd, -2, 2) == 'js') $outputMode = 1; - - // Handle an URL here - $replacer = "\" . encodeUrl(\"" . $matches[4][$key] . "\", " . $outputMode . ") . \""; - - // Replace it - $code = str_replace($matches[0][$key], $replacer, $code); - break; - - case 'server': - // This will make 'foo_bar' to detectFooBar() - $functionName = "'detect' . implode('', array_map('ucfirst', explode('_', '" . $callback . "')))"; - - // Replace it - $code = str_replace($matches[0][$key], "\" . call_user_func(" . $functionName . ") . \"", $code); - break; - - case 'user': - // Use current userid by default - $functionName = 'getMemberId()'; - - // User-related data, so is there a userid? - if (!empty($matches[4][$key])) { - // Do we have a userid or $userid? - if ($matches[4][$key] == '$userid') { - // Use dynamic call - $functionName = "getFetchedUserData('userid', \$userid, '" . $callback . "')"; - } elseif ($matches[4][$key] > 0) { - // User data found - $functionName = "getFetchedUserData('userid', " . $matches[4][$key] . ", " . $callback . "')"; - } // END - if - } elseif ((!empty($callback)) && (isUserDataValid())) { - // "Call-back" alias column for current logged in user's data - $functionName = "getUserData('" . $callback . "')"; - } - - // Do we have another function to run (e.g. translations) - if (!empty($extraFunction)) { - // Surround the original function call with it - $functionName = $extraFunction . '(' . $functionName . ')'; - } // END - if - - // Now replace the code - $code = str_replace($matches[0][$key], "\" . " . $functionName . " . \"", $code); - break; - - case 'ext': - // Not installed is default - $replacer = 'false'; - - // Is the extension installed? - if (isExtensionInstalled($matches[4][$key])) { - // Construct call-back function name - $functionName = 'getExtension' . ucfirst(strtolower($callback)); - - // Construct call of the function - $replacer = "\" . call_user_func_array('" . $functionName . "', array('" . $matches[4][$key] . "', true)) . \""; - } // END - if - - // Replace it and insert parameter for GET request - $code = str_replace($matches[0][$key], sprintf("&ext=%s&ver=%s&rev=\" . getConfig('CURR_SVN_REVISION') . \"", $matches[4][$key], $replacer), $code); - break; - - case 'config': // @TODO FILTER_COMPILE_CONFIG does not handle call-back functions so we handle it here again - // Read configuration - $configValue = getConfig($matches[4][$key]); - - // Do we have a call-back? - if (!empty($callback)) { - // Parse it through this function - $configValue = call_user_func_array($callback, array($configValue)); - } // END - if - - // Replace the config entry - $code = str_replace($matches[0][$key], $configValue, $code); - break; - - case 'filter': - // Construct replacement - $replacer = "\" . runFilterChain('" . $matches[4][$key] . "') . \""; - - // Run the filter and insert result - $code = str_replace($matches[0][$key], $replacer, $code); - break; - - default: - // Unsupported command detected - debug_report_bug('Command=' . $cmd . ', callback=' . $callback . ', extra=' . $extraFunction . ' is unsupported.'); - break; - } // END - switch + // Construct call-back function name for the command + $commandFunction = 'doExpression' . ucfirst(strtolower($cmd)); + + // Is this function there? + if (function_exists($commandFunction)) { + // Prepare $matches, $key, $outputMode, etc. + $data = array( + 'matches' => $matches, + 'key' => $key, + 'mode' => $outputMode, + 'code' => $code, + 'callback' => $callback + ); + + // Call it + $code = call_user_func($commandFunction, $data); + } else { + // Unsupported command detected + debug_report_bug('Command=' . $cmd . ', callback=' . $callback . ', extra=' . $extraFunction . ' is unsupported.'); + } } // END - foreach } // END - if diff --git a/inc/functions.php b/inc/functions.php index 515455d3da..3b95accab0 100644 --- a/inc/functions.php +++ b/inc/functions.php @@ -936,7 +936,7 @@ function redirectToUrl ($URL) { } // END - if // Three different ways to debug... - /* DEBUG: */ debug_report_bug(sprintf("%s[%s:] URL=%s", __FUNCTION__, __LINE__, $URL)); + //* DEBUG: */ debug_report_bug(sprintf("%s[%s:] URL=%s", __FUNCTION__, __LINE__, $URL)); //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'URL=' . $URL); //* DEBUG: */ die($URL); diff --git a/inc/mysql-connect.php b/inc/mysql-connect.php index 92d7936266..0dd8936b1e 100644 --- a/inc/mysql-connect.php +++ b/inc/mysql-connect.php @@ -42,7 +42,7 @@ if (!defined('__SECURITY')) { } // END - if // Load more function libraries or includes -foreach (array('functions', 'request-functions', 'session-functions', 'code-functions', 'language-functions', 'sql-functions', 'filter-functions', 'filters', 'mysql-manager', 'extensions-functions', 'handler') as $lib) { +foreach (array('functions', 'request-functions', 'session-functions', 'code-functions', 'language-functions', 'sql-functions', 'expression-functions', 'filter-functions', 'filters', 'mysql-manager', 'extensions-functions', 'handler') as $lib) { // Load special functions loadIncludeOnce('inc/' . $lib . '.php'); } // END - foreach