1f443670ac371dc20d0f1cc994a5c5efbeb55479
[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 // Load default sql_patches extension if present
49 if (FILE_READABLE(PATH."inc/extensions/ext-sql_patches.php")) {
50         // Load it...
51         $EXT_LOAD_MODE = "";
52         require_once(PATH."inc/extensions/ext-sql_patches.php");
53         $cacheArray['active_extensions'] = array('sql_patches' => 'Y'); // KEEP THIS ALWAYS ACTIVE!
54 } else {
55         // Initialize array for "always keep active extensions"
56         $cacheArray['active_extensions'] = array();
57 }
58
59 // By default no cache is set
60 $cacheMode = "no";
61
62 //
63 // Load extensions
64 //
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");
69
70         // Check extension cache
71         switch (($cacheInstance->cache_file("extensions", true)) && ($cacheInstance->ext_version_matches("sql_patches"))) {
72                 case true : $cacheMode = "load"; break;
73                 case false: $cacheMode = "init"; break;
74         }
75
76         // Do we need to init the cache?
77         if (($cacheMode == "init") && ($_CONFIG['cache_exts'] == "Y")) {
78                 // Init cache file
79                 $cacheInstance->cache_init("EXTENSIONS");
80                 $cacheInstance->store_extension_version("sql_patches");
81         } elseif ($_CONFIG['cache_exts'] == "N") {
82                 // Cache will not be created for extensions
83                 $cacheMode = "skip";
84         }
85 } // END - if
86
87 // Load cache?
88 if ($cacheMode == "load") {
89         // Init include array
90         $EXT_POOL = array();
91
92         // Re-initialize handler
93         $cacheInstance->cache_file("extensions", true);
94
95         // Load extension data from cache file
96         $EXT_DUMMY = $cacheInstance->cache_load();
97
98         // Is the cache file fine?
99         if (!isset($EXT_DUMMY['ext_name'])) {
100                 // Cache file is damaged so kill it
101                 $cacheInstance->cache_destroy();
102
103                 // Skip any further execution
104                 return;
105         } // END -  if
106
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");
113                 } // END - if
114
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)) {
119                                 // Add it
120                                 $EXT_POOL[] = $INC;
121                         } // END -  if
122                 } // END - if
123
124                 // Load CSS file
125                 if ($EXT_DUMMY['ext_css'][$k] == "Y") $EXT_CSS_FILES[] = "".$name.".css";
126
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);
130                 } // END - if
131
132                 // Version number
133                 $EXT_DUMMY['ext_version'][$name] = $EXT_DUMMY['ext_version'][$k];
134                 unset($EXT_DUMMY['ext_version'][$k]);
135
136                 // Extension is active
137                 $EXT_DUMMY['ext_active'][$name] = $EXT_DUMMY['ext_active'][$k];
138                 unset($EXT_DUMMY['ext_active'][$k]);
139
140                 // Ext menu
141                 $EXT_DUMMY['ext_menu'][$name] = $EXT_DUMMY['ext_menu'][$k];
142                 unset($EXT_DUMMY['ext_menu'][$k]);
143
144                 // Extension id
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]);
148
149                 // Add ext name
150                 $EXT_NAMES[$id] = $name;
151
152                 // Add deprecated flag (defaults to "not deprecated")
153                 $EXT_DUMMY['ext_deprecated'][$name] = "N";
154
155                 // Mark it as active extension
156                 $cacheArray['active_extensions']['$name'] = $EXT_DUMMY['ext_keep'][$k];
157                 unset($EXT_DUMMY['ext_keep'][$k]);
158
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]);
163         } // END - foreach
164
165         // Write dummy array back
166         $EXT_DUMMY['ext_name'] = $EXT_NAMES;
167         unset($EXT_NAMES);
168
169         // Close cache file
170         $cacheInstance->cache_close();
171
172         // Loading cache is done so let's free some memory!
173         unset($EXT_DUMMY['ext_lang']);
174         unset($EXT_DUMMY['ext_keep']);
175         unset($EXT_DUMMY['ext_css']);
176         unset($EXT_DUMMY['ext_funcs']);
177         $cacheArray['extensions'] = $EXT_DUMMY;
178         unset($EXT_DUMMY);
179
180         // No database load needed
181         $res_ext_crt = false;
182
183         // Load more cache files (like admins)
184         require_once(PATH."inc/load_cache.php");
185
186         // Load all extension files
187         foreach ($EXT_POOL as $inc) {
188                 require_once($inc);
189         } // END - foreach
190
191         // Remove array
192         unset($EXT_POOL);
193 } else {
194         // If current user is not admin load only activated extensions. But load
195         // them all if we are going to init the cache files. The admin shall use
196         // every available extension for testing purposes.
197         if ((!IS_ADMIN()) && ($cacheMode != "init")) $ADD = " WHERE ext_active='Y'";
198
199         if (GET_EXT_VERSION("sql_patches") >= "0.0.6") {
200                 // Query with CSS file from DB
201                 $res_ext_crt = SQL_QUERY("SELECT id, ext_name, ext_lang_file, ext_has_css, ext_active, ext_version
202 FROM "._MYSQL_PREFIX."_extensions".$ADD."
203 ORDER BY ext_name", __FILE__, __LINE__);
204         } else {
205                 // Old obsolete query string
206                 $res_ext_crt = SQL_QUERY("SELECT id, ext_name, ext_lang_file, ext_name, ext_active, ext_version
207 FROM "._MYSQL_PREFIX."_extensions".$ADD."
208 ORDER BY ext_name", __FILE__, __LINE__);
209         }
210 }
211
212 // Array for removed but not uninstalled extensions
213 $DEL = array();
214
215 // At least one found?
216 if ((SQL_NUMROWS($res_ext_crt) > 0) && ((($cacheMode == "init") && ($CSS != "1") && ($CSS != "-1")) || ($cacheMode == "no"))) {
217         // Load theme management
218         require_once(PATH."inc/theme-manager.php");
219
220         // Extensions are registered so we load them
221         while (list($EXT_ID, $name, $lang, $css, $active, $version) = SQL_FETCHROW($res_ext_crt)) {
222                 // Get menu entry
223                 $menu = "N";
224                 if (MODULE_HAS_MENU($name, true)) {
225                         $menu = "Y";
226                 } // END - if
227
228                 // Load extensions
229                 $file1 = sprintf("%sinc/extensions/ext-%s.php", PATH, $name);
230                 $file2 = $file1; $EXT_CSS = "N"; $EXT_ALWAYS_ACTIVE = "N";
231
232                 // Special functions file
233                 $file3 = sprintf("%sinc/libs/%s_functions.php", PATH, $name);
234
235                 // Does the extension file exists?
236                 if (FILE_READABLE($file1)) {
237                         // If there's no language file specified we don't need to load one... ;-)
238                         if (!empty($lang)) {
239                                 // Create language file
240                                 $file2 = sprintf("%sinc/language/%s_%s.php", PATH, $lang, GET_LANGUAGE());
241                         } // END - if
242
243                         if (FILE_READABLE($file3)) {
244                                 // Special functions file
245                                 $funcs = "Y";
246                                 require_once($file3);
247                         } else {
248                                 // Don't load functions file
249                                 $funcs = "N";
250                         }
251
252                         // Do we need a language file?
253                         if (($file1 != $file2) && (FILE_READABLE($file2))) {
254                                 // Load language file
255                                 $lang = "Y";
256                                 include($file2);
257                         } else {
258                                 // Don't load language file
259                                 $lang = "N";
260                         }
261
262                         // Load extension
263                         if ($name != "sql_patches") {
264                                 // Load extension's file
265                                 include_once($file1);
266                         } else {
267                                 // KEEP sql_patches ALWAYS ACTIVE!
268                                 $EXT_ALWAYS_ACTIVE = "Y";
269                         }
270
271                         if ($css == "Y") {
272                                 $CSS_FILE = sprintf("%stheme/%s/css/%s.css", PATH, GET_CURR_THEME(), $name);
273                                 if (FILE_READABLE($CSS_FILE)) {
274                                         // CSS file for extension was found (use only relative path for now!)
275                                         $EXT_CSS_FILES[] = $name.".css";
276                                 } else {
277                                         // Don't load CSS file
278                                         $css = "N";
279                                 }
280                         } // END - if
281
282                         // Add cache row
283                         if ($cacheMode == "init") {
284                                 $cacheInstance->add_row(array(
285                                         'ext_id'      => $EXT_ID,
286                                         'ext_name'    => $name,
287                                         'ext_lang'    => $lang,
288                                         'ext_css'     => $css,
289                                         'ext_menu'    => $menu,
290                                         'ext_funcs'   => $funcs,
291                                         'ext_active'  => $active,
292                                         'ext_version' => $version,
293                                         'ext_keep'    => $EXT_ALWAYS_ACTIVE,
294                                 ));
295                         } elseif ($cacheMode == "no") {
296                                 // Remember this value for later usage
297                                 $cacheArray['active_extensions'][$name] = $EXT_ALWAYS_ACTIVE;
298                         }
299                 } elseif (!FILE_READABLE($file1)) {
300                         // Deleted extension file so we mark it for removal from DB
301                         $DEL[] = $name;
302                 }
303         } // END - while
304
305         if ($cacheMode == "init") {
306                 // Close cache file
307                 $cacheInstance->cache_close();
308
309                 // Load more cache files (like admins)
310                 require_once(PATH."inc/load_cache.php");
311         } // END - if
312
313         // Free memory
314         SQL_FREERESULT($res_ext_crt);
315 } // END - if
316
317 // Load include files if found
318 if (!empty($INC_POOL[0])) {
319         foreach ($INC_POOL as $inc) {
320                 require_once($inc);
321         } // END - foreach
322
323         // Remove array
324         unset($INC_POOL);
325 } // END - if
326
327 // Uninstall extensions that are no longer in our system
328 if (!empty($DEL[0])) {
329         // Remove extensions from two tables: extension registry and tasks table
330         foreach ($DEL as $name) {
331                 // First remove entry from extensions table
332                 SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM "._MYSQL_PREFIX."_extensions WHERE ext_name='%s' LIMIT 1",
333                         array($name), __FILE__, __LINE__);
334
335                 // Remove (maybe?) found tasks (main task and possible updates
336                 SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM "._MYSQL_PREFIX."_task_system WHERE subject LIKE '[%s:]%%' AND (task_type='EXTENSION' OR task_type='EXTENSION_UPDATE')",
337                         array($name), __FILE__, __LINE__);
338         } // END - foreach
339
340         // I think it's not neccessary to run the optimization function here
341         // because we didn't delete so much data from database. Can you aggree?
342 } // END - if
343
344 //
345 ?>