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