]> git.mxchange.org Git - mailer.git/blob - inc/filters.php
c0b3702d2eb191ac615e61dbe32cfccedb6f7f63
[mailer.git] / inc / filters.php
1 <?php
2 /************************************************************************
3  * MXChange v0.2.1                                    Start: 12/16/2008 *
4  * ===============                              Last change: 12/16/2008 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : filters.php                                      *
8  * -------------------------------------------------------------------- *
9  * Short description : Functions for filter system                      *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Funktionen fuer Filter-System                    *
12  * -------------------------------------------------------------------- *
13  *                                                                      *
14  * -------------------------------------------------------------------- *
15  * Copyright (c) 2003 - 2008 by Roland Haeder                           *
16  * For more information visit: http://www.mxchange.org                  *
17  *                                                                      *
18  * This program is free software; you can redistribute it and/or modify *
19  * it under the terms of the GNU General Public License as published by *
20  * the Free Software Foundation; either version 2 of the License, or    *
21  * (at your option) any later version.                                  *
22  *                                                                      *
23  * This program is distributed in the hope that it will be useful,      *
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
26  * GNU General Public License for more details.                         *
27  *                                                                      *
28  * You should have received a copy of the GNU General Public License    *
29  * along with this program; if not, write to the Free Software          *
30  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
31  * MA  02110-1301  USA                                                  *
32  ************************************************************************/
33
34 // Some security stuff...
35 if (!defined('__SECURITY')) {
36         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4)."/security.php";
37         require($INC);
38 }
39
40 // Init "generic filter system"
41 function INIT_FILTER_SYSTEM() {
42         global $filters, $loadedFilters, $counter;
43
44         // Is the filter already initialized?
45         if ((isset($filters)) && (is_array($filters))) {
46                 // Then abort here
47                 ADD_FATAL(FILTER_FAILED_ALREADY_INIT);
48                 return false;
49         } // END - if
50
51         // Init the filter system (just some ideas)
52         $filters = array(
53                 // Filters for pre-init phase
54                 'preinit'   => array(),
55                 // Filters for post-init phase
56                 'postinit'  => array(),
57                 // Filters for shutdown phase
58                 'shutdown'  => array()
59         );
60
61         // Init loaded filters and counter
62         $loadedFilters =  array();
63         $counter = array();
64
65         // Load all saved filers if sql_patches is updated
66         if (GET_EXT_VERSION("sql_patches") >= "0.5.9") {
67                 // Init add
68                 $ADD = "";
69                 if (GET_EXT_VERSION("sql_patches") >= "0.6.0") $ADD = ", `filter_counter`";
70
71                 // Load all active filers
72                 $result = SQL_QUERY("SELECT `filter_name`, `filter_function`, `filter_active`".$ADD."
73 FROM `"._MYSQL_PREFIX."_filters`
74 ORDER BY `filter_id` ASC", __FILE__, __LINE__);
75
76                 // Are there entries?
77                 if (SQL_NUMROWS($result) > 0) {
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                                 $counter[$filterName][$filterFunction] = 0;
86
87                                 // Mark this filter as loaded (from database)
88                                 $loadedFilters[$filterName][$filterFunction] = true;
89
90                                 // Set this filter
91                                 $filters[$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                                         $counter[$filterName][$filterFunction] = $filterArray['filter_counter'];
97                                 } // END - if
98                         } // END - while
99                 } // END - if
100         
101                 // Free result
102                 SQL_FREERESULT($result);
103         } // END - if
104
105         // Login failtures handler
106         REGISTER_FILTER('post_youhere_line', 'CALL_HANDLER_LOGIN_FAILTURES');
107
108         // Filters for pre-extension-registration
109         REGISTER_FILTER('pre_extension_installed', 'RUN_SQLS');
110
111         // Filters for post-extension-registration
112         REGISTER_FILTER('post_extension_installed', 'AUTO_ACTIVATE_EXTENSION');
113         REGISTER_FILTER('post_extension_installed', 'SOLVE_TASK');
114         REGISTER_FILTER('post_extension_installed', 'LOAD_INCLUDES');
115
116         // Solving tasks
117         REGISTER_FILTER('solve_task', 'SOLVE_TASK');
118
119         // Loading includes in general
120         REGISTER_FILTER('load_includes', 'LOAD_INCLUDES');
121
122         // Run SQLs
123         REGISTER_FILTER('run_sqls', 'RUN_SQLS');
124
125         // Register shutdown filters
126         REGISTER_FILTER('shutdown', 'FLUSH_FILTERS');
127 }
128
129 // "Registers" a new filter function
130 function REGISTER_FILTER ($filterName, $filterFunction, $silentAbort = true, $force = false, $add = true) {
131         global $filters, $counter;
132
133         // Extend the filter function name
134         $filterFunction = sprintf("FILTER_%s", strtoupper($filterFunction));
135
136         // Is that filter already there?
137         if ((isset($filters[$filterName][$filterFunction])) && (!$force)) {
138                 // Then abort here
139                 if (!$silentAbort) {
140                         ADD_FATAL(sprintf(FILTER_FAILED_ALREADY_ADDED, $filterFunction, $filterName));
141                 } // END - if
142
143                 // Abort here
144                 return false;
145         } // END - if
146
147         // Is the function there?
148         if (!function_exists($filterFunction)) {
149                 // Then abort here
150                 ADD_FATAL(sprintf(FILTER_FAILED_NOT_FOUND, $filterFunction, $filterName));
151                 return false;
152         } // END - if
153
154         // Shall we add it?
155         if ($add) {
156                 // Simply add it to the array
157                 $filters[$filterName][$filterFunction] = "Y";
158                 $counter[$filterName][$filterFunction] = 0;
159         } // END - if
160 }
161
162 // "Unregisters" a filter from the given chain
163 function UNREGISTER_FILTER ($filterName, $filterFunction, $force = false, $remove = true) {
164         global $filters, $counter;
165
166         // Extend the filter function name
167         $filterFunction = sprintf("FILTER_%s", strtoupper($filterFunction));
168
169         // Is that filter there?
170         if ((!isset($filters[$filterName][$filterFunction])) && (!$force)) {
171                 // Not found, so abort here
172                 ADD_FATAL(sprintf(FILTER_FAILED_NOT_REMOVED, $filterFunction, $filterName));
173                 return false;
174         } // END - if
175
176         // Shall we remove? (default, not while just showing an extension removal)
177         if ($remove) {
178                 // Mark for filter removal
179                 $filters[$filterName][$filterFunction] = "R";
180                 unset($counter[$filterName][$filterFunction]);
181         } // END  - if
182 }
183
184 // "Runs" the given filters, data is optional and can be any type of data
185 function RUN_FILTER ($filterName, $data = null, $silentAbort = true) {
186         global $filters, $counter;
187
188         // Is that filter chain there?
189         if (!isset($filters[$filterName])) {
190                 // Then abort here (quick'N'dirty hack)
191                 if ((!$silentAbort) && (defined('FILTER_FAILED_NO_FILTER_FOUND'))) {
192                         // Add fatal message
193                         ADD_FATAL(sprintf(FILTER_FAILED_NO_FILTER_FOUND, $filterName));
194                 } // END - if
195
196                 // Abort here
197                 return false;
198         } // END - if
199
200         // Default return value
201         $returnValue = $data;
202
203         // Then run all filters
204         foreach ($filters[$filterName] as $filterFunction=>$active) {
205                 // Debug message
206                 //* DEBUG: */ echo __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>): name={$filterName}, func={$filterFunction}, active={$active}<br />\n";
207
208                 // Is the filter active?
209                 if ($active == "Y") {
210                         // Is this filter there?
211                         if (!function_exists($filterFunction)) {
212                                 // Unregister it
213                                 UNREGISTER_FILTER($filterName, $filterFunction);
214
215                                 // Skip this entry
216                                 continue;
217                         } // END - if
218
219                         // Call the filter chain
220                         $returnValue = call_user_func_array($filterFunction, array($returnValue));
221
222                         // Update usage counter
223                         $counter[$filterName][$filterFunction]++;
224                 } // END - if
225         } // END - foreach
226
227         // Return the filtered content
228         return $returnValue;
229 }
230
231 // -----------------------------------------------------------------------------
232 // Generic filter functions we always need
233 // -----------------------------------------------------------------------------
234
235 // Filter for flushing all new filters to the database
236 function FILTER_FLUSH_FILTERS () {
237         global $filters, $counter, $link, $loadedFilters, $SQLs;
238
239         // Clear all previous SQL queries
240         $SQLs = array();
241
242         // Is a database link here and not in installation mode?
243         if ((!is_resource($link)) && (!isBooleanConstantAndTrue('mxchange_installing'))) {
244                 // Abort here
245                 ADD_FATAL(sprintf(FILTER_FLUSH_FAILED_NO_DATABASE, $filterFunction, $filterName));
246                 return false;
247         } // END - if
248
249         // Is the extension sql_patches updated?
250         if (EXT_VERSION_IS_OLDER("sql_patches", "0.5.9")) {
251                 // Abort silently here
252                 return false;
253         } // END - if
254
255         // Nothing is added/remove by default
256         $inserted = 0; $removed = 0;
257
258         // Prepare SQL queries
259         $insertSQL = "INSERT INTO `"._MYSQL_PREFIX."_filters` (`filter_name`,`filter_function`,`filter_active`) VALUES";
260         $removeSQL = "DELETE LOW_PRIORITY FROM `"._MYSQL_PREFIX."_filters` WHERE";
261
262         // Write all filters to database
263         foreach ($filters as $filterName => $filterArray) {
264                 // Walk through all filters
265                 foreach ($filterArray as $filterFunction => $active) {
266                         // Is this filter loaded?
267                         if (!isset($loadedFilters[$filterName][$filterFunction])) {
268                                 // Add this filter (all filters are active by default)
269                                 $insertSQL .= sprintf("('%s','%s','Y'),", $filterName, $filterFunction);
270                                 $inserted++;
271                         } elseif ($active == "R") {
272                                 // Remove this filter
273                                 $removeSQL .= sprintf(" (`filter_name`='%s' AND `filter_function`='%s') OR", $filterName, $filterFunction);
274                                 $removed++;
275                         }
276                 } // END - foreach
277         } // END - foreach
278
279         // Something has been added?
280         if ($inserted > 0) {
281                 // Finish SQL command
282                 $insertSQL = substr($insertSQL, 0, -1);
283
284                 // And run it
285                 $SQLs[] = $insertSQL;
286         } // END - if
287
288         // Something has been removed?
289         if ($removed > 0) {
290                 // Finish SQL command
291                 $removeSQL = substr($removeSQL, 0, -2) . "LIMIT ".$removed;
292
293                 // And run it
294                 $removeSQL;
295         } // END - if
296
297         // Shall we update usage counters (ONLY FOR DEBUGGING!)
298         if (getConfig('update_filter_usage') == "Y") {
299                 // Update all counters
300                 foreach ($counter as $filterName => $filterArray) {
301                         // Walk through all filters
302                         foreach ($filterArray as $filterFunction => $cnt) {
303                                 // Construct and add the query
304                                 $SQLs[] = sprintf("UPDATE `"._MYSQL_PREFIX."_filters` SET `filter_counter`=%s WHERE `filter_name`='%s' AND `filter_function`='%s' LIMIT 1",
305                                         bigintval($cnt),
306                                         $filterName,
307                                         $filterFunction
308                                 );
309                         } // END - foreach
310                 } // END - foreach
311         } // END - if
312
313         // Run the run_sqls filter in non-dry mode
314         RUN_FILTER('run_sqls', false);
315 }
316
317 // Filter for calling the handler for login failtures
318 function FILTER_CALL_HANDLER_LOGIN_FAILTURES ($data) {
319         // Init content
320         $content = $data;
321
322         // Handle failed logins here if not in guest
323         //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):type={$data['type']},action={$GLOBALS['action']},what={$GLOBALS['what']},lvl={$data['access_level']}<br />\n";
324         if ((($data['type'] == "what") || ($data['type'] == "action") && ((!isset($GLOBALS['what'])) || ($GLOBALS['what'] == "overview") || ($GLOBALS['what'] == getConfig('index_home')))) && ($data['access_level'] != "guest") && ((GET_EXT_VERSION("sql_patches") >= "0.4.7") || (GET_EXT_VERSION("admins") >= "0.7.0"))) {
325                 // Handle failture
326                 $content['content'] .= HANDLE_LOGIN_FAILTURES($data['access_level']);
327         } // END - if
328
329         // Return the content
330         return $content;
331 }
332
333 // Filter for redirecting to logout if sql_patches has been installed
334 function FILTER_REDIRECT_TO_LOGOUT_SQL_PATCHES () {
335         // Remove this filter
336         UNREGISTER_FILTER('shutdown', __FUNCTION__);
337
338         // Is the element set?
339         if (isset($GLOBALS['ext_load_mode'])) {
340                 // Redirect here
341                 LOAD_URL("modules.php?module=admin&logout=1&".$GLOBALS['ext_load_mode']."=sql_patches");
342         } // END - if
343
344         // This should not happen!
345         DEBUG_LOG(__FUNCTION__, __LINE__, "Cannot auto-logout because no extension load-mode has been set.");
346 }
347
348 // Filter for auto-activation of a extension
349 function FILTER_AUTO_ACTIVATE_EXTENSION ($data) {
350         global $EXT_ALWAYS_ACTIVE;
351
352         // Is this extension always activated?
353         if ($EXT_ALWAYS_ACTIVE == "Y") {
354                 // Then activate the extension
355                 //* DEBUG: */ echo __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>): ext_name={$data['ext_name']}<br />\n";
356                 ACTIVATE_EXTENSION($data['ext_name']);
357         } // END - if
358
359         // Return the data
360         return $data;
361 }
362
363 // Filter for solving task given task
364 function FILTER_SOLVE_TASK ($data) {
365         // Don't solve anything if no admin!
366         if (!IS_ADMIN()) return $data;
367
368         // Is this a direct task id or array element task_id is found?
369         if (is_int($data)) {
370                 // Then solve it...
371                 ADMIN_SOLVE_TASK($data);
372         } elseif ((is_array($data)) && (isset($data['task_id']))) {
373                 // Solve it...
374                 ADMIN_SOLVE_TASK($data['task_id']);
375         }
376
377         // Return the data
378         return $data;
379 }
380
381 // Filter to load include files
382 function FILTER_LOAD_INCLUDES ($data) {
383         global $INC_POOL;
384
385         // Is it an array?
386         if ((!isset($INC_POOL)) || (!is_array($INC_POOL))) {
387                 // Then abort here
388                 DEBUG_LOG(__FILE__, __LINE__, "INC_POOL is no array!");
389                 return $data;
390         } // END - if
391
392         // Check for added include files
393         if (count($INC_POOL) > 0) {
394                 // Loads every include file
395                 foreach ($INC_POOL as $fqfn) {
396                         require_once($fqfn);
397                 } // END - foreach
398
399                 // Remove array
400                 unset($INC_POOL);
401         } // END - if
402
403         // Return $data
404         return $data;
405 }
406
407 // Filter for running SQL commands
408 function FILTER_RUN_SQLS ($dry_run) {
409         global $SQLs;
410
411         // Is the array there?
412         if ((is_array($SQLs)) && (!$dry_run)) {
413                 // Run SQL commands
414                 foreach ($SQLs as $sql) {
415                         $sql = trim($sql);
416                         if (!empty($sql)) {
417                                 // Do we have an "ALTER TABLE" command?
418                                 if (substr(strtolower($sql), 0, 11) == "alter table") {
419                                         // Analyse the alteration command
420                                         SQL_ALTER_TABLE($sql, __FILE__, __LINE__);
421                                 } else {
422                                         // Run regular SQL command
423                                         $result = SQL_QUERY($sql, __FILE__, __LINE__, false);
424                                 }
425                         } // END - if
426                 } // END - foreach
427         } elseif (GET_EXT_VERSION("sql_patches") == "") {
428                 // Remove SQLs if extension is not installed
429                 $SQLs = array();
430         }
431 }
432
433 //
434 ?>