dd8c722038b1ea7c5a1294a041e6114c6d9ba3c3
[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  * @TODO Rewrite this whole file to load_cache-extensions.php           *
14  * -------------------------------------------------------------------- *
15  *                                                                      *
16  * -------------------------------------------------------------------- *
17  * Copyright (c) 2003 - 2008 by Roland Haeder                           *
18  * For more information visit: http://www.mxchange.org                  *
19  *                                                                      *
20  * This program is free software; you can redistribute it and/or modify *
21  * it under the terms of the GNU General Public License as published by *
22  * the Free Software Foundation; either version 2 of the License, or    *
23  * (at your option) any later version.                                  *
24  *                                                                      *
25  * This program is distributed in the hope that it will be useful,      *
26  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
27  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
28  * GNU General Public License for more details.                         *
29  *                                                                      *
30  * You should have received a copy of the GNU General Public License    *
31  * along with this program; if not, write to the Free Software          *
32  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
33  * MA  02110-1301  USA                                                  *
34  ************************************************************************/
35
36 // Some security stuff...
37 if (!defined('__SECURITY')) {
38         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
39         require($INC);
40 }
41
42 // Init variables
43 EXT_INIT_CSS_FILES();
44 $ADD = "";
45
46 // Init cache instance and array
47 $GLOBALS['cache_instance'] = null;
48 $GLOBALS['cache_array'] = array();
49
50 // Skip loading extensions
51 if ((!isInstalled()) || (isInstalling())) {
52         // Init filter system here
53         INIT_FILTER_SYSTEM();
54
55         // Skip loading
56         return;
57 } // END - if
58
59 // Initialize array for "always keep active extensions"
60 $GLOBALS['cache_array']['active_extensions'] = array();
61
62 // By default no cache is set
63 $GLOBALS['cacheMode'] = "no";
64
65 // Load sql_patchrs extension alone
66 if (!LOAD_EXTENSION("sql_patches")) {
67         // Could not load sql_patches! ARGH!!!
68         trigger_error("Cannot load extension sql_patches.");
69 } // END - if
70
71 // Init inc pool array
72 $INC_POOL = array();
73
74 //
75 // Load extensions
76 //
77 if (EXT_IS_ACTIVE("cache")) {
78         // Load cache extension alone
79         if (!LOAD_EXTENSION("cache")) {
80                 // Extension 'cache' was not loaded
81                 trigger_error("Cannot load extension cache.");
82         } // END - if
83
84         // Check extension cache
85         switch (($GLOBALS['cache_instance']->loadCacheFile("extensions", true)) && ($GLOBALS['cache_instance']->extensionVersionMatches("sql_patches"))) {
86                 case true : $GLOBALS['cacheMode'] = "load"; break;
87                 case false: $GLOBALS['cacheMode'] = "init"; break;
88         }
89
90         // Do we need to init the cache?
91         if (($GLOBALS['cacheMode'] == "init") && (getConfig('cache_exts') == "Y")) {
92                 // Init cache file
93                 $GLOBALS['cache_instance']->init("EXTENSIONS");
94                 $GLOBALS['cache_instance']->storeExtensionVersion("sql_patches");
95         } elseif (getConfig('cache_exts') != "Y") {
96                 // Cache will not be created for extensions
97                 $GLOBALS['cacheMode'] = "skip";
98         }
99 } // END - if
100
101 // Load cache?
102 if ($GLOBALS['cacheMode'] == "load") {
103         // Init include array
104         $EXT_POOL = array();
105
106         // Re-initialize handler
107         $GLOBALS['cache_instance']->loadCacheFile("extensions", true);
108
109         // Load extension data from cache file
110         $EXT_DUMMY = $GLOBALS['cache_instance']->getArrayFromCache();
111
112         // Is the cache file fine?
113         if (!isset($EXT_DUMMY['ext_name'])) {
114                 // Cache file is damaged so kill it
115                 $GLOBALS['cache_instance']->destroyCacheFile();
116
117                 // Retry it
118                 require(__FILE__);
119                 return;
120         } // END -  if
121
122         // Begin with the cache preparation of extensions
123         $EXT_NAMES = array();
124         foreach ($EXT_DUMMY['ext_name'] as $k => $name) {
125                 // Load CSS file
126                 if ($EXT_DUMMY['ext_css'][$k] == "Y") EXT_ADD_CSS_FILE("".$name.".css");
127
128                 // Load extension file itself
129                 if ((($EXT_DUMMY['ext_active'][$k] == "Y") || ($EXT_DUMMY['ext_keep'][$k] == "Y") || (IS_ADMIN())) && (!in_array($name, array("sql_patches", "cache")))) {
130                         $EXT_POOL[] = $name;
131                 } // END - if
132
133                 // Version number
134                 $EXT_DUMMY['ext_version'][$name] = $EXT_DUMMY['ext_version'][$k];
135                 unset($EXT_DUMMY['ext_version'][$k]);
136
137                 // Extension is active
138                 $EXT_DUMMY['ext_active'][$name] = $EXT_DUMMY['ext_active'][$k];
139                 unset($EXT_DUMMY['ext_active'][$k]);
140
141                 // Ext menu
142                 $EXT_DUMMY['ext_menu'][$name] = $EXT_DUMMY['ext_menu'][$k];
143                 unset($EXT_DUMMY['ext_menu'][$k]);
144
145                 // Extension id
146                 $EXT_DUMMY['ext_id'][$name] = $EXT_DUMMY['ext_id'][$k];
147                 $id = $EXT_DUMMY['ext_id'][$name];
148                 unset($EXT_DUMMY['ext_id'][$k]);
149
150                 // Add ext name
151                 $EXT_NAMES[$id] = $name;
152
153                 // Add deprecated flag (defaults to "not deprecated")
154                 $EXT_DUMMY['ext_deprecated'][$name] = "N";
155
156                 // Mark it as active extension
157                 $GLOBALS['cache_array']['active_extensions']['$name'] = $EXT_DUMMY['ext_keep'][$k];
158                 unset($EXT_DUMMY['ext_keep'][$k]);
159
160                 // Remove unneccessary data from memory
161                 unset($EXT_DUMMY['ext_css'][$k]);
162         } // END - foreach
163
164         // Write dummy array back
165         $EXT_DUMMY['ext_name'] = $EXT_NAMES;
166         unset($EXT_NAMES);
167
168         // Loading cache is done so let's free some memory!
169         unset($EXT_DUMMY['ext_keep']);
170         unset($EXT_DUMMY['ext_css']);
171         $GLOBALS['cache_array']['extensions'] = $EXT_DUMMY;
172         unset($EXT_DUMMY);
173
174         // No database load needed
175         $res_ext_crt = false;
176
177         // Load all extension files
178         foreach ($EXT_POOL as $ext) {
179                 LOAD_EXTENSION($ext);
180         } // END - foreach
181
182         // Init filter system
183         INIT_FILTER_SYSTEM();
184
185         // Load more cache files (like admins)
186         LOAD_INC_ONCE("inc/load_cache.php");
187
188         // Remove array
189         unset($EXT_POOL);
190 } else {
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()) && ($GLOBALS['cacheMode'] != "init")) $ADD = " WHERE ext_active='Y'";
195
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_has_css AS ext_css, ext_active, ext_version
199 FROM `{!_MYSQL_PREFIX!}_extensions`".$ADD."
200 ORDER BY ext_name", __FILE__, __LINE__);
201         } else {
202                 // Old obsolete query string
203                 $res_ext_crt = SQL_QUERY("SELECT id AS ext_id, ext_name, ext_name, ext_active, ext_version
204 FROM `{!_MYSQL_PREFIX!}_extensions`".$ADD."
205 ORDER BY ext_name", __FILE__, __LINE__);
206         }
207 }
208
209 // Array for removed but not uninstalled extensions
210 $DEL = array();
211
212 // At least one found?
213 if ((SQL_NUMROWS($res_ext_crt) > 0) && ((($GLOBALS['cacheMode'] == "init") && ($GLOBALS['output_mode'] != "1") && ($GLOBALS['output_mode'] != "-1")) || ($GLOBALS['cacheMode'] == "no"))) {
214         // Extensions are registered so we load them
215         while ($content = SQL_FETCHARRAY($res_ext_crt)) {
216                 // Get menu entry
217                 $content['ext_menu'] = "N";
218                 if (MODULE_HAS_MENU($content['ext_name'], true)) {
219                         $content['ext_menu'] = "Y";
220                 } // END - if
221
222                 // Generate FQFN for extension
223                 $FQFN = sprintf("%sinc/extensions/ext-%s.php", constant('PATH'), $content['ext_name']);
224
225                 // Does the extension file exists?
226                 if (FILE_READABLE($FQFN)) {
227                         // By default no extension is always active, except sql_patches
228                         EXT_SET_ALWAYS_ACTIVE("N");
229
230                         // Load extension
231                         if (($content['ext_name'] != "sql_patches") && (($content['ext_name'] != "cache") || (!EXT_IS_ACTIVE("cache")))) {
232                                 // Load extension
233                                 LOAD_EXTENSION($content['ext_name']);
234                         } else {
235                                 // Keep sql_patches always active
236                                 EXT_SET_ALWAYS_ACTIVE("Y");
237                         }
238
239                         // Transfer EXT_ALWAYS_ACTIVE flag
240                         $content['ext_keep'] = EXT_GET_ALWAYS_ACTIVE();
241
242                         // CSS file handling:
243                         if ((!isset($content['ext_css'])) || ($content['ext_css'] == "Y")) {
244                                 // Create FQFN for the CSS file
245                                 $FQFN = sprintf("%stheme/%s/css/%s.css", constant('PATH'), GET_CURR_THEME(), $content['ext_name']);
246
247                                 // Is the file there?
248                                 if (FILE_READABLE($FQFN)) {
249                                         // CSS file for extension was found (use only relative path for now!)
250                                         EXT_ADD_CSS_FILE($content['ext_name'].".css");
251                                         $content['ext_css'] = "Y";
252                                 } else {
253                                         // Don't load CSS file
254                                         $content['ext_css'] = "N";
255                                 }
256                         } // END - if
257
258                         // Shall we cache?
259                         if ($GLOBALS['cacheMode'] == "init") {
260                                 // Add cache row
261                                 $GLOBALS['cache_instance']->addRow($content);
262                         } elseif ($GLOBALS['cacheMode'] == "no") {
263                                 // Remember this value for later usage
264                                 $GLOBALS['cache_array']['active_extensions'][$content['ext_name']] = EXT_GET_ALWAYS_ACTIVE();
265                         }
266                 } elseif (!FILE_READABLE($FQFN)) {
267                         // Deleted extension file so we mark it for removal from DB
268                         $DEL[] = $content['ext_name'];
269                 }
270         } // END - while
271
272         // Init filter system
273         INIT_FILTER_SYSTEM();
274
275         if ($GLOBALS['cacheMode'] == "init") {
276                 // Close cache file
277                 $GLOBALS['cache_instance']->finalize();
278
279                 // Load more cache files (like admins)
280                 LOAD_INC_ONCE("inc/load_cache.php");
281         } // END - if
282
283         // Free memory
284         SQL_FREERESULT($res_ext_crt);
285 } elseif (!EXT_IS_ACTIVE("cache")) {
286         // Init filter system even when there are no extensions installed. #16
287         INIT_FILTER_SYSTEM();
288 }
289
290 // Run the filter
291 runFilterChain('load_includes', $INC_POOL);
292
293 // Uninstall extensions that are no longer in our system
294 if (!empty($DEL[0])) {
295         // Remove extensions from two tables: extension registry and tasks table
296         foreach ($DEL as $del_ext) {
297                 // First remove entry from extensions table
298                 SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{!_MYSQL_PREFIX!}_extensions` WHERE ext_name='%s' LIMIT 1",
299                         array($del_ext), __FILE__, __LINE__);
300
301                 // Remove (maybe?) found tasks (main task and possible updates
302                 SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{!_MYSQL_PREFIX!}_task_system` WHERE subject='[%s:]' AND (task_type='EXTENSION' OR task_type='EXTENSION_UPDATE')",
303                         array($del_ext), __FILE__, __LINE__);
304         } // END - foreach
305
306         // I think it's not neccessary to run the optimization function here
307         // because we didn't delete so much data from database. Can you aggree?
308 } // END - if
309
310 //
311 ?>