Removed comment introduced by Profi-Concept, this comment should fine (in a much...
[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, 2010 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('ucfirst', 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={?CURR_SVN_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                 //if ($data['callback'] == 'getMemberId') die('<pre>'.encodeEntities(print_r($data, true)).'</pre>');
190                 // If the value is empty, we don't add it
191                 if (empty($data['value'])) {
192                         // No value is set
193                         $replacer = '{DQUOTE} . ' . $data['extra_func2'] . '(' . $data['extra_func'] . '(' . $data['callback'] . '())) . {DQUOTE}';
194                 } else {
195                         // Some value is set
196                         $replacer = '{DQUOTE} . ' . $data['extra_func2'] . '(' . $data['extra_func'] . '(' . $data['callback'] . "('" . $data['value'] . "'))) . {DQUOTE}";
197                 }
198         } // END - if
199
200         // Replace the config entry
201         $code = replaceExpressionCode($data, $replacer);
202
203         // Return replaced code
204         return $code;
205 }
206
207 // Expression call-back function for calling filters
208 function doExpressionFilter ($data) {
209         // Construct replacement
210         $replacer = "{DQUOTE} . runFilterChain('" . $data['matches'][4][$data['key']] . "') . {DQUOTE}";
211
212         // Run the filter and insert result
213         $code = replaceExpressionCode($data, $replacer);
214
215         // Return replaced code
216         return $code;
217 }
218
219 // Expression call-back function for validator links
220 function doExpressionValidatorLinks ($data) {
221         // Default is nothing
222         $replacer = '';
223
224         // Get the code from data array for replacement/pipe-through
225         $code = $data['code'];
226
227         // Should we generally include validator links?
228         if ((isExtensionInstalled('validator')) && (getConfig('enable_validator') == 'Y') && (!in_array(getModule(), array('admin', 'login')))) {
229                 // Load the validator template
230                 $replacer = escapeQuotes(loadTemplate('validator_links', true));
231         } // END - if
232
233         // Replace the code
234         $code = replaceExpressionCode($data, $replacer);
235
236         // Return the (maybe) replaced code
237         return $code;
238 }
239
240 // Expression call-back for dynamic messages
241 function doExpressionMessage ($data) {
242         // Debug message
243         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'callback=' . $data['callback'] . ',extra_func=' . $data['extra_func'] . ',value=' . $data['value']);
244
245         // Message string replacement depends on if message is masked
246         if ((isMessageMasked($data['callback'])) && ((!empty($data['extra_func'])) || ($data['extra_func'] == '0'))) {
247                 // Message should be masked
248                 $replacer = "{DQUOTE} . getMaskedMessage('" . $data['callback'] . "', '" . $data['extra_func'] . "') . {DQUOTE}";
249         } elseif (!empty($data['value'])) {
250                 // value is set, so it is masked message
251                 $replacer = "{DQUOTE} . getMaskedMessage('" . $data['callback'] . "', '" . $data['value'] . "') . {DQUOTE}";
252         } else {
253                 // Regular message
254                 $replacer = "{DQUOTE} . getMessage('" . $data['callback'] . "') . {DQUOTE}";
255         }
256
257         // Replace the code
258         $code = replaceExpressionCode($data, $replacer);
259
260         // Return the (maybe) replaced code
261         return $code;
262 }
263
264 // Expression call-back for template functions
265 function doExpressionTemplate ($data) {
266         // Do the replacement
267         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'template='.$GLOBALS['current_template']);
268         $replacer = '{DQUOTE} . doTemplate' . $data['callback'] . "('" . $GLOBALS['current_template'] . "', true";
269
270         // Is 'value' set?
271         if (!empty($data['value'])) {
272                 // Then include it as well
273                 $replacer .= ", '" . $data['value'] . "'";
274         } // END - if
275
276         // Replacer is ready
277         $replacer .= ') . {DQUOTE}';
278
279         // Replace the code
280         $code = replaceExpressionCode($data, $replacer);
281
282         // Return the (maybe) replaced code
283         return $code;
284 }
285
286 // Expression call-back for math functions
287 function doExpressionMath ($data) {
288         // Do the replacement
289         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'template='.$GLOBALS['current_template']);
290         $replacer = '{DQUOTE} . doCalculate' . $data['callback'] . '(' . $data['value'] . ') . {DQUOTE}';
291
292         // Replace the code
293         $code = replaceExpressionCode($data, $replacer);
294
295         // Load include once
296         loadIncludeOnce('inc/math-functions.php');
297
298         // Return the (maybe) replaced code
299         return $code;
300 }
301
302 // Expression call-back for GET request
303 function doExpressionGet ($data) {
304         // Construct the replacer
305         $replacer = '{%pipe,getRequestParameter=' . $data['value'] . '%}';
306
307         // Replace the code
308         $code = replaceExpressionCode($data, $replacer);
309
310         // Return the (maybe) replaced code
311         return $code;
312 }
313
314 // Expression call-back for POST request
315 function doExpressionPost ($data) {
316         // Construct the replacer
317         $replacer = '{%pipe,postRequestParameter=' . $data['value'] . '%}';
318
319         // Replace the code
320         $code = replaceExpressionCode($data, $replacer);
321
322         // Return the (maybe) replaced code
323         return $code;
324 }
325
326 // [EOF]
327 ?>