A lot forced-space characters ( ) removed, naming convention applied also in...
[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 // Expression call-back function for getCode() calls
53 function doExpressionCode ($data) {
54         // Replace the code
55         $code = str_replace($data['matches'][0][$data['key']], "{DQUOTE} . getCode('" . $data['matches'][4][$data['key']] . "') . {DQUOTE}", $data['code']);
56
57         // Return replaced code
58         return $code;
59 }
60
61 // Expression call-back function for URLs
62 function doExpressionUrl ($data) {
63         // Do we have JS-mode?
64         if ($data['callback'] == 'js') $data['mode'] = 1;
65
66         // Handle an URL here
67         $replacer = "{DQUOTE} . encodeUrl('" . $data['matches'][4][$data['key']] . "', " . $data['mode'] . ') . {DQUOTE}';
68
69         // Replace it
70         $code = replaceExpressionCode($data, $replacer);
71
72         // Return replaced code
73         return $code;
74 }
75
76 // Expression call-back function for reading data from $_SERVER
77 function doExpressionServer ($data) {
78         // This will make 'foo_bar' to detectFooBar()
79         $functionName = "'detect' . implode('', array_map('ucfirst', explode('_', '" . $data['callback'] . "')))";
80
81         // Generate replacer
82         $replacer = '{DQUOTE} . call_user_func(' . $functionName . ') . {DQUOTE}';
83
84         // Replace it
85         $code = replaceExpressionCode($data, $replacer);
86
87         // Return replaced code
88         return $code;
89 }
90
91 // Expression call-back function for fetching user data
92 function doExpressionUser ($data) {
93         // Use current userid by default
94         $functionName = 'getMemberId()';
95
96         // User-related data, so is there a userid?
97         if (!empty($data['matches'][4][$data['key']])) {
98                 // Do we have a userid or $userid?
99                 if ($data['matches'][4][$data['key']] == '$userid') {
100                         // Use dynamic call
101                         $functionName = "getFetchedUserData('userid', \$userid, '" . $data['callback'] . "')";
102                 } elseif ($data['matches'][4][$data['key']] > 0) {
103                         // User data found
104                         $functionName = "getFetchedUserData('userid', " . $data['matches'][4][$data['key']] . ", " . $data['callback'] . "')";
105                 }
106         } elseif ((!empty($data['callback'])) && (isUserDataValid())) {
107                 // "Call-back" alias column for current logged in user's data
108                 $functionName = "getUserData('" . $data['callback'] . "')";
109         }
110
111         // Do we have another function to run (e.g. translations)
112         if (!empty($data['extra_func'])) {
113                 // Surround the original function call with it
114                 $functionName = $data['extra_func'] . '(' . $functionName . ')';
115         } // END - if
116
117         // Generate replacer
118         $replacer = '{DQUOTE} . ' . $functionName . ' . "';
119
120         // Now replace the code
121         $code = replaceExpressionCode($data, $replacer);
122
123         // Return replaced code
124         return $code;
125 }
126
127 // Expression call-back function for getting extension data
128 function doExpressionExt ($data) {
129         // Not installed is default
130         $replacer = 'false';
131
132         // Is the extension installed?
133         if (isExtensionInstalled($data['matches'][4][$data['key']])) {
134                 // Construct call-back function name
135                 $functionName = 'getExtension' . ucfirst(strtolower($data['callback']));
136
137                 // Construct call of the function
138                 $replacer = "{DQUOTE} . call_user_func_array('" . $functionName . "', array('" . $data['matches'][4][$data['key']] . "', true)) . {DQUOTE}";
139         } // END - if
140
141         // Generate replacer
142         $replacer = sprintf("&amp;ext=%s&amp;ver=%s&amp;rev={DQUOTE} . getConfig('CURR_SVN_REVISION') . {DQUOTE}", $data['matches'][4][$data['key']], $replacer);
143
144         // Replace it and insert parameter for GET request
145         $code = replaceExpressionCode($data, $replacer);
146
147         // Return replaced code
148         return $code;
149 }
150
151 // Expression call-back function for getting configuration data
152 // @TODO FILTER_COMPILE_CONFIG does not handle call-back functions so we handle it here again
153 function doExpressionConfig ($data) {
154         // Read configuration
155         $configValue = getConfig($data['matches'][4][$data['key']]);
156
157         // Default replacer is the config value itself
158         $replacer = $configValue;
159
160         // Is there a call-back function provided?
161         if (!empty($data['callback'])) {
162                 // Construct a new expression
163                 $replacer = '{%pipe,' . $data['callback'] . ',' . $configValue . '%}';
164         } // END - if
165
166         // Replace the config entry
167         $code = replaceExpressionCode($data, $replacer);
168
169         // Return replaced code
170         return $code;
171 }
172
173 // Expression call-back function for piping data through functions
174 function doExpressionPipe ($data) {
175         // We need callback and extra_func: callback is really the call-back function, extra_func is our value
176         $replacer = $data['extra_func'];
177
178         // Do we have a call-back? Should always be there!
179         if (!empty($data['callback'])) {
180                 // Parse it through this function
181                 $replacer = '{DQUOTE} . ' . $data['callback'] . "('" . $replacer . "') . {DQUOTE}";
182         } // END - if
183
184         // Replace the config entry
185         $code = replaceExpressionCode($data, $replacer);
186
187         // Return replaced code
188         return $code;
189 }
190
191 // Expression call-back function for calling filters
192 function doExpressionFilter ($data) {
193         // Construct replacement
194         $replacer = "{DQUOTE} . runFilterChain('" . $data['matches'][4][$data['key']] . "') . {DQUOTE}";
195
196         // Run the filter and insert result
197         $code = replaceExpressionCode($data, $replacer);
198
199         // Return replaced code
200         return $code;
201 }
202
203 // Expression call-back function for validator links
204 function doExpressionValidatorLinks ($data) {
205         // Default is nothing
206         $replacer = '';
207
208         // Get the code from data array for replacement/pipe-through
209         $code = $data['code'];
210
211         // Should we generally include validator links?
212         if ((isExtensionInstalled('validator')) && (getConfig('enable_validator') == 'Y') && (!in_array(getModule(), array('admin', 'login')))) {
213                 // Load the validator template
214                 $replacer = escapeQuotes(loadTemplate('validator_links', true));
215         } // END - if
216
217         // Replace the code
218         $code = replaceExpressionCode($data, $replacer);
219
220         // Return the (maybe) replaced code
221         return $code;
222 }
223
224 // Expression call-back for dynamic messages
225 function doExpressionMessage ($data) {
226         // Message string replacement depends on if message is masked
227         if (isMessageMasked($data['callback'])) {
228                 // Message should be masked
229                 $replacer = "{DQUOTE} . getMaskedMessage('" . $data['callback'] . "', '" . $data['extra_func'] . "') . {DQUOTE}";
230         } else {
231                 // Regular message
232                 $replacer = "{DQUOTE} . getMessage('" . $data['callback'] . "') . {DQUOTE}";
233         }
234
235         // Replace the code
236         $code = replaceExpressionCode($data, $replacer);
237
238         // Return the (maybe) replaced code
239         return $code;
240 }
241
242 // [EOF]
243 ?>