Several fixes for guest menu system and template engine
[mailer.git] / inc / filter-functions.php
1 <?php
2 /************************************************************************
3  * MXChange v0.2.1                                    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:: 1185                                                   $ *
14  * $Date:: 2009-10-11 04:16:39 +0200 (Sun, 11 Oct 2009)               $ *
15  * $Tag:: 0.2.1-FINAL                                                 $ *
16  * $Author:: quix0r                                                   $ *
17  * Needs to be in all Files and every File needs "svn propset           *
18  * svn:keywords Date Revision" (autoprobset!) at least!!!!!!            *
19  * -------------------------------------------------------------------- *
20  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
21  * For more information visit: http://www.mxchange.org                  *
22  *                                                                      *
23  * This program is free software; you can redistribute it and/or modify *
24  * it under the terms of the GNU General Public License as published by *
25  * the Free Software Foundation; either version 2 of the License, or    *
26  * (at your option) any later version.                                  *
27  *                                                                      *
28  * This program is distributed in the hope that it will be useful,      *
29  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
30  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
31  * GNU General Public License for more details.                         *
32  *                                                                      *
33  * You should have received a copy of the GNU General Public License    *
34  * along with this program; if not, write to the Free Software          *
35  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
36  * MA  02110-1301  USA                                                  *
37  ************************************************************************/
38
39 // Some security stuff...
40 if (!defined('__SECURITY')) {
41         die();
42 } // END - if
43
44 // Init "generic filter system"
45 function initFilterSystem () {
46         // Is the filter already initialized?
47         if (isset($GLOBALS['filters'])) {
48                 // Then abort here
49                 debug_report_bug(getMessage('FILTER_FAILED_ALREADY_INIT'));
50         } // END - if
51
52         // Load all saved filers if sql_patches is updated
53         if (isset($GLOBALS['cache_array']['filter']['filter_name'])) {
54                 // Init dummy array
55                 $filterArray = array(
56                         'chains'  => array(),
57                         'loaded'  => array(),
58                         'counter' => array()
59                 );
60
61                 // Found in cache so rewrite the array
62                 foreach ($GLOBALS['cache_array']['filter']['filter_name'] as $idx => $filterName) {
63                         // Get filter function
64                         $filterFunction = $GLOBALS['cache_array']['filter']['filter_function'][$idx];
65
66                         // Add the element with mapped index
67                         $filterArray['counter'][$filterName][$filterFunction] = $GLOBALS['cache_array']['filter']['filter_counter'][$idx];
68                         $filterArray['loaded'][$filterName][$filterFunction]  = true;
69                         $filterArray['chains'][$filterName][$filterFunction]  = $GLOBALS['cache_array']['filter']['filter_active'][$idx];
70                 } // END - foreach
71
72                 // Remove the cache
73                 $GLOBALS['cache_array']['filter'] = $filterArray;
74         } elseif ((!isInstallationPhase()) && (isExtensionInstalledAndNewer('sql_patches', '0.5.9'))) {
75                 // Init add
76                 $add = '';
77                 if (getExtensionVersion('sql_patches') >= '0.6.0') $add = ", `filter_counter`";
78
79                 // Load all active filers
80                 $result = SQL_QUERY("SELECT
81         `filter_name`,`filter_function`,`filter_active`".$add."
82 FROM
83         `{?_MYSQL_PREFIX?}_filters`
84 ORDER BY
85         `filter_id` ASC", __FUNCTION__, __LINE__);
86
87                 // Are there entries?
88                 if (SQL_NUMROWS($result) > 0) {
89                         // Load all filters
90                         while ($filterArray = SQL_FETCHARRAY($result)) {
91                                 // Get filter name and function
92                                 $filterName     = $filterArray['filter_name'];
93                                 $filterFunction = $filterArray['filter_function'];
94
95                                 // Set counter to default
96                                 $GLOBALS['cache_array']['filter']['counter'][$filterName][$filterFunction] = 0;
97
98                                 // Mark this filter as loaded (from database)
99                                 $GLOBALS['cache_array']['filter']['loaded'][$filterName][$filterFunction] = true;
100
101                                 // Set this filter
102                                 $GLOBALS['cache_array']['filter']['chains'][$filterName][$filterFunction] = $filterArray['filter_active'];
103
104                                 // Is the array element for counter there?
105                                 if (isset($filterArray['filter_counter'])) {
106                                         // Then use this value!
107                                         $GLOBALS['cache_array']['filter']['counter'][$filterName][$filterFunction] = $filterArray['filter_counter'];
108                                 } // END - if
109                         } // END - while
110                 } // END - if
111
112                 // Free result
113                 SQL_FREERESULT($result);
114         }
115
116         // Init filters
117         registerFilter('init', 'LOAD_CONFIGURATION');
118         registerFilter('init', 'INIT_EXTENSIONS');
119         registerFilter('init', 'LOAD_RUNTIME_INCLUDES');
120         registerFilter('init', 'CHECK_SVN_REVISION');
121         registerFilter('init', 'RUN_DAILY_RESET');
122         registerFilter('init', 'INIT_RANDOMIZER');
123         registerFilter('init', 'TRIGGER_SENDING_POOL');
124         registerFilter('init', 'DETERMINE_USERNAME');
125         registerFilter('init', 'DETERMINE_WHAT_ACTION');
126         registerFilter('init', 'UPDATE_LOGIN_DATA');
127         registerFilter('init', 'ACTIVATE_EXCHANGE');
128
129         // Login failures handler
130         registerFilter('post_youhere_line', 'CALL_HANDLER_LOGIN_FAILTURES');
131
132         // Filters for pre-extension-registration
133         registerFilter('pre_extension_installed', 'RUN_SQLS');
134
135         // Filters for post-extension-registration
136         registerFilter('post_extension_installed', 'AUTO_ACTIVATE_EXTENSION');
137         registerFilter('post_extension_installed', 'SOLVE_TASK');
138         registerFilter('post_extension_installed', 'LOAD_INCLUDES');
139         registerFilter('post_extension_installed', 'REMOVE_UPDATES');
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_OUTPUT');
157
158         // Compiling code
159         registerFilter('compile_code', 'COMPILE_CONFIG');
160         registerFilter('compile_code', 'COMPILE_EXTENSION');
161
162         // Generic extension update filters
163         registerFilter('extension_update', 'UPDATE_EXTENSION_DATA');
164
165         // Do reset stuff, keep this entry first in this chain:
166         registerFilter('reset', 'RUN_RESET_INCLUDES');
167
168         // Remove extension
169         registerFilter('extension_remove', 'REMOVE_EXTENSION');
170
171         // Exclude some users
172         registerFilter('exclude_users', 'HTML_INCLUDE_USERS');
173 }
174
175 // "Registers" a new filter function
176 function registerFilter ($filterName, $filterFunction, $silentAbort = true, $force = false, $dry_run = false) {
177         // Extend the filter function name
178         $filterFunction = sprintf("FILTER_%s", strtoupper($filterFunction));
179
180         // Is that filter already there?
181         if ((isset($GLOBALS['cache_array']['filter']['chains'][$filterName][$filterFunction])) && ($force === false)) {
182                 // Then abort here
183                 if ($silentAbort === false) {
184                         addFatalMessage(__FUNCTION__, __LINE__, getMessage('FILTER_FAILED_ALREADY_ADDED'), array($filterFunction, $filterName));
185                 } // END - if
186
187                 // Abort here
188                 return false;
189         } // END - if
190
191         // Shall we add it?
192         if ($dry_run === false) {
193                 // Is the function there?
194                 if (!function_exists($filterFunction)) {
195                         // Then abort here
196                         addFatalMessage(__FUNCTION__, __LINE__, sprintf(getMessage('FILTER_FAILED_404'), $filterFunction, $filterName));
197                         return false;
198                 } // END - if
199
200                 // Simply add it to the array
201                 //* DEBUG: */ print __FUNCTION__.': filterName='.$filterName.',filterFunction='.$filterFunction.'<br />';
202                 $GLOBALS['cache_array']['filter']['chains'][$filterName][$filterFunction] = 'A';
203                 $GLOBALS['cache_array']['filter']['counter'][$filterName][$filterFunction] = 0;
204         } // END - if
205 }
206
207 // "Unregisters" a filter from the given chain
208 function unregisterFilter ($filterName, $filterFunction, $force = false, $dry_run = false) {
209         // Extend the filter function name only if not loaded from database
210         if (!isset($GLOBALS['cache_array']['filter']['loaded'][$filterName][$filterFunction])) {
211                 $filterFunction = sprintf("FILTER_%s", strtoupper($filterFunction));
212         } // END - if
213
214         // Is that filter there?
215         if ((!isset($GLOBALS['cache_array']['filter']['chains'][$filterName][$filterFunction])) && ($force === false)) {
216                 // Not found, so abort here
217                 addFatalMessage(__FUNCTION__, __LINE__, getMessage('FILTER_FAILED_NOT_REMOVED'), array($filterFunction, $filterName));
218                 return false;
219         } // END - if
220
221         // Shall we remove? (default, not while just showing an extension removal)
222         if ($dry_run === false) {
223                 // Mark for filter removal
224                 //* DEBUG: */ print __FUNCTION__.': filterName='.$filterName.',filterFunction='.$filterFunction.'<br />';
225                 $GLOBALS['cache_array']['filter']['chains'][$filterName][$filterFunction] = 'R';
226         } // END - if
227 }
228
229 // "Runs" the given filters, data is optional and can be any type of data
230 function runFilterChain ($filterName, $data = null) {
231         // Is that filter chain there?
232         if (!isset($GLOBALS['cache_array']['filter']['chains'][$filterName])) {
233                 // We should find all these non-existing filter chains
234                 //* Only for tracking: */ if ($filterName != 'sql_admin_extra_data') {
235                 //* Only for tracking: */ debug_report_bug('Filter chain <strong>' . $filterName . '</strong> not found!');
236                 //* Only for tracking: */ }
237                 /* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Filter chain ' . $filterName . ' not found!');
238
239                 // Abort here and return content
240                 return $data;
241         } // END - if
242
243         // Default return value
244         $returnValue = $data;
245
246         // Then run all filters
247         foreach ($GLOBALS['cache_array']['filter']['chains'][$filterName] as $filterFunction => $active) {
248                 // Debug message
249                 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, "Running: name={$filterName},func={$filterFunction},active={$active}");
250
251                 // Is the filter active?
252                 if (($active == 'Y') || ($active == 'A') || ((in_array($filterName, array('shutdown','extension_remove','post_extension_run_sql'))) && ($active == 'R'))) {
253                         // Is this filter there?
254                         if (!function_exists($filterFunction)) {
255                                 // Unregister it
256                                 unregisterFilter($filterName, $filterFunction);
257
258                                 // Skip this entry
259                                 continue;
260                         } // END - if
261
262                         // Call the filter chain
263                         //* DEBUG: */ print $filterName.'/'.$filterFunction.',[]='.gettype($returnValue).'<br />';
264                         $returnValue = call_user_func_array($filterFunction, array($returnValue));
265
266                         // Update usage counter
267                         countFilterUsage($filterName, $filterFunction);
268                 } elseif (isDebugModeEnabled()) {
269                         // Debug message
270                         logDebugMessage(__FUNCTION__, __LINE__, "Skipped: name={$filterName},func={$filterFunction},active={$active}");
271                 }
272         } // END - foreach
273
274         // Return the filtered content
275         return $returnValue;
276 }
277
278 // Count the filter usage
279 function countFilterUsage ($filterName, $filterFunction) {
280         // Is it there?
281         if (isset($GLOBALS['cache_array']['filter']['counter'][$filterName][$filterFunction])) {
282                 // Yes, then increase
283                 $GLOBALS['cache_array']['filter']['counter'][$filterName][$filterFunction]++;
284         } else {
285                 // No, then create
286                 $GLOBALS['cache_array']['filter']['counter'][$filterName][$filterFunction] = 1;
287         }
288 }
289
290 // [EOF]
291 ?>