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) {
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])) {
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) {
127 // Is that filter there?
128 if (!isset($filters[$filterName][$filterFunction])) {
129 // Not found, so abort here
130 ADD_FATAL(sprintf(FILTER_FAILED_NOT_REMOVED, $filterFunction, $filterName));
134 // Mark for filter removal
135 $filters[$filterName][$filterFunction] = "R";
138 // "Runs" the given filters, data is optional and can be any type of data
139 function RUN_FILTER ($filterName, $data = null, $silentAbort = true) {
142 // Is that filter chain there?
143 if (!isset($filters[$filterName])) {
144 // Then abort here (quick'N'dirty hack)
145 if ((!$silentAbort) && (defined('FILTER_FAILED_NO_FILTER_FOUND'))) {
146 ADD_FATAL(sprintf(FILTER_FAILED_NO_FILTER_FOUND, $filterName));
153 // Default return value
154 $returnValue = $data;
156 // Then run all filters
157 foreach ($filters[$filterName] as $filterFunction=>$active) {
159 /* DEBUG: */ echo __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>): name={$filterName}, func={$filterFunction}, active={$active}<br />\n";
161 // Is the filter active?
162 if ($active == "Y") {
163 // Call the filter chain
164 $returnValue = call_user_func_array($filterFunction, array($returnValue));
168 // Return the filtered content
172 // -----------------------------------------------------------------------------
173 // Generic filter functions we always need
174 // -----------------------------------------------------------------------------
176 // Filter for flushing all new filters to the database
177 function FILTER_FLUSH_FILTERS () {
178 global $filters, $link, $loadedFilters;
180 // Is a database link here and not in installation mode?
181 if ((!is_resource($link)) && (!isBooleanConstantAndTrue('mxchange_installing'))) {
183 ADD_FATAL(sprintf(FILTER_FLUSH_FAILED_NO_DATABASE, $filterFunction, $filterName));
187 // Is the extension sql_patches updated?
188 if (EXT_VERSION_IS_OLDER("sql_patches", "0.5.9")) {
189 // Abort silently here
193 // Nothing is added/remove by default
194 $inserted = 0; $removed = 0;
196 // Prepare SQL queries
197 $insertSQL = "INSERT INTO `"._MYSQL_PREFIX."_filters` (`filter_name`,`filter_function`,`filter_active`) VALUES";
198 $removeSQL = "DELETE LOW_PRIORITY FROM `"._MYSQL_PREFIX."_filters` WHERE";
200 // Write all filters to database
201 foreach ($filters as $filterName => $filterArray) {
202 // Walk through all filters
203 foreach ($filterArray as $filterFunction => $active) {
204 // Is this filter loaded?
205 if (!isset($loadedFilters[$filterName][$filterFunction])) {
206 // Add this filter (all filters are active by default)
207 $insertSQL .= sprintf("('%s','%s','Y'),", $filterName, $filterFunction);
209 } elseif ($active == "R") {
210 // Remove this filter
211 $removeSQL .= sprintf(" (`filter_name`='%s' AND `filter_function`='%s') OR", $filterName, $filterFunction);
217 // Something has been added?
219 // Finish SQL command
220 $insertSQL = substr($insertSQL, 0, -1);
223 SQL_QUERY($insertSQL, __FILE__, __LINE__);
226 // Something has been removed?
228 // Finish SQL command
229 $removeSQL = substr($removeSQL, 0, -2) . "LIMIT ".$removed;
232 SQL_QUERY($removeSQL, __FILE__, __LINE__);
236 // Filter for shutting down the database link
237 function FILTER_SHUTDOWN_DATABASE () {
240 if (is_resource($link)) {
242 SQL_CLOSE($link, __FILE__, __LINE__);
245 ADD_FATAL(NO_DB_LINK);