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