Rewrites/fixes for CURRENT_DATE, better encapsulation of redirect, coding convention:
[mailer.git] / inc / filter-functions.php
1 <?php
2 /************************************************************************
3  * Mailer v0.2.1-FINAL                                Start: 10/27/2009 *
4  * ===================                          Last change: 10/27/2009 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : filter-functions.php                             *
8  * -------------------------------------------------------------------- *
9  * Short description : Functions for filter system                      *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Funktionen fuer Filter-System                    *
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 // Init "generic filter system"
46 function initFilterSystem () {
47         // Is the filter already initialized?
48         if (isset($GLOBALS['filter_init'])) {
49                 // Then abort here
50                 debug_report_bug(__FUNCTION__, __LINE__, 'Filter system already initialized.');
51         } // END - if
52
53         // Load all saved filers if sql_patches is updated
54         if ((isset($GLOBALS['cache_array']['filter']['filter_name'])) && (!isset($GLOBALS['cache_array']['filter']['chains']))) {
55                 // Prepare filter array
56                 prepareFilterArray();
57
58                 // Mark it as initialized
59                 $GLOBALS['filter_init'] = true;
60         } elseif ((!isInstallationPhase()) && (isExtensionInstalledAndNewer('sql_patches', '0.5.9'))) {
61                 // Init add
62                 $add = '';
63                 if (isExtensionINstalledAndNewer('sql_patches', '0.6.0')) $add = ", `filter_counter`";
64
65                 // Load all filters
66                 $result = SQL_QUERY('SELECT
67         `filter_name`,`filter_function`,`filter_active`' . $add . '
68 FROM
69         `{?_MYSQL_PREFIX?}_filters`
70 ORDER BY
71         `filter_id` ASC', __FUNCTION__, __LINE__);
72
73                 // Are there entries?
74                 if (!SQL_HASZERONUMS($result)) {
75                         // Load all filters
76                         while ($filterArray = SQL_FETCHARRAY($result)) {
77                                 // Get filter name and function
78                                 $filterName     = $filterArray['filter_name'];
79                                 $filterFunction = $filterArray['filter_function'];
80
81                                 // Set counter to default
82                                 $GLOBALS['cache_array']['filter']['counter'][$filterName][$filterFunction] = '0';
83
84                                 // Mark this filter as loaded (from database)
85                                 $GLOBALS['cache_array']['filter']['loaded'][$filterName][$filterFunction] = true;
86
87                                 // Set this filter
88                                 $GLOBALS['cache_array']['filter']['chains'][$filterName][$filterFunction] = $filterArray['filter_active'];
89
90                                 // Is the array element for counter there?
91                                 if (isset($filterArray['filter_counter'])) {
92                                         // Then use this value!
93                                         $GLOBALS['cache_array']['filter']['counter'][$filterName][$filterFunction] = $filterArray['filter_counter'];
94                                 } // END - if
95                         } // END - while
96                 } // END - if
97
98                 // Free result
99                 SQL_FREERESULT($result);
100         }
101
102         // Init filters
103         registerFilter('init', 'LOAD_CONFIGURATION');
104         registerFilter('init', 'INIT_RANDOMIZER');
105         registerFilter('init', 'LOAD_RUNTIME_INCLUDES');
106         registerFilter('init', 'INIT_EXTENSIONS');
107         registerFilter('init', 'SET_CURRENT_DATE');
108         registerFilter('init', 'INIT_RANDOM_NUMBER');
109         registerFilter('init', 'CHECK_SVN_REVISION');
110         registerFilter('init', 'RUN_DAILY_RESET');
111         registerFilter('init', 'TRIGGER_SENDING_POOL');
112         registerFilter('init', 'DETERMINE_USERNAME');
113         registerFilter('init', 'DETERMINE_WHAT_ACTION');
114         registerFilter('init', 'COUNT_MODULE');
115         registerFilter('init', 'UPDATE_LOGIN_DATA');
116         registerFilter('init', 'ACTIVATE_EXCHANGE');
117
118         // Page headers - pre-filter (normally, you want to register here)
119         registerFilter('pre_page_header', 'LOAD_PAGE_HEADER');
120
121         // Page headers - post-filter (normally, you don't want to register here)
122         // ------------------- LAST FILTER FOR THIS CHAIN! ------------------------
123         registerFilter('post_page_header', 'FINISH_PAGE_HEADER');
124         // ------------------- LAST FILTER FOR THIS CHAIN! ------------------------
125
126         // 'You are here' navigation - post filter
127         registerFilter('post_youhere_line', 'CALL_HANDLER_LOGIN_FAILTURES');
128
129         // Filters for pre-extension-registration
130         registerFilter('pre_extension_installed', 'RUN_SQLS');
131
132         // Filters for post-extension-registration
133         registerFilter('post_extension_installed', 'AUTO_ACTIVATE_EXTENSION');
134         registerFilter('post_extension_installed', 'SOLVE_TASK');
135         registerFilter('post_extension_installed', 'LOAD_INCLUDES');
136         registerFilter('post_extension_installed', 'REMOVE_UPDATES');
137
138         // Solving tasks
139         registerFilter('solve_task', 'SOLVE_TASK');
140
141         // Loading includes in general
142         registerFilter('load_includes', 'LOAD_INCLUDES');
143
144         // Run SQLs
145         registerFilter('run_sqls', 'RUN_SQLS');
146
147         // Admin ACL check
148         registerFilter('check_admin_acl', 'CHECK_ADMIN_ACL');
149
150         // Register shutdown filters
151         registerFilter('shutdown', 'FLUSH_FILTERS');
152         registerFilter('shutdown', 'FLUSH_STATS');
153         registerFilter('shutdown', 'FLUSH_TEMPLATE_CACHE');
154         registerFilter('shutdown', 'FLUSH_OUTPUT');
155
156         // Compiling code
157         registerFilter('compile_code', 'COMPILE_CONFIG');
158         registerFilter('compile_code', 'COMPILE_EXPRESSION_CODE');
159
160         // Generic extension update filters
161         registerFilter('extension_update', 'UPDATE_EXTENSION_DATA');
162
163         // Do reset stuff, keep this entry first in this chain:
164         registerFilter('reset', 'RUN_RESET_INCLUDES');
165
166         // Remove extension
167         registerFilter('extension_remove', 'REMOVE_EXTENSION');
168
169         // Exclude some users
170         registerFilter('exclude_users', 'HTML_INCLUDE_USERS');
171
172         // Handling of fatal errors
173         registerFilter('handle_fatal_errors', 'HANDLE_FATAL_ERRORS');
174
175         // Page footer filters
176         registerFilter('page_footer', 'HANDLE_FATAL_ERRORS');
177         registerFilter('page_footer', 'DISPLAY_COPYRIGHT');
178         registerFilter('page_footer', 'DISPLAY_PARSING_TIME');
179
180         // Member login check. Always keep FETCH_USER_DATA as first entry!
181         registerFilter('member_login_check', 'FETCH_USER_DATA');
182
183         // Admin login
184         registerFilter('do_admin_login_done', 'DO_LOGIN_ADMIN');
185 }
186
187 // "Registers" a new filter function
188 function registerFilter ($filterName, $filterFunction, $silentAbort = true, $force = false, $dry_run = false) {
189         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ENTRY: filterName=' . $filterName . ',filterFunction=' . $filterFunction . ' - ENTERED!');
190
191         // Extend the filter function name
192         $filterFunction = sprintf("FILTER_%s", strtoupper($filterFunction));
193
194         // Is that filter already there?
195         if ((isset($GLOBALS['cache_array']['filter']['chains'][$filterName][$filterFunction])) && ($force === false)) {
196                 // In installation phase we always want to abort
197                 if (($silentAbort === false) || (isInstallationPhase())) {
198                         // Add fatal message
199                         debug_report_bug(__FUNCTION__, __LINE__, sprintf("Filter chain %s has already filter function %s registered!", $filterName, $filterFunction));
200                 } // END - if
201
202                 // Abort here
203                 return false;
204         } // END - if
205
206         // Shall we add it?
207         if ($dry_run === false) {
208                 // Is the function there?
209                 if (!function_exists($filterFunction)) {
210                         // Then abort here
211                         addFatalMessage(__FUNCTION__, __LINE__, sprintf("Filter function %s could not be added to filter chain %s.", $filterFunction, $filterName));
212                         return false;
213                 } // END - if
214
215                 // Simply add it to the array
216                 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'REGISTER: filterName=' . $filterName . ',filterFunction=' . $filterFunction);
217                 $GLOBALS['cache_array']['filter']['chains'][$filterName][$filterFunction] = 'A';
218                 $GLOBALS['cache_array']['filter']['counter'][$filterName][$filterFunction] = '0';
219         } // END - if
220
221         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ENTRY: filterName=' . $filterName . ',filterFunction=' . $filterFunction . ' - EXIT!');
222 }
223
224 // "Unregisters" a filter from the given chain
225 function unregisterFilter ($F, $L, $filterName, $filterFunction, $force = false, $dry_run = false) {
226         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ENTRY: F=' . $F . ',L=' . $L . ',filterName=' . $filterName . ',filterFunction=' . $filterFunction . ',force=' . intval($force) . ',dry=' . intval($dry_run) . ' - ENTERED!');
227
228         // Extend the filter function name only if not loaded from database
229         if (!isset($GLOBALS['cache_array']['filter']['loaded'][$filterName][$filterFunction])) {
230                 $filterFunction = sprintf("FILTER_%s", strtoupper($filterFunction));
231         } // END - if
232
233         // Is that filter there?
234         if ((!isset($GLOBALS['cache_array']['filter']['chains'][$filterName][$filterFunction])) && ($force === false)) {
235                 // Not found, so abort here
236                 addFatalMessage(__FUNCTION__, __LINE__, sprintf(getMessage('FILTER_FAILED_NOT_REMOVED'), $filterFunction, $filterName));
237                 return false;
238         } // END - if
239
240         // Shall we remove? (default, not while just showing an extension removal)
241         if ($dry_run === false) {
242                 // Mark for filter removal
243                 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'REMOVE: F=' . $F . ',L=' . $L . ',filterName=' . $filterName . ',filterFunction=' . $filterFunction);
244                 $GLOBALS['cache_array']['filter']['chains'][$filterName][$filterFunction] = 'R';
245         } // END - if
246
247         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ENTRY: F=' . $F . ',L=' . $L . ',filterName=' . $filterName . ',filterFunction=' . $filterFunction . ',force=' . intval($force) . ',dry=' . intval($dry_run) . ' - EXIT!');
248 }
249
250 // "Runs" the given filters, data is optional and can be any type of data
251 function runFilterChain ($filterName, $data = null) {
252         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'filterName=' . $filterName . ',data[]=' . gettype($data) . ' - ENTERED!');
253
254         // Is that filter chain there?
255         if (!isset($GLOBALS['cache_array']['filter']['chains'][$filterName])) {
256                 // Log not found filters in debug-mode
257                 if (isDebugModeEnabled()) {
258                         // Log it away...
259                         logDebugMessage(__FUNCTION__, __LINE__, 'Filter chain ' . $filterName . ' not found!');
260                 } // END - if
261
262                 // Abort here and return content
263                 return $data;
264         } // END - if
265
266         // Default return value
267         $returnValue = $data;
268
269         // Then run all filters
270         foreach ($GLOBALS['cache_array']['filter']['chains'][$filterName] as $filterFunction => $active) {
271                 // Debug message
272                 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Running: name=' . $filterName . ',func=' . $filterFunction . ',active=' . $active);
273
274                 // Is the filter active or newly added??
275                 if (($active == 'Y') || ($active == 'A') || ((in_array($filterName, array('shutdown','extension_remove','post_extension_run_sql'))) && ($active == 'R'))) {
276                         // Is this filter there?
277                         if (!function_exists($filterFunction)) {
278                                 // Unregister it
279                                 unregisterFilter(__FUNCTION__, __LINE__, $filterName, $filterFunction);
280
281                                 // Skip this entry
282                                 continue;
283                         } // END - if
284
285                         // Call the filter chain
286                         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, $filterName . '/' . $filterFunction . ',[]=' . gettype($returnValue));
287                         $returnValue = call_user_func_array($filterFunction, array($returnValue));
288
289                         // Update usage counter
290                         countFilterUsage($filterName, $filterFunction);
291                 } elseif (isDebugModeEnabled()) {
292                         // Debug message
293                         logDebugMessage(__FUNCTION__, __LINE__, 'Skipped: name=' . $filterName . ',func=' . $filterFunction . ',active=' . $active);
294                 }
295         } // END - foreach
296
297         // Return the filtered content
298         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'filterName=' . $filterName . ',data[]=' . gettype($data) . ',returnValue[]=' . gettype($returnValue) . ' - EXIT!');
299         return $returnValue;
300 }
301
302 // Count the filter usage
303 function countFilterUsage ($filterName, $filterFunction) {
304         // Is it there?
305         if (isset($GLOBALS['cache_array']['filter']['counter'][$filterName][$filterFunction])) {
306                 // Yes, then increase
307                 $GLOBALS['cache_array']['filter']['counter'][$filterName][$filterFunction]++;
308         } else {
309                 // No, then create
310                 $GLOBALS['cache_array']['filter']['counter'][$filterName][$filterFunction] = 1;
311         }
312 }
313
314 // Prepares the filter array for usage
315 function prepareFilterArray () {
316         // Abort here if array is absend (e.g. not cached)
317         if (!isset($GLOBALS['cache_array']['filter']['filter_name'])) {
318                 // Abort silently
319                 return false;
320         } // END - if
321
322         // Init dummy array
323         $filterArray = array(
324                 'chains'  => array(),
325                 'loaded'  => array(),
326                 'counter' => array()
327         );
328
329         // Found in cache so rewrite the array
330         foreach ($GLOBALS['cache_array']['filter']['filter_name'] as $idx => $filterName) {
331                 // Get filter function
332                 $filterFunction = $GLOBALS['cache_array']['filter']['filter_function'][$idx];
333
334                 // Add the element with mapped index
335                 $filterArray['counter'][$filterName][$filterFunction] = $GLOBALS['cache_array']['filter']['filter_counter'][$idx];
336                 $filterArray['loaded'][$filterName][$filterFunction]  = true;
337                 $filterArray['chains'][$filterName][$filterFunction]  = $GLOBALS['cache_array']['filter']['filter_active'][$idx];
338         } // END - foreach
339
340         // Remove the cache
341         $GLOBALS['cache_array']['filter'] = $filterArray;
342 }
343
344 // [EOF]
345 ?>