Debug line removed
[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;
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
62         $loadedFilters =  array();
63
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__);
70
71                 // Are there entries?
72                 if (SQL_NUMROWS($result) > 0) {
73                         // Load all filters
74                         while ($filterArray = SQL_FETCHARRAY($result)) {
75                                 // Mark this filter as loaded (from database)
76                                 $loadedFilters[$filterArray['filter_name']][$filterArray['filter_function']] = true;
77
78                                 // Set this filter
79                                 $filters[$filterArray['filter_name']][$filterArray['filter_function']] = $filterArray['filter_active'];
80                         } // END - while
81                 } // END - if
82         
83                 // Free result
84                 SQL_FREERESULT($result);
85         } // END - if
86
87         // @TODO Find some more init/shutdown filter functions
88
89         // Register shutdown filters
90         REGISTER_FILTER('shutdown', 'FLUSH_FILTERS');
91         REGISTER_FILTER('shutdown', 'SHUTDOWN_DATABASE');
92 }
93
94 // "Registers" a new filter function
95 function REGISTER_FILTER ($filterName, $filterFunction, $silentAbort = true, $force = false) {
96         global $filters;
97
98         // Extend the filter function name
99         $filterFunction = sprintf("FILTER_%s", strtoupper($filterFunction));
100
101         // Is that filter already there?
102         if ((isset($filters[$filterName][$filterFunction])) && (!$force)) {
103                 // Then abort here
104                 if (!$silentAbort) {
105                         ADD_FATAL(sprintf(FILTER_FAILED_ALREADY_ADDED, $filterFunction, $filterName));
106                 } // END - if
107
108                 // Abort here
109                 return false;
110         } // END - if
111
112         // Is the function there?
113         if (!function_exists($filterFunction)) {
114                 // Then abort here
115                 ADD_FATAL(sprintf(FILTER_FAILED_NOT_FOUND, $filterFunction, $filterName));
116                 return false;
117         } // END - if
118
119         // Simply add it to the array
120         $filters[$filterName][$filterFunction] = "Y";
121 }
122
123 // "Unregisters" a filter from the given chain
124 function UNREGISTER_FILTER ($filterName, $filterFunction, $force = false, $remove = true) {
125         global $filters;
126
127         // Extend the filter function name
128         $filterFunction = sprintf("FILTER_%s", strtoupper($filterFunction));
129
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));
134                 return false;
135         } // END - if
136
137         // Shall we remove? (default, not while just showing an extension removal)
138         if ($remove) {
139                 // Mark for filter removal
140                 $filters[$filterName][$filterFunction] = "R";
141         } // END  - if
142 }
143
144 // "Runs" the given filters, data is optional and can be any type of data
145 function RUN_FILTER ($filterName, $data = null, $silentAbort = true) {
146         global $filters;
147
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'))) {
152                         // Add fatal message
153                         ADD_FATAL(sprintf(FILTER_FAILED_NO_FILTER_FOUND, $filterName));
154                 } // END - if
155
156                 // Abort here
157                 return false;
158         } // END - if
159
160         // Default return value
161         $returnValue = $data;
162
163         // Then run all filters
164         foreach ($filters[$filterName] as $filterFunction=>$active) {
165                 // Debug message
166                 //* DEBUG: */ echo __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>): name={$filterName}, func={$filterFunction}, active={$active}<br />\n";
167
168                 // Is the filter active?
169                 if ($active == "Y") {
170                         // Call the filter chain
171                         $returnValue = call_user_func_array($filterFunction, array($returnValue));
172                 } // END - if
173         } // END - foreach
174
175         // Return the filtered content
176         return $returnValue;
177 }
178
179 // -----------------------------------------------------------------------------
180 // Generic filter functions we always need
181 // -----------------------------------------------------------------------------
182
183 // Filter for flushing all new filters to the database
184 function FILTER_FLUSH_FILTERS () {
185         global $filters, $link, $loadedFilters;
186
187         // Is a database link here and not in installation mode?
188         if ((!is_resource($link)) && (!isBooleanConstantAndTrue('mxchange_installing'))) {
189                 // Abort here
190                 ADD_FATAL(sprintf(FILTER_FLUSH_FAILED_NO_DATABASE, $filterFunction, $filterName));
191                 return false;
192         } // END - if
193
194         // Is the extension sql_patches updated?
195         if (EXT_VERSION_IS_OLDER("sql_patches", "0.5.9")) {
196                 // Abort silently here
197                 return false;
198         } // END - if
199
200         // Nothing is added/remove by default
201         $inserted = 0; $removed = 0;
202
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";
206
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);
215                                 $inserted++;
216                         } elseif ($active == "R") {
217                                 // Remove this filter
218                                 $removeSQL .= sprintf(" (`filter_name`='%s' AND `filter_function`='%s') OR", $filterName, $filterFunction);
219                                 $removed++;
220                         }
221                 } // END - foreach
222         } // END - foreach
223
224         // Something has been added?
225         if ($inserted > 0) {
226                 // Finish SQL command
227                 $insertSQL = substr($insertSQL, 0, -1);
228
229                 // And run it
230                 SQL_QUERY($insertSQL, __FILE__, __LINE__);
231         } // END - if
232
233         // Something has been removed?
234         if ($removed > 0) {
235                 // Finish SQL command
236                 $removeSQL = substr($removeSQL, 0, -2) . "LIMIT ".$removed;
237
238                 // And run it
239                 SQL_QUERY($removeSQL, __FILE__, __LINE__);
240         } // END - if
241 }
242
243 // Filter for shutting down the database link
244 function FILTER_SHUTDOWN_DATABASE () {
245         global $link;
246
247         if (is_resource($link)) {
248                 // Close link
249                 SQL_CLOSE($link, __FILE__, __LINE__);
250         } else {
251                 // No database link
252                 ADD_FATAL(NO_DB_LINK);
253         }
254 }
255
256 //
257 ?>