2 /************************************************************************
3 * MXChange v0.2.1 Start: 06/26/2004 *
4 * =============== Last change: 07/01/2005 *
6 * -------------------------------------------------------------------- *
7 * File : load_extensions.php *
8 * -------------------------------------------------------------------- *
9 * Short description : Load all extensions *
10 * -------------------------------------------------------------------- *
11 * Kurzbeschreibung : Alle Erweiterungen laden *
12 * -------------------------------------------------------------------- *
14 * -------------------------------------------------------------------- *
15 * Copyright (c) 2003 - 2008 by Roland Haeder *
16 * For more information visit: http://www.mxchange.org *
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. *
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. *
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, *
32 ************************************************************************/
34 // Some security stuff...
35 if (!defined('__SECURITY')) {
36 $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
44 // Init cache instance and array
45 $GLOBALS['cache_instance'] = null;
46 $GLOBALS['cache_array'] = array();
48 // Skip loading extensions
49 if ((!isBooleanConstantAndTrue('mxchange_installed')) || (isBooleanConstantAndTrue('mxchange_installing'))) return;
51 // Initialize array for "always keep active extensions"
52 $GLOBALS['cache_array']['active_extensions'] = array();
54 // By default no cache is set
55 $GLOBALS['cacheMode'] = "no";
57 // Load sql_patchrs extension alone
58 LOAD_EXTENSION("sql_patches");
60 // Init inc pool array
66 if (EXT_IS_ACTIVE("cache")) {
67 // Load cache extension alone
68 LOAD_EXTENSION("cache");
70 // Check extension cache
71 switch (($GLOBALS['cache_instance']->loadCacheFile("extensions", true)) && ($GLOBALS['cache_instance']->extensionVersionMatches("sql_patches"))) {
72 case true : $GLOBALS['cacheMode'] = "load"; break;
73 case false: $GLOBALS['cacheMode'] = "init"; break;
76 // Do we need to init the cache?
77 if (($GLOBALS['cacheMode'] == "init") && (getConfig('cache_exts') == "Y")) {
79 $GLOBALS['cache_instance']->init("EXTENSIONS");
80 $GLOBALS['cache_instance']->storeExtensionVersion("sql_patches");
81 } elseif (getConfig('cache_exts') != "Y") {
82 // Cache will not be created for extensions
83 $GLOBALS['cacheMode'] = "skip";
88 if ($GLOBALS['cacheMode'] == "load") {
92 // Re-initialize handler
93 $GLOBALS['cache_instance']->loadCacheFile("extensions", true);
95 // Load extension data from cache file
96 $EXT_DUMMY = $GLOBALS['cache_instance']->getArrayFromCache();
98 // Is the cache file fine?
99 if (!isset($EXT_DUMMY['ext_name'])) {
100 // Cache file is damaged so kill it
101 $GLOBALS['cache_instance']->destroyCacheFile();
108 // Begin with the cache preparation of extensions
109 $EXT_NAMES = array();
110 foreach ($EXT_DUMMY['ext_name'] as $k => $name) {
112 if ($EXT_DUMMY['ext_css'][$k] == "Y") EXT_ADD_CSS_FILE("".$name.".css");
114 // Load extension file itself
115 if ((($EXT_DUMMY['ext_active'][$k] == "Y") || ($EXT_DUMMY['ext_keep'][$k] == "Y") || (IS_ADMIN())) && (!in_array($name, array("sql_patches", "cache")))) {
120 $EXT_DUMMY['ext_version'][$name] = $EXT_DUMMY['ext_version'][$k];
121 unset($EXT_DUMMY['ext_version'][$k]);
123 // Extension is active
124 $EXT_DUMMY['ext_active'][$name] = $EXT_DUMMY['ext_active'][$k];
125 unset($EXT_DUMMY['ext_active'][$k]);
128 $EXT_DUMMY['ext_menu'][$name] = $EXT_DUMMY['ext_menu'][$k];
129 unset($EXT_DUMMY['ext_menu'][$k]);
132 $EXT_DUMMY['ext_id'][$name] = $EXT_DUMMY['ext_id'][$k];
133 $id = $EXT_DUMMY['ext_id'][$name];
134 unset($EXT_DUMMY['ext_id'][$k]);
137 $EXT_NAMES[$id] = $name;
139 // Add deprecated flag (defaults to "not deprecated")
140 $EXT_DUMMY['ext_deprecated'][$name] = "N";
142 // Mark it as active extension
143 $GLOBALS['cache_array']['active_extensions']['$name'] = $EXT_DUMMY['ext_keep'][$k];
144 unset($EXT_DUMMY['ext_keep'][$k]);
146 // Remove unneccessary data from memory
147 unset($EXT_DUMMY['ext_css'][$k]);
150 // Write dummy array back
151 $EXT_DUMMY['ext_name'] = $EXT_NAMES;
154 // Loading cache is done so let's free some memory!
155 unset($EXT_DUMMY['ext_keep']);
156 unset($EXT_DUMMY['ext_css']);
157 $GLOBALS['cache_array']['extensions'] = $EXT_DUMMY;
160 // No database load needed
161 $res_ext_crt = false;
163 // Load all extension files
164 foreach ($EXT_POOL as $ext) {
165 LOAD_EXTENSION($ext);
168 // Init filter system
169 INIT_FILTER_SYSTEM();
171 // Load more cache files (like admins)
172 LOAD_INC_ONCE("inc/load_cache.php");
177 // If current user is not admin load only activated extensions. But load
178 // them all if we are going to init the cache files. The admin shall use
179 // every available extension for testing purposes.
180 if ((!IS_ADMIN()) && ($GLOBALS['cacheMode'] != "init")) $ADD = " WHERE ext_active='Y'";
182 if (GET_EXT_VERSION("sql_patches") >= "0.0.6") {
183 // Query with CSS file from DB
184 $res_ext_crt = SQL_QUERY("SELECT id AS ext_id, ext_name, ext_has_css AS ext_css, ext_active, ext_version
185 FROM `{!_MYSQL_PREFIX!}_extensions`".$ADD."
186 ORDER BY ext_name", __FILE__, __LINE__);
188 // Old obsolete query string
189 $res_ext_crt = SQL_QUERY("SELECT id AS ext_id, ext_name, ext_name, ext_active, ext_version
190 FROM `{!_MYSQL_PREFIX!}_extensions`".$ADD."
191 ORDER BY ext_name", __FILE__, __LINE__);
195 // Array for removed but not uninstalled extensions
198 // At least one found?
199 if ((SQL_NUMROWS($res_ext_crt) > 0) && ((($GLOBALS['cacheMode'] == "init") && ($GLOBALS['output_mode'] != "1") && ($GLOBALS['output_mode'] != "-1")) || ($GLOBALS['cacheMode'] == "no"))) {
200 // Extensions are registered so we load them
201 while ($content = SQL_FETCHARRAY($res_ext_crt)) {
203 $content['ext_menu'] = "N";
204 if (MODULE_HAS_MENU($content['ext_name'], true)) {
205 $content['ext_menu'] = "Y";
208 // Generate FQFN for extension
209 $FQFN = sprintf("%sinc/extensions/ext-%s.php", constant('PATH'), $content['ext_name']);
211 // Does the extension file exists?
212 if (FILE_READABLE($FQFN)) {
213 // By default no extension is always active, except sql_patches
214 $EXT_ALWAYS_ACTIVE = "N";
217 if (($content['ext_name'] != "sql_patches") && (($content['ext_name'] != "cache") || (!EXT_IS_ACTIVE("cache")))) {
219 LOAD_EXTENSION($content['ext_name']);
221 // Keep sql_patches always active
222 $EXT_ALWAYS_ACTIVE = "Y";
225 // Transfer EXT_ALWAYS_ACTIVE flag
226 $content['ext_keep'] = $EXT_ALWAYS_ACTIVE;
228 // CSS file handling:
229 if ((!isset($content['ext_css'])) || ($content['ext_css'] == "Y")) {
230 // Create FQFN for the CSS file
231 $FQFN = sprintf("%stheme/%s/css/%s.css", constant('PATH'), GET_CURR_THEME(), $content['ext_name']);
233 // Is the file there?
234 if (FILE_READABLE($FQFN)) {
235 // CSS file for extension was found (use only relative path for now!)
236 EXT_ADD_CSS_FILE($content['ext_name'].".css");
237 $content['ext_css'] = "Y";
239 // Don't load CSS file
240 $content['ext_css'] = "N";
245 if ($GLOBALS['cacheMode'] == "init") {
247 $GLOBALS['cache_instance']->addRow($content);
248 } elseif ($GLOBALS['cacheMode'] == "no") {
249 // Remember this value for later usage
250 $GLOBALS['cache_array']['active_extensions'][$content['ext_name']] = $EXT_ALWAYS_ACTIVE;
252 } elseif (!FILE_READABLE($FQFN)) {
253 // Deleted extension file so we mark it for removal from DB
254 $DEL[] = $content['ext_name'];
258 // Init filter system
259 INIT_FILTER_SYSTEM();
261 if ($GLOBALS['cacheMode'] == "init") {
263 $GLOBALS['cache_instance']->finalize();
265 // Load more cache files (like admins)
266 LOAD_INC_ONCE("inc/load_cache.php");
270 SQL_FREERESULT($res_ext_crt);
271 } elseif (!EXT_IS_ACTIVE("cache")) {
272 // Init filter system even when there are no extensions installed. #16
273 INIT_FILTER_SYSTEM();
277 RUN_FILTER('load_includes', $INC_POOL);
279 // Uninstall extensions that are no longer in our system
280 if (!empty($DEL[0])) {
281 // Remove extensions from two tables: extension registry and tasks table
282 foreach ($DEL as $del_ext) {
283 // First remove entry from extensions table
284 SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{!_MYSQL_PREFIX!}_extensions` WHERE ext_name='%s' LIMIT 1",
285 array($del_ext), __FILE__, __LINE__);
287 // Remove (maybe?) found tasks (main task and possible updates
288 SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{!_MYSQL_PREFIX!}_task_system` WHERE subject='[%s:]' AND (task_type='EXTENSION' OR task_type='EXTENSION_UPDATE')",
289 array($del_ext), __FILE__, __LINE__);
292 // I think it's not neccessary to run the optimization function here
293 // because we didn't delete so much data from database. Can you aggree?