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