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