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