Automatic deactivation of installed but deprecated extwensions 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  * -------------------------------------------------------------------- *
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                      *
21  *                                                                      *
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.                                  *
26  *                                                                      *
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.                         *
31  *                                                                      *
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,               *
35  * MA  02110-1301  USA                                                  *
36  ************************************************************************/
37
38 // Some security stuff...
39 if (!defined('__SECURITY')) {
40         die();
41 } // END - if
42
43 // Init "generic filter system"
44 function initFilterSystem () {
45         // Is the filter already initialized?
46         if (isset($GLOBALS['filter_init'])) {
47                 // Then abort here
48                 debug_report_bug(__FUNCTION__, __LINE__, 'Filter system already initialized.');
49         } // END - if
50
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
54                 prepareFilterArray();
55
56                 // Mark it as initialized
57                 $GLOBALS['filter_init'] = true;
58         } elseif ((!isInstallationPhase()) && (isExtensionInstalledAndNewer('sql_patches', '0.5.9'))) {
59                 // Init add
60                 $add = '';
61                 if (isExtensionINstalledAndNewer('sql_patches', '0.6.0')) {
62                         $add = ", `filter_counter`";
63                 } // END - if
64
65                 // Load all filters
66                 $result = SQL_QUERY('SELECT
67         `filter_name`,`filter_function`,`filter_active`' . $add . '
68 FROM
69         `{?_MYSQL_PREFIX?}_filters`
70 ORDER BY
71         `filter_id` ASC', __FUNCTION__, __LINE__);
72
73                 // Are there entries?
74                 if (!SQL_HASZERONUMS($result)) {
75                         // Load all filters
76                         while ($filterArray = SQL_FETCHARRAY($result)) {
77                                 // Get filter name and function
78                                 $filterName     = $filterArray['filter_name'];
79                                 $filterFunction = $filterArray['filter_function'];
80
81                                 // Set counter to default
82                                 $GLOBALS['cache_array']['filter']['counter'][$filterName][$filterFunction] = '0';
83
84                                 // Mark this filter as loaded (from database)
85                                 $GLOBALS['cache_array']['filter']['loaded'][$filterName][$filterFunction] = true;
86
87                                 // Set this filter
88                                 $GLOBALS['cache_array']['filter']['chains'][$filterName][$filterFunction] = $filterArray['filter_active'];
89
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'];
94                                 } // END - if
95                         } // END - while
96                 } // END - if
97
98                 // Free result
99                 SQL_FREERESULT($result);
100         }
101
102         // Init filters
103         registerFilter('init', 'LOAD_CONFIGURATION');
104         registerFilter('init', 'INIT_RANDOMIZER');
105         registerFilter('init', 'LOAD_RUNTIME_INCLUDES');
106         registerFilter('init', 'INIT_EXTENSIONS');
107         registerFilter('init', 'SET_CURRENT_DATE');
108         registerFilter('init', 'INIT_RANDOM_NUMBER');
109         registerFilter('init', 'CHECK_REPOSITORY_REVISION');
110         registerFilter('init', 'RUN_HOURLY_RESET');
111         registerFilter('init', 'RUN_DAILY_RESET');
112         registerFilter('init', 'TRIGGER_SENDING_POOL');
113         registerFilter('init', 'DETERMINE_USERNAME');
114         registerFilter('init', 'DETERMINE_WHAT_ACTION');
115         registerFilter('init', 'COUNT_MODULE');
116         registerFilter('init', 'UPDATE_LOGIN_DATA');
117         registerFilter('init', 'ACTIVATE_EXCHANGE');
118
119         // Page headers - pre-filter (normally, you want to register here)
120         registerFilter('pre_page_header', 'LOAD_PAGE_HEADER');
121
122         // Page headers - post-filter (normally, you don't want to register here)
123         //-------------------- LAST FILTER FOR THIS CHAIN! ------------------------
124         registerFilter('post_page_header', 'FINISH_PAGE_HEADER');
125         //-------------------- LAST FILTER FOR THIS CHAIN! ------------------------
126
127         // 'You are here' navigation - post filter
128         registerFilter('post_youhere_line', 'CALL_HANDLER_LOGIN_FAILTURES');
129         registerFilter('post_youhere_line', 'HANDLE_HOME_IN_INDEX_SETTING');
130
131         // Filters for pre-extension-registration
132         registerFilter('pre_extension_installed', 'RUN_SQLS');
133
134         // Filters for post-extension-registration
135         registerFilter('post_extension_installed', 'AUTO_ACTIVATE_EXTENSION');
136         registerFilter('post_extension_installed', 'SOLVE_TASK');
137         registerFilter('post_extension_installed', 'LOAD_INCLUDES');
138         registerFilter('post_extension_installed', 'REMOVE_UPDATES');
139         registerFilter('post_extension_installed', 'EXTENSION_MARK_INSTALLED');
140
141         // Solving tasks
142         registerFilter('solve_task', 'SOLVE_TASK');
143
144         // Loading includes in general
145         registerFilter('load_includes', 'LOAD_INCLUDES');
146
147         // Run SQLs
148         registerFilter('run_sqls', 'RUN_SQLS');
149
150         // Admin ACL check
151         registerFilter('check_admin_acl', 'CHECK_ADMIN_ACL');
152
153         // Register shutdown filters
154         registerFilter('shutdown', 'FLUSH_FILTERS');
155         registerFilter('shutdown', 'FLUSH_STATS');
156         registerFilter('shutdown', 'FLUSH_TEMPLATE_CACHE');
157         registerFilter('shutdown', 'FLUSH_OUTPUT');
158
159         // Compiling code
160         registerFilter('compile_code', 'COMPILE_CONFIG');
161         registerFilter('compile_code', 'COMPILE_EXPRESSION_CODE');
162
163         // Generic extension update filters
164         registerFilter('extension_update', 'UPDATE_EXTENSION_DATA');
165
166         // Do hourly reset stuff, keep this entry first in this chain:
167         registerFilter('hourly', 'RUN_HOURLY_INCLUDES');
168
169         // Do reset stuff, keep this entry first in this chain:
170         registerFilter('reset', 'RUN_RESET_INCLUDES');
171
172         // Remove extension
173         registerFilter('extension_remove', 'REMOVE_EXTENSION');
174
175         // Exclude some users
176         registerFilter('exclude_users', 'HTML_INCLUDE_USERS');
177
178         // Handling of fatal errors
179         registerFilter('handle_fatal_errors', 'HANDLE_FATAL_ERRORS');
180
181         // Page footer filters
182         registerFilter('page_footer', 'HANDLE_FATAL_ERRORS');
183         registerFilter('page_footer', 'DISPLAY_COPYRIGHT');
184         registerFilter('page_footer', 'DISPLAY_PARSING_TIME');
185
186         // Member login check. Always keep FETCH_USER_DATA as first entry!
187         registerFilter('member_login_check', 'FETCH_USER_DATA');
188
189         // Admin login
190         registerFilter('do_admin_login_done', 'DO_LOGIN_ADMIN');
191
192         // Admin mail links
193         registerFilter('generate_admin_mail_links', 'GENERATE_POOL_MAIL_LINKS');
194 }
195
196 // "Registers" a new filter function
197 function registerFilter ($filterName, $filterFunction, $silentAbort = true, $force = false, $dry_run = false) {
198         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ENTRY: filterName=' . $filterName . ',filterFunction=' . $filterFunction . ' - ENTERED!');
199
200         // Extend the filter function name
201         $filterFunction = sprintf("FILTER_%s", strtoupper($filterFunction));
202
203         // Is that filter already there?
204         if ((isset($GLOBALS['cache_array']['filter']['chains'][$filterName][$filterFunction])) && ($force === false)) {
205                 // In installation phase we always want to abort
206                 if (($silentAbort === false) || (isInstallationPhase())) {
207                         // Add fatal message
208                         debug_report_bug(__FUNCTION__, __LINE__, sprintf("Filter chain %s has already filter function %s registered!", $filterName, $filterFunction));
209                 } // END - if
210
211                 // Abort here
212                 return false;
213         } // END - if
214
215         // Shall we add it?
216         if ($dry_run === false) {
217                 // Is the function there?
218                 if (!function_exists($filterFunction)) {
219                         // Then abort here
220                         addFatalMessage(__FUNCTION__, __LINE__, sprintf("Filter function %s could not be added to filter chain %s.", $filterFunction, $filterName));
221                         return false;
222                 } // END - if
223
224                 // Simply add it to the array
225                 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'REGISTER: filterName=' . $filterName . ',filterFunction=' . $filterFunction);
226                 $GLOBALS['cache_array']['filter']['chains'][$filterName][$filterFunction] = 'A';
227                 $GLOBALS['cache_array']['filter']['counter'][$filterName][$filterFunction] = '0';
228         } // END - if
229
230         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ENTRY: filterName=' . $filterName . ',filterFunction=' . $filterFunction . ' - EXIT!');
231 }
232
233 // "Unregisters" a filter from the given chain
234 function unregisterFilter ($F, $L, $filterName, $filterFunction, $force = false, $dry_run = false) {
235         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'F=' . $F . ',L=' . $L . ',filterName=' . $filterName . ',filterFunction=' . $filterFunction . ',force=' . intval($force) . ',dry_run=' . intval($dry_run) . ' - ENTERED!');
236
237         // Extend the filter function name only if not loaded from database
238         if (!isset($GLOBALS['cache_array']['filter']['loaded'][$filterName][$filterFunction])) {
239                 $filterFunction = sprintf("FILTER_%s", strtoupper($filterFunction));
240         } // END - if
241
242         // Is that filter there?
243         if ((!isset($GLOBALS['cache_array']['filter']['chains'][$filterName][$filterFunction])) && ($force === false)) {
244                 // Not found, so abort here
245                 addFatalMessage(__FUNCTION__, __LINE__, sprintf(getMessage('FILTER_FAILED_NOT_REMOVED'), $filterFunction, $filterName));
246                 return false;
247         } // END - if
248
249         // Shall we remove? (default, not while just showing an extension removal)
250         if ($dry_run === false) {
251                 // Mark for filter removal
252                 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'F=' . $F . ',L=' . $L . ',filterName=' . $filterName . ',filterFunction=' . $filterFunction . ' - REMOVE!');
253                 $GLOBALS['cache_array']['filter']['chains'][$filterName][$filterFunction] = 'R';
254         } // END - if
255
256         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'F=' . $F . ',L=' . $L . ',filterName=' . $filterName . ',filterFunction=' . $filterFunction . ',force=' . intval($force) . ',dry_run=' . intval($dry_run) . ' - EXIT!');
257 }
258
259 // "Runs" the given filters, filterData is optional and can be any type of data
260 function runFilterChain ($filterName, $filterData = NULL) {
261         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'filterName=' . $filterName . ',filterData[]=' . gettype($filterData) . ' - ENTERED!');
262
263         // Is that filter chain there?
264         if (!isset($GLOBALS['cache_array']['filter']['chains'][$filterName])) {
265                 // Log not found filters in debug-mode
266                 if (isDebugModeEnabled()) {
267                         // Log it away...
268                         logDebugMessage(__FUNCTION__, __LINE__, 'Filter chain ' . $filterName . ' not found.');
269                 } // END - if
270
271                 // Abort here and return content
272                 return $filterData;
273         } // END - if
274
275         // Default return value
276         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'filterName=' . $filterName . ',count()=' . count($GLOBALS['cache_array']['filter']['chains'][$filterName]));
277         $returnValue = $filterData;
278
279         // Then run all filters
280         foreach ($GLOBALS['cache_array']['filter']['chains'][$filterName] as $filterFunction => $active) {
281                 // Debug message
282                 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Running: name=' . $filterName . ',func=' . $filterFunction . ',active=' . $active);
283
284                 // Is the filter active or newly added??
285                 if (($active == 'Y') || ($active == 'A') || ((in_array($filterName, array('shutdown', 'extension_remove', 'post_extension_run_sql'))) && ($active == 'R'))) {
286                         // Is this filter there?
287                         if (!function_exists($filterFunction)) {
288                                 // Should be fixed
289                                 debug_report_bug(__FUNCTION__, __LINE__, 'filterName=' . $filterName . ',filterFunction=' . $filterFunction . ' - AUTO-UNREGISTERED!');
290                         } // END - if
291
292                         // Call the filter chain
293                         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, $filterName . '/' . $filterFunction . ',[]=' . gettype($returnValue));
294                         $returnValue = call_user_func_array($filterFunction, array($returnValue));
295
296                         // Update usage counter
297                         countFilterUsage($filterName, $filterFunction);
298                 } elseif (isDebugModeEnabled()) {
299                         // Debug message
300                         logDebugMessage(__FUNCTION__, __LINE__, 'Skipped: name=' . $filterName . ',func=' . $filterFunction . ',active=' . $active);
301                 }
302         } // END - foreach
303
304         // Return the filtered content
305         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'filterName=' . $filterName . ',filterData[]=' . gettype($filterData) . ',returnValue[]=' . gettype($returnValue) . ' - EXIT!');
306         return $returnValue;
307 }
308
309 // Count the filter usage
310 function countFilterUsage ($filterName, $filterFunction) {
311         // Is it there?
312         if (isset($GLOBALS['cache_array']['filter']['counter'][$filterName][$filterFunction])) {
313                 // Yes, then increase
314                 $GLOBALS['cache_array']['filter']['counter'][$filterName][$filterFunction]++;
315         } else {
316                 // No, then create
317                 $GLOBALS['cache_array']['filter']['counter'][$filterName][$filterFunction] = 1;
318         }
319 }
320
321 // Prepares the filter array for usage
322 function prepareFilterArray () {
323         // Abort here if array is absend (e.g. not cached)
324         if (!isset($GLOBALS['cache_array']['filter']['filter_name'])) {
325                 // Abort silently
326                 return false;
327         } // END - if
328
329         // Init dummy array
330         $filterArray = array(
331                 'chains'  => array(),
332                 'loaded'  => array(),
333                 'counter' => array()
334         );
335
336         // Found in cache so rewrite the array
337         foreach ($GLOBALS['cache_array']['filter']['filter_name'] as $idx => $filterName) {
338                 // Get filter function
339                 $filterFunction = $GLOBALS['cache_array']['filter']['filter_function'][$idx];
340
341                 // Add the element with mapped index
342                 $filterArray['counter'][$filterName][$filterFunction] = $GLOBALS['cache_array']['filter']['filter_counter'][$idx];
343                 $filterArray['loaded'][$filterName][$filterFunction]  = true;
344                 $filterArray['chains'][$filterName][$filterFunction]  = $GLOBALS['cache_array']['filter']['filter_active'][$idx];
345         } // END - foreach
346
347         // Remove the cache
348         $GLOBALS['cache_array']['filter'] = $filterArray;
349 }
350
351 // Loads filter for given extension if present. This function will silently
352 // ignore absent filter files.
353 function loadExtensionFilters ($ext_name) {
354         // Do we have cache entry?
355         if (!isset($GLOBALS[__FUNCTION__][$ext_name])) {
356                 // Default is not found
357                 $GLOBALS[__FUNCTION__][$ext_name] = false;
358
359                 // Construct include file name
360                 $incFileName = sprintf("inc/filter/%s_filter.php", $ext_name);
361
362                 // Is the include file readable?
363                 if (isIncludeReadable($incFileName)) {
364                         // Load the include file
365                         loadIncludeOnce($incFileName);
366
367                         // Mark the file as loaded
368                         $GLOBALS[__FUNCTION__][$ext_name] = true;
369                 } elseif (isDebugModeEnabled()) {
370                         // Log missing file
371                         logDebugMessage(__FUNCTION__, __LINE__, 'Filter include file ' . $incFileName . ' for extension ' . $ext_name . ' is missing.');
372                 }
373         } // END - if
374 }
375
376 // [EOF]
377 ?>