2 /************************************************************************
3 * MXChange v0.2.1 Start: 12/16/2008 *
4 * =============== Last change: 12/16/2008 *
6 * -------------------------------------------------------------------- *
8 * -------------------------------------------------------------------- *
9 * Short description : Functions for filter system *
10 * -------------------------------------------------------------------- *
11 * Kurzbeschreibung : Funktionen fuer Filter-System *
12 * -------------------------------------------------------------------- *
14 * -------------------------------------------------------------------- *
15 * Copyright (c) 2003 - 2008 by Roland Haeder *
16 * For more information visit: http://www.mxchange.org *
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. *
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. *
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, *
32 ************************************************************************/
34 // Some security stuff...
35 if (!defined('__SECURITY')) {
36 $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4)."/security.php";
40 // Init "generic filter system"
41 function INIT_FILTER_SYSTEM() {
42 global $filters, $loadedFilters;
44 // Is the filter already initialized?
45 if ((isset($filters)) && (is_array($filters))) {
47 ADD_FATAL(FILTER_FAILED_ALREADY_INIT);
51 // Init the filter system (just some ideas)
53 // Filters for pre-init phase
55 // Filters for post-init phase
56 'postinit' => array(),
57 // Filters for shutdown phase
61 // Init loaded filters
62 $loadedFilters = array();
64 // Load all saved filers if sql_patches is updated
65 if (GET_EXT_VERSION("sql_patches") >= "0.5.9") {
66 // Load all active filers
67 $result = SQL_QUERY("SELECT `filter_name`, `filter_function`, `filter_active`
68 FROM `"._MYSQL_PREFIX."_filters`
69 ORDER BY `filter_id` ASC", __FILE__, __LINE__);
72 if (SQL_NUMROWS($result) > 0) {
74 while ($filterArray = SQL_FETCHARRAY($result)) {
75 // Mark this filter as loaded (from database)
76 $loadedFilters[$filterArray['filter_name']][$filterArray['filter_function']] = true;
79 $filters[$filterArray['filter_name']][$filterArray['filter_function']] = $filterArray['filter_active'];
84 SQL_FREERESULT($result);
87 // @TODO Find some more init/shutdown filter functions
89 // Register shutdown filters
90 REGISTER_FILTER('shutdown', 'FLUSH_FILTERS');
91 REGISTER_FILTER('shutdown', 'SHUTDOWN_DATABASE');
94 // "Registers" a new filter function
95 function REGISTER_FILTER ($filterName, $filterFunction, $silentAbort = true, $force = false) {
98 // Extend the filter function name
99 $filterFunction = sprintf("FILTER_%s", strtoupper($filterFunction));
101 // Is that filter already there?
102 if ((isset($filters[$filterName][$filterFunction])) && (!$force)) {
105 ADD_FATAL(sprintf(FILTER_FAILED_ALREADY_ADDED, $filterFunction, $filterName));
112 // Is the function there?
113 if (!function_exists($filterFunction)) {
115 ADD_FATAL(sprintf(FILTER_FAILED_NOT_FOUND, $filterFunction, $filterName));
119 // Simply add it to the array
120 $filters[$filterName][$filterFunction] = "Y";
123 // "Unregisters" a filter from the given chain
124 function UNREGISTER_FILTER ($filterName, $filterFunction, $force = false, $remove = true) {
127 // Extend the filter function name
128 $filterFunction = sprintf("FILTER_%s", strtoupper($filterFunction));
130 // Is that filter there?
131 if ((!isset($filters[$filterName][$filterFunction])) && (!$force)) {
132 // Not found, so abort here
133 ADD_FATAL(sprintf(FILTER_FAILED_NOT_REMOVED, $filterFunction, $filterName));
137 // Shall we remove? (default, not while just showing an extension removal)
139 // Mark for filter removal
140 $filters[$filterName][$filterFunction] = "R";
144 // "Runs" the given filters, data is optional and can be any type of data
145 function RUN_FILTER ($filterName, $data = null, $silentAbort = true) {
148 // Is that filter chain there?
149 if (!isset($filters[$filterName])) {
150 // Then abort here (quick'N'dirty hack)
151 if ((!$silentAbort) && (defined('FILTER_FAILED_NO_FILTER_FOUND'))) {
153 ADD_FATAL(sprintf(FILTER_FAILED_NO_FILTER_FOUND, $filterName));
160 // Default return value
161 $returnValue = $data;
163 // Then run all filters
164 foreach ($filters[$filterName] as $filterFunction=>$active) {
166 //* DEBUG: */ echo __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>): name={$filterName}, func={$filterFunction}, active={$active}<br />\n";
168 // Is the filter active?
169 if ($active == "Y") {
170 // Call the filter chain
171 $returnValue = call_user_func_array($filterFunction, array($returnValue));
175 // Return the filtered content
179 // -----------------------------------------------------------------------------
180 // Generic filter functions we always need
181 // -----------------------------------------------------------------------------
183 // Filter for flushing all new filters to the database
184 function FILTER_FLUSH_FILTERS () {
185 global $filters, $link, $loadedFilters;
187 // Is a database link here and not in installation mode?
188 if ((!is_resource($link)) && (!isBooleanConstantAndTrue('mxchange_installing'))) {
190 ADD_FATAL(sprintf(FILTER_FLUSH_FAILED_NO_DATABASE, $filterFunction, $filterName));
194 // Is the extension sql_patches updated?
195 if (EXT_VERSION_IS_OLDER("sql_patches", "0.5.9")) {
196 // Abort silently here
200 // Nothing is added/remove by default
201 $inserted = 0; $removed = 0;
203 // Prepare SQL queries
204 $insertSQL = "INSERT INTO `"._MYSQL_PREFIX."_filters` (`filter_name`,`filter_function`,`filter_active`) VALUES";
205 $removeSQL = "DELETE LOW_PRIORITY FROM `"._MYSQL_PREFIX."_filters` WHERE";
207 // Write all filters to database
208 foreach ($filters as $filterName => $filterArray) {
209 // Walk through all filters
210 foreach ($filterArray as $filterFunction => $active) {
211 // Is this filter loaded?
212 if (!isset($loadedFilters[$filterName][$filterFunction])) {
213 // Add this filter (all filters are active by default)
214 $insertSQL .= sprintf("('%s','%s','Y'),", $filterName, $filterFunction);
216 } elseif ($active == "R") {
217 // Remove this filter
218 $removeSQL .= sprintf(" (`filter_name`='%s' AND `filter_function`='%s') OR", $filterName, $filterFunction);
224 // Something has been added?
226 // Finish SQL command
227 $insertSQL = substr($insertSQL, 0, -1);
230 SQL_QUERY($insertSQL, __FILE__, __LINE__);
233 // Something has been removed?
235 // Finish SQL command
236 $removeSQL = substr($removeSQL, 0, -2) . "LIMIT ".$removed;
239 SQL_QUERY($removeSQL, __FILE__, __LINE__);
243 // Filter for shutting down the database link
244 function FILTER_SHUTDOWN_DATABASE () {
247 if (is_resource($link)) {
249 SQL_CLOSE($link, __FILE__, __LINE__);
252 ADD_FATAL(NO_DB_LINK);