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