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