]> git.mxchange.org Git - mailer.git/blob - inc/filters.php
Several fixes for extension handling
[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) {
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         // Mark for filter removal
138         $filters[$filterName][$filterFunction] = "R";
139 }
140
141 // "Runs" the given filters, data is optional and can be any type of data
142 function RUN_FILTER ($filterName, $data = null, $silentAbort = true) {
143         global $filters;
144
145         // Is that filter chain there?
146         if (!isset($filters[$filterName])) {
147                 // Then abort here (quick'N'dirty hack)
148                 if ((!$silentAbort) && (defined('FILTER_FAILED_NO_FILTER_FOUND'))) {
149                         ADD_FATAL(sprintf(FILTER_FAILED_NO_FILTER_FOUND, $filterName));
150                 } // END - if
151
152                 // Abort here
153                 return false;
154         } // END - if
155
156         // Default return value
157         $returnValue = $data;
158
159         // Then run all filters
160         foreach ($filters[$filterName] as $filterFunction=>$active) {
161                 // Debug message
162                 /* DEBUG: */ echo __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>): name={$filterName}, func={$filterFunction}, active={$active}<br />\n";
163
164                 // Is the filter active?
165                 if ($active == "Y") {
166                         // Call the filter chain
167                         $returnValue = call_user_func_array($filterFunction, array($returnValue));
168                 } // END - if
169         } // END - foreach
170
171         // Return the filtered content
172         return $returnValue;
173 }
174
175 // -----------------------------------------------------------------------------
176 // Generic filter functions we always need
177 // -----------------------------------------------------------------------------
178
179 // Filter for flushing all new filters to the database
180 function FILTER_FLUSH_FILTERS () {
181         global $filters, $link, $loadedFilters;
182
183         // Is a database link here and not in installation mode?
184         if ((!is_resource($link)) && (!isBooleanConstantAndTrue('mxchange_installing'))) {
185                 // Abort here
186                 ADD_FATAL(sprintf(FILTER_FLUSH_FAILED_NO_DATABASE, $filterFunction, $filterName));
187                 return false;
188         } // END - if
189
190         // Is the extension sql_patches updated?
191         if (EXT_VERSION_IS_OLDER("sql_patches", "0.5.9")) {
192                 // Abort silently here
193                 return false;
194         } // END - if
195
196         // Nothing is added/remove by default
197         $inserted = 0; $removed = 0;
198
199         // Prepare SQL queries
200         $insertSQL = "INSERT INTO `"._MYSQL_PREFIX."_filters` (`filter_name`,`filter_function`,`filter_active`) VALUES";
201         $removeSQL = "DELETE LOW_PRIORITY FROM `"._MYSQL_PREFIX."_filters` WHERE";
202
203         // Write all filters to database
204         foreach ($filters as $filterName => $filterArray) {
205                 // Walk through all filters
206                 foreach ($filterArray as $filterFunction => $active) {
207                         // Is this filter loaded?
208                         if (!isset($loadedFilters[$filterName][$filterFunction])) {
209                                 // Add this filter (all filters are active by default)
210                                 $insertSQL .= sprintf("('%s','%s','Y'),", $filterName, $filterFunction);
211                                 $inserted++;
212                         } elseif ($active == "R") {
213                                 // Remove this filter
214                                 $removeSQL .= sprintf(" (`filter_name`='%s' AND `filter_function`='%s') OR", $filterName, $filterFunction);
215                                 $removed++;
216                         }
217                 } // END - foreach
218         } // END - foreach
219
220         // Something has been added?
221         if ($inserted > 0) {
222                 // Finish SQL command
223                 $insertSQL = substr($insertSQL, 0, -1);
224
225                 // And run it
226                 SQL_QUERY($insertSQL, __FILE__, __LINE__);
227         } // END - if
228
229         // Something has been removed?
230         if ($removed > 0) {
231                 // Finish SQL command
232                 $removeSQL = substr($removeSQL, 0, -2) . "LIMIT ".$removed;
233
234                 // And run it
235                 SQL_QUERY($removeSQL, __FILE__, __LINE__);
236         } // END - if
237 }
238
239 // Filter for shutting down the database link
240 function FILTER_SHUTDOWN_DATABASE () {
241         global $link;
242
243         if (is_resource($link)) {
244                 // Close link
245                 SQL_CLOSE($link, __FILE__, __LINE__);
246         } else {
247                 // No database link
248                 ADD_FATAL(NO_DB_LINK);
249         }
250 }
251
252 //
253 ?>