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