First batch of fixed language ids (renamed, see ticket #219)
[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 - 2011 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 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']]);
55
56         // Do we have 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 not found.');
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         // Do we have JS-mode?
112         if ($data['callback'] == 'js') {
113                 $data['output_mode'] = 1;
114         } // END - if
115
116         // Handle an URL here
117         $replacer = "{DQUOTE} . encodeUrl('" . $data['matches'][4][$data['key']] . "', " . $data['output_mode'] . ') . {DQUOTE}';
118
119         // Replace it
120         $code = replaceExpressionCode($data, $replacer);
121
122         // Return replaced code
123         return $code;
124 }
125
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'] . "')))";
130
131         // Generate replacer
132         $replacer = '{DQUOTE} . call_user_func(' . $functionName . ') . {DQUOTE}';
133
134         // Replace it
135         $code = replaceExpressionCode($data, $replacer);
136
137         // Return replaced code
138         return $code;
139 }
140
141 // Expression call-back function for getting extension data
142 function doExpressionExt ($data) {
143         // Not installed is default
144         $replacer = 'false';
145
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']);
150
151                 // Construct call of the function
152                 $replacer = "{DQUOTE} . call_user_func_array('" . $functionName . "', array('" . $data['matches'][4][$data['key']] . "', true)) . {DQUOTE}";
153         } // END - if
154
155         // Generate replacer
156         $replacer = sprintf("&amp;ext=%s&amp;ver=%s&amp;rev={?CURRENT_REPOSITORY_REVISION?}", $data['matches'][4][$data['key']], $replacer);
157
158         // Replace it and insert parameter for GET request
159         $code = replaceExpressionCode($data, $replacer);
160
161         // Return replaced code
162         return $code;
163 }
164
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)) {
170                 // Then use it
171                 $replacer = '{DQUOTE}  . ' . $data['callback'] . '(' . getExpressionFunction($data) . '()) . {DQUOTE}';
172         } else {
173                 // Default replacer is the config value itself
174                 $replacer = '{DQUOTE}  . ' . $data['callback'] . '(getConfig(' . "'" . $data['matches'][4][$data['key']] . "'" . ')) . {DQUOTE}';
175         }
176
177         // Replace the config entry
178         $code = replaceExpressionCode($data, $replacer);
179
180         // Return replaced code
181         return $code;
182 }
183
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'];
188
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')) {
195                         // No value is set
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}';
200                 } else {
201                         // Some string/integer value is set
202                         $replacer = '{DQUOTE} . ' . $data['extra_func2'] . '(' . $data['extra_func'] . '(' . $data['callback'] . "('" . $data['value'] . "'))) . {DQUOTE}";
203                 }
204         } // END - if
205
206         // Replace the config entry
207         $code = replaceExpressionCode($data, $replacer);
208
209         // Return replaced code
210         return $code;
211 }
212
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}";
217
218         // Run the filter and insert result
219         $code = replaceExpressionCode($data, $replacer);
220
221         // Return replaced code
222         return $code;
223 }
224
225 // Expression call-back function for validator links
226 function doExpressionValidatorLinks ($data) {
227         // Default is nothing
228         $replacer = '';
229
230         // Get the code from data array for replacement/pipe-through
231         $code = $data['code'];
232
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));
237         } // END - if
238
239         // Replace the code
240         $code = replaceExpressionCode($data, $replacer);
241
242         // Return the (maybe) replaced code
243         return $code;
244 }
245
246 // Expression call-back for dynamic messages
247 function doExpressionMessage ($data) {
248         // Debug message
249         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'callback=' . $data['callback'] . ',extra_func=' . $data['extra_func'] . ',value=' . $data['value']);
250
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}";
258         } else {
259                 // Regular message
260                 $replacer = "{DQUOTE} . getMessage('" . $data['callback'] . "') . {DQUOTE}";
261         }
262
263         // Replace the code
264         $code = replaceExpressionCode($data, $replacer);
265
266         // Return the (maybe) replaced code
267         return $code;
268 }
269
270 // Expression call-back for template functions
271 function doExpressionTemplate ($data) {
272         // Construct call-back function name
273         $callback = 'doTemplate' . $data['callback'];
274
275         // Init replacer
276         $replacer = '<!-- [' . __FUNCTION__ . ':' . __LINE__.'] Call-back function ' . $callback  . ' does not exist. //-->';
277
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";
283
284                 // Is 'value' set?
285                 if (!empty($data['value'])) {
286                         // Then include it as well
287                         $replacer .= ", '" . $data['value'] . "'";
288                 } // END - if
289
290                 // Replacer is ready
291                 $replacer .= ') . {DQUOTE}';
292         } else {
293                 // Log missing function
294                 logDebugMessage(__FUNCTION__, __LINE__, 'Call-back function ' . $callback . ' does not exist.');
295         }
296
297         // Replace the code
298         $code = replaceExpressionCode($data, $replacer);
299
300         // Return the (maybe) replaced code
301         return $code;
302 }
303
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}';
309
310         // Replace the code
311         $code = replaceExpressionCode($data, $replacer);
312
313         // Load include once
314         loadIncludeOnce('inc/math-functions.php');
315
316         // Return the (maybe) replaced code
317         return $code;
318 }
319
320 // Expression call-back for GET request
321 function doExpressionGet ($data) {
322         // Construct the replacer
323         $replacer = '{%pipe,getRequestElement=' . $data['value'] . '%}';
324
325         // Replace the code
326         $code = replaceExpressionCode($data, $replacer);
327
328         // Return the (maybe) replaced code
329         return $code;
330 }
331
332 // Expression call-back for POST request
333 function doExpressionPost ($data) {
334         // Construct the replacer
335         $replacer = '{%pipe,postRequestElement=' . $data['value'] . '%}';
336
337         // Replace the code
338         $code = replaceExpressionCode($data, $replacer);
339
340         // Return the (maybe) replaced code
341         return $code;
342 }
343
344 // [EOF]
345 ?>