Extension ext-repair extended, outdated calls DEBUG_LOG() fixed:
[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'])) {
54                 // Init dummy array
55                 $filterArray = array(
56                         'chains'  => array(),
57                         'loaded'  => array(),
58                         'counter' => array()
59                 );
60
61                 // Found in cache so rewrite the array
62                 foreach ($GLOBALS['cache_array']['filter']['filter_name'] as $idx => $filterName) {
63                         // Get filter function
64                         $filterFunction = $GLOBALS['cache_array']['filter']['filter_function'][$idx];
65
66                         // Add the element with mapped index
67                         $filterArray['counter'][$filterName][$filterFunction] = $GLOBALS['cache_array']['filter']['filter_counter'][$idx];
68                         $filterArray['loaded'][$filterName][$filterFunction]  = true;
69                         $filterArray['chains'][$filterName][$filterFunction]  = $GLOBALS['cache_array']['filter']['filter_active'][$idx];
70                 } // END - foreach
71
72                 // Mark it as initialized
73                 $GLOBALS['filter_init'] = true;
74
75                 // Remove the cache
76                 $GLOBALS['cache_array']['filter'] = $filterArray;
77         } elseif ((!isInstallationPhase()) && (isExtensionInstalledAndNewer('sql_patches', '0.5.9'))) {
78                 // Init add
79                 $add = '';
80                 if (getExtensionVersion('sql_patches') >= '0.6.0') $add = ", `filter_counter`";
81
82                 // Load all active filers
83                 $result = SQL_QUERY("SELECT
84         `filter_name`,`filter_function`,`filter_active`".$add."
85 FROM
86         `{?_MYSQL_PREFIX?}_filters`
87 ORDER BY
88         `filter_id` ASC", __FUNCTION__, __LINE__);
89
90                 // Are there entries?
91                 if (SQL_NUMROWS($result) > 0) {
92                         // Load all filters
93                         while ($filterArray = SQL_FETCHARRAY($result)) {
94                                 // Get filter name and function
95                                 $filterName     = $filterArray['filter_name'];
96                                 $filterFunction = $filterArray['filter_function'];
97
98                                 // Set counter to default
99                                 $GLOBALS['cache_array']['filter']['counter'][$filterName][$filterFunction] = '0';
100
101                                 // Mark this filter as loaded (from database)
102                                 $GLOBALS['cache_array']['filter']['loaded'][$filterName][$filterFunction] = true;
103
104                                 // Set this filter
105                                 $GLOBALS['cache_array']['filter']['chains'][$filterName][$filterFunction] = $filterArray['filter_active'];
106
107                                 // Is the array element for counter there?
108                                 if (isset($filterArray['filter_counter'])) {
109                                         // Then use this value!
110                                         $GLOBALS['cache_array']['filter']['counter'][$filterName][$filterFunction] = $filterArray['filter_counter'];
111                                 } // END - if
112                         } // END - while
113                 } // END - if
114
115                 // Free result
116                 SQL_FREERESULT($result);
117         }
118
119         // Init filters
120         registerFilter('init', 'LOAD_CONFIGURATION');
121         registerFilter('init', 'INIT_EXTENSIONS');
122         registerFilter('init', 'INIT_RANDOMIZER');
123         registerFilter('init', 'LOAD_RUNTIME_INCLUDES');
124         registerFilter('init', 'INIT_RANDOM_NUMBER');
125         registerFilter('init', 'CHECK_SVN_REVISION');
126         registerFilter('init', 'RUN_DAILY_RESET');
127         registerFilter('init', 'TRIGGER_SENDING_POOL');
128         registerFilter('init', 'DETERMINE_USERNAME');
129         registerFilter('init', 'DETERMINE_WHAT_ACTION');
130         registerFilter('init', 'COUNT_MODULE');
131         registerFilter('init', 'UPDATE_LOGIN_DATA');
132         registerFilter('init', 'ACTIVATE_EXCHANGE');
133
134         // Login failures handler
135         registerFilter('post_youhere_line', 'CALL_HANDLER_LOGIN_FAILTURES');
136
137         // Filters for pre-extension-registration
138         registerFilter('pre_extension_installed', 'RUN_SQLS');
139
140         // Filters for post-extension-registration
141         registerFilter('post_extension_installed', 'AUTO_ACTIVATE_EXTENSION');
142         registerFilter('post_extension_installed', 'SOLVE_TASK');
143         registerFilter('post_extension_installed', 'LOAD_INCLUDES');
144         registerFilter('post_extension_installed', 'REMOVE_UPDATES');
145
146         // Solving tasks
147         registerFilter('solve_task', 'SOLVE_TASK');
148
149         // Loading includes in general
150         registerFilter('load_includes', 'LOAD_INCLUDES');
151
152         // Run SQLs
153         registerFilter('run_sqls', 'RUN_SQLS');
154
155         // Admin ACL check
156         registerFilter('check_admin_acl', 'CHECK_ADMIN_ACL');
157
158         // Register shutdown filters
159         registerFilter('shutdown', 'FLUSH_FILTERS');
160         registerFilter('shutdown', 'FLUSH_STATS');
161         registerFilter('shutdown', 'FLUSH_TEMPLATE_CACHE');
162         registerFilter('shutdown', 'FLUSH_OUTPUT');
163
164         // Compiling code
165         registerFilter('compile_code', 'COMPILE_CONFIG');
166         registerFilter('compile_code', 'COMPILE_EXTENSION');
167
168         // Generic extension update filters
169         registerFilter('extension_update', 'UPDATE_EXTENSION_DATA');
170
171         // Do reset stuff, keep this entry first in this chain:
172         registerFilter('reset', 'RUN_RESET_INCLUDES');
173
174         // Remove extension
175         registerFilter('extension_remove', 'REMOVE_EXTENSION');
176
177         // Exclude some users
178         registerFilter('exclude_users', 'HTML_INCLUDE_USERS');
179
180         // Handling of fatal errors
181         registerFilter('handle_fatal_errors', 'HANDLE_FATAL_ERRORS');
182
183         // Page footer filters
184         registerFilter('page_footer', 'HANDLE_FATAL_ERRORS');
185         registerFilter('page_footer', 'DISPLAY_COPYRIGHT');
186         registerFilter('page_footer', 'DISPLAY_PARSING_TIME');
187
188         // Member login check. Always keep FETCH_USER_DATA as first entry!
189         registerFilter('member_login_check', 'FETCH_USER_DATA');
190 }
191
192 // "Registers" a new filter function
193 function registerFilter ($filterName, $filterFunction, $silentAbort = true, $force = false, $dry_run = false) {
194         // Extend the filter function name
195         $filterFunction = sprintf("FILTER_%s", strtoupper($filterFunction));
196
197         // Is that filter already there?
198         if ((isset($GLOBALS['cache_array']['filter']['chains'][$filterName][$filterFunction])) && ($force === false)) {
199                 // Then abort here
200                 if ($silentAbort === false) {
201                         // Add fatal message
202                         addFatalMessage(__FUNCTION__, __LINE__, sprintf("Filter chain %s has already filter function %s registered!", $filterName, $filterFunction));
203                 } // END - if
204
205                 // Abort here
206                 return false;
207         } // END - if
208
209         // Shall we add it?
210         if ($dry_run === false) {
211                 // Is the function there?
212                 if (!function_exists($filterFunction)) {
213                         // Then abort here
214                         addFatalMessage(__FUNCTION__, __LINE__, sprintf("Filter function %s could not be added to filter chain %s.", $filterFunction, $filterName));
215                         return false;
216                 } // END - if
217
218                 // Simply add it to the array
219                 //* DEBUG: */ print __FUNCTION__.': filterName='.$filterName.',filterFunction='.$filterFunction.'<br />';
220                 $GLOBALS['cache_array']['filter']['chains'][$filterName][$filterFunction] = 'A';
221                 $GLOBALS['cache_array']['filter']['counter'][$filterName][$filterFunction] = '0';
222         } // END - if
223 }
224
225 // "Unregisters" a filter from the given chain
226 function unregisterFilter ($filterName, $filterFunction, $force = false, $dry_run = false) {
227         // Extend the filter function name only if not loaded from database
228         if (!isset($GLOBALS['cache_array']['filter']['loaded'][$filterName][$filterFunction])) {
229                 $filterFunction = sprintf("FILTER_%s", strtoupper($filterFunction));
230         } // END - if
231
232         // Is that filter there?
233         if ((!isset($GLOBALS['cache_array']['filter']['chains'][$filterName][$filterFunction])) && ($force === false)) {
234                 // Not found, so abort here
235                 addFatalMessage(__FUNCTION__, __LINE__, getMessage('FILTER_FAILED_NOT_REMOVED'), array($filterFunction, $filterName));
236                 return false;
237         } // END - if
238
239         // Shall we remove? (default, not while just showing an extension removal)
240         if ($dry_run === false) {
241                 // Mark for filter removal
242                 //* DEBUG: */ print __FUNCTION__.': filterName='.$filterName.',filterFunction='.$filterFunction.'<br />';
243                 $GLOBALS['cache_array']['filter']['chains'][$filterName][$filterFunction] = 'R';
244         } // END - if
245 }
246
247 // "Runs" the given filters, data is optional and can be any type of data
248 function runFilterChain ($filterName, $data = null) {
249         // Is that filter chain there?
250         if (!isset($GLOBALS['cache_array']['filter']['chains'][$filterName])) {
251                 // We should find all these non-existing filter chains
252                 //* Only for tracking: */ if ($filterName != 'sql_admin_extra_data') {
253                 //* Only for tracking: */ debug_report_bug('Filter chain <strong>' . $filterName . '</strong> not found!');
254                 //* Only for tracking: */ }
255                 /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Filter chain ' . $filterName . ' not found!');
256
257                 // Abort here and return content
258                 return $data;
259         } // END - if
260
261         // Default return value
262         $returnValue = $data;
263
264         // Then run all filters
265         foreach ($GLOBALS['cache_array']['filter']['chains'][$filterName] as $filterFunction => $active) {
266                 // Debug message
267                 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "Running: name={$filterName},func={$filterFunction},active={$active}");
268
269                 // Is the filter active?
270                 if (($active == 'Y') || ($active == 'A') || ((in_array($filterName, array('shutdown','extension_remove','post_extension_run_sql'))) && ($active == 'R'))) {
271                         // Is this filter there?
272                         if (!function_exists($filterFunction)) {
273                                 // Unregister it
274                                 unregisterFilter($filterName, $filterFunction);
275
276                                 // Skip this entry
277                                 continue;
278                         } // END - if
279
280                         // Call the filter chain
281                         //* DEBUG: */ print $filterName.'/'.$filterFunction.',[]='.gettype($returnValue).'<br />';
282                         $returnValue = call_user_func_array($filterFunction, array($returnValue));
283
284                         // Update usage counter
285                         countFilterUsage($filterName, $filterFunction);
286                 } elseif (isDebugModeEnabled()) {
287                         // Debug message
288                         logDebugMessage(__FUNCTION__, __LINE__, "Skipped: name={$filterName},func={$filterFunction},active={$active}");
289                 }
290         } // END - foreach
291
292         // Return the filtered content
293         return $returnValue;
294 }
295
296 // Count the filter usage
297 function countFilterUsage ($filterName, $filterFunction) {
298         // Is it there?
299         if (isset($GLOBALS['cache_array']['filter']['counter'][$filterName][$filterFunction])) {
300                 // Yes, then increase
301                 $GLOBALS['cache_array']['filter']['counter'][$filterName][$filterFunction]++;
302         } else {
303                 // No, then create
304                 $GLOBALS['cache_array']['filter']['counter'][$filterName][$filterFunction] = 1;
305         }
306 }
307
308 // [EOF]
309 ?>