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