2 /************************************************************************
3 * Mailer v0.2.1-FINAL Start: 04/12/2009 *
4 * =================== Last change: 04/12/2009 *
6 * -------------------------------------------------------------------- *
7 * File : expression-functions.php *
8 * -------------------------------------------------------------------- *
9 * Short description : Expression callback functions *
10 * -------------------------------------------------------------------- *
11 * Kurzbeschreibung : Expression-Callback-Funktionen *
12 * -------------------------------------------------------------------- *
15 * $Tag:: 0.2.1-FINAL $ *
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 *
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. *
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. *
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, *
38 ************************************************************************/
40 // Some security stuff...
41 if (!defined('__SECURITY')) {
45 // Private function to replace the code
46 function replaceExpressionCode ($data, $replacer) {
48 // @TODO is escapeQuotes() enougth for strings with single/double quotes?
49 return str_replace($data['matches'][0][$data['key']], $replacer, escapeQuotes($data['code']));
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']];
59 if (!isset($GLOBALS['expression_function_available'][$entry])) {
61 $functionName = 'get';
63 // Explode it in an array
64 foreach (explode('_', $entry) as $piece) {
65 // Add non-empty parts
68 $functionName .= ucfirst(strtolower($piece));
72 // Is that function there?
73 if (function_exists($functionName)) {
75 $GLOBALS['expression_function_name'][$entry] = $functionName;
76 $GLOBALS['expression_function_available'][$entry] = true;
79 logDebugMessage(__FUNCTION__, __LINE__, 'Expression function ' . $functionName . ' not found. Please consider adding it to improve execution speed.');
82 $GLOBALS['expression_function_available'][$entry] = false;
84 } elseif ($GLOBALS['expression_function_available'][$entry] == false) {
86 logDebugMessage(__FUNCTION__, __LINE__, 'Expression function for entry ' . $entry . ' requested but not found.');
90 return $GLOBALS['expression_function_available'][$entry];
93 // Getter for above expression function
94 function getExpressionFunction ($data) {
95 // Get the enty we need
96 $entry = $data['matches'][4][$data['key']];
99 return $GLOBALS['expression_function_name'][$entry];
102 // Expression call-back function for getCode() calls
103 function doExpressionCode ($data) {
105 $code = str_replace($data['matches'][0][$data['key']], "{DQUOTE} . getCode('" . $data['matches'][4][$data['key']] . "') . {DQUOTE}", $data['code']);
107 // Return replaced code
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;
116 // Handle an URL here
117 $replacer = "{DQUOTE} . encodeUrl('" . $data['matches'][4][$data['key']] . "', " . $data['mode'] . ') . {DQUOTE}';
120 $code = replaceExpressionCode($data, $replacer);
122 // Return replaced code
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'] . "')))";
132 $replacer = '{DQUOTE} . call_user_func(' . $functionName . ') . {DQUOTE}';
135 $code = replaceExpressionCode($data, $replacer);
137 // Return replaced code
141 // Expression call-back function for fetching user data
142 function doExpressionUser ($data) {
143 // Use current userid by default
144 $functionName = 'getMemberId()';
146 // User-related data, so is there a userid?
147 if (!empty($data['matches'][4][$data['key']])) {
148 // Do we have a userid or $userid?
149 if ($data['matches'][4][$data['key']] == '$userid') {
151 $functionName = "getFetchedUserData('userid', \$userid, '" . $data['callback'] . "')";
152 } elseif (!empty($data['matches'][4][$data['key']])) {
154 $functionName = "getFetchedUserData('userid', " . $data['matches'][4][$data['key']] . ", '" . $data['callback'] . "')";
156 } elseif ((!empty($data['callback'])) && (isUserDataValid())) {
157 // "Call-back" alias column for current logged in user's data
158 $functionName = "getUserData('" . $data['callback'] . "')";
161 // Do we have another function to run (e.g. translations)
162 if (!empty($data['extra_func'])) {
163 // Surround the original function call with it
164 $functionName = $data['extra_func'] . '(' . $functionName . ')';
168 $replacer = '{DQUOTE} . ' . $functionName . ' . {DQUOTE}';
170 // Now replace the code
171 $code = replaceExpressionCode($data, $replacer);
173 // Return replaced code
177 // Expression call-back function for getting extension data
178 function doExpressionExt ($data) {
179 // Not installed is default
182 // Is the extension installed?
183 if (isExtensionInstalled($data['matches'][4][$data['key']])) {
184 // Construct call-back function name
185 $functionName = 'getExtension' . ucfirst(strtolower($data['callback']));
187 // Construct call of the function
188 $replacer = "{DQUOTE} . call_user_func_array('" . $functionName . "', array('" . $data['matches'][4][$data['key']] . "', true)) . {DQUOTE}";
192 $replacer = sprintf("&ext=%s&ver=%s&rev={?CURR_SVN_REVISION?}", $data['matches'][4][$data['key']], $replacer);
194 // Replace it and insert parameter for GET request
195 $code = replaceExpressionCode($data, $replacer);
197 // Return replaced code
201 // Expression call-back function for getting configuration data
202 // @TODO FILTER_COMPILE_CONFIG does not handle call-back functions so we handle it here again
203 function doExpressionConfig ($data) {
204 // Do we have a special expression function for it?
205 if (isExpressionFunctionAvaiable($data)) {
207 $replacer = '{DQUOTE} . ' . $data['callback'] . '('.getExpressionFunction($data).'(' . "'" . $data['matches'][4][$data['key']] . "'" . ')) . {DQUOTE}';
209 // Default replacer is the config value itself
210 $replacer = '{DQUOTE} . ' . $data['callback'] . '(getConfig(' . "'" . $data['matches'][4][$data['key']] . "'" . ')) . {DQUOTE}';
213 // Replace the config entry
214 $code = replaceExpressionCode($data, $replacer);
216 // Return replaced code
220 // Expression call-back function for piping data through functions
221 function doExpressionPipe ($data) {
222 // We need callback and extra_func: callback is really the call-back function, extra_func is our value
223 $replacer = $data['extra_func'];
225 // Do we have a call-back? Should always be there!
226 if (!empty($data['callback'])) {
227 //if ($data['callback'] == 'getMemberId') die('<pre>'.encodeEntities(print_r($data, true)).'</pre>');
228 // If the value is empty, we don't add it
229 if (empty($data['value'])) {
231 $replacer = '{DQUOTE} . ' . $data['extra_func2'] . '(' . $data['extra_func'] . '(' . $data['callback'] . '())) . {DQUOTE}';
234 $replacer = '{DQUOTE} . ' . $data['extra_func2'] . '(' . $data['extra_func'] . '(' . $data['callback'] . "('" . $data['value'] . "'))) . {DQUOTE}";
238 // Replace the config entry
239 $code = replaceExpressionCode($data, $replacer);
241 // Return replaced code
245 // Expression call-back function for calling filters
246 function doExpressionFilter ($data) {
247 // Construct replacement
248 $replacer = "{DQUOTE} . runFilterChain('" . $data['matches'][4][$data['key']] . "') . {DQUOTE}";
250 // Run the filter and insert result
251 $code = replaceExpressionCode($data, $replacer);
253 // Return replaced code
257 // Expression call-back function for validator links
258 function doExpressionValidatorLinks ($data) {
259 // Default is nothing
262 // Get the code from data array for replacement/pipe-through
263 $code = $data['code'];
265 // Should we generally include validator links?
266 if ((isExtensionInstalled('validator')) && (getConfig('enable_validator') == 'Y') && (!in_array(getModule(), array('admin', 'login')))) {
267 // Load the validator template
268 $replacer = escapeQuotes(loadTemplate('validator_links', true));
272 $code = replaceExpressionCode($data, $replacer);
274 // Return the (maybe) replaced code
278 // Expression call-back for dynamic messages
279 function doExpressionMessage ($data) {
281 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'callback=' . $data['callback'] . ',extra_func=' . $data['extra_func'] . ',value=' . $data['value']);
283 // Message string replacement depends on if message is masked
284 if ((isMessageMasked($data['callback'])) && ((!empty($data['extra_func'])) || ($data['extra_func'] == '0'))) {
285 // Message should be masked
286 $replacer = "{DQUOTE} . getMaskedMessage('" . $data['callback'] . "', '" . $data['extra_func'] . "') . {DQUOTE}";
287 } elseif (!empty($data['value'])) {
288 // value is set, so it is masked message
289 $replacer = "{DQUOTE} . getMaskedMessage('" . $data['callback'] . "', '" . $data['value'] . "') . {DQUOTE}";
292 $replacer = "{DQUOTE} . getMessage('" . $data['callback'] . "') . {DQUOTE}";
296 $code = replaceExpressionCode($data, $replacer);
298 // Return the (maybe) replaced code
302 // Expression call-back for template functions
303 function doExpressionTemplate ($data) {
304 // Do the replacement
305 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'template='.$GLOBALS['current_template']);
306 $replacer = '{DQUOTE} . doTemplate' . $data['callback'] . "('" . $GLOBALS['current_template'] . "', true) . {DQUOTE}";
309 $code = replaceExpressionCode($data, $replacer);
311 // Return the (maybe) replaced code