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