4e7c7f264ad73cc89b2786e9051a9b28cd4a53a4
[mailer.git] / inc / expression-functions.php
1 <?php
2 /************************************************************************
3  * Mailer v0.2.1-FINAL                                Start: 04/12/2009 *
4  * ===================                          Last change: 04/12/2009 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : expression-functions.php                         *
8  * -------------------------------------------------------------------- *
9  * Short description : Expression callback functions                    *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Expression-Callback-Funktionen                   *
12  * -------------------------------------------------------------------- *
13  * $Revision::                                                        $ *
14  * $Date::                                                            $ *
15  * $Tag:: 0.2.1-FINAL                                                 $ *
16  * $Author::                                                          $ *
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                      *
21  *                                                                      *
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.                                  *
26  *                                                                      *
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.                         *
31  *                                                                      *
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,               *
35  * MA  02110-1301  USA                                                  *
36  ************************************************************************/
37
38 // Some security stuff...
39 if (!defined('__SECURITY')) {
40         die();
41 } // END - if
42
43 // Private function to replace the code
44 function replaceExpressionCode ($data, $replacer) {
45         // Replace the code
46         // @TODO is escapeQuotes() enougth for strings with single/double quotes?
47         return str_replace($data['matches'][0][$data['key']], $replacer, escapeQuotes($data['code']));
48 }
49
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']]);
55
56         // Is there cache?
57         if (!isset($GLOBALS['expression_function_available'][$entry])) {
58                 // Init function name
59                 $functionName = 'get';
60
61                 // Explode it in an array
62                 foreach (explode('_', $entry) as $piece) {
63                         // Add non-empty parts
64                         if (!empty($piece)) {
65                                 // Add it
66                                 $functionName .= capitalizeUnderscoreString($piece);
67                         } // END - if
68                 } // END - foreach
69
70                 // Is that function there?
71                 if (function_exists($functionName)) {
72                         // Cache it all
73                         $GLOBALS['expression_function_name'][$entry]      = $functionName;
74                         $GLOBALS['expression_function_available'][$entry] = TRUE;
75                 } else {
76                         // Not avaiable
77                         logDebugMessage(__FUNCTION__, __LINE__, 'Expression function ' . $functionName . ' not found. Please consider adding it to improve execution speed.');
78
79                         // And cache it
80                         $GLOBALS['expression_function_available'][$entry] = FALSE;
81                 }
82         } elseif ($GLOBALS['expression_function_available'][$entry] == FALSE) {
83                 // Debug message
84                 logDebugMessage(__FUNCTION__, __LINE__, 'Expression function for entry ' . $entry . ' requested but does not exist.');
85         }
86
87         // Return cache
88         return $GLOBALS['expression_function_available'][$entry];
89 }
90
91 // Getter for above expression function
92 function getExpressionFunction ($data) {
93         // Get the enty we need
94         $entry = $data['matches'][4][$data['key']];
95
96         // Return it
97         return $GLOBALS['expression_function_name'][$entry];
98 }
99
100 // Expression call-back function for getCode() calls
101 function doExpressionCode ($data) {
102         // Replace the code
103         $code = str_replace($data['matches'][0][$data['key']], "{DQUOTE} . getCode('" . $data['matches'][4][$data['key']] . "') . {DQUOTE}", $data['code']);
104
105         // Return replaced code
106         return $code;
107 }
108
109 // Expression call-back function for URLs
110 function doExpressionUrl ($data) {
111         // Is there JS-mode?
112         if ($data['callback'] == 'js') {
113                 // Switch to it
114                 $data['output_mode'] = '1';
115         } // END - if
116
117         // Handle an URL here
118         $replacer = "{DQUOTE} . encodeUrl('" . $data['matches'][4][$data['key']] . "', " . $data['output_mode'] . ') . {DQUOTE}';
119
120         // Debug log
121         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'replacer=' . $replacer . ',callback=' . $data['callback']);
122
123         // Replace it
124         $code = replaceExpressionCode($data, $replacer);
125
126         // Return replaced code
127         return $code;
128 }
129
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'] . "')))";
134
135         // Generate replacer
136         $replacer = '{DQUOTE} . call_user_func(' . $functionName . ') . {DQUOTE}';
137
138         // Replace it
139         $code = replaceExpressionCode($data, $replacer);
140
141         // Return replaced code
142         return $code;
143 }
144
145 // Expression call-back function for getting extension data
146 function doExpressionExt ($data) {
147         // Not installed is default
148         $replacer = 'false';
149
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']);
154
155                 // Construct call of the function
156                 $replacer = "{DQUOTE} . call_user_func_array('" . $functionName . "', array('" . $data['matches'][4][$data['key']] . "', TRUE)) . {DQUOTE}";
157         } // END - if
158
159         // Generate replacer
160         $replacer = sprintf("&amp;ext=%s&amp;ver=%s&amp;rev={?CURRENT_REPOSITORY_REVISION?}", $data['matches'][4][$data['key']], $replacer);
161
162         // Replace it and insert parameter for GET request
163         $code = replaceExpressionCode($data, $replacer);
164
165         // Return replaced code
166         return $code;
167 }
168
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         // Is there a special expression function for it?
173         if (isExpressionFunctionAvaiable($data)) {
174                 // Then use it
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}';
179         } else {
180                 // Default replacer is the config value itself
181                 $replacer = '{DQUOTE}  . ' . $data['callback'] . '(getConfig(' . chr(39) . $data['matches'][4][$data['key']] . chr(39) . ')) . {DQUOTE}';
182         }
183
184         // Replace the config entry
185         $code = replaceExpressionCode($data, $replacer);
186
187         // Return replaced code
188         return $code;
189 }
190
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'];
195
196         // Is there 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')) {
202                         // No value is set
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}';
207                 } else {
208                         // Some string/integer value is set
209                         $replacer = '{DQUOTE} . ' . $data['extra_func2'] . '(' . $data['extra_func'] . '(' . $data['callback'] . "('" . $data['value'] . "'))) . {DQUOTE}";
210                 }
211         } // END - if
212
213         // Replace the config entry
214         //* NOISY-DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'replacer=' . $replacer);
215         $code = replaceExpressionCode($data, $replacer);
216
217         // Return replaced code
218         return $code;
219 }
220
221 // Expression call-back function for calling filters
222 function doExpressionFilter ($data) {
223         // Construct replacement
224         $replacer = "{DQUOTE} . runFilterChain('" . $data['matches'][4][$data['key']] . "') . {DQUOTE}";
225
226         // Run the filter and insert result
227         $code = replaceExpressionCode($data, $replacer);
228
229         // Return replaced code
230         return $code;
231 }
232
233 // Expression call-back function for validator links
234 function doExpressionValidatorLinks ($data) {
235         // Default is nothing
236         $replacer = '';
237
238         // Get the code from data array for replacement/pipe-through
239         $code = $data['code'];
240
241         // Should we generally include validator links?
242         if ((isExtensionInstalled('validator')) && (getConfig('enable_validator') == 'Y') && (!in_array(getModule(), array('admin', 'login')))) {
243                 // Load the validator template
244                 $replacer = escapeQuotes(loadTemplate('validator_links', TRUE));
245         } // END - if
246
247         // Replace the code
248         $code = replaceExpressionCode($data, $replacer);
249
250         // Return the (maybe) replaced code
251         return $code;
252 }
253
254 // Expression call-back for dynamic messages
255 function doExpressionMessage ($data) {
256         // Debug message
257         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'callback=' . $data['callback'] . ',extra_func=' . $data['extra_func'] . ',value=' . $data['value']);
258
259         // Message string replacement depends on if message is masked
260         if ((isMessageMasked($data['callback'], FALSE)) && ((!empty($data['extra_func'])) || ($data['extra_func'] == '0'))) {
261                 // Message should be masked
262                 $replacer = "{DQUOTE} . getMaskedMessage('" . $data['callback'] . "', '" . $data['extra_func'] . "') . {DQUOTE}";
263         } elseif (!empty($data['value'])) {
264                 // value is set, so it is masked message
265                 $replacer = "{DQUOTE} . getMaskedMessage('" . $data['callback'] . "', '" . $data['value'] . "') . {DQUOTE}";
266         } else {
267                 // Regular message
268                 $replacer = "{DQUOTE} . getMessage('" . $data['callback'] . "') . {DQUOTE}";
269         }
270
271         // Replace the code
272         $code = replaceExpressionCode($data, $replacer);
273
274         // Return the (maybe) replaced code
275         return $code;
276 }
277
278 // Expression call-back for template functions
279 function doExpressionTemplate ($data) {
280         // Construct call-back function name
281         $callbackFunction = 'doTemplate' . $data['callback'];
282
283         // Init replacer
284         $replacer = '<!-- [' . __FUNCTION__ . ':' . __LINE__.'] Call-back function ' . $callbackFunction  . ' does not exist. //-->';
285
286         // Is the function there?
287         if (!isset($GLOBALS['current_template'])) {
288                 // This is very bad and needs fixing
289                 reportBug(__FUNCTION__, __LINE__, 'current_template in GLOBALS not set, callbackFunction=' . $callbackFunction . ',function_exists()=' . intval(function_exists($callbackFunction)));
290         } elseif (!function_exists($callbackFunction)) {
291                 // Log missing function only in debug mode
292                 if (isDebugModeEnabled()) {
293                         // Log it here
294                         logDebugMessage(__FUNCTION__, __LINE__, 'Call-back function ' . $callbackFunction . ' does not exist.');
295                 } // END - if
296         } else {
297                 // Do the replacement
298                 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'template='.$GLOBALS['current_template']);
299                 $replacer = '{DQUOTE} . ' . $callbackFunction . '(' . chr(39) . $GLOBALS['current_template'] . chr(39) . ', TRUE';
300
301                 // Is 'value' set?
302                 if (!empty($data['value'])) {
303                         // Then include it as well
304                         $replacer .= ', ' . chr(39) . $data['value'] . chr(39);
305                 } // END - if
306
307                 // Replacer is ready
308                 $replacer .= ') . {DQUOTE}';
309         }
310
311         // Replace the code
312         $code = replaceExpressionCode($data, $replacer);
313
314         // Return the (maybe) replaced code
315         return $code;
316 }
317
318 // Expression call-back for math functions
319 function doExpressionMath ($data) {
320         // Do the replacement
321         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'template='.$GLOBALS['current_template']);
322         $replacer = '{DQUOTE} . doCalculate' . $data['callback'] . '(' . $data['value'] . ') . {DQUOTE}';
323
324         // Replace the code
325         $code = replaceExpressionCode($data, $replacer);
326
327         // Load include once
328         loadIncludeOnce('inc/math-functions.php');
329
330         // Return the (maybe) replaced code
331         return $code;
332 }
333
334 // Expression call-back for GET request
335 function doExpressionGet ($data) {
336         // Construct the replacer:
337         // - GET request element
338         $replacer = '{%pipe,getRequestElement';
339
340         // Add more call-back functions?
341         if (!empty($data['callback'])) {
342                 // - Okay, add them
343                 $replacer .= ',' . $data['callback'];
344         } // END - if
345
346         // - Finalize replacer
347         $replacer .= '=' . $data['value'] . '%}';
348
349         // Replace the code
350         $code = replaceExpressionCode($data, $replacer);
351
352         // Return the (maybe) replaced code
353         return $code;
354 }
355
356 // Expression call-back for POST request
357 function doExpressionPost ($data) {
358         // Construct the replacer:
359         // - POST request element
360         $replacer = '{%pipe,postRequestElement';
361
362         // Add more call-back functions?
363         if (!empty($data['callback'])) {
364                 // - Okay, add them
365                 $replacer .= ',' . $data['callback'];
366         } // END - if
367
368         // - Finalize replacer
369         $replacer .= '=' . $data['value'] . '%}';
370
371         // Replace the code
372         $code = replaceExpressionCode($data, $replacer);
373
374         // Return the (maybe) replaced code
375         return $code;
376 }
377
378 // Expression call-back for session data
379 function doExpressionSession ($data) {
380         // Construct the replacer:
381         // - Session element
382         $replacer = '{%pipe,getSession';
383
384         // Add more call-back functions?
385         if (!empty($data['callback'])) {
386                 // - Okay, add them
387                 $replacer .= ',' . $data['callback'];
388         } // END - if
389
390         // - Finalize replacer
391         $replacer .= '=' . $data['value'] . '%}';
392
393         // Debug message
394         //* NOISY-DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'value=' . $data['value'] . ',replacer=' . $replacer);
395
396         // Replace the code
397         $code = replaceExpressionCode($data, $replacer);
398
399         // Return the (maybe) replaced code
400         return $code;
401 }
402
403 /**
404  * Expression call-back for session piplining, this means:
405  *
406  * 1) Read session data
407  * 2) Wrap the raw data into {%pipe,fooFunction=$rawValue%}
408  */
409 function doExpressionSessionPipe ($data) {
410         // Get the session data
411         $rawValue = getSession($data['value']);
412
413         // Construct the replacer
414         $replacer = '{%pipe,' . $data['callback'] . '=' . $rawValue . '%}';
415
416         // Debug message
417         //* NOISY-DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'value=' . $data['value'] . ',rawValue=' . $rawValue . ',replacer=' . $replacer);
418
419         // Replace the code
420         $code = replaceExpressionCode($data, $replacer);
421
422         // Return the (maybe) replaced code
423         return $code;
424 }
425
426 // [EOF]
427 ?>