query masking rewritten in more SQLs, several cleanups and fix on beg link
[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 (ereg(basename(__FILE__), $_SERVER['PHP_SELF']))
36 {
37         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
38         require($INC);
39 }
40 //
41 global $EXT_CSS_FILES;
42 $EXT_CSS_FILES = array();
43 $ADD = "";
44
45 // Skip loading extensions
46 if ((!mxchange_installed) || (mxchange_installing)) return;
47
48 // Load default sql_patches extension if present
49 if (file_exists(PATH."inc/extensions/ext-sql_patches.php") && is_readable(PATH."inc/extensions/ext-sql_patches.php"))
50 {
51         // Load it...
52         $EXT_LOAD_MODE = "";
53         require_once(PATH."inc/extensions/ext-sql_patches.php");
54         $KEEP_ACTIVE = array('sql_patches' => 'Y'); // KEEP THIS ALWAYS ACTIVE!
55 }
56  else
57 {
58         // Initialize array for "always keep active extensions"
59         $KEEP_ACTIVE = array();
60 }
61
62 //
63 // Load extensions
64 //
65 if (EXT_IS_ACTIVE("cache"))
66 {
67         // Load cache extension alone
68         include_once(PATH."inc/libs/cache_functions.php");
69         $CACHE_FILE = "";
70         include_once(PATH."inc/extensions/ext-cache.php");
71         switch($CACHE->cache_file("extensions", true))
72         {
73                 case true : $CACHE_FILE = "load"; break;
74                 case false: $CACHE_FILE = "init"; break;
75         }
76
77         // Do not recreate cache file when it's switched off!
78         if (($CACHE_FILE == "init") && ($CONFIG['cache_exts'] == 'N')) $CACHE_FILE = "skip";
79
80         // Load language
81         if ($CACHE_FILE == "load") include(PATH."inc/language/cache_".GET_LANGUAGE().".php");
82 }
83  else
84 {
85         $CACHE_FILE = "no";
86 }
87
88 if ($CACHE_FILE == "load")
89 {
90         // Load more cache files (like admins)
91         require_once(PATH."inc/load_cache.php");
92
93         // Re-initialize handler
94         $CACHE->cache_file("extensions", true);
95
96         // Load extension data from cache file
97         $EXT_DUMMY = $CACHE->cache_load();
98         foreach ($EXT_DUMMY['ext_name'] as $k=>$name)
99         {
100                 // Load functions file
101                 if ($EXT_DUMMY['ext_funcs'][$k] == 'Y') require_once(PATH."inc/libs/".$name."_functions.php");
102
103                 // Load Language file
104                 if ($EXT_DUMMY['ext_lang'][$k] == 'Y')
105                 {
106                         $INC = sprintf(PATH."inc/language/%s_%s.php", $name, GET_LANGUAGE());
107                         if (file_exists($INC)) require_once($INC);
108                 }
109
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()))
115                 {
116                         require_once(PATH."inc/extensions/ext-".$name.".php");
117                 }
118
119                 // Transfer version number and active status
120                 $EXT_DUMMY['ext_version'][$name] = $EXT_DUMMY['ext_version'][$k];
121                 unset($EXT_DUMMY['ext_version'][$k]);
122                 $EXT_DUMMY['ext_active'][$name] = $EXT_DUMMY['ext_active'][$k];
123                 unset($EXT_DUMMY['ext_active'][$k]);
124                 $EXT_DUMMY['ext_menu'][$name] = $EXT_DUMMY['ext_menu'][$k];
125                 unset($EXT_DUMMY['ext_menu'][$k]);
126                 $KEEP_ACTIVE['$name'] = $EXT_DUMMY['ext_keep'][$k];
127                 unset($EXT_DUMMY['ext_keep'][$k]);
128                 $k2 = $EXT_DUMMY['ext_id'][$k];
129                 $EXT_DUMMY['ext_id'][$k2] = $name;
130                 if ($k2 != $k) unset($EXT_DUMMY['ext_id'][$k]);
131
132                 // Remove unneccessary data from memory
133                 unset($EXT_DUMMY['ext_lang'][$k]);
134                 unset($EXT_DUMMY['ext_css'][$k]);
135                 unset($EXT_DUMMY['ext_funcs'][$k]);
136         }
137
138         // Close cache file
139         $CACHE->cache_close();
140
141         // Loading cache is done so let's free some memory!
142         unset($EXT_DUMMY['ext_lang']);
143         unset($EXT_DUMMY['ext_keep']);
144         unset($EXT_DUMMY['ext_css']);
145         unset($EXT_DUMMY['ext_funcs']);
146         $EXTENSIONS = $EXT_DUMMY;
147         unset($EXT_DUMMY);
148
149         // No database load needed
150         $res_ext_crt = false;
151 }
152  else
153 {
154         // If current user is not admin load only activated extensions
155         // The admin shall use every available extension for testing purposes
156         if (!IS_ADMIN()) $ADD = " WHERE ext_active='Y'";
157
158         if (GET_EXT_VERSION("sql_patches") >= "0.0.6")
159         {
160                 // Query with CSS file from DB
161                 $res_ext_crt = SQL_QUERY("SELECT id, ext_name, ext_lang_file, ext_has_css, ext_active, ext_version
162 FROM "._MYSQL_PREFIX."_extensions".$ADD."
163 ORDER BY ext_name", __FILE__, __LINE__);
164         }
165          else
166         {
167                 // Old obsulete query string
168                 $res_ext_crt = SQL_QUERY("SELECT id, ext_name, ext_lang_file, ext_name, ext_active, ext_version
169 FROM "._MYSQL_PREFIX."_extensions".$ADD."
170 ORDER BY ext_name", __FILE__, __LINE__);
171         }
172 }
173
174 // Array for removed but not uninstalled extensions
175 $DEL = array();
176
177 // At least one found?
178 if ((SQL_NUMROWS($res_ext_crt) > 0) && (($CACHE_FILE == "init") || ($CACHE_FILE == "no")) && ($CSS != "1") && ($CSS != "-1"))
179 {
180         // Load theme management
181         require_once(PATH."inc/theme-manager.php");
182
183         // If we need to init the cache init it now
184         if ($CACHE_FILE == "init") $CACHE->cache_init("EXTENSIONS");
185
186         // Extensions are registered so we load them
187         while (list($EXT_ID, $name, $lang, $css, $active, $version) = SQL_FETCHROW($res_ext_crt))
188         {
189                 // Get menu entry
190                 $result_menu = SQL_QUERY_ESC("SELECT has_menu FROM "._MYSQL_PREFIX."_mod_reg WHERE module='%s' LIMIT 1",
191                  array($name), __FILE__, __LINE__);
192                 list($menu) = SQL_FETCHROW($result_menu);
193                 //* DEBUG: */ echo "*".$name."/".$menu."*<br>";
194
195                 // An empty menu entry will be interpreted as N (no menu) to avoid problems
196                 if (empty($menu)) $menu = 'N';
197
198                 // Load extensions
199                 $file1 = sprintf(PATH."inc/extensions/ext-%s.php", $name);
200                 $file2 = $file1; $EXT_CSS = 'N'; $EXT_ALWAYS_ACTIVE = 'N';
201
202                 // Special functions file
203                 $file3 = sprintf(PATH."inc/libs/%s_functions.php", $name);
204
205                 // Does the extension file exists?
206                 if (file_exists($file1) && is_readable($file1))
207                 {
208                         // If there's no language file specified we don't need to load one... ;-)
209                         if (!empty($lang)) {
210                                 // Create language file
211                                 $file2 = sprintf(PATH."inc/language/%s_%s.php", $lang, GET_LANGUAGE());
212                         }
213
214                         if (file_exists($file3) && is_readable($file3))
215                         {
216                                 // Special functions file
217                                 $funcs = 'Y';
218                                 require_once($file3);
219                         }
220                          else
221                         {
222                                 // Don't load functions file
223                                 $funcs = 'N';
224                         }
225
226                         // Do we need a language file?
227                         if (($file1 != $file2) && (file_exists($file2)) && (is_readable($file2)))
228                         {
229                                 // Load language file
230                                 $lang = 'Y';
231                                 include($file2);
232                         }
233                          else
234                         {
235                                 // Don't load language file
236                                 $lang = 'N';
237                         }
238
239                         // Load extension
240                         if ($name != "sql_patches")
241                         {
242                                 // Load extension's file
243                                 include_once($file1);
244                         }
245                          else
246                         {
247                                 // KEEP sql_patches ALWAYS ACTIVE!
248                                 $EXT_ALWAYS_ACTIVE = 'Y';
249                         }
250
251                         if ($css == 'Y')
252                         {
253                                 $CSS_FILE = PATH."theme/".GET_CURR_THEME()."/css/".$name.".css";
254                                 if (file_exists($CSS_FILE))
255                                 {
256                                         // CSS file for extension was found (use only relative path for now!)
257                                         $EXT_CSS_FILES[] = $name.".css";
258                                 }
259                                  else
260                                 {
261                                         // Don't load CSS file
262                                         $css = 'N';
263                                 }
264                         }
265
266                         // Add cache row
267                         if ($CACHE_FILE == "init")
268                         {
269                                 $CACHE->add_row(array(
270                                         'ext_id'      => $EXT_ID,
271                                         'ext_name'    => $name,
272                                         'ext_lang'    => $lang,
273                                         'ext_css'     => $css,
274                                         'ext_menu'    => $menu,
275                                         'ext_funcs'   => $funcs,
276                                         'ext_active'  => $active,
277                                         'ext_version' => $version,
278                                         'ext_keep'    => $EXT_ALWAYS_ACTIVE,
279                                 ));
280                         }
281                          elseif ($CACHE_FILE == "no")
282                         {
283                                 // Remember this value for later usage
284                                 $KEEP_ACTIVE[$name] = $EXT_ALWAYS_ACTIVE;
285                         }
286                 }
287                  elseif (!file_exists($file1))
288                 {
289                         // Deleted extension file so we mark it for removal from DB
290                         $DEL[] = $name;
291                 }
292         }
293
294         if ($CACHE_FILE == "init")
295         {
296                 // Close cache file
297                 $CACHE->cache_close();
298
299                 // Load more cache files (like admins)
300                 require_once(PATH."inc/load_cache.php");
301         }
302 }
303
304 // Free memory
305 SQL_FREERESULT($res_ext_crt);
306
307 // Compile configuration system
308 /**
309  * Commented out...
310  *
311  *foreach ($CONFIG as $k=>$v)
312  *{
313  *      $CONFIG[$k] = COMPILE_CODE($v);
314  *}
315  */
316
317 // Load include files
318 if (!empty($INC_POOL[0]))
319 {
320         foreach ($INC_POOL as $inc)
321         {
322                 require_once($inc);
323         }
324 }
325
326 // Uninstall extensions that are no longer in our system
327 if (!empty($DEL[0]))
328 {
329         // Remove extensions from two tables: extension registry and tasks table
330         foreach ($DEL as $name)
331         {
332                 // First remove entry from extensions table
333                 $result = SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM "._MYSQL_PREFIX."_extensions WHERE ext_name='%s' LIMIT 1",
334                  array($name), __FILE__, __LINE__);
335
336                 // Remove (maybe?) found tasks (main task and possible updates
337                 $result = SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM "._MYSQL_PREFIX."_task_system WHERE subject LIKE '[%s:] %' AND (task_type='EXTENSION' OR task_type='EXTENSION_UPDATE')",
338                  array($name), __FILE__, __LINE__);
339         }
340
341         // I think it's not neccessary to run the optimization function here
342         // because we didn't delete so much data from database. Can you aggree?
343 }
344 //
345 ?>