33c59228a02b7cc2329c13c3870f2834ee053ee9
[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) {
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])) {
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) {
125         global $filters;
126
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));
131                 return false;
132         } // END - if
133
134         // Mark for filter removal
135         $filters[$filterName][$filterFunction] = "R";
136 }
137
138 // "Runs" the given filters, data is optional and can be any type of data
139 function RUN_FILTER ($filterName, $data = null, $silentAbort = true) {
140         global $filters;
141
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));
147                 } // END - if
148
149                 // Abort here
150                 return false;
151         } // END - if
152
153         // Default return value
154         $returnValue = $data;
155
156         // Then run all filters
157         foreach ($filters[$filterName] as $filterFunction=>$active) {
158                 // Debug message
159                 /* DEBUG: */ echo __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>): name={$filterName}, func={$filterFunction}, active={$active}<br />\n";
160
161                 // Is the filter active?
162                 if ($active == "Y") {
163                         // Call the filter chain
164                         $returnValue = call_user_func_array($filterFunction, array($returnValue));
165                 } // END - if
166         } // END - foreach
167
168         // Return the filtered content
169         return $returnValue;
170 }
171
172 // -----------------------------------------------------------------------------
173 // Generic filter functions we always need
174 // -----------------------------------------------------------------------------
175
176 // Filter for flushing all new filters to the database
177 function FILTER_FLUSH_FILTERS () {
178         global $filters, $link, $loadedFilters;
179
180         // Is a database link here and not in installation mode?
181         if ((!is_resource($link)) && (!isBooleanConstantAndTrue('mxchange_installing'))) {
182                 // Abort here
183                 ADD_FATAL(sprintf(FILTER_FLUSH_FAILED_NO_DATABASE, $filterFunction, $filterName));
184                 return false;
185         } // END - if
186
187         // Is the extension sql_patches updated?
188         if (EXT_VERSION_IS_OLDER("sql_patches", "0.5.9")) {
189                 // Abort silently here
190                 return false;
191         } // END - if
192
193         // Nothing is added/remove by default
194         $inserted = 0; $removed = 0;
195
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";
199
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);
208                                 $inserted++;
209                         } elseif ($active == "R") {
210                                 // Remove this filter
211                                 $removeSQL .= sprintf(" (`filter_name`='%s' AND `filter_function`='%s') OR", $filterName, $filterFunction);
212                                 $removed++;
213                         }
214                 } // END - foreach
215         } // END - foreach
216
217         // Something has been added?
218         if ($inserted > 0) {
219                 // Finish SQL command
220                 $insertSQL = substr($insertSQL, 0, -1);
221
222                 // And run it
223                 SQL_QUERY($insertSQL, __FILE__, __LINE__);
224         } // END - if
225
226         // Something has been removed?
227         if ($removed > 0) {
228                 // Finish SQL command
229                 $removeSQL = substr($removeSQL, 0, -2) . "LIMIT ".$removed;
230
231                 // And run it
232                 SQL_QUERY($removeSQL, __FILE__, __LINE__);
233         } // END - if
234 }
235
236 // Filter for shutting down the database link
237 function FILTER_SHUTDOWN_DATABASE () {
238         global $link;
239
240         if (is_resource($link)) {
241                 // Close link
242                 SQL_CLOSE($link, __FILE__, __LINE__);
243         } else {
244                 // No database link
245                 ADD_FATAL(NO_DB_LINK);
246         }
247 }
248
249 //
250 ?>