Renamed templates + added some filters for mail order
[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 - 2013 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 ext-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 ((!isInstaller()) && (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 = sqlQuery('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 (!ifSqlHasZeroNums($result)) {
78                         // Load all filters
79                         while ($filterArray = sqlFetchArray($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                 sqlFreeResult($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', 'RUN_WEEKLY_RESET');
117         registerFilter(__FUNCTION__, __LINE__, 'init', 'RUN_MONTHLY_RESET');
118         registerFilter(__FUNCTION__, __LINE__, 'init', 'RUN_YEARLY_RESET');
119         registerFilter(__FUNCTION__, __LINE__, 'init', 'TRIGGER_SENDING_POOL');
120         // @TODO Remove this forced removal after a year or so
121         unregisterFilter(__FUNCTION__, __LINE__, 'init', 'DETERMINE_USERNAME', TRUE);
122         registerFilter(__FUNCTION__, __LINE__, 'init', 'DETERMINE_WHAT_ACTION');
123         registerFilter(__FUNCTION__, __LINE__, 'init', 'COUNT_MODULE');
124         registerFilter(__FUNCTION__, __LINE__, 'init', 'UPDATE_LOGIN_DATA');
125         registerFilter(__FUNCTION__, __LINE__, 'init', 'ACTIVATE_EXCHANGE');
126         registerFilter(__FUNCTION__, __LINE__, 'init', 'REDIRECT_WRONG_SERVER_NAME');
127
128         // Post-initialization
129         registerFilter(__FUNCTION__, __LINE__, 'post_init', 'DETERMINE_USERNAME');
130
131         // Page headers - pre-filter (normally, you want to register here)
132         registerFilter(__FUNCTION__, __LINE__, 'pre_page_header', 'LOAD_PAGE_HEADER');
133
134         // Page headers - post-filter (normally, you don't want to register here)
135         //-------------------- LAST FILTER FOR THIS CHAIN! ------------------------
136         registerFilter(__FUNCTION__, __LINE__, 'post_page_header', 'FINISH_PAGE_HEADER');
137         //-------------------- LAST FILTER FOR THIS CHAIN! ------------------------
138
139         // 'You are here' navigation - post filter
140         registerFilter(__FUNCTION__, __LINE__, 'post_youhere_line', 'CALL_HANDLER_LOGIN_FAILTURES');
141         registerFilter(__FUNCTION__, __LINE__, 'post_youhere_line', 'HANDLE_HOME_IN_INDEX_SETTING');
142
143         // Filters for pre-extension-registration
144         registerFilter(__FUNCTION__, __LINE__, 'pre_extension_installed', 'RUN_SQLS');
145
146         // Filters for post-extension-registration
147         registerFilter(__FUNCTION__, __LINE__, 'post_extension_installed', 'AUTO_ACTIVATE_EXTENSION');
148         registerFilter(__FUNCTION__, __LINE__, 'post_extension_installed', 'SOLVE_TASK');
149         registerFilter(__FUNCTION__, __LINE__, 'post_extension_installed', 'LOAD_INCLUDES');
150         registerFilter(__FUNCTION__, __LINE__, 'post_extension_installed', 'REMOVE_UPDATES');
151         registerFilter(__FUNCTION__, __LINE__, 'post_extension_installed', 'EXTENSION_MARK_INSTALLED');
152
153         // Solving tasks
154         registerFilter(__FUNCTION__, __LINE__, 'solve_task', 'SOLVE_TASK');
155
156         // Loading includes in general
157         registerFilter(__FUNCTION__, __LINE__, 'load_includes', 'LOAD_INCLUDES');
158
159         // Run SQLs
160         registerFilter(__FUNCTION__, __LINE__, 'run_sqls', 'RUN_SQLS');
161
162         // Admin ACL check
163         registerFilter(__FUNCTION__, __LINE__, 'check_admin_acl', 'CHECK_ADMIN_ACL');
164
165         // Register shutdown filters
166         registerFilter(__FUNCTION__, __LINE__, 'shutdown', 'FLUSH_FILTERS');
167         registerFilter(__FUNCTION__, __LINE__, 'shutdown', 'FLUSH_STATS');
168         registerFilter(__FUNCTION__, __LINE__, 'shutdown', 'FLUSH_TEMPLATE_CACHE');
169         registerFilter(__FUNCTION__, __LINE__, 'shutdown', 'FLUSH_OUTPUT');
170
171         // Compiling code
172         registerFilter(__FUNCTION__, __LINE__, 'compile_code', 'COMPILE_CONFIG');
173         registerFilter(__FUNCTION__, __LINE__, 'compile_code', 'COMPILE_EXPRESSION_CODE');
174
175         // Generic extension update filters
176         registerFilter(__FUNCTION__, __LINE__, 'extension_update', 'UPDATE_EXTENSION_DATA');
177
178         // Do hourly reset stuff, keep this entry first in this chain:
179         registerFilter(__FUNCTION__, __LINE__, 'hourly', 'RUN_HOURLY_INCLUDES');
180
181         // Do daily stuff, keep this entry first in this chain:
182         registerFilter(__FUNCTION__, __LINE__, 'daily', 'RUN_DAILY_INCLUDES');
183
184         // Do weekly stuff, keep this entry first in this chain:
185         registerFilter(__FUNCTION__, __LINE__, 'weekly', 'RUN_WEEKLY_INCLUDES');
186
187         // Do monthly stuff, keep this entry first in this chain:
188         registerFilter(__FUNCTION__, __LINE__, 'monthly', 'RUN_MONTHLY_INCLUDES');
189
190         // Do yearly stuff, keep this entry first in this chain:
191         registerFilter(__FUNCTION__, __LINE__, 'yearly', 'RUN_YEARLY_INCLUDES');
192
193         // Remove extension
194         registerFilter(__FUNCTION__, __LINE__, 'extension_remove', 'REMOVE_EXTENSION');
195
196         // Handling of fatal errors
197         registerFilter(__FUNCTION__, __LINE__, 'handle_fatal_errors', 'HANDLE_FATAL_ERRORS');
198
199         // Page footer filters
200         registerFilter(__FUNCTION__, __LINE__, 'page_footer', 'HANDLE_FATAL_ERRORS');
201         registerFilter(__FUNCTION__, __LINE__, 'page_footer', 'DISPLAY_COPYRIGHT');
202         registerFilter(__FUNCTION__, __LINE__, 'page_footer', 'DISPLAY_PARSING_TIME');
203
204         // Member login check. Always keep FETCH_USER_DATA as first entry!
205         registerFilter(__FUNCTION__, __LINE__, 'member_login_check', 'FETCH_USER_DATA');
206
207         // Admin login
208         registerFilter(__FUNCTION__, __LINE__, 'do_admin_login_done', 'DO_LOGIN_ADMIN');
209
210         // Admin mail links
211         registerFilter(__FUNCTION__, __LINE__, 'generate_admin_mail_links', 'GENERATE_POOL_MAIL_LINKS');
212
213         // Build mails
214         registerFilter(__FUNCTION__, __LINE__, 'send_build_mail', 'SEND_BUILD_MAIL');
215
216         // Handle referral banner click/view
217         registerFilter(__FUNCTION__, __LINE__, 'handle_click_php', 'HANDLE_REFERRER_BANNER_CLICK');
218         registerFilter(__FUNCTION__, __LINE__, 'handle_view_php', 'HANDLE_REFERRER_BANNER_VIEW');
219
220         // Generic filter to add hidden fields to formulars
221         registerFilter(__FUNCTION__, __LINE__, 'open_form_fields', 'ADD_INPUT_HIDDEN_SESSION_ID');
222 }
223
224 // "Registers" a new filter function
225 function registerFilter ($file, $line, $filterName, $filterFunction, $silentAbort = TRUE, $force = FALSE, $isDryRun = FALSE) {
226         // Extend the filter function name
227         $filterFunction = 'FILTER_' . strtoupper($filterFunction);
228
229         // Debug message with FILTER_ prefix
230         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ENTRY: filterName=' . $filterName . ',filterFunction=' . $filterFunction . ',force=' . intval($force) . ',isDryRun=' . intval($isDryRun) . ',file=' . basename($file) . ',line=' . $line . ' - ENTERED!');
231
232         // Is that filter already there?
233         if ((isset($GLOBALS['cache_array']['filter']['chains'][$filterName][$filterFunction])) && ($force === FALSE)) {
234                 // In installation phase we always want to abort
235                 if (($silentAbort === FALSE) || (isInstaller())) {
236                         // Add fatal message
237                         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)));
238                 } // END - if
239
240                 // Abort here
241                 return FALSE;
242         } // END - if
243
244         // Shall we add it?
245         if ($isDryRun === FALSE) {
246                 // Is the function there?
247                 if (!function_exists($filterFunction)) {
248                         // Then abort here
249                         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)));
250                         return FALSE;
251                 } // END - if
252
253                 // Simply add it to the array
254                 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'REGISTER: filterName=' . $filterName . ',filterFunction=' . $filterFunction . ',force=' . intval($force) . ',file=' . basename($file) . ',line=' . $line);
255                 $GLOBALS['cache_array']['filter']['chains'][$filterName][$filterFunction] = 'A';
256                 $GLOBALS['cache_array']['filter']['counter'][$filterName][$filterFunction] = '0';
257         } // END - if
258
259         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ENTRY: filterName=' . $filterName . ',filterFunction=' . $filterFunction . ',force=' . intval($force) . ',isDryRun=' . intval($isDryRun) . ',file=' . basename($file) . ',line=' . $line . ' - EXIT!');
260
261         // Worked
262         return TRUE;
263 }
264
265 // "Unregisters" a filter from the given chain
266 function unregisterFilter ($file, $line, $filterName, $filterFunction, $force = FALSE, $isDryRun = FALSE) {
267         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'file=' . $file . ',line=' . $line . ',filterName=' . $filterName . ',filterFunction=' . $filterFunction . ',force=' . intval($force) . ',isDryRun=' . intval($isDryRun) . ' - ENTERED!');
268
269         // Extend the filter function name only if not loaded from database
270         if (!isset($GLOBALS['cache_array']['filter']['loaded'][$filterName][$filterFunction])) {
271                 $filterFunction = 'FILTER_' . strtoupper($filterFunction);
272         } // END - if
273
274         // Is that filter there?
275         if ((!isset($GLOBALS['cache_array']['filter']['chains'][$filterName][$filterFunction])) && ($force === FALSE)) {
276                 // Not found, so abort here
277                 logDebugMessage(__FUNCTION__, __LINE__, sprintf('Filter function %s cannot be unregistered from filter %s.', $filterFunction, $filterName));
278                 return FALSE;
279         } // END - if
280
281         // Shall we remove? (default, not while just showing an extension removal)
282         if (($isDryRun === FALSE) && (isset($GLOBALS['cache_array']['filter']['chains'][$filterName][$filterFunction]))) {
283                 // Mark for filter removal
284                 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'file=' . $file . ',line=' . $line . ',filterName=' . $filterName . ',filterFunction=' . $filterFunction . ' - REMOVE!');
285                 $GLOBALS['cache_array']['filter']['chains'][$filterName][$filterFunction] = 'R';
286         } // END - if
287
288         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'file=' . $file . ',line=' . $line . ',filterName=' . $filterName . ',filterFunction=' . $filterFunction . ',force=' . intval($force) . ',isDryRun=' . intval($isDryRun) . ' - EXIT!');
289
290         // Worked
291         return TRUE;
292 }
293
294 // "Runs" the given filters, filterData is optional and can be any type of data
295 function runFilterChain ($filterName, $filterData = NULL) {
296         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'filterName=' . $filterName . ',filterData[]=' . gettype($filterData) . ' - ENTERED!');
297
298         // Is that filter chain there?
299         if (!isset($GLOBALS['cache_array']['filter']['chains'][$filterName])) {
300                 // Log not found filters in debug-mode
301                 if (isDebugModeEnabled()) {
302                         // Log it away...
303                         logDebugMessage(__FUNCTION__, __LINE__, 'Filter chain ' . $filterName . ' does not exist.');
304                 } // END - if
305
306                 // Abort here and return content
307                 return $filterData;
308         } // END - if
309
310         // Default return value
311         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'filterName=' . $filterName . ',count()=' . count($GLOBALS['cache_array']['filter']['chains'][$filterName]));
312         $returnValue = $filterData;
313
314         // Remove any existing flag(s)
315         unset($GLOBALS['filter_chain_aborted']);
316
317         // Then run all filters
318         foreach ($GLOBALS['cache_array']['filter']['chains'][$filterName] as $filterFunction => $active) {
319                 // Debug message
320                 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Running: name=' . $filterName . ',func=' . $filterFunction . ',active=' . $active);
321
322                 // Is the filter active or newly added??
323                 if (($active == 'Y') || ($active == 'A') || ((in_array($filterName, array('shutdown', 'extension_remove', 'post_extension_run_sql'))) && ($active == 'R'))) {
324                         // Is this filter there?
325                         if (!function_exists($filterFunction)) {
326                                 // Should be fixed
327                                 reportBug(__FUNCTION__, __LINE__, 'filterName=' . $filterName . ',filterFunction=' . $filterFunction . ',active=' . $active . ' - AUTO-UNREGISTERED!');
328                         } // END - if
329
330                         // Call the filter chain
331                         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, $filterName . '/' . $filterFunction . ',[]=' . gettype($returnValue) . ' - CALLING!');
332                         $returnValue = call_user_func_array($filterFunction, array($returnValue));
333
334                         // Update usage counter
335                         countFilterUsage($filterName, $filterFunction);
336                 } elseif (isDebugModeEnabled()) {
337                         // Debug message
338                         logDebugMessage(__FUNCTION__, __LINE__, 'Skipped: name=' . $filterName . ',func=' . $filterFunction . ',active=' . $active);
339                 }
340
341                 // Abort loop?
342                 if (isFilterChainAborted()) {
343                         // Yes, then abort here
344                         break;
345                 } // END - if
346         } // END - foreach
347
348         // Return the filtered content
349         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'filterName=' . $filterName . ',filterData[]=' . gettype($filterData) . ',returnValue[]=' . gettype($returnValue) . ' - EXIT!');
350         return $returnValue;
351 }
352
353 // Count the filter usage
354 function countFilterUsage ($filterName, $filterFunction) {
355         // Is it there?
356         if (isset($GLOBALS['cache_array']['filter']['counter'][$filterName][$filterFunction])) {
357                 // Yes, then increase
358                 $GLOBALS['cache_array']['filter']['counter'][$filterName][$filterFunction]++;
359         } else {
360                 // No, then create
361                 $GLOBALS['cache_array']['filter']['counter'][$filterName][$filterFunction] = 1;
362         }
363 }
364
365 // Prepares the filter array for usage
366 function prepareFilterArray () {
367         // Abort here if array is absend (e.g. not cached)
368         if (!isset($GLOBALS['cache_array']['filter']['filter_name'])) {
369                 // Abort silently
370                 return FALSE;
371         } // END - if
372
373         // Init dummy array
374         $filterArray = array(
375                 'chains'  => array(),
376                 'loaded'  => array(),
377                 'counter' => array()
378         );
379
380         // Found in cache so rewrite the array
381         foreach ($GLOBALS['cache_array']['filter']['filter_name'] as $idx => $filterName) {
382                 // Get filter function
383                 $filterFunction = $GLOBALS['cache_array']['filter']['filter_function'][$idx];
384
385                 // Add the element with mapped index
386                 $filterArray['counter'][$filterName][$filterFunction] = $GLOBALS['cache_array']['filter']['filter_counter'][$idx];
387                 $filterArray['loaded'][$filterName][$filterFunction]  = TRUE;
388                 $filterArray['chains'][$filterName][$filterFunction]  = $GLOBALS['cache_array']['filter']['filter_active'][$idx];
389         } // END - foreach
390
391         // Remove the cache
392         $GLOBALS['cache_array']['filter'] = $filterArray;
393 }
394
395 /**
396  * Loads filter for given extension if present. This function will silently
397  * ignore absent filter files.
398  *
399  * @param       $ext_name       Name of extension
400  * @return      void
401  */
402 function loadExtensionFilters ($ext_name) {
403         // Debug message
404         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ' - ENTERED!');
405
406         // Is there a cache entry?
407         if (!isset($GLOBALS[__FUNCTION__][$ext_name])) {
408                 // Default is not found
409                 $GLOBALS[__FUNCTION__][$ext_name] = FALSE;
410
411                 // Construct include file name
412                 $incFileName = sprintf('inc/filter/%s_filter.php', $ext_name);
413
414                 // Is the include file readable?
415                 if (isIncludeReadable($incFileName)) {
416                         // Load the include file
417                         loadIncludeOnce($incFileName);
418
419                         // Mark the file as loaded
420                         $GLOBALS[__FUNCTION__][$ext_name] = TRUE;
421                 } elseif (isDebugModeEnabled()) {
422                         // Log missing file
423                         //* NOISY-DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Filter include file ' . $incFileName . ' for extension ' . $ext_name . ' is missing.');
424                 }
425         } // END - if
426
427         // Debug message
428         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'ext_name=' . $ext_name . ',result=' . intval($GLOBALS[__FUNCTION__][$ext_name]) . ' - EXIT!');
429 }
430
431 // Checks whether the filter chain has been aborted
432 function isFilterChainAborted () {
433         // Determine it
434         return ((isset($GLOBALS['filter_chain_aborted'])) && ($GLOBALS['filter_chain_aborted'] === TRUE));
435 }
436
437 // [EOF]
438 ?>