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