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