Incomplete code added to handle absent user data
[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         // Do we have a call-back?
142         if (!empty($data['callback'])) {
143                 // Parse it through this function
144                 $configValue = call_user_func_array($data['callback'], array($configValue));
145         } // END - if
146
147         // Replace the config entry
148         $code = str_replace($data['matches'][0][$data['key']], $configValue, $data['code']);
149
150         // Return replaced code
151         return $code;
152 }
153
154 // Expression call-back function for calling filters
155 function doExpressionFilter ($data) {
156         // Construct replacement
157         $replacer = "\" . runFilterChain('" . $data['matches'][4][$data['key']] . "') . \"";
158
159         // Run the filter and insert result
160         $code = str_replace($data['matches'][0][$data['key']], $replacer, $data['code']);
161
162         // Return replaced code
163         return $code;
164 }
165
166 // Expression call-back function for validator links
167 function doExpressionValidatorLinks ($data) {
168         // Default is nothing
169         $replacer = '';
170
171         // Get the code from data array for replacement/pipe-through
172         $code = $data['code'];
173
174         // Should we generally include validator links?
175         if ((isExtensionInstalled('validator')) && (getConfig('enable_validator') == 'Y') && (!in_array(getModule(), array('admin', 'login')))) {
176                 // Load the validator template
177                 $replacer = escapeQuotes(loadTemplate('validator_links', true));
178         } // END - if
179
180         // Replace the code
181         $code = str_replace($data['matches'][0][$data['key']], $replacer, $code);
182
183         // Return the (maybe) replaced code
184         return $code;
185 }
186
187 // Expression call-back for dynamic messages
188 function doExpressionMessage ($data) {
189         die('<pre>'.htmlentities(print_r($data, true)).'</pre>');
190 }
191
192 // [EOF]
193 ?>