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