Introduced new function capitalizeUnderscoreString(), rewritten 'do' parameter:
[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  * Needs to be in all Files and every File needs "svn propset           *
18  * svn:keywords Date Revision" (autoprobset!) at least!!!!!!            *
19  * -------------------------------------------------------------------- *
20  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
21  * Copyright (c) 2009, 2010 by Mailer Developer Team                    *
22  * For more information visit: http://www.mxchange.org                  *
23  *                                                                      *
24  * This program is free software; you can redistribute it and/or modify *
25  * it under the terms of the GNU General Public License as published by *
26  * the Free Software Foundation; either version 2 of the License, or    *
27  * (at your option) any later version.                                  *
28  *                                                                      *
29  * This program is distributed in the hope that it will be useful,      *
30  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
31  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
32  * GNU General Public License for more details.                         *
33  *                                                                      *
34  * You should have received a copy of the GNU General Public License    *
35  * along with this program; if not, write to the Free Software          *
36  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
37  * MA  02110-1301  USA                                                  *
38  ************************************************************************/
39
40 // Some security stuff...
41 if (!defined('__SECURITY')) {
42         die();
43 } // END - if
44
45 // Private function to replace the code
46 function replaceExpressionCode ($data, $replacer) {
47         // Replace the code
48         // @TODO is escapeQuotes() enougth for strings with single/double quotes?
49         return str_replace($data['matches'][0][$data['key']], $replacer, escapeQuotes($data['code']));
50 }
51
52 // Private function to determine wether we have a special expression function avaible
53 // (mostly located in wrapper-functions.php)
54 function isExpressionFunctionAvaiable ($data) {
55         // Get the enty we need
56         $entry = $data['matches'][4][$data['key']];
57
58         // Do we have cache?
59         if (!isset($GLOBALS['expression_function_available'][$entry])) {
60                 // Init function name
61                 $functionName = 'get';
62
63                 // Explode it in an array
64                 foreach (explode('_', $entry) as $piece) {
65                         // Add non-empty parts
66                         if (!empty($piece)) {
67                                 // Add it
68                                 $functionName .= capitalizeUnderscoreString($piece);
69                         } // END - if
70                 } // END - foreach
71
72                 // Is that function there?
73                 if (function_exists($functionName)) {
74                         // Cache it all
75                         $GLOBALS['expression_function_name'][$entry] = $functionName;
76                         $GLOBALS['expression_function_available'][$entry] = true;
77                 } else {
78                         // Not avaiable
79                         logDebugMessage(__FUNCTION__, __LINE__, 'Expression function ' . $functionName . ' not found. Please consider adding it to improve execution speed.');
80
81                         // And cache it
82                         $GLOBALS['expression_function_available'][$entry] = false;
83                 }
84         } elseif ($GLOBALS['expression_function_available'][$entry] == false) {
85                 // Debug message
86                 logDebugMessage(__FUNCTION__, __LINE__, 'Expression function for entry ' . $entry . ' requested but not found.');
87         }
88
89         // Return cache
90         return $GLOBALS['expression_function_available'][$entry];
91 }
92
93 // Getter for above expression function
94 function getExpressionFunction ($data) {
95         // Get the enty we need
96         $entry = $data['matches'][4][$data['key']];
97
98         // Return it
99         return $GLOBALS['expression_function_name'][$entry];
100 }
101
102 // Expression call-back function for getCode() calls
103 function doExpressionCode ($data) {
104         // Replace the code
105         $code = str_replace($data['matches'][0][$data['key']], "{DQUOTE} . getCode('" . $data['matches'][4][$data['key']] . "') . {DQUOTE}", $data['code']);
106
107         // Return replaced code
108         return $code;
109 }
110
111 // Expression call-back function for URLs
112 function doExpressionUrl ($data) {
113         // Do we have JS-mode?
114         if ($data['callback'] == 'js') $data['mode'] = 1;
115
116         // Handle an URL here
117         $replacer = "{DQUOTE} . encodeUrl('" . $data['matches'][4][$data['key']] . "', " . $data['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('ucfirst', 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={?CURR_SVN_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).'(' . "'" . $data['matches'][4][$data['key']] . "'" . ')) . {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                 //if ($data['callback'] == 'getMemberId') die('<pre>'.encodeEntities(print_r($data, true)).'</pre>');
192                 // If the value is empty, we don't add it
193                 if (empty($data['value'])) {
194                         // No value is set
195                         $replacer = '{DQUOTE} . ' . $data['extra_func2'] . '(' . $data['extra_func'] . '(' . $data['callback'] . '())) . {DQUOTE}';
196                 } else {
197                         // Some value is set
198                         $replacer = '{DQUOTE} . ' . $data['extra_func2'] . '(' . $data['extra_func'] . '(' . $data['callback'] . "('" . $data['value'] . "'))) . {DQUOTE}";
199                 }
200         } // END - if
201
202         // Replace the config entry
203         $code = replaceExpressionCode($data, $replacer);
204
205         // Return replaced code
206         return $code;
207 }
208
209 // Expression call-back function for calling filters
210 function doExpressionFilter ($data) {
211         // Construct replacement
212         $replacer = "{DQUOTE} . runFilterChain('" . $data['matches'][4][$data['key']] . "') . {DQUOTE}";
213
214         // Run the filter and insert result
215         $code = replaceExpressionCode($data, $replacer);
216
217         // Return replaced code
218         return $code;
219 }
220
221 // Expression call-back function for validator links
222 function doExpressionValidatorLinks ($data) {
223         // Default is nothing
224         $replacer = '';
225
226         // Get the code from data array for replacement/pipe-through
227         $code = $data['code'];
228
229         // Should we generally include validator links?
230         if ((isExtensionInstalled('validator')) && (getConfig('enable_validator') == 'Y') && (!in_array(getModule(), array('admin', 'login')))) {
231                 // Load the validator template
232                 $replacer = escapeQuotes(loadTemplate('validator_links', true));
233         } // END - if
234
235         // Replace the code
236         $code = replaceExpressionCode($data, $replacer);
237
238         // Return the (maybe) replaced code
239         return $code;
240 }
241
242 // Expression call-back for dynamic messages
243 function doExpressionMessage ($data) {
244         // Debug message
245         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'callback=' . $data['callback'] . ',extra_func=' . $data['extra_func'] . ',value=' . $data['value']);
246
247         // Message string replacement depends on if message is masked
248         if ((isMessageMasked($data['callback'])) && ((!empty($data['extra_func'])) || ($data['extra_func'] == '0'))) {
249                 // Message should be masked
250                 $replacer = "{DQUOTE} . getMaskedMessage('" . $data['callback'] . "', '" . $data['extra_func'] . "') . {DQUOTE}";
251         } elseif (!empty($data['value'])) {
252                 // value is set, so it is masked message
253                 $replacer = "{DQUOTE} . getMaskedMessage('" . $data['callback'] . "', '" . $data['value'] . "') . {DQUOTE}";
254         } else {
255                 // Regular message
256                 $replacer = "{DQUOTE} . getMessage('" . $data['callback'] . "') . {DQUOTE}";
257         }
258
259         // Replace the code
260         $code = replaceExpressionCode($data, $replacer);
261
262         // Return the (maybe) replaced code
263         return $code;
264 }
265
266 // Expression call-back for template functions
267 function doExpressionTemplate ($data) {
268         // Do the replacement
269         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'template='.$GLOBALS['current_template']);
270         $replacer = '{DQUOTE} . doTemplate' . $data['callback'] . "('" . $GLOBALS['current_template'] . "', true";
271
272         // Is 'value' set?
273         if (!empty($data['value'])) {
274                 // Then include it as well
275                 $replacer .= ", '" . $data['value'] . "'";
276         } // END - if
277
278         // Replacer is ready
279         $replacer .= ') . {DQUOTE}';
280
281         // Replace the code
282         $code = replaceExpressionCode($data, $replacer);
283
284         // Return the (maybe) replaced code
285         return $code;
286 }
287
288 // Expression call-back for math functions
289 function doExpressionMath ($data) {
290         // Do the replacement
291         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'template='.$GLOBALS['current_template']);
292         $replacer = '{DQUOTE} . doCalculate' . $data['callback'] . '(' . $data['value'] . ') . {DQUOTE}';
293
294         // Replace the code
295         $code = replaceExpressionCode($data, $replacer);
296
297         // Load include once
298         loadIncludeOnce('inc/math-functions.php');
299
300         // Return the (maybe) replaced code
301         return $code;
302 }
303
304 // [EOF]
305 ?>