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