Double question mark fixed
[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://www.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
54         $entry = $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') $data['mode'] = 1;
113
114         // Handle an URL here
115         $replacer = "{DQUOTE} . encodeUrl('" . $data['matches'][4][$data['key']] . "', " . $data['mode'] . ') . {DQUOTE}';
116
117         // Replace it
118         $code = replaceExpressionCode($data, $replacer);
119
120         // Return replaced code
121         return $code;
122 }
123
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'] . "')))";
128
129         // Generate replacer
130         $replacer = '{DQUOTE} . call_user_func(' . $functionName . ') . {DQUOTE}';
131
132         // Replace it
133         $code = replaceExpressionCode($data, $replacer);
134
135         // Return replaced code
136         return $code;
137 }
138
139 // Expression call-back function for getting extension data
140 function doExpressionExt ($data) {
141         // Not installed is default
142         $replacer = 'false';
143
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']);
148
149                 // Construct call of the function
150                 $replacer = "{DQUOTE} . call_user_func_array('" . $functionName . "', array('" . $data['matches'][4][$data['key']] . "', true)) . {DQUOTE}";
151         } // END - if
152
153         // Generate replacer
154         $replacer = sprintf("&amp;ext=%s&amp;ver=%s&amp;rev={?CURRENT_REPOSITORY_REVISION?}", $data['matches'][4][$data['key']], $replacer);
155
156         // Replace it and insert parameter for GET request
157         $code = replaceExpressionCode($data, $replacer);
158
159         // Return replaced code
160         return $code;
161 }
162
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)) {
168                 // Then use it
169                 $replacer = '{DQUOTE}  . ' . $data['callback'] . '(' . getExpressionFunction($data) . '()) . {DQUOTE}';
170         } else {
171                 // Default replacer is the config value itself
172                 $replacer = '{DQUOTE}  . ' . $data['callback'] . '(getConfig(' . "'" . $data['matches'][4][$data['key']] . "'" . ')) . {DQUOTE}';
173         }
174
175         // Replace the config entry
176         $code = replaceExpressionCode($data, $replacer);
177
178         // Return replaced code
179         return $code;
180 }
181
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'];
186
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')) {
193                         // No value is set
194                         $replacer = '{DQUOTE} . ' . $data['extra_func2'] . '(' . $data['extra_func'] . '(' . $data['callback'] . '())) . {DQUOTE}';
195                 } else {
196                         // Some value is set
197                         $replacer = '{DQUOTE} . ' . $data['extra_func2'] . '(' . $data['extra_func'] . '(' . $data['callback'] . "('" . $data['value'] . "'))) . {DQUOTE}";
198                 }
199         } // END - if
200
201         // Replace the config entry
202         $code = replaceExpressionCode($data, $replacer);
203
204         // Return replaced code
205         return $code;
206 }
207
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}";
212
213         // Run the filter and insert result
214         $code = replaceExpressionCode($data, $replacer);
215
216         // Return replaced code
217         return $code;
218 }
219
220 // Expression call-back function for validator links
221 function doExpressionValidatorLinks ($data) {
222         // Default is nothing
223         $replacer = '';
224
225         // Get the code from data array for replacement/pipe-through
226         $code = $data['code'];
227
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));
232         } // END - if
233
234         // Replace the code
235         $code = replaceExpressionCode($data, $replacer);
236
237         // Return the (maybe) replaced code
238         return $code;
239 }
240
241 // Expression call-back for dynamic messages
242 function doExpressionMessage ($data) {
243         // Debug message
244         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'callback=' . $data['callback'] . ',extra_func=' . $data['extra_func'] . ',value=' . $data['value']);
245
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}";
253         } else {
254                 // Regular message
255                 $replacer = "{DQUOTE} . getMessage('" . $data['callback'] . "') . {DQUOTE}";
256         }
257
258         // Replace the code
259         $code = replaceExpressionCode($data, $replacer);
260
261         // Return the (maybe) replaced code
262         return $code;
263 }
264
265 // Expression call-back for template functions
266 function doExpressionTemplate ($data) {
267         // Construct call-back function name
268         $callback = 'doTemplate' . $data['callback'];
269
270         // Init replacer
271         $replacer = '<!-- [' . __FUNCTION__ . ':' . __LINE__.'] Call-back function ' . $callback  . ' does not exist. //-->';
272
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";
278
279                 // Is 'value' set?
280                 if (!empty($data['value'])) {
281                         // Then include it as well
282                         $replacer .= ", '" . $data['value'] . "'";
283                 } // END - if
284
285                 // Replacer is ready
286                 $replacer .= ') . {DQUOTE}';
287         } else {
288                 // Log missing function
289                 logDebugMessage(__FUNCTION__, __LINE__, 'Call-back function ' . $callback . ' does not exist.');
290         }
291
292         // Replace the code
293         $code = replaceExpressionCode($data, $replacer);
294
295         // Return the (maybe) replaced code
296         return $code;
297 }
298
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}';
304
305         // Replace the code
306         $code = replaceExpressionCode($data, $replacer);
307
308         // Load include once
309         loadIncludeOnce('inc/math-functions.php');
310
311         // Return the (maybe) replaced code
312         return $code;
313 }
314
315 // Expression call-back for GET request
316 function doExpressionGet ($data) {
317         // Construct the replacer
318         $replacer = '{%pipe,getRequestParameter=' . $data['value'] . '%}';
319
320         // Replace the code
321         $code = replaceExpressionCode($data, $replacer);
322
323         // Return the (maybe) replaced code
324         return $code;
325 }
326
327 // Expression call-back for POST request
328 function doExpressionPost ($data) {
329         // Construct the replacer
330         $replacer = '{%pipe,postRequestParameter=' . $data['value'] . '%}';
331
332         // Replace the code
333         $code = replaceExpressionCode($data, $replacer);
334
335         // Return the (maybe) replaced code
336         return $code;
337 }
338
339 // [EOF]
340 ?>