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";
41 global $EXT_CSS_FILES;
42 $EXT_CSS_FILES = array();
45 // Skip loading extensions
46 if ((!isBooleanConstantAndTrue('mxchange_installed')) || (isBooleanConstantAndTrue('mxchange_installing'))) return;
48 // Load default sql_patches extension if present
49 if (FILE_READABLE(PATH."inc/extensions/ext-sql_patches.php")) {
52 require_once(PATH."inc/extensions/ext-sql_patches.php");
53 $cacheArray['active_extensions'] = array('sql_patches' => 'Y'); // KEEP THIS ALWAYS ACTIVE!
55 // Initialize array for "always keep active extensions"
56 $cacheArray['active_extensions'] = array();
59 // By default no cache is set
65 if (EXT_IS_ACTIVE("cache")) {
66 // Load cache extension alone
67 include_once(PATH."inc/libs/cache_functions.php");
68 include_once(PATH."inc/extensions/ext-cache.php");
70 // Check extension cache
71 switch (($cacheInstance->loadCacheFile("extensions", true)) && ($cacheInstance->extensionVersionMatches("sql_patches"))) {
72 case true : $cacheMode = "load"; break;
73 case false: $cacheMode = "init"; break;
76 // Do we need to init the cache?
77 if (($cacheMode == "init") && ($_CONFIG['cache_exts'] == "Y")) {
79 $cacheInstance->init("EXTENSIONS");
80 $cacheInstance->storeExtensionVersion("sql_patches");
81 } elseif ($_CONFIG['cache_exts'] == "N") {
82 // Cache will not be created for extensions
88 if ($cacheMode == "load") {
92 // Re-initialize handler
93 $cacheInstance->loadCacheFile("extensions", true);
95 // Load extension data from cache file
96 $EXT_DUMMY = $cacheInstance->getArrayFromCache();
98 // Is the cache file fine?
99 if (!isset($EXT_DUMMY['ext_name'])) {
100 // Cache file is damaged so kill it
101 $cacheInstance->destroyCacheFile();
103 // Skip any further execution
107 // Begin with the cache preparation of extensions
108 $EXT_NAMES = array();
109 foreach ($EXT_DUMMY['ext_name'] as $k => $name) {
110 // Load functions file
111 if ($EXT_DUMMY['ext_funcs'][$k] == "Y") {
112 require_once(PATH."inc/libs/".$name."_functions.php");
115 // Load Language file
116 if ($EXT_DUMMY['ext_lang'][$k] == "Y") {
117 $INC = sprintf("%sinc/language/%s_%s.php", PATH, $name, GET_LANGUAGE());
118 if (FILE_READABLE($INC)) {
125 if ($EXT_DUMMY['ext_css'][$k] == "Y") $EXT_CSS_FILES[] = "".$name.".css";
127 // Load extension file itself
128 if (($EXT_DUMMY['ext_active'][$k] == "Y") || ($EXT_DUMMY['ext_keep'][$k] == "Y") || (IS_ADMIN())) {
129 $EXT_POOL[] = sprintf("%sinc/extensions/ext-%s.php", PATH, $name);
133 $EXT_DUMMY['ext_version'][$name] = $EXT_DUMMY['ext_version'][$k];
134 unset($EXT_DUMMY['ext_version'][$k]);
136 // Extension is active
137 $EXT_DUMMY['ext_active'][$name] = $EXT_DUMMY['ext_active'][$k];
138 unset($EXT_DUMMY['ext_active'][$k]);
141 $EXT_DUMMY['ext_menu'][$name] = $EXT_DUMMY['ext_menu'][$k];
142 unset($EXT_DUMMY['ext_menu'][$k]);
145 $EXT_DUMMY['ext_id'][$name] = $EXT_DUMMY['ext_id'][$k];
146 $id = $EXT_DUMMY['ext_id'][$name];
147 unset($EXT_DUMMY['ext_id'][$k]);
150 $EXT_NAMES[$id] = $name;
152 // Add deprecated flag (defaults to "not deprecated")
153 $EXT_DUMMY['ext_deprecated'][$name] = "N";
155 // Mark it as active extension
156 $cacheArray['active_extensions']['$name'] = $EXT_DUMMY['ext_keep'][$k];
157 unset($EXT_DUMMY['ext_keep'][$k]);
159 // Remove unneccessary data from memory
160 unset($EXT_DUMMY['ext_lang'][$k]);
161 unset($EXT_DUMMY['ext_css'][$k]);
162 unset($EXT_DUMMY['ext_funcs'][$k]);
165 // Write dummy array back
166 $EXT_DUMMY['ext_name'] = $EXT_NAMES;
169 // Loading cache is done so let's free some memory!
170 unset($EXT_DUMMY['ext_lang']);
171 unset($EXT_DUMMY['ext_keep']);
172 unset($EXT_DUMMY['ext_css']);
173 unset($EXT_DUMMY['ext_funcs']);
174 $cacheArray['extensions'] = $EXT_DUMMY;
177 // No database load needed
178 $res_ext_crt = false;
180 // Load more cache files (like admins)
181 require_once(PATH."inc/load_cache.php");
183 // Load all extension files
184 foreach ($EXT_POOL as $inc) {
191 // If current user is not admin load only activated extensions. But load
192 // them all if we are going to init the cache files. The admin shall use
193 // every available extension for testing purposes.
194 if ((!IS_ADMIN()) && ($cacheMode != "init")) $ADD = " WHERE ext_active='Y'";
196 if (GET_EXT_VERSION("sql_patches") >= "0.0.6") {
197 // Query with CSS file from DB
198 $res_ext_crt = SQL_QUERY("SELECT id AS ext_id, ext_name, ext_lang_file AS ext_lang, ext_has_css AS ext_css, ext_active, ext_version
199 FROM "._MYSQL_PREFIX."_extensions".$ADD."
200 ORDER BY ext_name", __FILE__, __LINE__);
202 // Old obsolete query string
203 $res_ext_crt = SQL_QUERY("SELECT id AS ext_id, ext_name, ext_lang_file AS ext_lang, ext_name, ext_active, ext_version
204 FROM "._MYSQL_PREFIX."_extensions".$ADD."
205 ORDER BY ext_name", __FILE__, __LINE__);
209 // Array for removed but not uninstalled extensions
212 // At least one found?
213 if ((SQL_NUMROWS($res_ext_crt) > 0) && ((($cacheMode == "init") && ($CSS != "1") && ($CSS != "-1")) || ($cacheMode == "no"))) {
214 // Load theme management
215 require_once(PATH."inc/theme-manager.php");
217 // Extensions are registered so we load them
218 while ($content = SQL_FETCHARRAY($res_ext_crt)) {
220 $content['ext_menu'] = "N";
221 if (MODULE_HAS_MENU($content['ext_name'], true)) {
222 $content['ext_menu'] = "Y";
226 $file1 = sprintf("%sinc/extensions/ext-%s.php", PATH, $content['ext_name']);
227 $file2 = $file1; $EXT_CSS = "N"; $EXT_ALWAYS_ACTIVE = "N";
229 // Special functions file
230 $file3 = sprintf("%sinc/libs/%s_functions.php", PATH, $content['ext_name']);
232 // Does the extension file exists?
233 if (FILE_READABLE($file1)) {
234 // If there's no language file specified we don't need to load one... ;-)
235 if (!empty($content['ext_lang'])) {
236 // Create language file
237 $file2 = sprintf("%sinc/language/%s_%s.php", PATH, $content['ext_lang'], GET_LANGUAGE());
240 if (FILE_READABLE($file3)) {
241 // Special functions file
242 $content['ext_funcs'] = "Y";
243 require_once($file3);
245 // Don't load functions file
246 $content['ext_funcs'] = "N";
249 // Do we need a language file?
250 if (($file1 != $file2) && (FILE_READABLE($file2))) {
251 // Load language file
252 $content['ext_lang'] = "Y";
255 // Don't load language file
256 $content['ext_lang'] = "N";
259 // By default no extension is always active, except sql_patches
260 $EXT_ALWAYS_ACTIVE = "N";
263 if ($content['ext_name'] != "sql_patches") {
264 // Load extension's file
265 include_once($file1);
267 // Keep sql_patches always active
268 $EXT_ALWAYS_ACTIVE = "Y";
271 // Transfer EXT_ALWAYS_ACTIVE flag
272 $content['ext_keep'] = $EXT_ALWAYS_ACTIVE;
274 // CSS file handling:
275 if ((!isset($content['ext_css'])) || ($content['ext_css'] == "Y")) {
276 // Create FQFN for the CSS file
277 $CSS_FILE = sprintf("%stheme/%s/css/%s.css", PATH, GET_CURR_THEME(), $content['ext_name']);
279 // Is the file there?
280 if (FILE_READABLE($CSS_FILE)) {
281 // CSS file for extension was found (use only relative path for now!)
282 $EXT_CSS_FILES[] = $content['ext_name'].".css";
283 $content['ext_css'] = "Y";
285 // Don't load CSS file
286 $content['ext_css'] = "N";
291 if ($cacheMode == "init") {
293 $cacheInstance->addRow($content);
294 } elseif ($cacheMode == "no") {
295 // Remember this value for later usage
296 $cacheArray['active_extensions'][$content['ext_name']] = $EXT_ALWAYS_ACTIVE;
298 } elseif (!FILE_READABLE($file1)) {
299 // Deleted extension file so we mark it for removal from DB
300 $DEL[] = $content['ext_name'];
304 if ($cacheMode == "init") {
306 $cacheInstance->finalize();
308 // Load more cache files (like admins)
309 require_once(PATH."inc/load_cache.php");
313 SQL_FREERESULT($res_ext_crt);
316 // Load include files if found
317 if (!empty($INC_POOL[0])) {
318 foreach ($INC_POOL as $inc) {
326 // Uninstall extensions that are no longer in our system
327 if (!empty($DEL[0])) {
328 // Remove extensions from two tables: extension registry and tasks table
329 foreach ($DEL as $del_ext) {
330 // First remove entry from extensions table
331 SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM "._MYSQL_PREFIX."_extensions WHERE ext_name='%s' LIMIT 1",
332 array($del_ext), __FILE__, __LINE__);
334 // Remove (maybe?) found tasks (main task and possible updates
335 SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM "._MYSQL_PREFIX."_task_system WHERE subject LIKE '[%s:]%%' AND (task_type='EXTENSION' OR task_type='EXTENSION_UPDATE')",
336 array($del_ext), __FILE__, __LINE__);
339 // I think it's not neccessary to run the optimization function here
340 // because we didn't delete so much data from database. Can you aggree?