Added update_year.sh (still not fully flexible) and updated all years with it.
[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  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
14  * Copyright (c) 2009 - 2015 by Mailer Developer Team                   *
15  * For more information visit: http://mxchange.org                      *
16  *                                                                      *
17  * This program is free software; you can redistribute it and/or modify *
18  * it under the terms of the GNU General Public License as published by *
19  * the Free Software Foundation; either version 2 of the License, or    *
20  * (at your option) any later version.                                  *
21  *                                                                      *
22  * This program is distributed in the hope that it will be useful,      *
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
25  * GNU General Public License for more details.                         *
26  *                                                                      *
27  * You should have received a copy of the GNU General Public License    *
28  * along with this program; if not, write to the Free Software          *
29  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
30  * MA  02110-1301  USA                                                  *
31  ************************************************************************/
32
33 // Some security stuff...
34 if (!defined('__SECURITY')) {
35         die();
36 } // END - if
37
38 // Init "generic filter system"
39 function initFilterSystem () {
40         // Is the filter already initialized?
41         if (isset($GLOBALS['filter_init'])) {
42                 // Then abort here
43                 reportBug(__FUNCTION__, __LINE__, 'Filter system already initialized.');
44         } // END - if
45
46         // Load all saved filers if ext-sql_patches is updated
47         if ((isset($GLOBALS['cache_array']['filter']['filter_name'])) && (!isset($GLOBALS['cache_array']['filter']['chains']))) {
48                 // Prepare filter array
49                 prepareFilterArray();
50
51                 // Mark it as initialized
52                 $GLOBALS['filter_init'] = TRUE;
53         } elseif ((!isInstaller()) && (isExtensionInstalledAndNewer('sql_patches', '0.5.9'))) {
54                 // Init add
55                 $add = '';
56                 if (isExtensionINstalledAndNewer('sql_patches', '0.6.0')) {
57                         $add = ',`filter_counter`';
58                 } // END - if
59
60                 // Load all filters
61                 $result = sqlQuery('SELECT
62         `filter_name`,
63         `filter_function`,
64         `filter_active`
65         ' . $add . '
66 FROM
67         `{?_MYSQL_PREFIX?}_filters`
68 ORDER BY
69         `filter_id` ASC', __FUNCTION__, __LINE__);
70
71                 // Are there entries?
72                 if (!ifSqlHasZeroNums($result)) {
73                         // Load all filters
74                         while ($filterArray = sqlFetchArray($result)) {
75                                 // Get filter name and function
76                                 $filterName     = $filterArray['filter_name'];
77                                 $filterFunction = $filterArray['filter_function'];
78
79                                 // Set counter to default
80                                 $GLOBALS['cache_array']['filter']['counter'][$filterName][$filterFunction] = '0';
81
82                                 // Mark this filter as loaded (from database)
83                                 $GLOBALS['cache_array']['filter']['loaded'][$filterName][$filterFunction] = TRUE;
84
85                                 // Set this filter
86                                 $GLOBALS['cache_array']['filter']['chains'][$filterName][$filterFunction] = $filterArray['filter_active'];
87
88                                 // Is the array element for counter there?
89                                 if (isset($filterArray['filter_counter'])) {
90                                         // Then use this value!
91                                         $GLOBALS['cache_array']['filter']['counter'][$filterName][$filterFunction] = $filterArray['filter_counter'];
92                                 } // END - if
93                         } // END - while
94                 } // END - if
95
96                 // Free result
97                 sqlFreeResult($result);
98         }
99
100         // Init filters
101         registerFilter(__FUNCTION__, __LINE__, 'init', 'LOAD_CONFIGURATION');
102         registerFilter(__FUNCTION__, __LINE__, 'init', 'INIT_RANDOMIZER');
103         registerFilter(__FUNCTION__, __LINE__, 'init', 'LOAD_RUNTIME_INCLUDES');
104         registerFilter(__FUNCTION__, __LINE__, 'init', 'INIT_LANGUAGE');
105         registerFilter(__FUNCTION__, __LINE__, 'init', 'INIT_EXTENSIONS');
106         registerFilter(__FUNCTION__, __LINE__, 'init', 'INIT_SESSION');
107         registerFilter(__FUNCTION__, __LINE__, 'init', 'SET_CURRENT_DATE');
108         registerFilter(__FUNCTION__, __LINE__, 'init', 'INIT_RANDOM_NUMBER');
109         registerFilter(__FUNCTION__, __LINE__, 'init', 'RUN_HOURLY_RESET');
110         registerFilter(__FUNCTION__, __LINE__, 'init', 'RUN_DAILY_RESET');
111         registerFilter(__FUNCTION__, __LINE__, 'init', 'RUN_WEEKLY_RESET');
112         registerFilter(__FUNCTION__, __LINE__, 'init', 'RUN_MONTHLY_RESET');
113         registerFilter(__FUNCTION__, __LINE__, 'init', 'RUN_YEARLY_RESET');
114         registerFilter(__FUNCTION__, __LINE__, 'init', 'TRIGGER_SENDING_POOL');
115         // @TODO Remove this forced removal after a year or so
116         unregisterFilter(__FUNCTION__, __LINE__, 'init', 'DETERMINE_USERNAME', TRUE);
117         registerFilter(__FUNCTION__, __LINE__, 'init', 'DETERMINE_WHAT_ACTION');
118         registerFilter(__FUNCTION__, __LINE__, 'init', 'COUNT_MODULE');
119         registerFilter(__FUNCTION__, __LINE__, 'init', 'UPDATE_LOGIN_DATA');
120         registerFilter(__FUNCTION__, __LINE__, 'init', 'ACTIVATE_EXCHANGE');
121         registerFilter(__FUNCTION__, __LINE__, 'init', 'REDIRECT_WRONG_SERVER_NAME');
122
123         // Post-initialization
124         registerFilter(__FUNCTION__, __LINE__, 'post_init', 'DETERMINE_USERNAME');
125
126         // Page headers - pre-filter (normally, you want to register here)
127         registerFilter(__FUNCTION__, __LINE__, 'pre_page_header', 'LOAD_PAGE_HEADER');
128
129         // Page headers - post-filter (normally, you don't want to register here)
130         //-------------------- LAST FILTER FOR THIS CHAIN! ------------------------
131         registerFilter(__FUNCTION__, __LINE__, 'post_page_header', 'FINISH_PAGE_HEADER');
132         //-------------------- LAST FILTER FOR THIS CHAIN! ------------------------
133
134         // 'You are here' navigation - post filter
135         registerFilter(__FUNCTION__, __LINE__, 'post_youhere_line', 'CALL_HANDLER_LOGIN_FAILTURES');
136         registerFilter(__FUNCTION__, __LINE__, 'post_youhere_line', 'HANDLE_HOME_IN_INDEX_SETTING');
137
138         // Filters for pre-extension-registration
139         registerFilter(__FUNCTION__, __LINE__, 'pre_extension_installed', 'RUN_SQLS');
140
141         // Filters for post-extension-registration
142         registerFilter(__FUNCTION__, __LINE__, 'post_extension_installed', 'AUTO_ACTIVATE_EXTENSION');
143         registerFilter(__FUNCTION__, __LINE__, 'post_extension_installed', 'SOLVE_TASK');
144         registerFilter(__FUNCTION__, __LINE__, 'post_extension_installed', 'LOAD_INCLUDES');
145         registerFilter(__FUNCTION__, __LINE__, 'post_extension_installed', 'REMOVE_UPDATES');
146         registerFilter(__FUNCTION__, __LINE__, 'post_extension_installed', 'EXTENSION_MARK_INSTALLED');
147
148         // Solving tasks
149         registerFilter(__FUNCTION__, __LINE__, 'solve_task', 'SOLVE_TASK');
150
151         // Loading includes in general
152         registerFilter(__FUNCTION__, __LINE__, 'load_includes', 'LOAD_INCLUDES');
153
154         // Run SQLs
155         registerFilter(__FUNCTION__, __LINE__, 'run_sqls', 'RUN_SQLS');
156
157         // Admin ACL check
158         registerFilter(__FUNCTION__, __LINE__, 'check_admin_acl', 'CHECK_ADMIN_ACL');
159
160         // Register shutdown filters
161         registerFilter(__FUNCTION__, __LINE__, 'shutdown', 'FLUSH_FILTERS');
162         registerFilter(__FUNCTION__, __LINE__, 'shutdown', 'FLUSH_STATS');
163         registerFilter(__FUNCTION__, __LINE__, 'shutdown', 'FLUSH_TEMPLATE_CACHE');
164         registerFilter(__FUNCTION__, __LINE__, 'shutdown', 'FLUSH_OUTPUT');
165
166         // Compiling code
167         registerFilter(__FUNCTION__, __LINE__, 'compile_code', 'COMPILE_CONFIG');
168         registerFilter(__FUNCTION__, __LINE__, 'compile_code', 'COMPILE_EXPRESSION_CODE');
169
170         // Generic extension update filters
171         registerFilter(__FUNCTION__, __LINE__, 'extension_update', 'UPDATE_EXTENSION_DATA');
172
173         // Do hourly reset stuff, keep this entry first in this chain:
174         registerFilter(__FUNCTION__, __LINE__, 'hourly', 'RUN_HOURLY_INCLUDES');
175
176         // Do daily stuff, keep this entry first in this chain:
177         registerFilter(__FUNCTION__, __LINE__, 'daily', 'RUN_DAILY_INCLUDES');
178
179         // Do weekly stuff, keep this entry first in this chain:
180         registerFilter(__FUNCTION__, __LINE__, 'weekly', 'RUN_WEEKLY_INCLUDES');
181
182         // Do monthly stuff, keep this entry first in this chain:
183         registerFilter(__FUNCTION__, __LINE__, 'monthly', 'RUN_MONTHLY_INCLUDES');
184
185         // Do yearly stuff, keep this entry first in this chain:
186         registerFilter(__FUNCTION__, __LINE__, 'yearly', 'RUN_YEARLY_INCLUDES');
187
188         // Remove extension
189         registerFilter(__FUNCTION__, __LINE__, 'extension_remove', 'REMOVE_EXTENSION');
190
191         // Handling of fatal errors
192         registerFilter(__FUNCTION__, __LINE__, 'handle_fatal_errors', 'HANDLE_FATAL_ERRORS');
193
194         // Page footer filters
195         registerFilter(__FUNCTION__, __LINE__, 'page_footer', 'HANDLE_FATAL_ERRORS');
196         registerFilter(__FUNCTION__, __LINE__, 'page_footer', 'DISPLAY_COPYRIGHT');
197         registerFilter(__FUNCTION__, __LINE__, 'page_footer', 'DISPLAY_PARSING_TIME');
198
199         // Member login check. Always keep FETCH_USER_DATA as first entry!
200         registerFilter(__FUNCTION__, __LINE__, 'member_login_check', 'FETCH_USER_DATA');
201
202         // Admin login
203         registerFilter(__FUNCTION__, __LINE__, 'do_admin_login_done', 'DO_LOGIN_ADMIN');
204
205         // Admin mail links
206         registerFilter(__FUNCTION__, __LINE__, 'generate_admin_mail_links', 'GENERATE_POOL_MAIL_LINKS');
207
208         // Build mails
209         registerFilter(__FUNCTION__, __LINE__, 'send_build_mail', 'SEND_BUILD_MAIL');
210
211         // Handle referral banner click/view
212         registerFilter(__FUNCTION__, __LINE__, 'handle_click_php', 'HANDLE_REFERRER_BANNER_CLICK');
213         registerFilter(__FUNCTION__, __LINE__, 'handle_view_php', 'HANDLE_REFERRER_BANNER_VIEW');
214
215         // Generic filter to add hidden fields to formulars
216         registerFilter(__FUNCTION__, __LINE__, 'open_form_fields', 'ADD_INPUT_HIDDEN_SESSION_ID');
217 }
218
219 // "Registers" a new filter function
220 function registerFilter ($file, $line, $filterName, $filterFunction, $silentAbort = TRUE, $force = FALSE, $isDryRun = FALSE) {
221         // Extend the filter function name
222         $filterFunction = 'FILTER_' . strtoupper($filterFunction);
223
224         // Debug message with FILTER_ prefix
225         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ENTRY: filterName=' . $filterName . ',filterFunction=' . $filterFunction . ',force=' . intval($force) . ',isDryRun=' . intval($isDryRun) . ',file=' . basename($file) . ',line=' . $line . ' - ENTERED!');
226
227         // Is that filter already there?
228         if ((isset($GLOBALS['cache_array']['filter']['chains'][$filterName][$filterFunction])) && ($force === FALSE)) {
229                 // In installation phase we always want to abort
230                 if (($silentAbort === FALSE) || (isInstaller())) {
231                         // Add fatal message
232                         reportBug(__FUNCTION__, __LINE__, sprintf('Filter chain %s has already filter function %s registered! file=%s,L=%s,force=%d', $filterName, $filterFunction, basename($file), $line, intval($force)));
233                 } // END - if
234
235                 // Abort here
236                 return FALSE;
237         } // END - if
238
239         // Shall we add it?
240         if ($isDryRun === FALSE) {
241                 // Is the function there?
242                 if (!function_exists($filterFunction)) {
243                         // Then abort here
244                         logDebugMessage(__FUNCTION__, __LINE__, sprintf('Filter function %s could not be added to filter chain %s. file=%s,L=%s,force=%d', $filterFunction, $filterName, basename($file), $line, intval($force)));
245                         return FALSE;
246                 } // END - if
247
248                 // Simply add it to the array
249                 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'REGISTER: filterName=' . $filterName . ',filterFunction=' . $filterFunction . ',force=' . intval($force) . ',file=' . basename($file) . ',line=' . $line);
250                 $GLOBALS['cache_array']['filter']['chains'][$filterName][$filterFunction] = 'A';
251                 $GLOBALS['cache_array']['filter']['counter'][$filterName][$filterFunction] = '0';
252         } // END - if
253
254         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ENTRY: filterName=' . $filterName . ',filterFunction=' . $filterFunction . ',force=' . intval($force) . ',isDryRun=' . intval($isDryRun) . ',file=' . basename($file) . ',line=' . $line . ' - EXIT!');
255
256         // Worked
257         return TRUE;
258 }
259
260 // "Unregisters" a filter from the given chain
261 function unregisterFilter ($file, $line, $filterName, $filterFunction, $force = FALSE, $isDryRun = FALSE) {
262         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'file=' . $file . ',line=' . $line . ',filterName=' . $filterName . ',filterFunction=' . $filterFunction . ',force=' . intval($force) . ',isDryRun=' . intval($isDryRun) . ' - ENTERED!');
263
264         // Extend the filter function name only if not loaded from database
265         if (!isset($GLOBALS['cache_array']['filter']['loaded'][$filterName][$filterFunction])) {
266                 $filterFunction = 'FILTER_' . strtoupper($filterFunction);
267         } // END - if
268
269         // Is that filter there?
270         if ((!isset($GLOBALS['cache_array']['filter']['chains'][$filterName][$filterFunction])) && ($force === FALSE)) {
271                 // Not found, so abort here
272                 logDebugMessage(__FUNCTION__, __LINE__, sprintf('Filter function %s cannot be unregistered from filter %s.', $filterFunction, $filterName));
273                 return FALSE;
274         } // END - if
275
276         // Shall we remove? (default, not while just showing an extension removal)
277         if (($isDryRun === FALSE) && (isset($GLOBALS['cache_array']['filter']['chains'][$filterName][$filterFunction]))) {
278                 // Mark for filter removal
279                 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'file=' . $file . ',line=' . $line . ',filterName=' . $filterName . ',filterFunction=' . $filterFunction . ' - REMOVE!');
280                 $GLOBALS['cache_array']['filter']['chains'][$filterName][$filterFunction] = 'R';
281         } // END - if
282
283         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'file=' . $file . ',line=' . $line . ',filterName=' . $filterName . ',filterFunction=' . $filterFunction . ',force=' . intval($force) . ',isDryRun=' . intval($isDryRun) . ' - EXIT!');
284
285         // Worked
286         return TRUE;
287 }
288
289 // "Runs" the given filters, filterData is optional and can be any type of data
290 function runFilterChain ($filterName, $filterData = NULL) {
291         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'filterName=' . $filterName . ',filterData[]=' . gettype($filterData) . ' - ENTERED!');
292
293         // Is that filter chain there?
294         if (!isset($GLOBALS['cache_array']['filter']['chains'][$filterName])) {
295                 // Filter chain not found filters in debug-mode
296                 if (isDebugModeEnabled()) {
297                         // Log it away...
298                         logDebugMessage(__FUNCTION__, __LINE__, 'Filter chain ' . $filterName . ' does not exist.');
299                 } // END - if
300
301                 // Abort here and return content
302                 return $filterData;
303         } // END - if
304
305         // Default return value
306         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'filterName=' . $filterName . ',count()=' . count($GLOBALS['cache_array']['filter']['chains'][$filterName]));
307         $returnValue = $filterData;
308
309         // Continue filter chain is default
310         continueFilterChain();
311
312         // Then run all filters
313         foreach ($GLOBALS['cache_array']['filter']['chains'][$filterName] as $filterFunction => $active) {
314                 // Debug message
315                 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Running: name=' . $filterName . ',func=' . $filterFunction . ',active=' . $active);
316
317                 // Is the filter active or newly added??
318                 if (($active == 'Y') || ($active == 'A') || ((in_array($filterName, array('shutdown', 'extension_remove', 'post_extension_run_sql'))) && ($active == 'R'))) {
319                         // Is this filter there?
320                         if (!function_exists($filterFunction)) {
321                                 // Should be fixed
322                                 reportBug(__FUNCTION__, __LINE__, 'filterName=' . $filterName . ',filterFunction=' . $filterFunction . ',active=' . $active . ' - AUTO-UNREGISTERED!');
323                         } // END - if
324
325                         // Call the filter chain
326                         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, $filterName . '/' . $filterFunction . ',[]=' . gettype($returnValue) . ' - CALLING!');
327                         $returnValue = call_user_func_array($filterFunction, array($returnValue));
328
329                         // Update usage counter
330                         countFilterUsage($filterName, $filterFunction);
331                 } elseif (isDebugModeEnabled()) {
332                         // Debug message
333                         logDebugMessage(__FUNCTION__, __LINE__, 'Skipped: name=' . $filterName . ',func=' . $filterFunction . ',active=' . $active);
334                 }
335
336                 // Abort loop?
337                 if (isFilterChainAborted()) {
338                         // Yes, then abort here
339                         break;
340                 } // END - if
341         } // END - foreach
342
343         // Return the filtered content
344         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'filterName=' . $filterName . ',filterData[]=' . gettype($filterData) . ',returnValue[]=' . gettype($returnValue) . ' - EXIT!');
345         return $returnValue;
346 }
347
348 // Count the filter usage
349 function countFilterUsage ($filterName, $filterFunction) {
350         // Is it there?
351         if (isset($GLOBALS['cache_array']['filter']['counter'][$filterName][$filterFunction])) {
352                 // Yes, then increase
353                 $GLOBALS['cache_array']['filter']['counter'][$filterName][$filterFunction]++;
354         } else {
355                 // No, then create
356                 $GLOBALS['cache_array']['filter']['counter'][$filterName][$filterFunction] = 1;
357         }
358 }
359
360 // Prepares the filter array for usage
361 function prepareFilterArray () {
362         // Abort here if array is absend (e.g. not cached)
363         if (!isset($GLOBALS['cache_array']['filter']['filter_name'])) {
364                 // Abort silently
365                 return FALSE;
366         } // END - if
367
368         // Init dummy array
369         $filterArray = array(
370                 'chains'  => array(),
371                 'loaded'  => array(),
372                 'counter' => array()
373         );
374
375         // Found in cache so rewrite the array
376         foreach ($GLOBALS['cache_array']['filter']['filter_name'] as $idx => $filterName) {
377                 // Get filter function
378                 $filterFunction = $GLOBALS['cache_array']['filter']['filter_function'][$idx];
379
380                 // Add the element with mapped index
381                 $filterArray['counter'][$filterName][$filterFunction] = $GLOBALS['cache_array']['filter']['filter_counter'][$idx];
382                 $filterArray['loaded'][$filterName][$filterFunction]  = TRUE;
383                 $filterArray['chains'][$filterName][$filterFunction]  = $GLOBALS['cache_array']['filter']['filter_active'][$idx];
384         } // END - foreach
385
386         // Remove the cache
387         $GLOBALS['cache_array']['filter'] = $filterArray;
388 }
389
390 /**
391  * Loads filter for given extension if present. This function will silently
392  * ignore absent filter files.
393  *
394  * @param       $ext_name       Name of extension
395  * @return      void
396  */
397 function loadExtensionFilters ($ext_name) {
398         // Debug message
399         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ' - ENTERED!');
400
401         // Is there a cache entry?
402         if (!isset($GLOBALS[__FUNCTION__][$ext_name])) {
403                 // Default is not found
404                 $GLOBALS[__FUNCTION__][$ext_name] = FALSE;
405
406                 // Construct include file name
407                 $incFileName = sprintf('inc/filter/%s_filter.php', $ext_name);
408
409                 // Is the include file readable?
410                 if (isIncludeReadable($incFileName)) {
411                         // Load the include file
412                         loadIncludeOnce($incFileName);
413
414                         // Mark the file as loaded
415                         $GLOBALS[__FUNCTION__][$ext_name] = TRUE;
416                 } elseif (isDebugModeEnabled()) {
417                         // Log missing file
418                         //* NOISY-DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Filter include file ' . $incFileName . ' for extension ' . $ext_name . ' is missing.');
419                 }
420         } // END - if
421
422         // Debug message
423         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',result=' . intval($GLOBALS[__FUNCTION__][$ext_name]) . ' - EXIT!');
424 }
425
426 // Checks whether the filter chain has been aborted
427 function isFilterChainAborted () {
428         // Determine it
429         return ((isset($GLOBALS['filter_chain_interrupted'])) && ($GLOBALS['filter_chain_interrupted'] === TRUE));
430 }
431
432 // Interrupts the filter chain by enabling flag 'filter_chain_aborted'
433 function interruptFilterChain () {
434         // Set it
435         $GLOBALS['filter_chain_interrupted'] = TRUE;
436 }
437
438 // Continues the filter chain by disabling flag 'filter_chain_aborted'
439 function continueFilterChain () {
440         // Set it
441         $GLOBALS['filter_chain_interrupted'] = FALSE;
442 }
443
444 // [EOF]
445 ?>