2 /************************************************************************
3 * Mailer v0.2.1-FINAL Start: 04/12/2009 *
4 * =================== Last change: 04/12/2009 *
6 * -------------------------------------------------------------------- *
7 * File : expression-functions.php *
8 * -------------------------------------------------------------------- *
9 * Short description : Expression callback functions *
10 * -------------------------------------------------------------------- *
11 * Kurzbeschreibung : Expression-Callback-Funktionen *
12 * -------------------------------------------------------------------- *
15 * $Tag:: 0.2.1-FINAL $ *
17 * Needs to be in all Files and every File needs "svn propset *
18 * svn:keywords Date Revision" (autoprobset!) at least!!!!!! *
19 * -------------------------------------------------------------------- *
20 * Copyright (c) 2003 - 2009 by Roland Haeder *
21 * Copyright (c) 2009, 2010 by Mailer Developer Team *
22 * For more information visit: http://www.mxchange.org *
24 * This program is free software; you can redistribute it and/or modify *
25 * it under the terms of the GNU General Public License as published by *
26 * the Free Software Foundation; either version 2 of the License, or *
27 * (at your option) any later version. *
29 * This program is distributed in the hope that it will be useful, *
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
32 * GNU General Public License for more details. *
34 * You should have received a copy of the GNU General Public License *
35 * along with this program; if not, write to the Free Software *
36 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, *
38 ************************************************************************/
40 // Some security stuff...
41 if (!defined('__SECURITY')) {
45 // Private function to replace the code
46 function replaceExpressionCode ($data, $replacer) {
48 // @TODO is escapeQuotes() enougth for strings with single/double quotes?
49 return str_replace($data['matches'][0][$data['key']], $replacer, escapeQuotes($data['code']));
52 // Private function to determine wether we have a special expression function avaible
53 // (mostly located in wrapper-functions.php)
54 function isExpressionFunctionAvaiable ($data) {
55 // Get the enty we need
56 $entry = $data['matches'][4][$data['key']];
59 if (!isset($GLOBALS['expression_function_available'][$entry])) {
61 $functionName = 'get';
63 // Explode it in an array
64 foreach (explode('_', $entry) as $piece) {
65 // Add non-empty parts
68 $functionName .= capitalizeUnderscoreString($piece);
72 // Is that function there?
73 if (function_exists($functionName)) {
75 $GLOBALS['expression_function_name'][$entry] = $functionName;
76 $GLOBALS['expression_function_available'][$entry] = true;
79 logDebugMessage(__FUNCTION__, __LINE__, 'Expression function ' . $functionName . ' not found. Please consider adding it to improve execution speed.');
82 $GLOBALS['expression_function_available'][$entry] = false;
84 } elseif ($GLOBALS['expression_function_available'][$entry] == false) {
86 logDebugMessage(__FUNCTION__, __LINE__, 'Expression function for entry ' . $entry . ' requested but not found.');
90 return $GLOBALS['expression_function_available'][$entry];
93 // Getter for above expression function
94 function getExpressionFunction ($data) {
95 // Get the enty we need
96 $entry = $data['matches'][4][$data['key']];
99 return $GLOBALS['expression_function_name'][$entry];
102 // Expression call-back function for getCode() calls
103 function doExpressionCode ($data) {
105 $code = str_replace($data['matches'][0][$data['key']], "{DQUOTE} . getCode('" . $data['matches'][4][$data['key']] . "') . {DQUOTE}", $data['code']);
107 // Return replaced code
111 // Expression call-back function for URLs
112 function doExpressionUrl ($data) {
113 // Do we have JS-mode?
114 if ($data['callback'] == 'js') $data['mode'] = 1;
116 // Handle an URL here
117 $replacer = "{DQUOTE} . encodeUrl('" . $data['matches'][4][$data['key']] . "', " . $data['mode'] . ') . {DQUOTE}';
120 $code = replaceExpressionCode($data, $replacer);
122 // Return replaced code
126 // Expression call-back function for reading data from $_SERVER
127 function doExpressionServer ($data) {
128 // This will make 'foo_bar' to detectFooBar()
129 $functionName = "'detect' . implode('', array_map('ucfirst', explode('_', '" . $data['callback'] . "')))";
132 $replacer = '{DQUOTE} . call_user_func(' . $functionName . ') . {DQUOTE}';
135 $code = replaceExpressionCode($data, $replacer);
137 // Return replaced code
141 // Expression call-back function for getting extension data
142 function doExpressionExt ($data) {
143 // Not installed is default
146 // Is the extension installed?
147 if (isExtensionInstalled($data['matches'][4][$data['key']])) {
148 // Construct call-back function name
149 $functionName = 'getExtension' . capitalizeUnderscoreString($data['callback']);
151 // Construct call of the function
152 $replacer = "{DQUOTE} . call_user_func_array('" . $functionName . "', array('" . $data['matches'][4][$data['key']] . "', true)) . {DQUOTE}";
156 $replacer = sprintf("&ext=%s&ver=%s&rev={?CURR_SVN_REVISION?}", $data['matches'][4][$data['key']], $replacer);
158 // Replace it and insert parameter for GET request
159 $code = replaceExpressionCode($data, $replacer);
161 // Return replaced code
165 // Expression call-back function for getting configuration data
166 // @TODO FILTER_COMPILE_CONFIG does not handle call-back functions so we handle it here again
167 function doExpressionConfig ($data) {
168 // Do we have a special expression function for it?
169 if (isExpressionFunctionAvaiable($data)) {
171 $replacer = '{DQUOTE} . ' . $data['callback'] . '(' . getExpressionFunction($data) . '()) . {DQUOTE}';
173 // Default replacer is the config value itself
174 $replacer = '{DQUOTE} . ' . $data['callback'] . '(getConfig(' . "'" . $data['matches'][4][$data['key']] . "'" . ')) . {DQUOTE}';
177 // Replace the config entry
178 $code = replaceExpressionCode($data, $replacer);
180 // Return replaced code
184 // Expression call-back function for piping data through functions
185 function doExpressionPipe ($data) {
186 // We need callback and extra_func: callback is really the call-back function, extra_func is our value
187 $replacer = $data['extra_func'];
189 // Do we have a call-back? Should always be there!
190 if (!empty($data['callback'])) {
191 //if ($data['callback'] == 'getMemberId') die('<pre>'.encodeEntities(print_r($data, true)).'</pre>');
192 // If the value is empty, we don't add it
193 if (empty($data['value'])) {
195 $replacer = '{DQUOTE} . ' . $data['extra_func2'] . '(' . $data['extra_func'] . '(' . $data['callback'] . '())) . {DQUOTE}';
198 $replacer = '{DQUOTE} . ' . $data['extra_func2'] . '(' . $data['extra_func'] . '(' . $data['callback'] . "('" . $data['value'] . "'))) . {DQUOTE}";
202 // Replace the config entry
203 $code = replaceExpressionCode($data, $replacer);
205 // Return replaced code
209 // Expression call-back function for calling filters
210 function doExpressionFilter ($data) {
211 // Construct replacement
212 $replacer = "{DQUOTE} . runFilterChain('" . $data['matches'][4][$data['key']] . "') . {DQUOTE}";
214 // Run the filter and insert result
215 $code = replaceExpressionCode($data, $replacer);
217 // Return replaced code
221 // Expression call-back function for validator links
222 function doExpressionValidatorLinks ($data) {
223 // Default is nothing
226 // Get the code from data array for replacement/pipe-through
227 $code = $data['code'];
229 // Should we generally include validator links?
230 if ((isExtensionInstalled('validator')) && (getConfig('enable_validator') == 'Y') && (!in_array(getModule(), array('admin', 'login')))) {
231 // Load the validator template
232 $replacer = escapeQuotes(loadTemplate('validator_links', true));
236 $code = replaceExpressionCode($data, $replacer);
238 // Return the (maybe) replaced code
242 // Expression call-back for dynamic messages
243 function doExpressionMessage ($data) {
245 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'callback=' . $data['callback'] . ',extra_func=' . $data['extra_func'] . ',value=' . $data['value']);
247 // Message string replacement depends on if message is masked
248 if ((isMessageMasked($data['callback'])) && ((!empty($data['extra_func'])) || ($data['extra_func'] == '0'))) {
249 // Message should be masked
250 $replacer = "{DQUOTE} . getMaskedMessage('" . $data['callback'] . "', '" . $data['extra_func'] . "') . {DQUOTE}";
251 } elseif (!empty($data['value'])) {
252 // value is set, so it is masked message
253 $replacer = "{DQUOTE} . getMaskedMessage('" . $data['callback'] . "', '" . $data['value'] . "') . {DQUOTE}";
256 $replacer = "{DQUOTE} . getMessage('" . $data['callback'] . "') . {DQUOTE}";
260 $code = replaceExpressionCode($data, $replacer);
262 // Return the (maybe) replaced code
266 // Expression call-back for template functions
267 function doExpressionTemplate ($data) {
268 // Do the replacement
269 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'template='.$GLOBALS['current_template']);
270 $replacer = '{DQUOTE} . doTemplate' . $data['callback'] . "('" . $GLOBALS['current_template'] . "', true";
273 if (!empty($data['value'])) {
274 // Then include it as well
275 $replacer .= ", '" . $data['value'] . "'";
279 $replacer .= ') . {DQUOTE}';
282 $code = replaceExpressionCode($data, $replacer);
284 // Return the (maybe) replaced code
288 // Expression call-back for math functions
289 function doExpressionMath ($data) {
290 // Do the replacement
291 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'template='.$GLOBALS['current_template']);
292 $replacer = '{DQUOTE} . doCalculate' . $data['callback'] . '(' . $data['value'] . ') . {DQUOTE}';
295 $code = replaceExpressionCode($data, $replacer);
298 loadIncludeOnce('inc/math-functions.php');
300 // Return the (maybe) replaced code
304 // Expression call-back for GET request
305 function doExpressionGet ($data) {
306 // Construct the replacer
307 $replacer = '{%pipe,getRequestParameter=' . $data['value'] . '%}';
310 $code = replaceExpressionCode($data, $replacer);
312 // Return the (maybe) replaced code
316 // Expression call-back for POST request
317 function doExpressionPost ($data) {
318 // Construct the replacer
319 $replacer = '{%pipe,postRequestParameter=' . $data['value'] . '%}';
322 $code = replaceExpressionCode($data, $replacer);
324 // Return the (maybe) replaced code