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