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