Fixes for stripped HTML tags, and false warnings in debug log
[mailer.git] / inc / load_extensions.php
1 <?php
2 /************************************************************************
3  * MXChange v0.2.1                                    Start: 06/26/2004 *
4  * ===============                              Last change: 07/01/2005 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : load_extensions.php                              *
8  * -------------------------------------------------------------------- *
9  * Short description : Load all extensions                              *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Alle Erweiterungen laden                         *
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 variables
41 EXT_INIT_CSS_FILES();
42 $ADD = "";
43
44 // Init cache instance and array
45 $GLOBALS['cache_instance'] = null;
46 $GLOBALS['cache_array'] = array();
47
48 // Skip loading extensions
49 if ((!isBooleanConstantAndTrue('mxchange_installed')) || (isInstalling())) {
50         // Init filter system here
51         INIT_FILTER_SYSTEM();
52
53         // Skip loading
54         return;
55 } // END - if
56
57 // Initialize array for "always keep active extensions"
58 $GLOBALS['cache_array']['active_extensions'] = array();
59
60 // By default no cache is set
61 $GLOBALS['cacheMode'] = "no";
62
63 // Load sql_patchrs extension alone
64 LOAD_EXTENSION("sql_patches");
65
66 // Init inc pool array
67 $INC_POOL = array();
68
69 //
70 // Load extensions
71 //
72 if (EXT_IS_ACTIVE("cache")) {
73         // Load cache extension alone
74         LOAD_EXTENSION("cache");
75
76         // Check extension cache
77         switch (($GLOBALS['cache_instance']->loadCacheFile("extensions", true)) && ($GLOBALS['cache_instance']->extensionVersionMatches("sql_patches"))) {
78                 case true : $GLOBALS['cacheMode'] = "load"; break;
79                 case false: $GLOBALS['cacheMode'] = "init"; break;
80         }
81
82         // Do we need to init the cache?
83         if (($GLOBALS['cacheMode'] == "init") && (getConfig('cache_exts') == "Y")) {
84                 // Init cache file
85                 $GLOBALS['cache_instance']->init("EXTENSIONS");
86                 $GLOBALS['cache_instance']->storeExtensionVersion("sql_patches");
87         } elseif (getConfig('cache_exts') != "Y") {
88                 // Cache will not be created for extensions
89                 $GLOBALS['cacheMode'] = "skip";
90         }
91 } // END - if
92
93 // Load cache?
94 if ($GLOBALS['cacheMode'] == "load") {
95         // Init include array
96         $EXT_POOL = array();
97
98         // Re-initialize handler
99         $GLOBALS['cache_instance']->loadCacheFile("extensions", true);
100
101         // Load extension data from cache file
102         $EXT_DUMMY = $GLOBALS['cache_instance']->getArrayFromCache();
103
104         // Is the cache file fine?
105         if (!isset($EXT_DUMMY['ext_name'])) {
106                 // Cache file is damaged so kill it
107                 $GLOBALS['cache_instance']->destroyCacheFile();
108
109                 // Retry it
110                 LOAD_INC(__FILE__);
111                 return;
112         } // END -  if
113
114         // Begin with the cache preparation of extensions
115         $EXT_NAMES = array();
116         foreach ($EXT_DUMMY['ext_name'] as $k => $name) {
117                 // Load CSS file
118                 if ($EXT_DUMMY['ext_css'][$k] == "Y") EXT_ADD_CSS_FILE("".$name.".css");
119
120                 // Load extension file itself
121                 if ((($EXT_DUMMY['ext_active'][$k] == "Y") || ($EXT_DUMMY['ext_keep'][$k] == "Y") || (IS_ADMIN())) && (!in_array($name, array("sql_patches", "cache")))) {
122                         $EXT_POOL[] = $name;
123                 } // END - if
124
125                 // Version number
126                 $EXT_DUMMY['ext_version'][$name] = $EXT_DUMMY['ext_version'][$k];
127                 unset($EXT_DUMMY['ext_version'][$k]);
128
129                 // Extension is active
130                 $EXT_DUMMY['ext_active'][$name] = $EXT_DUMMY['ext_active'][$k];
131                 unset($EXT_DUMMY['ext_active'][$k]);
132
133                 // Ext menu
134                 $EXT_DUMMY['ext_menu'][$name] = $EXT_DUMMY['ext_menu'][$k];
135                 unset($EXT_DUMMY['ext_menu'][$k]);
136
137                 // Extension id
138                 $EXT_DUMMY['ext_id'][$name] = $EXT_DUMMY['ext_id'][$k];
139                 $id = $EXT_DUMMY['ext_id'][$name];
140                 unset($EXT_DUMMY['ext_id'][$k]);
141
142                 // Add ext name
143                 $EXT_NAMES[$id] = $name;
144
145                 // Add deprecated flag (defaults to "not deprecated")
146                 $EXT_DUMMY['ext_deprecated'][$name] = "N";
147
148                 // Mark it as active extension
149                 $GLOBALS['cache_array']['active_extensions']['$name'] = $EXT_DUMMY['ext_keep'][$k];
150                 unset($EXT_DUMMY['ext_keep'][$k]);
151
152                 // Remove unneccessary data from memory
153                 unset($EXT_DUMMY['ext_css'][$k]);
154         } // END - foreach
155
156         // Write dummy array back
157         $EXT_DUMMY['ext_name'] = $EXT_NAMES;
158         unset($EXT_NAMES);
159
160         // Loading cache is done so let's free some memory!
161         unset($EXT_DUMMY['ext_keep']);
162         unset($EXT_DUMMY['ext_css']);
163         $GLOBALS['cache_array']['extensions'] = $EXT_DUMMY;
164         unset($EXT_DUMMY);
165
166         // No database load needed
167         $res_ext_crt = false;
168
169         // Load all extension files
170         foreach ($EXT_POOL as $ext) {
171                 LOAD_EXTENSION($ext);
172         } // END - foreach
173
174         // Init filter system
175         INIT_FILTER_SYSTEM();
176
177         // Load more cache files (like admins)
178         LOAD_INC_ONCE("inc/load_cache.php");
179
180         // Remove array
181         unset($EXT_POOL);
182 } else {
183         // If current user is not admin load only activated extensions. But load
184         // them all if we are going to init the cache files. The admin shall use
185         // every available extension for testing purposes.
186         if ((!IS_ADMIN()) && ($GLOBALS['cacheMode'] != "init")) $ADD = " WHERE ext_active='Y'";
187
188         if (GET_EXT_VERSION("sql_patches") >= "0.0.6") {
189                 // Query with CSS file from DB
190                 $res_ext_crt = SQL_QUERY("SELECT id AS ext_id, ext_name, ext_has_css AS ext_css, ext_active, ext_version
191 FROM `{!_MYSQL_PREFIX!}_extensions`".$ADD."
192 ORDER BY ext_name", __FILE__, __LINE__);
193         } else {
194                 // Old obsolete query string
195                 $res_ext_crt = SQL_QUERY("SELECT id AS ext_id, ext_name, ext_name, ext_active, ext_version
196 FROM `{!_MYSQL_PREFIX!}_extensions`".$ADD."
197 ORDER BY ext_name", __FILE__, __LINE__);
198         }
199 }
200
201 // Array for removed but not uninstalled extensions
202 $DEL = array();
203
204 // At least one found?
205 if ((SQL_NUMROWS($res_ext_crt) > 0) && ((($GLOBALS['cacheMode'] == "init") && ($GLOBALS['output_mode'] != "1") && ($GLOBALS['output_mode'] != "-1")) || ($GLOBALS['cacheMode'] == "no"))) {
206         // Extensions are registered so we load them
207         while ($content = SQL_FETCHARRAY($res_ext_crt)) {
208                 // Get menu entry
209                 $content['ext_menu'] = "N";
210                 if (MODULE_HAS_MENU($content['ext_name'], true)) {
211                         $content['ext_menu'] = "Y";
212                 } // END - if
213
214                 // Generate FQFN for extension
215                 $FQFN = sprintf("%sinc/extensions/ext-%s.php", constant('PATH'), $content['ext_name']);
216
217                 // Does the extension file exists?
218                 if (FILE_READABLE($FQFN)) {
219                         // By default no extension is always active, except sql_patches
220                         $EXT_ALWAYS_ACTIVE = "N";
221
222                         // Load extension
223                         if (($content['ext_name'] != "sql_patches") && (($content['ext_name'] != "cache") || (!EXT_IS_ACTIVE("cache")))) {
224                                 // Load extension
225                                 LOAD_EXTENSION($content['ext_name']);
226                         } else {
227                                 // Keep sql_patches always active
228                                 $EXT_ALWAYS_ACTIVE = "Y";
229                         }
230
231                         // Transfer EXT_ALWAYS_ACTIVE flag
232                         $content['ext_keep'] = $EXT_ALWAYS_ACTIVE;
233
234                         // CSS file handling:
235                         if ((!isset($content['ext_css'])) || ($content['ext_css'] == "Y")) {
236                                 // Create FQFN for the CSS file
237                                 $FQFN = sprintf("%stheme/%s/css/%s.css", constant('PATH'), GET_CURR_THEME(), $content['ext_name']);
238
239                                 // Is the file there?
240                                 if (FILE_READABLE($FQFN)) {
241                                         // CSS file for extension was found (use only relative path for now!)
242                                         EXT_ADD_CSS_FILE($content['ext_name'].".css");
243                                         $content['ext_css'] = "Y";
244                                 } else {
245                                         // Don't load CSS file
246                                         $content['ext_css'] = "N";
247                                 }
248                         } // END - if
249
250                         // Shall we cache?
251                         if ($GLOBALS['cacheMode'] == "init") {
252                                 // Add cache row
253                                 $GLOBALS['cache_instance']->addRow($content);
254                         } elseif ($GLOBALS['cacheMode'] == "no") {
255                                 // Remember this value for later usage
256                                 $GLOBALS['cache_array']['active_extensions'][$content['ext_name']] = $EXT_ALWAYS_ACTIVE;
257                         }
258                 } elseif (!FILE_READABLE($FQFN)) {
259                         // Deleted extension file so we mark it for removal from DB
260                         $DEL[] = $content['ext_name'];
261                 }
262         } // END - while
263
264         // Init filter system
265         INIT_FILTER_SYSTEM();
266
267         if ($GLOBALS['cacheMode'] == "init") {
268                 // Close cache file
269                 $GLOBALS['cache_instance']->finalize();
270
271                 // Load more cache files (like admins)
272                 LOAD_INC_ONCE("inc/load_cache.php");
273         } // END - if
274
275         // Free memory
276         SQL_FREERESULT($res_ext_crt);
277 } elseif (!EXT_IS_ACTIVE("cache")) {
278         // Init filter system even when there are no extensions installed. #16
279         INIT_FILTER_SYSTEM();
280 }
281
282 // Run the filter
283 RUN_FILTER('load_includes', $INC_POOL);
284
285 // Uninstall extensions that are no longer in our system
286 if (!empty($DEL[0])) {
287         // Remove extensions from two tables: extension registry and tasks table
288         foreach ($DEL as $del_ext) {
289                 // First remove entry from extensions table
290                 SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{!_MYSQL_PREFIX!}_extensions` WHERE ext_name='%s' LIMIT 1",
291                         array($del_ext), __FILE__, __LINE__);
292
293                 // Remove (maybe?) found tasks (main task and possible updates
294                 SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{!_MYSQL_PREFIX!}_task_system` WHERE subject='[%s:]' AND (task_type='EXTENSION' OR task_type='EXTENSION_UPDATE')",
295                         array($del_ext), __FILE__, __LINE__);
296         } // END - foreach
297
298         // I think it's not neccessary to run the optimization function here
299         // because we didn't delete so much data from database. Can you aggree?
300 } // END - if
301
302 //
303 ?>