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 - 2011 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 wether 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 not found.');
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') {
113 $data['output_mode'] = 1;
116 // Handle an URL here
117 $replacer = "{DQUOTE} . encodeUrl('" . $data['matches'][4][$data['key']] . "', " . $data['output_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('firstCharUpperCase', 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={?CURRENT_REPOSITORY_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 //* DEBUG: */ if ($data['callback'] == 'getMemberId') die('<pre>'.encodeEntities(print_r($data, true)).'</pre>');
192 // If the value is empty, we don't add it
193 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'value[' . gettype($data['value']) . ']=' . $data['value']);
194 if ((empty($data['value'])) && ($data['value'] != '0')) {
196 $replacer = '{DQUOTE} . ' . $data['extra_func2'] . '(' . $data['extra_func'] . '(' . $data['callback'] . '())) . {DQUOTE}';
197 } elseif (isXmlTypeBool($data['value'])) {
198 // Boolean value detected
199 $replacer = '{DQUOTE} . ' . $data['extra_func2'] . '(' . $data['extra_func'] . '(' . $data['callback'] . '(' . $data['value'] . '))) . {DQUOTE}';
201 // Some string/integer value is set
202 $replacer = '{DQUOTE} . ' . $data['extra_func2'] . '(' . $data['extra_func'] . '(' . $data['callback'] . "('" . $data['value'] . "'))) . {DQUOTE}";
206 // Replace the config entry
207 $code = replaceExpressionCode($data, $replacer);
209 // Return replaced code
213 // Expression call-back function for calling filters
214 function doExpressionFilter ($data) {
215 // Construct replacement
216 $replacer = "{DQUOTE} . runFilterChain('" . $data['matches'][4][$data['key']] . "') . {DQUOTE}";
218 // Run the filter and insert result
219 $code = replaceExpressionCode($data, $replacer);
221 // Return replaced code
225 // Expression call-back function for validator links
226 function doExpressionValidatorLinks ($data) {
227 // Default is nothing
230 // Get the code from data array for replacement/pipe-through
231 $code = $data['code'];
233 // Should we generally include validator links?
234 if ((isExtensionInstalled('validator')) && (getConfig('enable_validator') == 'Y') && (!in_array(getModule(), array('admin', 'login')))) {
235 // Load the validator template
236 $replacer = escapeQuotes(loadTemplate('validator_links', true));
240 $code = replaceExpressionCode($data, $replacer);
242 // Return the (maybe) replaced code
246 // Expression call-back for dynamic messages
247 function doExpressionMessage ($data) {
249 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'callback=' . $data['callback'] . ',extra_func=' . $data['extra_func'] . ',value=' . $data['value']);
251 // Message string replacement depends on if message is masked
252 if ((isMessageMasked($data['callback'])) && ((!empty($data['extra_func'])) || ($data['extra_func'] == '0'))) {
253 // Message should be masked
254 $replacer = "{DQUOTE} . getMaskedMessage('" . $data['callback'] . "', '" . $data['extra_func'] . "') . {DQUOTE}";
255 } elseif (!empty($data['value'])) {
256 // value is set, so it is masked message
257 $replacer = "{DQUOTE} . getMaskedMessage('" . $data['callback'] . "', '" . $data['value'] . "') . {DQUOTE}";
260 $replacer = "{DQUOTE} . getMessage('" . $data['callback'] . "') . {DQUOTE}";
264 $code = replaceExpressionCode($data, $replacer);
266 // Return the (maybe) replaced code
270 // Expression call-back for template functions
271 function doExpressionTemplate ($data) {
272 // Construct call-back function name
273 $callback = 'doTemplate' . $data['callback'];
276 $replacer = '<!-- [' . __FUNCTION__ . ':' . __LINE__.'] Call-back function ' . $callback . ' does not exist. //-->';
278 // Is the function there?
279 if (function_exists($callback)) {
280 // Do the replacement
281 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'template='.$GLOBALS['current_template']);
282 $replacer = '{DQUOTE} . ' . $callback . "('" . $GLOBALS['current_template'] . "', true";
285 if (!empty($data['value'])) {
286 // Then include it as well
287 $replacer .= ", '" . $data['value'] . "'";
291 $replacer .= ') . {DQUOTE}';
293 // Log missing function
294 logDebugMessage(__FUNCTION__, __LINE__, 'Call-back function ' . $callback . ' does not exist.');
298 $code = replaceExpressionCode($data, $replacer);
300 // Return the (maybe) replaced code
304 // Expression call-back for math functions
305 function doExpressionMath ($data) {
306 // Do the replacement
307 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'template='.$GLOBALS['current_template']);
308 $replacer = '{DQUOTE} . doCalculate' . $data['callback'] . '(' . $data['value'] . ') . {DQUOTE}';
311 $code = replaceExpressionCode($data, $replacer);
314 loadIncludeOnce('inc/math-functions.php');
316 // Return the (maybe) replaced code
320 // Expression call-back for GET request
321 function doExpressionGet ($data) {
322 // Construct the replacer
323 $replacer = '{%pipe,getRequestElement=' . $data['value'] . '%}';
326 $code = replaceExpressionCode($data, $replacer);
328 // Return the (maybe) replaced code
332 // Expression call-back for POST request
333 function doExpressionPost ($data) {
334 // Construct the replacer
335 $replacer = '{%pipe,postRequestElement=' . $data['value'] . '%}';
338 $code = replaceExpressionCode($data, $replacer);
340 // Return the (maybe) replaced code