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 * -------------------------------------------------------------------- *
18 * Copyright (c) 2003 - 2009 by Roland Haeder *
19 * Copyright (c) 2009 - 2012 by Mailer Developer Team *
20 * For more information visit: http://mxchange.org *
22 * This program is free software; you can redistribute it and/or modify *
23 * it under the terms of the GNU General Public License as published by *
24 * the Free Software Foundation; either version 2 of the License, or *
25 * (at your option) any later version. *
27 * This program is distributed in the hope that it will be useful, *
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
30 * GNU General Public License for more details. *
32 * You should have received a copy of the GNU General Public License *
33 * along with this program; if not, write to the Free Software *
34 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, *
36 ************************************************************************/
38 // Some security stuff...
39 if (!defined('__SECURITY')) {
43 // Private function to replace the code
44 function replaceExpressionCode ($data, $replacer) {
46 // @TODO is escapeQuotes() enougth for strings with single/double quotes?
47 return str_replace($data['matches'][0][$data['key']], $replacer, escapeQuotes($data['code']));
50 // Private function to determine whether we have a special expression function avaible
51 // (mostly located in wrapper-functions.php)
52 function isExpressionFunctionAvaiable ($data) {
53 // Get the enty we need and trim it
54 $entry = trim($data['matches'][4][$data['key']]);
57 if (!isset($GLOBALS['expression_function_available'][$entry])) {
59 $functionName = 'get';
61 // Explode it in an array
62 foreach (explode('_', $entry) as $piece) {
63 // Add non-empty parts
66 $functionName .= capitalizeUnderscoreString($piece);
70 // Is that function there?
71 if (function_exists($functionName)) {
73 $GLOBALS['expression_function_name'][$entry] = $functionName;
74 $GLOBALS['expression_function_available'][$entry] = true;
77 logDebugMessage(__FUNCTION__, __LINE__, 'Expression function ' . $functionName . ' not found. Please consider adding it to improve execution speed.');
80 $GLOBALS['expression_function_available'][$entry] = false;
82 } elseif ($GLOBALS['expression_function_available'][$entry] == false) {
84 logDebugMessage(__FUNCTION__, __LINE__, 'Expression function for entry ' . $entry . ' requested but does not exist.');
88 return $GLOBALS['expression_function_available'][$entry];
91 // Getter for above expression function
92 function getExpressionFunction ($data) {
93 // Get the enty we need
94 $entry = $data['matches'][4][$data['key']];
97 return $GLOBALS['expression_function_name'][$entry];
100 // Expression call-back function for getCode() calls
101 function doExpressionCode ($data) {
103 $code = str_replace($data['matches'][0][$data['key']], "{DQUOTE} . getCode('" . $data['matches'][4][$data['key']] . "') . {DQUOTE}", $data['code']);
105 // Return replaced code
109 // Expression call-back function for URLs
110 function doExpressionUrl ($data) {
111 // Do we have JS-mode?
112 if ($data['callback'] == 'js') {
114 $data['output_mode'] = '1';
117 // Handle an URL here
118 $replacer = "{DQUOTE} . encodeUrl('" . $data['matches'][4][$data['key']] . "', " . $data['output_mode'] . ') . {DQUOTE}';
121 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'replacer=' . $replacer . ',callback=' . $data['callback']);
124 $code = replaceExpressionCode($data, $replacer);
126 // Return replaced code
130 // Expression call-back function for reading data from $_SERVER
131 function doExpressionServer ($data) {
132 // This will make 'foo_bar' to detectFooBar()
133 $functionName = "'detect' . implode('', array_map('firstCharUpperCase', explode('_', '" . $data['callback'] . "')))";
136 $replacer = '{DQUOTE} . call_user_func(' . $functionName . ') . {DQUOTE}';
139 $code = replaceExpressionCode($data, $replacer);
141 // Return replaced code
145 // Expression call-back function for getting extension data
146 function doExpressionExt ($data) {
147 // Not installed is default
150 // Is the extension installed?
151 if (isExtensionInstalled($data['matches'][4][$data['key']])) {
152 // Construct call-back function name
153 $functionName = 'getExtension' . capitalizeUnderscoreString($data['callback']);
155 // Construct call of the function
156 $replacer = "{DQUOTE} . call_user_func_array('" . $functionName . "', array('" . $data['matches'][4][$data['key']] . "', true)) . {DQUOTE}";
160 $replacer = sprintf("&ext=%s&ver=%s&rev={?CURRENT_REPOSITORY_REVISION?}", $data['matches'][4][$data['key']], $replacer);
162 // Replace it and insert parameter for GET request
163 $code = replaceExpressionCode($data, $replacer);
165 // Return replaced code
169 // Expression call-back function for getting configuration data
170 // @TODO FILTER_COMPILE_CONFIG does not handle call-back functions so we handle it here again
171 function doExpressionConfig ($data) {
172 // Do we have a special expression function for it?
173 if (isExpressionFunctionAvaiable($data)) {
175 $replacer = '{DQUOTE} . ' . $data['callback'] . '(' . getExpressionFunction($data) . '()) . {DQUOTE}';
176 } elseif (!isConfigEntrySet($data['matches'][4][$data['key']])) {
177 // Config entry is not set
178 $replacer = '{DQUOTE} . ' . $data['callback'] . '(NULL) . {DQUOTE}';
180 // Default replacer is the config value itself
181 $replacer = '{DQUOTE} . ' . $data['callback'] . '(getConfig(' . chr(39) . $data['matches'][4][$data['key']] . chr(39) . ')) . {DQUOTE}';
184 // Replace the config entry
185 $code = replaceExpressionCode($data, $replacer);
187 // Return replaced code
191 // Expression call-back function for piping data through functions
192 function doExpressionPipe ($data) {
193 // We need callback and extra_func: callback is really the call-back function, extra_func is our value
194 $replacer = $data['extra_func'];
196 // Do we have a call-back? Should always be there!
197 if (!empty($data['callback'])) {
198 //* DEBUG: */ if ($data['callback'] == 'getMemberId') die('<pre>'.encodeEntities(print_r($data, true)).'</pre>');
199 // If the value is empty, we don't add it
200 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'value[' . gettype($data['value']) . ']=' . $data['value']);
201 if ((empty($data['value'])) && ($data['value'] != '0')) {
203 $replacer = '{DQUOTE} . ' . $data['extra_func2'] . '(' . $data['extra_func'] . '(' . $data['callback'] . '())) . {DQUOTE}';
204 } elseif (isXmlTypeBool($data['value'])) {
205 // Boolean value detected
206 $replacer = '{DQUOTE} . ' . $data['extra_func2'] . '(' . $data['extra_func'] . '(' . $data['callback'] . '(' . $data['value'] . '))) . {DQUOTE}';
208 // Some string/integer value is set
209 $replacer = '{DQUOTE} . ' . $data['extra_func2'] . '(' . $data['extra_func'] . '(' . $data['callback'] . "('" . $data['value'] . "'))) . {DQUOTE}";
213 // Replace the config entry
214 $code = replaceExpressionCode($data, $replacer);
216 // Return replaced code
220 // Expression call-back function for calling filters
221 function doExpressionFilter ($data) {
222 // Construct replacement
223 $replacer = "{DQUOTE} . runFilterChain('" . $data['matches'][4][$data['key']] . "') . {DQUOTE}";
225 // Run the filter and insert result
226 $code = replaceExpressionCode($data, $replacer);
228 // Return replaced code
232 // Expression call-back function for validator links
233 function doExpressionValidatorLinks ($data) {
234 // Default is nothing
237 // Get the code from data array for replacement/pipe-through
238 $code = $data['code'];
240 // Should we generally include validator links?
241 if ((isExtensionInstalled('validator')) && (getConfig('enable_validator') == 'Y') && (!in_array(getModule(), array('admin', 'login')))) {
242 // Load the validator template
243 $replacer = escapeQuotes(loadTemplate('validator_links', true));
247 $code = replaceExpressionCode($data, $replacer);
249 // Return the (maybe) replaced code
253 // Expression call-back for dynamic messages
254 function doExpressionMessage ($data) {
256 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'callback=' . $data['callback'] . ',extra_func=' . $data['extra_func'] . ',value=' . $data['value']);
258 // Message string replacement depends on if message is masked
259 if ((isMessageMasked($data['callback'], false)) && ((!empty($data['extra_func'])) || ($data['extra_func'] == '0'))) {
260 // Message should be masked
261 $replacer = "{DQUOTE} . getMaskedMessage('" . $data['callback'] . "', '" . $data['extra_func'] . "') . {DQUOTE}";
262 } elseif (!empty($data['value'])) {
263 // value is set, so it is masked message
264 $replacer = "{DQUOTE} . getMaskedMessage('" . $data['callback'] . "', '" . $data['value'] . "') . {DQUOTE}";
267 $replacer = "{DQUOTE} . getMessage('" . $data['callback'] . "') . {DQUOTE}";
271 $code = replaceExpressionCode($data, $replacer);
273 // Return the (maybe) replaced code
277 // Expression call-back for template functions
278 function doExpressionTemplate ($data) {
279 // Construct call-back function name
280 $callbackFunction = 'doTemplate' . $data['callback'];
283 $replacer = '<!-- [' . __FUNCTION__ . ':' . __LINE__.'] Call-back function ' . $callbackFunction . ' does not exist. //-->';
285 // Is the function there?
286 if (!isset($GLOBALS['current_template'])) {
287 // This is very bad and needs fixing
288 reportBug(__FUNCTION__, __LINE__, 'current_template in GLOBALS not set, callbackFunction=' . $callbackFunction . ',function_exists()=' . intval(function_exists($callbackFunction)));
289 } elseif (!function_exists($callbackFunction)) {
290 // Log missing function
291 logDebugMessage(__FUNCTION__, __LINE__, 'Call-back function ' . $callbackFunction . ' does not exist.');
293 // Do the replacement
294 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'template='.$GLOBALS['current_template']);
295 $replacer = '{DQUOTE} . ' . $callbackFunction . '(' . chr(39) . $GLOBALS['current_template'] . chr(39) . ', true';
298 if (!empty($data['value'])) {
299 // Then include it as well
300 $replacer .= ', ' . chr(39) . $data['value'] . chr(39);
304 $replacer .= ') . {DQUOTE}';
308 $code = replaceExpressionCode($data, $replacer);
310 // Return the (maybe) replaced code
314 // Expression call-back for math functions
315 function doExpressionMath ($data) {
316 // Do the replacement
317 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'template='.$GLOBALS['current_template']);
318 $replacer = '{DQUOTE} . doCalculate' . $data['callback'] . '(' . $data['value'] . ') . {DQUOTE}';
321 $code = replaceExpressionCode($data, $replacer);
324 loadIncludeOnce('inc/math-functions.php');
326 // Return the (maybe) replaced code
330 // Expression call-back for GET request
331 function doExpressionGet ($data) {
332 // Construct the replacer
333 $replacer = '{%pipe,getRequestElement=' . $data['value'] . '%}';
336 $code = replaceExpressionCode($data, $replacer);
338 // Return the (maybe) replaced code
342 // Expression call-back for POST request
343 function doExpressionPost ($data) {
344 // Construct the replacer
345 $replacer = '{%pipe,postRequestElement=' . $data['value'] . '%}';
348 $code = replaceExpressionCode($data, $replacer);
350 // Return the (maybe) replaced code
354 // Expression call-back for session data
355 function doExpressionSession ($data) {
356 // Construct the replacer
357 $replacer = '{%pipe,getSession=' . $data['value'] . '%}';
360 //* NOISY-DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'value=' . $data['value'] . ',replacer=' . $replacer);
363 $code = replaceExpressionCode($data, $replacer);
365 // Return the (maybe) replaced code
370 * Expression call-back for session piplining, this means:
372 * 1) Read session data
373 * 2) Wrap the raw data into {%pipe,fooFunction=$rawValue%}
375 function doExpressionSessionPipe ($data) {
376 // Get the session data
377 $rawValue = getSession($data['value']);
379 // Construct the replacer
380 $replacer = '{%pipe,' . $data['callback'] . '=' . $rawValue . '%}';
383 //* NOISY-DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'value=' . $data['value'] . ',rawValue=' . $rawValue . ',replacer=' . $replacer);
386 $code = replaceExpressionCode($data, $replacer);
388 // Return the (maybe) replaced code