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 - 2011 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 debug_report_bug(__FUNCTION__, __LINE__, 'Filter system already initialized.');
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
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('init', 'LOAD_CONFIGURATION');
107 registerFilter('init', 'INIT_RANDOMIZER');
108 registerFilter('init', 'LOAD_RUNTIME_INCLUDES');
109 registerFilter('init', 'INIT_EXTENSIONS');
110 registerFilter('init', 'SET_CURRENT_DATE');
111 registerFilter('init', 'INIT_RANDOM_NUMBER');
112 registerFilter('init', 'CHECK_REPOSITORY_REVISION');
113 registerFilter('init', 'RUN_HOURLY_RESET');
114 registerFilter('init', 'RUN_DAILY_RESET');
115 registerFilter('init', 'TRIGGER_SENDING_POOL');
116 registerFilter('init', 'DETERMINE_USERNAME');
117 registerFilter('init', 'DETERMINE_WHAT_ACTION');
118 registerFilter('init', 'COUNT_MODULE');
119 registerFilter('init', 'UPDATE_LOGIN_DATA');
120 registerFilter('init', 'ACTIVATE_EXCHANGE');
122 // Page headers - pre-filter (normally, you want to register here)
123 registerFilter('pre_page_header', 'LOAD_PAGE_HEADER');
125 // Page headers - post-filter (normally, you don't want to register here)
126 //-------------------- LAST FILTER FOR THIS CHAIN! ------------------------
127 registerFilter('post_page_header', 'FINISH_PAGE_HEADER');
128 //-------------------- LAST FILTER FOR THIS CHAIN! ------------------------
130 // 'You are here' navigation - post filter
131 registerFilter('post_youhere_line', 'CALL_HANDLER_LOGIN_FAILTURES');
132 registerFilter('post_youhere_line', 'HANDLE_HOME_IN_INDEX_SETTING');
134 // Filters for pre-extension-registration
135 registerFilter('pre_extension_installed', 'RUN_SQLS');
137 // Filters for post-extension-registration
138 registerFilter('post_extension_installed', 'AUTO_ACTIVATE_EXTENSION');
139 registerFilter('post_extension_installed', 'SOLVE_TASK');
140 registerFilter('post_extension_installed', 'LOAD_INCLUDES');
141 registerFilter('post_extension_installed', 'REMOVE_UPDATES');
142 registerFilter('post_extension_installed', 'EXTENSION_MARK_INSTALLED');
145 registerFilter('solve_task', 'SOLVE_TASK');
147 // Loading includes in general
148 registerFilter('load_includes', 'LOAD_INCLUDES');
151 registerFilter('run_sqls', 'RUN_SQLS');
154 registerFilter('check_admin_acl', 'CHECK_ADMIN_ACL');
156 // Register shutdown filters
157 registerFilter('shutdown', 'FLUSH_FILTERS');
158 registerFilter('shutdown', 'FLUSH_STATS');
159 registerFilter('shutdown', 'FLUSH_TEMPLATE_CACHE');
160 registerFilter('shutdown', 'FLUSH_OUTPUT');
163 registerFilter('compile_code', 'COMPILE_CONFIG');
164 registerFilter('compile_code', 'COMPILE_EXPRESSION_CODE');
166 // Generic extension update filters
167 registerFilter('extension_update', 'UPDATE_EXTENSION_DATA');
169 // Do hourly reset stuff, keep this entry first in this chain:
170 registerFilter('hourly', 'RUN_HOURLY_INCLUDES');
172 // Do reset stuff, keep this entry first in this chain:
173 registerFilter('reset', 'RUN_RESET_INCLUDES');
176 registerFilter('extension_remove', 'REMOVE_EXTENSION');
178 // Exclude some users
179 registerFilter('exclude_users', 'HTML_INCLUDE_USERS');
181 // Handling of fatal errors
182 registerFilter('handle_fatal_errors', 'HANDLE_FATAL_ERRORS');
184 // Page footer filters
185 registerFilter('page_footer', 'HANDLE_FATAL_ERRORS');
186 registerFilter('page_footer', 'DISPLAY_COPYRIGHT');
187 registerFilter('page_footer', 'DISPLAY_PARSING_TIME');
189 // Member login check. Always keep FETCH_USER_DATA as first entry!
190 registerFilter('member_login_check', 'FETCH_USER_DATA');
193 registerFilter('do_admin_login_done', 'DO_LOGIN_ADMIN');
196 registerFilter('generate_admin_mail_links', 'GENERATE_POOL_MAIL_LINKS');
199 // "Registers" a new filter function
200 function registerFilter ($filterName, $filterFunction, $silentAbort = true, $force = false, $dry_run = false) {
201 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ENTRY: filterName=' . $filterName . ',filterFunction=' . $filterFunction . ' - ENTERED!');
203 // Extend the filter function name
204 $filterFunction = sprintf("FILTER_%s", strtoupper($filterFunction));
206 // Is that filter already there?
207 if ((isset($GLOBALS['cache_array']['filter']['chains'][$filterName][$filterFunction])) && ($force === false)) {
208 // In installation phase we always want to abort
209 if (($silentAbort === false) || (isInstallationPhase())) {
211 debug_report_bug(__FUNCTION__, __LINE__, sprintf("Filter chain %s has already filter function %s registered!", $filterName, $filterFunction));
219 if ($dry_run === false) {
220 // Is the function there?
221 if (!function_exists($filterFunction)) {
223 addFatalMessage(__FUNCTION__, __LINE__, sprintf("Filter function %s could not be added to filter chain %s.", $filterFunction, $filterName));
227 // Simply add it to the array
228 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'REGISTER: filterName=' . $filterName . ',filterFunction=' . $filterFunction);
229 $GLOBALS['cache_array']['filter']['chains'][$filterName][$filterFunction] = 'A';
230 $GLOBALS['cache_array']['filter']['counter'][$filterName][$filterFunction] = '0';
233 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ENTRY: filterName=' . $filterName . ',filterFunction=' . $filterFunction . ' - EXIT!');
236 // "Unregisters" a filter from the given chain
237 function unregisterFilter ($F, $L, $filterName, $filterFunction, $force = false, $dry_run = false) {
238 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'F=' . $F . ',L=' . $L . ',filterName=' . $filterName . ',filterFunction=' . $filterFunction . ',force=' . intval($force) . ',dry_run=' . intval($dry_run) . ' - ENTERED!');
240 // Extend the filter function name only if not loaded from database
241 if (!isset($GLOBALS['cache_array']['filter']['loaded'][$filterName][$filterFunction])) {
242 $filterFunction = sprintf("FILTER_%s", strtoupper($filterFunction));
245 // Is that filter there?
246 if ((!isset($GLOBALS['cache_array']['filter']['chains'][$filterName][$filterFunction])) && ($force === false)) {
247 // Not found, so abort here
248 addFatalMessage(__FUNCTION__, __LINE__, sprintf(getMessage('FILTER_FAILED_NOT_REMOVED'), $filterFunction, $filterName));
252 // Shall we remove? (default, not while just showing an extension removal)
253 if ($dry_run === false) {
254 // Mark for filter removal
255 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'F=' . $F . ',L=' . $L . ',filterName=' . $filterName . ',filterFunction=' . $filterFunction . ' - REMOVE!');
256 $GLOBALS['cache_array']['filter']['chains'][$filterName][$filterFunction] = 'R';
259 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'F=' . $F . ',L=' . $L . ',filterName=' . $filterName . ',filterFunction=' . $filterFunction . ',force=' . intval($force) . ',dry_run=' . intval($dry_run) . ' - EXIT!');
262 // "Runs" the given filters, filterData is optional and can be any type of data
263 function runFilterChain ($filterName, $filterData = NULL) {
264 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'filterName=' . $filterName . ',filterData[]=' . gettype($filterData) . ' - ENTERED!');
266 // Is that filter chain there?
267 if (!isset($GLOBALS['cache_array']['filter']['chains'][$filterName])) {
268 // Log not found filters in debug-mode
269 if (isDebugModeEnabled()) {
271 logDebugMessage(__FUNCTION__, __LINE__, 'Filter chain ' . $filterName . ' not found.');
274 // Abort here and return content
278 // Default return value
279 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'filterName=' . $filterName . ',count()=' . count($GLOBALS['cache_array']['filter']['chains'][$filterName]));
280 $returnValue = $filterData;
282 // Then run all filters
283 foreach ($GLOBALS['cache_array']['filter']['chains'][$filterName] as $filterFunction => $active) {
285 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Running: name=' . $filterName . ',func=' . $filterFunction . ',active=' . $active);
287 // Is the filter active or newly added??
288 if (($active == 'Y') || ($active == 'A') || ((in_array($filterName, array('shutdown', 'extension_remove', 'post_extension_run_sql'))) && ($active == 'R'))) {
289 // Is this filter there?
290 if (!function_exists($filterFunction)) {
292 debug_report_bug(__FUNCTION__, __LINE__, 'filterName=' . $filterName . ',filterFunction=' . $filterFunction . ' - AUTO-UNREGISTERED!');
295 // Call the filter chain
296 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, $filterName . '/' . $filterFunction . ',[]=' . gettype($returnValue));
297 $returnValue = call_user_func_array($filterFunction, array($returnValue));
299 // Update usage counter
300 countFilterUsage($filterName, $filterFunction);
301 } elseif (isDebugModeEnabled()) {
303 logDebugMessage(__FUNCTION__, __LINE__, 'Skipped: name=' . $filterName . ',func=' . $filterFunction . ',active=' . $active);
307 // Return the filtered content
308 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'filterName=' . $filterName . ',filterData[]=' . gettype($filterData) . ',returnValue[]=' . gettype($returnValue) . ' - EXIT!');
312 // Count the filter usage
313 function countFilterUsage ($filterName, $filterFunction) {
315 if (isset($GLOBALS['cache_array']['filter']['counter'][$filterName][$filterFunction])) {
316 // Yes, then increase
317 $GLOBALS['cache_array']['filter']['counter'][$filterName][$filterFunction]++;
320 $GLOBALS['cache_array']['filter']['counter'][$filterName][$filterFunction] = 1;
324 // Prepares the filter array for usage
325 function prepareFilterArray () {
326 // Abort here if array is absend (e.g. not cached)
327 if (!isset($GLOBALS['cache_array']['filter']['filter_name'])) {
333 $filterArray = array(
339 // Found in cache so rewrite the array
340 foreach ($GLOBALS['cache_array']['filter']['filter_name'] as $idx => $filterName) {
341 // Get filter function
342 $filterFunction = $GLOBALS['cache_array']['filter']['filter_function'][$idx];
344 // Add the element with mapped index
345 $filterArray['counter'][$filterName][$filterFunction] = $GLOBALS['cache_array']['filter']['filter_counter'][$idx];
346 $filterArray['loaded'][$filterName][$filterFunction] = true;
347 $filterArray['chains'][$filterName][$filterFunction] = $GLOBALS['cache_array']['filter']['filter_active'][$idx];
351 $GLOBALS['cache_array']['filter'] = $filterArray;
354 // Loads filter for given extension if present. This function will silently
355 // ignore absent filter files.
356 function loadExtensionFilters ($ext_name) {
357 // Do we have cache entry?
358 if (!isset($GLOBALS[__FUNCTION__][$ext_name])) {
359 // Default is not found
360 $GLOBALS[__FUNCTION__][$ext_name] = false;
362 // Construct include file name
363 $incFileName = sprintf("inc/filter/%s_filter.php", $ext_name);
365 // Is the include file readable?
366 if (isIncludeReadable($incFileName)) {
367 // Load the include file
368 loadIncludeOnce($incFileName);
370 // Mark the file as loaded
371 $GLOBALS[__FUNCTION__][$ext_name] = true;
372 } elseif (isDebugModeEnabled()) {
374 logDebugMessage(__FUNCTION__, __LINE__, 'Filter include file ' . $incFileName . ' for extension ' . $ext_name . ' is missing.');