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://www.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
54 $entry = $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') $data['mode'] = 1;
114 // Handle an URL here
115 $replacer = "{DQUOTE} . encodeUrl('" . $data['matches'][4][$data['key']] . "', " . $data['mode'] . ') . {DQUOTE}';
118 $code = replaceExpressionCode($data, $replacer);
120 // Return replaced code
124 // Expression call-back function for reading data from $_SERVER
125 function doExpressionServer ($data) {
126 // This will make 'foo_bar' to detectFooBar()
127 $functionName = "'detect' . implode('', array_map('firstCharUpperCase', explode('_', '" . $data['callback'] . "')))";
130 $replacer = '{DQUOTE} . call_user_func(' . $functionName . ') . {DQUOTE}';
133 $code = replaceExpressionCode($data, $replacer);
135 // Return replaced code
139 // Expression call-back function for getting extension data
140 function doExpressionExt ($data) {
141 // Not installed is default
144 // Is the extension installed?
145 if (isExtensionInstalled($data['matches'][4][$data['key']])) {
146 // Construct call-back function name
147 $functionName = 'getExtension' . capitalizeUnderscoreString($data['callback']);
149 // Construct call of the function
150 $replacer = "{DQUOTE} . call_user_func_array('" . $functionName . "', array('" . $data['matches'][4][$data['key']] . "', true)) . {DQUOTE}";
154 $replacer = sprintf("&ext=%s&ver=%s&rev={?CURRENT_REPOSITORY_REVISION?}", $data['matches'][4][$data['key']], $replacer);
156 // Replace it and insert parameter for GET request
157 $code = replaceExpressionCode($data, $replacer);
159 // Return replaced code
163 // Expression call-back function for getting configuration data
164 // @TODO FILTER_COMPILE_CONFIG does not handle call-back functions so we handle it here again
165 function doExpressionConfig ($data) {
166 // Do we have a special expression function for it?
167 if (isExpressionFunctionAvaiable($data)) {
169 $replacer = '{DQUOTE} . ' . $data['callback'] . '(' . getExpressionFunction($data) . '()) . {DQUOTE}';
171 // Default replacer is the config value itself
172 $replacer = '{DQUOTE} . ' . $data['callback'] . '(getConfig(' . "'" . $data['matches'][4][$data['key']] . "'" . ')) . {DQUOTE}';
175 // Replace the config entry
176 $code = replaceExpressionCode($data, $replacer);
178 // Return replaced code
182 // Expression call-back function for piping data through functions
183 function doExpressionPipe ($data) {
184 // We need callback and extra_func: callback is really the call-back function, extra_func is our value
185 $replacer = $data['extra_func'];
187 // Do we have a call-back? Should always be there!
188 if (!empty($data['callback'])) {
189 //* DEBUG: */ if ($data['callback'] == 'getMemberId') die('<pre>'.encodeEntities(print_r($data, true)).'</pre>');
190 // If the value is empty, we don't add it
191 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'value[' . gettype($data['value']) . ']=' . $data['value']);
192 if ((empty($data['value'])) && ($data['value'] != '0')) {
194 $replacer = '{DQUOTE} . ' . $data['extra_func2'] . '(' . $data['extra_func'] . '(' . $data['callback'] . '())) . {DQUOTE}';
197 $replacer = '{DQUOTE} . ' . $data['extra_func2'] . '(' . $data['extra_func'] . '(' . $data['callback'] . "('" . $data['value'] . "'))) . {DQUOTE}";
201 // Replace the config entry
202 $code = replaceExpressionCode($data, $replacer);
204 // Return replaced code
208 // Expression call-back function for calling filters
209 function doExpressionFilter ($data) {
210 // Construct replacement
211 $replacer = "{DQUOTE} . runFilterChain('" . $data['matches'][4][$data['key']] . "') . {DQUOTE}";
213 // Run the filter and insert result
214 $code = replaceExpressionCode($data, $replacer);
216 // Return replaced code
220 // Expression call-back function for validator links
221 function doExpressionValidatorLinks ($data) {
222 // Default is nothing
225 // Get the code from data array for replacement/pipe-through
226 $code = $data['code'];
228 // Should we generally include validator links?
229 if ((isExtensionInstalled('validator')) && (getConfig('enable_validator') == 'Y') && (!in_array(getModule(), array('admin', 'login')))) {
230 // Load the validator template
231 $replacer = escapeQuotes(loadTemplate('validator_links', true));
235 $code = replaceExpressionCode($data, $replacer);
237 // Return the (maybe) replaced code
241 // Expression call-back for dynamic messages
242 function doExpressionMessage ($data) {
244 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'callback=' . $data['callback'] . ',extra_func=' . $data['extra_func'] . ',value=' . $data['value']);
246 // Message string replacement depends on if message is masked
247 if ((isMessageMasked($data['callback'])) && ((!empty($data['extra_func'])) || ($data['extra_func'] == '0'))) {
248 // Message should be masked
249 $replacer = "{DQUOTE} . getMaskedMessage('" . $data['callback'] . "', '" . $data['extra_func'] . "') . {DQUOTE}";
250 } elseif (!empty($data['value'])) {
251 // value is set, so it is masked message
252 $replacer = "{DQUOTE} . getMaskedMessage('" . $data['callback'] . "', '" . $data['value'] . "') . {DQUOTE}";
255 $replacer = "{DQUOTE} . getMessage('" . $data['callback'] . "') . {DQUOTE}";
259 $code = replaceExpressionCode($data, $replacer);
261 // Return the (maybe) replaced code
265 // Expression call-back for template functions
266 function doExpressionTemplate ($data) {
267 // Construct call-back function name
268 $callback = 'doTemplate' . $data['callback'];
271 $replacer = '<!-- [' . __FUNCTION__ . ':' . __LINE__.'] Call-back function ' . $callback . ' does not exist. //-->';
273 // Is the function there?
274 if (function_exists($callback)) {
275 // Do the replacement
276 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'template='.$GLOBALS['current_template']);
277 $replacer = '{DQUOTE} . ' . $callback . "('" . $GLOBALS['current_template'] . "', true";
280 if (!empty($data['value'])) {
281 // Then include it as well
282 $replacer .= ", '" . $data['value'] . "'";
286 $replacer .= ') . {DQUOTE}';
288 // Log missing function
289 logDebugMessage(__FUNCTION__, __LINE__, 'Call-back function ' . $callback . ' does not exist.');
293 $code = replaceExpressionCode($data, $replacer);
295 // Return the (maybe) replaced code
299 // Expression call-back for math functions
300 function doExpressionMath ($data) {
301 // Do the replacement
302 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'template='.$GLOBALS['current_template']);
303 $replacer = '{DQUOTE} . doCalculate' . $data['callback'] . '(' . $data['value'] . ') . {DQUOTE}';
306 $code = replaceExpressionCode($data, $replacer);
309 loadIncludeOnce('inc/math-functions.php');
311 // Return the (maybe) replaced code
315 // Expression call-back for GET request
316 function doExpressionGet ($data) {
317 // Construct the replacer
318 $replacer = '{%pipe,getRequestParameter=' . $data['value'] . '%}';
321 $code = replaceExpressionCode($data, $replacer);
323 // Return the (maybe) replaced code
327 // Expression call-back for POST request
328 function doExpressionPost ($data) {
329 // Construct the replacer
330 $replacer = '{%pipe,postRequestParameter=' . $data['value'] . '%}';
333 $code = replaceExpressionCode($data, $replacer);
335 // Return the (maybe) replaced code