Rewrote 'we' word a little, rewrote mail order to use SQL_INSERTID() instead of anoth...
[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         $code = replaceExpressionCode($data, $replacer);
215
216         // Return replaced code
217         return $code;
218 }
219
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}";
224
225         // Run the filter and insert result
226         $code = replaceExpressionCode($data, $replacer);
227
228         // Return replaced code
229         return $code;
230 }
231
232 // Expression call-back function for validator links
233 function doExpressionValidatorLinks ($data) {
234         // Default is nothing
235         $replacer = '';
236
237         // Get the code from data array for replacement/pipe-through
238         $code = $data['code'];
239
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));
244         } // END - if
245
246         // Replace the code
247         $code = replaceExpressionCode($data, $replacer);
248
249         // Return the (maybe) replaced code
250         return $code;
251 }
252
253 // Expression call-back for dynamic messages
254 function doExpressionMessage ($data) {
255         // Debug message
256         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'callback=' . $data['callback'] . ',extra_func=' . $data['extra_func'] . ',value=' . $data['value']);
257
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}";
265         } else {
266                 // Regular message
267                 $replacer = "{DQUOTE} . getMessage('" . $data['callback'] . "') . {DQUOTE}";
268         }
269
270         // Replace the code
271         $code = replaceExpressionCode($data, $replacer);
272
273         // Return the (maybe) replaced code
274         return $code;
275 }
276
277 // Expression call-back for template functions
278 function doExpressionTemplate ($data) {
279         // Construct call-back function name
280         $callbackFunction = 'doTemplate' . $data['callback'];
281
282         // Init replacer
283         $replacer = '<!-- [' . __FUNCTION__ . ':' . __LINE__.'] Call-back function ' . $callbackFunction  . ' does not exist. //-->';
284
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 only in debug mode
291                 if (isDebugModeEnabled()) {
292                         // Log it here
293                         logDebugMessage(__FUNCTION__, __LINE__, 'Call-back function ' . $callbackFunction . ' does not exist.');
294                 } // END - if
295         } else {
296                 // Do the replacement
297                 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'template='.$GLOBALS['current_template']);
298                 $replacer = '{DQUOTE} . ' . $callbackFunction . '(' . chr(39) . $GLOBALS['current_template'] . chr(39) . ', true';
299
300                 // Is 'value' set?
301                 if (!empty($data['value'])) {
302                         // Then include it as well
303                         $replacer .= ', ' . chr(39) . $data['value'] . chr(39);
304                 } // END - if
305
306                 // Replacer is ready
307                 $replacer .= ') . {DQUOTE}';
308         }
309
310         // Replace the code
311         $code = replaceExpressionCode($data, $replacer);
312
313         // Return the (maybe) replaced code
314         return $code;
315 }
316
317 // Expression call-back for math functions
318 function doExpressionMath ($data) {
319         // Do the replacement
320         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'template='.$GLOBALS['current_template']);
321         $replacer = '{DQUOTE} . doCalculate' . $data['callback'] . '(' . $data['value'] . ') . {DQUOTE}';
322
323         // Replace the code
324         $code = replaceExpressionCode($data, $replacer);
325
326         // Load include once
327         loadIncludeOnce('inc/math-functions.php');
328
329         // Return the (maybe) replaced code
330         return $code;
331 }
332
333 // Expression call-back for GET request
334 function doExpressionGet ($data) {
335         // Construct the replacer
336         $replacer = '{%pipe,getRequestElement=' . $data['value'] . '%}';
337
338         // Replace the code
339         $code = replaceExpressionCode($data, $replacer);
340
341         // Return the (maybe) replaced code
342         return $code;
343 }
344
345 // Expression call-back for POST request
346 function doExpressionPost ($data) {
347         // Construct the replacer
348         $replacer = '{%pipe,postRequestElement=' . $data['value'] . '%}';
349
350         // Replace the code
351         $code = replaceExpressionCode($data, $replacer);
352
353         // Return the (maybe) replaced code
354         return $code;
355 }
356
357 // Expression call-back for session data
358 function doExpressionSession ($data) {
359         // Construct the replacer
360         $replacer = '{%pipe,getSession=' . $data['value'] . '%}';
361
362         // Debug message
363         //* NOISY-DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'value=' . $data['value'] . ',replacer=' . $replacer);
364
365         // Replace the code
366         $code = replaceExpressionCode($data, $replacer);
367
368         // Return the (maybe) replaced code
369         return $code;
370 }
371
372 /**
373  * Expression call-back for session piplining, this means:
374  *
375  * 1) Read session data
376  * 2) Wrap the raw data into {%pipe,fooFunction=$rawValue%}
377  */
378 function doExpressionSessionPipe ($data) {
379         // Get the session data
380         $rawValue = getSession($data['value']);
381
382         // Construct the replacer
383         $replacer = '{%pipe,' . $data['callback'] . '=' . $rawValue . '%}';
384
385         // Debug message
386         //* NOISY-DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'value=' . $data['value'] . ',rawValue=' . $rawValue . ',replacer=' . $replacer);
387
388         // Replace the code
389         $code = replaceExpressionCode($data, $replacer);
390
391         // Return the (maybe) replaced code
392         return $code;
393 }
394
395 // [EOF]
396 ?>