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 * 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 *
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. *
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. *
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, *
38 ************************************************************************/
40 // Some security stuff...
41 if (!defined('__SECURITY')) {
45 // Init "generic filter system"
46 function initFilterSystem () {
47 // Is the filter already initialized?
48 if (isset($GLOBALS['filter_init'])) {
50 debug_report_bug('Filter system already initialized.');
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
58 // Mark it as initialized
59 $GLOBALS['filter_init'] = true;
60 } elseif ((!isInstallationPhase()) && (isExtensionInstalledAndNewer('sql_patches', '0.5.9'))) {
63 if (getExtensionVersion('sql_patches') >= '0.6.0') $add = ", `filter_counter`";
65 // Load all active filers
66 $result = SQL_QUERY("SELECT
67 `filter_name`,`filter_function`,`filter_active`".$add."
69 `{?_MYSQL_PREFIX?}_filters`
71 `filter_id` ASC", __FUNCTION__, __LINE__);
74 if (SQL_NUMROWS($result) > 0) {
76 while ($filterArray = SQL_FETCHARRAY($result)) {
77 // Get filter name and function
78 $filterName = $filterArray['filter_name'];
79 $filterFunction = $filterArray['filter_function'];
81 // Set counter to default
82 $GLOBALS['cache_array']['filter']['counter'][$filterName][$filterFunction] = '0';
84 // Mark this filter as loaded (from database)
85 $GLOBALS['cache_array']['filter']['loaded'][$filterName][$filterFunction] = true;
88 $GLOBALS['cache_array']['filter']['chains'][$filterName][$filterFunction] = $filterArray['filter_active'];
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'];
99 SQL_FREERESULT($result);
103 registerFilter('init', 'LOAD_CONFIGURATION');
104 registerFilter('init', 'INIT_RANDOMIZER');
105 registerFilter('init', 'LOAD_RUNTIME_INCLUDES');
106 registerFilter('init', 'INIT_EXTENSIONS');
107 registerFilter('init', 'INIT_RANDOM_NUMBER');
108 registerFilter('init', 'CHECK_SVN_REVISION');
109 registerFilter('init', 'RUN_DAILY_RESET');
110 registerFilter('init', 'TRIGGER_SENDING_POOL');
111 registerFilter('init', 'DETERMINE_USERNAME');
112 registerFilter('init', 'DETERMINE_WHAT_ACTION');
113 registerFilter('init', 'COUNT_MODULE');
114 registerFilter('init', 'UPDATE_LOGIN_DATA');
115 registerFilter('init', 'ACTIVATE_EXCHANGE');
117 // Login failures handler
118 registerFilter('post_youhere_line', 'CALL_HANDLER_LOGIN_FAILTURES');
120 // Filters for pre-extension-registration
121 registerFilter('pre_extension_installed', 'RUN_SQLS');
123 // Filters for post-extension-registration
124 registerFilter('post_extension_installed', 'AUTO_ACTIVATE_EXTENSION');
125 registerFilter('post_extension_installed', 'SOLVE_TASK');
126 registerFilter('post_extension_installed', 'LOAD_INCLUDES');
127 registerFilter('post_extension_installed', 'REMOVE_UPDATES');
130 registerFilter('solve_task', 'SOLVE_TASK');
132 // Loading includes in general
133 registerFilter('load_includes', 'LOAD_INCLUDES');
136 registerFilter('run_sqls', 'RUN_SQLS');
139 registerFilter('check_admin_acl', 'CHECK_ADMIN_ACL');
141 // Register shutdown filters
142 registerFilter('shutdown', 'FLUSH_FILTERS');
143 registerFilter('shutdown', 'FLUSH_STATS');
144 registerFilter('shutdown', 'FLUSH_TEMPLATE_CACHE');
145 registerFilter('shutdown', 'FLUSH_OUTPUT');
148 registerFilter('compile_code', 'COMPILE_CONFIG');
149 registerFilter('compile_code', 'COMPILE_EXPRESSION_CODE');
151 // Generic extension update filters
152 registerFilter('extension_update', 'UPDATE_EXTENSION_DATA');
154 // Do reset stuff, keep this entry first in this chain:
155 registerFilter('reset', 'RUN_RESET_INCLUDES');
158 registerFilter('extension_remove', 'REMOVE_EXTENSION');
160 // Exclude some users
161 registerFilter('exclude_users', 'HTML_INCLUDE_USERS');
163 // Handling of fatal errors
164 registerFilter('handle_fatal_errors', 'HANDLE_FATAL_ERRORS');
166 // Page footer filters
167 registerFilter('page_footer', 'HANDLE_FATAL_ERRORS');
168 registerFilter('page_footer', 'DISPLAY_COPYRIGHT');
169 registerFilter('page_footer', 'DISPLAY_PARSING_TIME');
171 // Member login check. Always keep FETCH_USER_DATA as first entry!
172 registerFilter('member_login_check', 'FETCH_USER_DATA');
175 registerFilter('do_admin_login_done', 'DO_LOGIN_ADMIN');
178 // "Registers" a new filter function
179 function registerFilter ($filterName, $filterFunction, $silentAbort = true, $force = false, $dry_run = false) {
180 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ENTRY: filterName=' . $filterName . ',filterFunction=' . $filterFunction);
182 // Extend the filter function name
183 $filterFunction = sprintf("FILTER_%s", strtoupper($filterFunction));
185 // Is that filter already there?
186 if ((isset($GLOBALS['cache_array']['filter']['chains'][$filterName][$filterFunction])) && ($force === false)) {
188 if ($silentAbort === false) {
190 addFatalMessage(__FUNCTION__, __LINE__, sprintf("Filter chain %s has already filter function %s registered!", $filterName, $filterFunction));
198 if ($dry_run === false) {
199 // Is the function there?
200 if (!function_exists($filterFunction)) {
202 addFatalMessage(__FUNCTION__, __LINE__, sprintf("Filter function %s could not be added to filter chain %s.", $filterFunction, $filterName));
206 // Simply add it to the array
207 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'REGISTER: filterName=' . $filterName . ',filterFunction=' . $filterFunction);
208 $GLOBALS['cache_array']['filter']['chains'][$filterName][$filterFunction] = 'A';
209 $GLOBALS['cache_array']['filter']['counter'][$filterName][$filterFunction] = '0';
213 // "Unregisters" a filter from the given chain
214 function unregisterFilter ($filterName, $filterFunction, $force = false, $dry_run = false) {
215 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ENTRY: filterName=' . $filterName . ',filterFunction=' . $filterFunction . ',force=' . intval($force) . ',dry=' . intval($dry_run));
217 // Extend the filter function name only if not loaded from database
218 if (!isset($GLOBALS['cache_array']['filter']['loaded'][$filterName][$filterFunction])) {
219 $filterFunction = sprintf("FILTER_%s", strtoupper($filterFunction));
222 // Is that filter there?
223 if ((!isset($GLOBALS['cache_array']['filter']['chains'][$filterName][$filterFunction])) && ($force === false)) {
224 // Not found, so abort here
225 addFatalMessage(__FUNCTION__, __LINE__, sprintf(getMessage('FILTER_FAILED_NOT_REMOVED'), $filterFunction, $filterName));
229 // Shall we remove? (default, not while just showing an extension removal)
230 if ($dry_run === false) {
231 // Mark for filter removal
232 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'REMOVE: filterName=' . $filterName . ',filterFunction=' . $filterFunction);
233 $GLOBALS['cache_array']['filter']['chains'][$filterName][$filterFunction] = 'R';
237 // "Runs" the given filters, data is optional and can be any type of data
238 function runFilterChain ($filterName, $data = null) {
239 // Is that filter chain there?
240 if (!isset($GLOBALS['cache_array']['filter']['chains'][$filterName])) {
241 // Log not found filters in debug-mode
242 if (isDebugModeEnabled()) {
244 logDebugMessage(__FUNCTION__, __LINE__, 'Filter chain ' . $filterName . ' not found!');
247 // Abort here and return content
251 // Default return value
252 $returnValue = $data;
254 // Then run all filters
255 foreach ($GLOBALS['cache_array']['filter']['chains'][$filterName] as $filterFunction => $active) {
257 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "Running: name={$filterName},func={$filterFunction},active={$active}");
259 // Is the filter active?
260 if (($active == 'Y') || ($active == 'A') || ((in_array($filterName, array('shutdown','extension_remove','post_extension_run_sql'))) && ($active == 'R'))) {
261 // Is this filter there?
262 if (!function_exists($filterFunction)) {
264 unregisterFilter($filterName, $filterFunction);
270 // Call the filter chain
271 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, $filterName.'/'.$filterFunction.',[]='.gettype($returnValue));
272 $returnValue = call_user_func_array($filterFunction, array($returnValue));
274 // Update usage counter
275 countFilterUsage($filterName, $filterFunction);
276 } elseif (isDebugModeEnabled()) {
278 logDebugMessage(__FUNCTION__, __LINE__, "Skipped: name={$filterName},func={$filterFunction},active={$active}");
282 // Return the filtered content
286 // Count the filter usage
287 function countFilterUsage ($filterName, $filterFunction) {
289 if (isset($GLOBALS['cache_array']['filter']['counter'][$filterName][$filterFunction])) {
290 // Yes, then increase
291 $GLOBALS['cache_array']['filter']['counter'][$filterName][$filterFunction]++;
294 $GLOBALS['cache_array']['filter']['counter'][$filterName][$filterFunction] = 1;
298 // Prepares the filter array for usage
299 function prepareFilterArray () {
300 // Abort here if array is absend (e.g. not cached)
301 if (!isset($GLOBALS['cache_array']['filter']['filter_name'])) {
307 $filterArray = array(
313 // Found in cache so rewrite the array
314 foreach ($GLOBALS['cache_array']['filter']['filter_name'] as $idx => $filterName) {
315 // Get filter function
316 $filterFunction = $GLOBALS['cache_array']['filter']['filter_function'][$idx];
318 // Add the element with mapped index
319 $filterArray['counter'][$filterName][$filterFunction] = $GLOBALS['cache_array']['filter']['filter_counter'][$idx];
320 $filterArray['loaded'][$filterName][$filterFunction] = true;
321 $filterArray['chains'][$filterName][$filterFunction] = $GLOBALS['cache_array']['filter']['filter_active'][$idx];
325 $GLOBALS['cache_array']['filter'] = $filterArray;