Global variables rewritten
[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 // Init cache instance and array
46 $GLOBALS['cache_instance'] = null;
47 $GLOBALS['cache_array'] = array();
48
49 // Skip loading extensions
50 if ((!isBooleanConstantAndTrue('mxchange_installed')) || (isBooleanConstantAndTrue('mxchange_installing'))) return;
51
52 // Initialize array for "always keep active extensions"
53 $GLOBALS['cache_array']['active_extensions'] = array();
54
55 // By default no cache is set
56 $GLOBALS['cacheMode'] = "no";
57
58 // Load sql_patchrs extension alone
59 LOAD_EXTENSION("sql_patches");
60
61 //
62 // Load extensions
63 //
64 if (EXT_IS_ACTIVE("cache")) {
65         // Load cache extension alone
66         LOAD_EXTENSION("cache");
67
68         // Check extension cache
69         switch (($GLOBALS['cache_instance']->loadCacheFile("extensions", true)) && ($GLOBALS['cache_instance']->extensionVersionMatches("sql_patches"))) {
70                 case true : $GLOBALS['cacheMode'] = "load"; break;
71                 case false: $GLOBALS['cacheMode'] = "init"; break;
72         }
73
74         // Do we need to init the cache?
75         if (($GLOBALS['cacheMode'] == "init") && (getConfig('cache_exts') == "Y")) {
76                 // Init cache file
77                 $GLOBALS['cache_instance']->init("EXTENSIONS");
78                 $GLOBALS['cache_instance']->storeExtensionVersion("sql_patches");
79         } elseif (getConfig('cache_exts') != "Y") {
80                 // Cache will not be created for extensions
81                 $GLOBALS['cacheMode'] = "skip";
82         }
83 } // END - if
84
85 // Load cache?
86 if ($GLOBALS['cacheMode'] == "load") {
87         // Init include array
88         $EXT_POOL = array();
89
90         // Re-initialize handler
91         $GLOBALS['cache_instance']->loadCacheFile("extensions", true);
92
93         // Load extension data from cache file
94         $EXT_DUMMY = $GLOBALS['cache_instance']->getArrayFromCache();
95
96         // Is the cache file fine?
97         if (!isset($EXT_DUMMY['ext_name'])) {
98                 // Cache file is damaged so kill it
99                 $GLOBALS['cache_instance']->destroyCacheFile();
100
101                 // Retry it
102                 LOAD_INC(__FILE__);
103                 return;
104         } // END -  if
105
106         // Begin with the cache preparation of extensions
107         $EXT_NAMES = array();
108         foreach ($EXT_DUMMY['ext_name'] as $k => $name) {
109                 // Load CSS file
110                 if ($EXT_DUMMY['ext_css'][$k] == "Y") $EXT_CSS_FILES[] = "".$name.".css";
111
112                 // Load extension file itself
113                 if ((($EXT_DUMMY['ext_active'][$k] == "Y") || ($EXT_DUMMY['ext_keep'][$k] == "Y") || (IS_ADMIN())) && (!in_array($name, array("sql_patches", "cache")))) {
114                         $EXT_POOL[] = $name;
115                 } // END - if
116
117                 // Version number
118                 $EXT_DUMMY['ext_version'][$name] = $EXT_DUMMY['ext_version'][$k];
119                 unset($EXT_DUMMY['ext_version'][$k]);
120
121                 // Extension is active
122                 $EXT_DUMMY['ext_active'][$name] = $EXT_DUMMY['ext_active'][$k];
123                 unset($EXT_DUMMY['ext_active'][$k]);
124
125                 // Ext menu
126                 $EXT_DUMMY['ext_menu'][$name] = $EXT_DUMMY['ext_menu'][$k];
127                 unset($EXT_DUMMY['ext_menu'][$k]);
128
129                 // Extension id
130                 $EXT_DUMMY['ext_id'][$name] = $EXT_DUMMY['ext_id'][$k];
131                 $id = $EXT_DUMMY['ext_id'][$name];
132                 unset($EXT_DUMMY['ext_id'][$k]);
133
134                 // Add ext name
135                 $EXT_NAMES[$id] = $name;
136
137                 // Add deprecated flag (defaults to "not deprecated")
138                 $EXT_DUMMY['ext_deprecated'][$name] = "N";
139
140                 // Mark it as active extension
141                 $GLOBALS['cache_array']['active_extensions']['$name'] = $EXT_DUMMY['ext_keep'][$k];
142                 unset($EXT_DUMMY['ext_keep'][$k]);
143
144                 // Remove unneccessary data from memory
145                 unset($EXT_DUMMY['ext_css'][$k]);
146         } // END - foreach
147
148         // Write dummy array back
149         $EXT_DUMMY['ext_name'] = $EXT_NAMES;
150         unset($EXT_NAMES);
151
152         // Loading cache is done so let's free some memory!
153         unset($EXT_DUMMY['ext_keep']);
154         unset($EXT_DUMMY['ext_css']);
155         $GLOBALS['cache_array']['extensions'] = $EXT_DUMMY;
156         unset($EXT_DUMMY);
157
158         // No database load needed
159         $res_ext_crt = false;
160
161         // Load all extension files
162         foreach ($EXT_POOL as $ext) {
163                 LOAD_EXTENSION($ext);
164         } // END - foreach
165
166         // Init filter system
167         INIT_FILTER_SYSTEM();
168
169         // Load more cache files (like admins)
170         LOAD_INC_ONCE("inc/load_cache.php");
171
172         // Remove array
173         unset($EXT_POOL);
174 } else {
175         // If current user is not admin load only activated extensions. But load
176         // them all if we are going to init the cache files. The admin shall use
177         // every available extension for testing purposes.
178         if ((!IS_ADMIN()) && ($GLOBALS['cacheMode'] != "init")) $ADD = " WHERE ext_active='Y'";
179
180         if (GET_EXT_VERSION("sql_patches") >= "0.0.6") {
181                 // Query with CSS file from DB
182                 $res_ext_crt = SQL_QUERY("SELECT id AS ext_id, ext_name, ext_has_css AS ext_css, ext_active, ext_version
183 FROM `{!_MYSQL_PREFIX!}_extensions`".$ADD."
184 ORDER BY ext_name", __FILE__, __LINE__);
185         } else {
186                 // Old obsolete query string
187                 $res_ext_crt = SQL_QUERY("SELECT id AS ext_id, ext_name, ext_name, ext_active, ext_version
188 FROM `{!_MYSQL_PREFIX!}_extensions`".$ADD."
189 ORDER BY ext_name", __FILE__, __LINE__);
190         }
191 }
192
193 // Array for removed but not uninstalled extensions
194 $DEL = array();
195
196 // At least one found?
197 if ((SQL_NUMROWS($res_ext_crt) > 0) && ((($GLOBALS['cacheMode'] == "init") && ($GLOBALS['output_mode'] != "1") && ($GLOBALS['output_mode'] != "-1")) || ($GLOBALS['cacheMode'] == "no"))) {
198         // Load session management
199         LOAD_INC_ONCE("inc/session.php");
200
201         // Extensions are registered so we load them
202         while ($content = SQL_FETCHARRAY($res_ext_crt)) {
203                 // Get menu entry
204                 $content['ext_menu'] = "N";
205                 if (MODULE_HAS_MENU($content['ext_name'], true)) {
206                         $content['ext_menu'] = "Y";
207                 } // END - if
208
209                 // Generate FQFN for extension
210                 $FQFN = sprintf("%sinc/extensions/ext-%s.php", constant('PATH'), $content['ext_name']);
211
212                 // Does the extension file exists?
213                 if (FILE_READABLE($FQFN)) {
214                         // By default no extension is always active, except sql_patches
215                         $EXT_ALWAYS_ACTIVE = "N";
216
217                         // Load extension
218                         if (($content['ext_name'] != "sql_patches") && (($content['ext_name'] != "cache") || (!EXT_IS_ACTIVE("cache")))) {
219                                 // Load extension
220                                 LOAD_EXTENSION($content['ext_name']);
221                         } else {
222                                 // Keep sql_patches always active
223                                 $EXT_ALWAYS_ACTIVE = "Y";
224                         }
225
226                         // Transfer EXT_ALWAYS_ACTIVE flag
227                         $content['ext_keep'] = $EXT_ALWAYS_ACTIVE;
228
229                         // CSS file handling:
230                         if ((!isset($content['ext_css'])) || ($content['ext_css'] == "Y")) {
231                                 // Create FQFN for the CSS file
232                                 $GLOBALS['output_mode']_FILE = sprintf("%stheme/%s/css/%s.css", constant('PATH'), GET_CURR_THEME(), $content['ext_name']);
233
234                                 // Is the file there?
235                                 if (FILE_READABLE($GLOBALS['output_mode']_FILE)) {
236                                         // CSS file for extension was found (use only relative path for now!)
237                                         $EXT_CSS_FILES[] = $content['ext_name'].".css";
238                                         $content['ext_css'] = "Y";
239                                 } else {
240                                         // Don't load CSS file
241                                         $content['ext_css'] = "N";
242                                 }
243                         } // END - if
244
245                         // Shall we cache?
246                         if ($GLOBALS['cacheMode'] == "init") {
247                                 // Add cache row
248                                 $GLOBALS['cache_instance']->addRow($content);
249                         } elseif ($GLOBALS['cacheMode'] == "no") {
250                                 // Remember this value for later usage
251                                 $GLOBALS['cache_array']['active_extensions'][$content['ext_name']] = $EXT_ALWAYS_ACTIVE;
252                         }
253                 } elseif (!FILE_READABLE($FQFN)) {
254                         // Deleted extension file so we mark it for removal from DB
255                         $DEL[] = $content['ext_name'];
256                 }
257         } // END - while
258
259         // Init filter system
260         INIT_FILTER_SYSTEM();
261
262         if ($GLOBALS['cacheMode'] == "init") {
263                 // Close cache file
264                 $GLOBALS['cache_instance']->finalize();
265
266                 // Load more cache files (like admins)
267                 LOAD_INC_ONCE("inc/load_cache.php");
268         } // END - if
269
270         // Free memory
271         SQL_FREERESULT($res_ext_crt);
272 } elseif (!EXT_IS_ACTIVE("cache")) {
273         // Init filter system even when there are no extensions installed. #16
274         INIT_FILTER_SYSTEM();
275 }
276
277 // Run the filter
278 RUN_FILTER('load_includes', $INC_POOL);
279
280 // Uninstall extensions that are no longer in our system
281 if (!empty($DEL[0])) {
282         // Remove extensions from two tables: extension registry and tasks table
283         foreach ($DEL as $del_ext) {
284                 // First remove entry from extensions table
285                 SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{!_MYSQL_PREFIX!}_extensions` WHERE ext_name='%s' LIMIT 1",
286                         array($del_ext), __FILE__, __LINE__);
287
288                 // Remove (maybe?) found tasks (main task and possible updates
289                 SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{!_MYSQL_PREFIX!}_task_system` WHERE subject='[%s:]' AND (task_type='EXTENSION' OR task_type='EXTENSION_UPDATE')",
290                         array($del_ext), __FILE__, __LINE__);
291         } // END - foreach
292
293         // I think it's not neccessary to run the optimization function here
294         // because we didn't delete so much data from database. Can you aggree?
295 } // END - if
296
297 //
298 ?>