87f1179fb1e3d431d2e58342842f350b4c80184b
[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 //
60 // Load extensions
61 //
62 if (EXT_IS_ACTIVE("cache")) {
63         // Load cache extension alone
64         include_once(PATH."inc/libs/cache_functions.php");
65         $cacheMode = "";
66         include_once(PATH."inc/extensions/ext-cache.php");
67         switch (($cacheInstance->cache_file("extensions", true)) && ($cacheInstance->ext_version_matches("sql_patches"))) {
68                 case true : $cacheMode = "load"; break;
69                 case false: $cacheMode = "init"; break;
70         }
71
72         // Do not recreate cache file when it's switched off!
73
74         // If we need to init the cache init it now
75         if ($cacheMode == "init") {
76                 // Init cache file
77                 $cacheInstance->cache_init("EXTENSIONS");
78                 $cacheInstance->store_extension_version("sql_patches");
79
80                 if ($_CONFIG['cache_exts'] == "N") $cacheMode = "skip";
81         } // END - if
82 } else {
83         // Cache extension not active
84         $cacheMode = "no";
85 }
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                 // Mark it as active extension
153                 $cacheArray['active_extensions']['$name'] = $EXT_DUMMY['ext_keep'][$k];
154                 unset($EXT_DUMMY['ext_keep'][$k]);
155
156                 // Remove unneccessary data from memory
157                 unset($EXT_DUMMY['ext_lang'][$k]);
158                 unset($EXT_DUMMY['ext_css'][$k]);
159                 unset($EXT_DUMMY['ext_funcs'][$k]);
160         } // END - foreach
161
162         // Write dummy array back
163         $EXT_DUMMY['ext_name'] = $EXT_NAMES;
164         unset($EXT_NAMES);
165
166         // Close cache file
167         $cacheInstance->cache_close();
168
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;
175         unset($EXT_DUMMY);
176
177         // No database load needed
178         $res_ext_crt = false;
179
180         // Load more cache files (like admins)
181         require_once(PATH."inc/load_cache.php");
182
183         // Load all extension files
184         foreach ($EXT_POOL as $inc) {
185                 require_once($inc);
186         } // END - foreach
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()) && ($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, ext_name, ext_lang_file, ext_has_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, ext_name, ext_lang_file, 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) && ((($cacheMode == "init") && ($CSS != "1") && ($CSS != "-1")) || ($cacheMode == "no"))) {
214         // Load theme management
215         require_once(PATH."inc/theme-manager.php");
216
217         // Extensions are registered so we load them
218         while (list($EXT_ID, $name, $lang, $css, $active, $version) = SQL_FETCHROW($res_ext_crt)) {
219                 // Get menu entry
220                 $menu = "N";
221                 if (MODULE_HAS_MENU($name, true)) {
222                         $menu = "Y";
223                 } // END - if
224
225                 // Load extensions
226                 $file1 = sprintf("%sinc/extensions/ext-%s.php", PATH, $name);
227                 $file2 = $file1; $EXT_CSS = "N"; $EXT_ALWAYS_ACTIVE = "N";
228
229                 // Special functions file
230                 $file3 = sprintf("%sinc/libs/%s_functions.php", PATH, $name);
231
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($lang)) {
236                                 // Create language file
237                                 $file2 = sprintf("%sinc/language/%s_%s.php", PATH, $lang, GET_LANGUAGE());
238                         }
239
240                         if (FILE_READABLE($file3)) {
241                                 // Special functions file
242                                 $funcs = "Y";
243                                 require_once($file3);
244                         } else {
245                                 // Don't load functions file
246                                 $funcs = "N";
247                         }
248
249                         // Do we need a language file?
250                         if (($file1 != $file2) && (FILE_READABLE($file2))) {
251                                 // Load language file
252                                 $lang = "Y";
253                                 include($file2);
254                         } else {
255                                 // Don't load language file
256                                 $lang = "N";
257                         }
258
259                         // Load extension
260                         if ($name != "sql_patches") {
261                                 // Load extension's file
262                                 include_once($file1);
263                         } else {
264                                 // KEEP sql_patches ALWAYS ACTIVE!
265                                 $EXT_ALWAYS_ACTIVE = "Y";
266                         }
267
268                         if ($css == "Y") {
269                                 $CSS_FILE = sprintf("%stheme/%s/css/%s.css", PATH, GET_CURR_THEME(), $name);
270                                 if (FILE_READABLE($CSS_FILE)) {
271                                         // CSS file for extension was found (use only relative path for now!)
272                                         $EXT_CSS_FILES[] = $name.".css";
273                                 } else {
274                                         // Don't load CSS file
275                                         $css = "N";
276                                 }
277                         } // END - if
278
279                         // Add cache row
280                         if ($cacheMode == "init") {
281                                 $cacheInstance->add_row(array(
282                                         'ext_id'      => $EXT_ID,
283                                         'ext_name'    => $name,
284                                         'ext_lang'    => $lang,
285                                         'ext_css'     => $css,
286                                         'ext_menu'    => $menu,
287                                         'ext_funcs'   => $funcs,
288                                         'ext_active'  => $active,
289                                         'ext_version' => $version,
290                                         'ext_keep'    => $EXT_ALWAYS_ACTIVE,
291                                 ));
292                         } elseif ($cacheMode == "no") {
293                                 // Remember this value for later usage
294                                 $cacheArray['active_extensions'][$name] = $EXT_ALWAYS_ACTIVE;
295                         }
296                 } elseif (!FILE_READABLE($file1)) {
297                         // Deleted extension file so we mark it for removal from DB
298                         $DEL[] = $name;
299                 }
300         } // END - while
301
302         if ($cacheMode == "init") {
303                 // Close cache file
304                 $cacheInstance->cache_close();
305
306                 // Load more cache files (like admins)
307                 require_once(PATH."inc/load_cache.php");
308         } // END - if
309
310         // Free memory
311         SQL_FREERESULT($res_ext_crt);
312 } // END - if
313
314 // Load include files if found
315 if (!empty($INC_POOL[0])) {
316         foreach ($INC_POOL as $inc) {
317                 require_once($inc);
318         } // END - foreach
319
320         // Remove array
321         unset($INC_POOL);
322 } // END - if
323
324 // Uninstall extensions that are no longer in our system
325 if (!empty($DEL[0])) {
326         // Remove extensions from two tables: extension registry and tasks table
327         foreach ($DEL as $name) {
328                 // First remove entry from extensions table
329                 SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM "._MYSQL_PREFIX."_extensions WHERE ext_name='%s' LIMIT 1",
330                         array($name), __FILE__, __LINE__);
331
332                 // Remove (maybe?) found tasks (main task and possible updates
333                 SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM "._MYSQL_PREFIX."_task_system WHERE subject LIKE '[%s:] %' AND (task_type='EXTENSION' OR task_type='EXTENSION_UPDATE')",
334                         array($name), __FILE__, __LINE__);
335         } // END - foreach
336
337         // I think it's not neccessary to run the optimization function here
338         // because we didn't delete so much data from database. Can you aggree?
339 } // END - if
340
341 //
342 ?>