2 /************************************************************************
3 * Mailer v0.2.1-FINAL Start: 10/27/2009 *
4 * =================== Last change: 10/27/2009 *
6 * -------------------------------------------------------------------- *
7 * File : filter-functions.php *
8 * -------------------------------------------------------------------- *
9 * Short description : Functions for filter system *
10 * -------------------------------------------------------------------- *
11 * Kurzbeschreibung : Funktionen fuer Filter-System *
12 * -------------------------------------------------------------------- *
15 * $Tag:: 0.2.1-FINAL $ *
17 * -------------------------------------------------------------------- *
18 * Copyright (c) 2003 - 2009 by Roland Haeder *
19 * Copyright (c) 2009 - 2012 by Mailer Developer Team *
20 * For more information visit: http://mxchange.org *
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. *
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. *
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, *
36 ************************************************************************/
38 // Some security stuff...
39 if (!defined('__SECURITY')) {
43 // Init "generic filter system"
44 function initFilterSystem () {
45 // Is the filter already initialized?
46 if (isset($GLOBALS['filter_init'])) {
48 reportBug(__FUNCTION__, __LINE__, 'Filter system already initialized.');
51 // Load all saved filers if ext-sql_patches is updated
52 if ((isset($GLOBALS['cache_array']['filter']['filter_name'])) && (!isset($GLOBALS['cache_array']['filter']['chains']))) {
53 // Prepare filter array
56 // Mark it as initialized
57 $GLOBALS['filter_init'] = TRUE;
58 } elseif ((!isInstallationPhase()) && (isExtensionInstalledAndNewer('sql_patches', '0.5.9'))) {
61 if (isExtensionINstalledAndNewer('sql_patches', '0.6.0')) {
62 $add = ',`filter_counter`';
66 $result = SQL_QUERY('SELECT
72 `{?_MYSQL_PREFIX?}_filters`
74 `filter_id` ASC', __FUNCTION__, __LINE__);
77 if (!SQL_HASZERONUMS($result)) {
79 while ($filterArray = SQL_FETCHARRAY($result)) {
80 // Get filter name and function
81 $filterName = $filterArray['filter_name'];
82 $filterFunction = $filterArray['filter_function'];
84 // Set counter to default
85 $GLOBALS['cache_array']['filter']['counter'][$filterName][$filterFunction] = '0';
87 // Mark this filter as loaded (from database)
88 $GLOBALS['cache_array']['filter']['loaded'][$filterName][$filterFunction] = TRUE;
91 $GLOBALS['cache_array']['filter']['chains'][$filterName][$filterFunction] = $filterArray['filter_active'];
93 // Is the array element for counter there?
94 if (isset($filterArray['filter_counter'])) {
95 // Then use this value!
96 $GLOBALS['cache_array']['filter']['counter'][$filterName][$filterFunction] = $filterArray['filter_counter'];
102 SQL_FREERESULT($result);
106 registerFilter(__FUNCTION__, __LINE__, 'init', 'LOAD_CONFIGURATION');
107 registerFilter(__FUNCTION__, __LINE__, 'init', 'INIT_RANDOMIZER');
108 registerFilter(__FUNCTION__, __LINE__, 'init', 'LOAD_RUNTIME_INCLUDES');
109 registerFilter(__FUNCTION__, __LINE__, 'init', 'INIT_EXTENSIONS');
110 registerFilter(__FUNCTION__, __LINE__, 'init', 'INIT_SESSION');
111 registerFilter(__FUNCTION__, __LINE__, 'init', 'SET_CURRENT_DATE');
112 registerFilter(__FUNCTION__, __LINE__, 'init', 'INIT_RANDOM_NUMBER');
113 registerFilter(__FUNCTION__, __LINE__, 'init', 'CHECK_REPOSITORY_REVISION');
114 registerFilter(__FUNCTION__, __LINE__, 'init', 'RUN_HOURLY_RESET');
115 registerFilter(__FUNCTION__, __LINE__, 'init', 'RUN_DAILY_RESET');
116 registerFilter(__FUNCTION__, __LINE__, 'init', 'RUN_WEEKLY_RESET');
117 registerFilter(__FUNCTION__, __LINE__, 'init', 'RUN_MONTHLY_RESET');
118 registerFilter(__FUNCTION__, __LINE__, 'init', 'TRIGGER_SENDING_POOL');
119 // @TODO Remove this forced removal after a year or so
120 unregisterFilter(__FUNCTION__, __LINE__, 'init', 'DETERMINE_USERNAME', TRUE);
121 registerFilter(__FUNCTION__, __LINE__, 'init', 'DETERMINE_WHAT_ACTION');
122 registerFilter(__FUNCTION__, __LINE__, 'init', 'COUNT_MODULE');
123 registerFilter(__FUNCTION__, __LINE__, 'init', 'UPDATE_LOGIN_DATA');
124 registerFilter(__FUNCTION__, __LINE__, 'init', 'ACTIVATE_EXCHANGE');
125 registerFilter(__FUNCTION__, __LINE__, 'init', 'REDIRECT_WRONG_SERVER_NAME');
127 // Post-initialization
128 registerFilter(__FUNCTION__, __LINE__, 'post_init', 'DETERMINE_USERNAME');
130 // Page headers - pre-filter (normally, you want to register here)
131 registerFilter(__FUNCTION__, __LINE__, 'pre_page_header', 'LOAD_PAGE_HEADER');
133 // Page headers - post-filter (normally, you don't want to register here)
134 //-------------------- LAST FILTER FOR THIS CHAIN! ------------------------
135 registerFilter(__FUNCTION__, __LINE__, 'post_page_header', 'FINISH_PAGE_HEADER');
136 //-------------------- LAST FILTER FOR THIS CHAIN! ------------------------
138 // 'You are here' navigation - post filter
139 registerFilter(__FUNCTION__, __LINE__, 'post_youhere_line', 'CALL_HANDLER_LOGIN_FAILTURES');
140 registerFilter(__FUNCTION__, __LINE__, 'post_youhere_line', 'HANDLE_HOME_IN_INDEX_SETTING');
142 // Filters for pre-extension-registration
143 registerFilter(__FUNCTION__, __LINE__, 'pre_extension_installed', 'RUN_SQLS');
145 // Filters for post-extension-registration
146 registerFilter(__FUNCTION__, __LINE__, 'post_extension_installed', 'AUTO_ACTIVATE_EXTENSION');
147 registerFilter(__FUNCTION__, __LINE__, 'post_extension_installed', 'SOLVE_TASK');
148 registerFilter(__FUNCTION__, __LINE__, 'post_extension_installed', 'LOAD_INCLUDES');
149 registerFilter(__FUNCTION__, __LINE__, 'post_extension_installed', 'REMOVE_UPDATES');
150 registerFilter(__FUNCTION__, __LINE__, 'post_extension_installed', 'EXTENSION_MARK_INSTALLED');
153 registerFilter(__FUNCTION__, __LINE__, 'solve_task', 'SOLVE_TASK');
155 // Loading includes in general
156 registerFilter(__FUNCTION__, __LINE__, 'load_includes', 'LOAD_INCLUDES');
159 registerFilter(__FUNCTION__, __LINE__, 'run_sqls', 'RUN_SQLS');
162 registerFilter(__FUNCTION__, __LINE__, 'check_admin_acl', 'CHECK_ADMIN_ACL');
164 // Register shutdown filters
165 registerFilter(__FUNCTION__, __LINE__, 'shutdown', 'FLUSH_FILTERS');
166 registerFilter(__FUNCTION__, __LINE__, 'shutdown', 'FLUSH_STATS');
167 registerFilter(__FUNCTION__, __LINE__, 'shutdown', 'FLUSH_TEMPLATE_CACHE');
168 registerFilter(__FUNCTION__, __LINE__, 'shutdown', 'FLUSH_OUTPUT');
171 registerFilter(__FUNCTION__, __LINE__, 'compile_code', 'COMPILE_CONFIG');
172 registerFilter(__FUNCTION__, __LINE__, 'compile_code', 'COMPILE_EXPRESSION_CODE');
174 // Generic extension update filters
175 registerFilter(__FUNCTION__, __LINE__, 'extension_update', 'UPDATE_EXTENSION_DATA');
177 // Do hourly reset stuff, keep this entry first in this chain:
178 registerFilter(__FUNCTION__, __LINE__, 'hourly', 'RUN_HOURLY_INCLUDES');
180 // Do daily stuff, keep this entry first in this chain:
181 registerFilter(__FUNCTION__, __LINE__, 'daily', 'RUN_DAILY_INCLUDES');
183 // Do weekly stuff, keep this entry first in this chain:
184 registerFilter(__FUNCTION__, __LINE__, 'weekly', 'RUN_WEEKLY_INCLUDES');
186 // Do monthly stuff, keep this entry first in this chain:
187 registerFilter(__FUNCTION__, __LINE__, 'monthly', 'RUN_MONTHLY_INCLUDES');
190 registerFilter(__FUNCTION__, __LINE__, 'extension_remove', 'REMOVE_EXTENSION');
192 // Exclude some users
193 registerFilter(__FUNCTION__, __LINE__, 'exclude_users', 'HTML_INCLUDE_USERS');
195 // Handling of fatal errors
196 registerFilter(__FUNCTION__, __LINE__, 'handle_fatal_errors', 'HANDLE_FATAL_ERRORS');
198 // Page footer filters
199 registerFilter(__FUNCTION__, __LINE__, 'page_footer', 'HANDLE_FATAL_ERRORS');
200 registerFilter(__FUNCTION__, __LINE__, 'page_footer', 'DISPLAY_COPYRIGHT');
201 registerFilter(__FUNCTION__, __LINE__, 'page_footer', 'DISPLAY_PARSING_TIME');
203 // Member login check. Always keep FETCH_USER_DATA as first entry!
204 registerFilter(__FUNCTION__, __LINE__, 'member_login_check', 'FETCH_USER_DATA');
207 registerFilter(__FUNCTION__, __LINE__, 'do_admin_login_done', 'DO_LOGIN_ADMIN');
210 registerFilter(__FUNCTION__, __LINE__, 'generate_admin_mail_links', 'GENERATE_POOL_MAIL_LINKS');
213 registerFilter(__FUNCTION__, __LINE__, 'send_build_mail', 'SEND_BUILD_MAIL');
215 // Handle referral banner click/view
216 registerFilter(__FUNCTION__, __LINE__, 'handle_click_php', 'HANDLE_REFERRER_BANNER_CLICK');
217 registerFilter(__FUNCTION__, __LINE__, 'handle_view_php', 'HANDLE_REFERRER_BANNER_VIEW');
219 // Generic filter to add hidden fields to formulars
220 registerFilter(__FUNCTION__, __LINE__, 'open_form_fields', 'ADD_INPUT_HIDDEN_SESSION_ID');
223 // "Registers" a new filter function
224 function registerFilter ($F, $L, $filterName, $filterFunction, $silentAbort = TRUE, $force = FALSE, $isDryRun = FALSE) {
225 // Extend the filter function name
226 $filterFunction = 'FILTER_' . strtoupper($filterFunction);
228 // Debug message with FILTER_ prefix
229 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ENTRY: filterName=' . $filterName . ',filterFunction=' . $filterFunction . ',force=' . intval($force) . ',isDryRun=' . intval($isDryRun) . ',F=' . basename($F) . ',L=' . $L . ' - ENTERED!');
231 // Is that filter already there?
232 if ((isset($GLOBALS['cache_array']['filter']['chains'][$filterName][$filterFunction])) && ($force === FALSE)) {
233 // In installation phase we always want to abort
234 if (($silentAbort === FALSE) || (isInstallationPhase())) {
236 reportBug(__FUNCTION__, __LINE__, sprintf("Filter chain %s has already filter function %s registered! F=%s,L=%s,force=%d", $filterName, $filterFunction, basename($F), $L, intval($force)));
244 if ($isDryRun === FALSE) {
245 // Is the function there?
246 if (!function_exists($filterFunction)) {
248 logDebugMessage(__FUNCTION__, __LINE__, sprintf("Filter function %s could not be added to filter chain %s. F=%s,L=%s,force=%d", $filterFunction, $filterName, basename($F), $L, intval($force)));
252 // Simply add it to the array
253 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'REGISTER: filterName=' . $filterName . ',filterFunction=' . $filterFunction . ',force=' . intval($force) . ',F=' . basename($F) . ',L=' . $L);
254 $GLOBALS['cache_array']['filter']['chains'][$filterName][$filterFunction] = 'A';
255 $GLOBALS['cache_array']['filter']['counter'][$filterName][$filterFunction] = '0';
258 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ENTRY: filterName=' . $filterName . ',filterFunction=' . $filterFunction . ',force=' . intval($force) . ',isDryRun=' . intval($isDryRun) . ',F=' . basename($F) . ',L=' . $L . ' - EXIT!');
264 // "Unregisters" a filter from the given chain
265 function unregisterFilter ($F, $L, $filterName, $filterFunction, $force = FALSE, $isDryRun = FALSE) {
266 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'F=' . $F . ',L=' . $L . ',filterName=' . $filterName . ',filterFunction=' . $filterFunction . ',force=' . intval($force) . ',isDryRun=' . intval($isDryRun) . ' - ENTERED!');
268 // Extend the filter function name only if not loaded from database
269 if (!isset($GLOBALS['cache_array']['filter']['loaded'][$filterName][$filterFunction])) {
270 $filterFunction = 'FILTER_' . strtoupper($filterFunction);
273 // Is that filter there?
274 if ((!isset($GLOBALS['cache_array']['filter']['chains'][$filterName][$filterFunction])) && ($force === FALSE)) {
275 // Not found, so abort here
276 logDebugMessage(__FUNCTION__, __LINE__, sprintf('Filter function %s cannot be unregistered from filter %s.', $filterFunction, $filterName));
280 // Shall we remove? (default, not while just showing an extension removal)
281 if (($isDryRun === FALSE) && (isset($GLOBALS['cache_array']['filter']['chains'][$filterName][$filterFunction]))) {
282 // Mark for filter removal
283 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'F=' . $F . ',L=' . $L . ',filterName=' . $filterName . ',filterFunction=' . $filterFunction . ' - REMOVE!');
284 $GLOBALS['cache_array']['filter']['chains'][$filterName][$filterFunction] = 'R';
287 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'F=' . $F . ',L=' . $L . ',filterName=' . $filterName . ',filterFunction=' . $filterFunction . ',force=' . intval($force) . ',isDryRun=' . intval($isDryRun) . ' - EXIT!');
293 // "Runs" the given filters, filterData is optional and can be any type of data
294 function runFilterChain ($filterName, $filterData = NULL) {
295 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'filterName=' . $filterName . ',filterData[]=' . gettype($filterData) . ' - ENTERED!');
297 // Is that filter chain there?
298 if (!isset($GLOBALS['cache_array']['filter']['chains'][$filterName])) {
299 // Log not found filters in debug-mode
300 if (isDebugModeEnabled()) {
302 logDebugMessage(__FUNCTION__, __LINE__, 'Filter chain ' . $filterName . ' does not exist.');
305 // Abort here and return content
309 // Default return value
310 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'filterName=' . $filterName . ',count()=' . count($GLOBALS['cache_array']['filter']['chains'][$filterName]));
311 $returnValue = $filterData;
313 // Then run all filters
314 foreach ($GLOBALS['cache_array']['filter']['chains'][$filterName] as $filterFunction => $active) {
316 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Running: name=' . $filterName . ',func=' . $filterFunction . ',active=' . $active);
318 // Is the filter active or newly added??
319 if (($active == 'Y') || ($active == 'A') || ((in_array($filterName, array('shutdown', 'extension_remove', 'post_extension_run_sql'))) && ($active == 'R'))) {
320 // Is this filter there?
321 if (!function_exists($filterFunction)) {
323 reportBug(__FUNCTION__, __LINE__, 'filterName=' . $filterName . ',filterFunction=' . $filterFunction . ' - AUTO-UNREGISTERED!');
326 // Call the filter chain
327 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, $filterName . '/' . $filterFunction . ',[]=' . gettype($returnValue) . ' - CALLING!');
328 $returnValue = call_user_func_array($filterFunction, array($returnValue));
330 // Update usage counter
331 countFilterUsage($filterName, $filterFunction);
332 } elseif (isDebugModeEnabled()) {
334 logDebugMessage(__FUNCTION__, __LINE__, 'Skipped: name=' . $filterName . ',func=' . $filterFunction . ',active=' . $active);
338 // Return the filtered content
339 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'filterName=' . $filterName . ',filterData[]=' . gettype($filterData) . ',returnValue[]=' . gettype($returnValue) . ' - EXIT!');
343 // Count the filter usage
344 function countFilterUsage ($filterName, $filterFunction) {
346 if (isset($GLOBALS['cache_array']['filter']['counter'][$filterName][$filterFunction])) {
347 // Yes, then increase
348 $GLOBALS['cache_array']['filter']['counter'][$filterName][$filterFunction]++;
351 $GLOBALS['cache_array']['filter']['counter'][$filterName][$filterFunction] = 1;
355 // Prepares the filter array for usage
356 function prepareFilterArray () {
357 // Abort here if array is absend (e.g. not cached)
358 if (!isset($GLOBALS['cache_array']['filter']['filter_name'])) {
364 $filterArray = array(
370 // Found in cache so rewrite the array
371 foreach ($GLOBALS['cache_array']['filter']['filter_name'] as $idx => $filterName) {
372 // Get filter function
373 $filterFunction = $GLOBALS['cache_array']['filter']['filter_function'][$idx];
375 // Add the element with mapped index
376 $filterArray['counter'][$filterName][$filterFunction] = $GLOBALS['cache_array']['filter']['filter_counter'][$idx];
377 $filterArray['loaded'][$filterName][$filterFunction] = TRUE;
378 $filterArray['chains'][$filterName][$filterFunction] = $GLOBALS['cache_array']['filter']['filter_active'][$idx];
382 $GLOBALS['cache_array']['filter'] = $filterArray;
386 * Loads filter for given extension if present. This function will silently
387 * ignore absent filter files.
389 * @param $ext_name Name of extension
392 function loadExtensionFilters ($ext_name) {
394 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ' - ENTERED!');
396 // Is there a cache entry?
397 if (!isset($GLOBALS[__FUNCTION__][$ext_name])) {
398 // Default is not found
399 $GLOBALS[__FUNCTION__][$ext_name] = FALSE;
401 // Construct include file name
402 $incFileName = sprintf("inc/filter/%s_filter.php", $ext_name);
404 // Is the include file readable?
405 if (isIncludeReadable($incFileName)) {
406 // Load the include file
407 loadIncludeOnce($incFileName);
409 // Mark the file as loaded
410 $GLOBALS[__FUNCTION__][$ext_name] = TRUE;
411 } elseif (isDebugModeEnabled()) {
413 //* NOISY-DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Filter include file ' . $incFileName . ' for extension ' . $ext_name . ' is missing.');
418 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',result=' . intval($GLOBALS[__FUNCTION__][$ext_name]) . ' - EXIT!');