2 /************************************************************************
3 * MXChange v0.2.1 Start: 03/25/2004 *
4 * =============== Last change: 09/29/2004 *
6 * -------------------------------------------------------------------- *
7 * File : extensions.php *
8 * -------------------------------------------------------------------- *
9 * Short description : Extension management *
10 * -------------------------------------------------------------------- *
11 * Kurzbeschreibung : Erweiterungen-Management *
12 * -------------------------------------------------------------------- *
14 * -------------------------------------------------------------------- *
15 * Copyright (c) 2003 - 2008 by Roland Haeder *
16 * For more information visit: http://www.mxchange.org *
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. *
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. *
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, *
32 ************************************************************************/
34 // Some security stuff...
35 if (!defined('__SECURITY')) {
36 $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
41 function EXTENSION_REGISTER ($ext_name, $id, $dry_run=false)
43 global $NOTES, $_CONFIG, $INC_POOL, $cacheInstance;
44 // We want to register an extension and registration status is by default "failed" (= false)
45 $EXT_LOAD_MODE = "register"; $ret = false; $SQLs = array();
48 // This shall never do a non-admin user!
49 if (!IS_ADMIN()) return false;
51 // Is this extension already installed?
52 if (EXT_IS_ACTIVE($ext_name)) return false;
55 $file = sprintf("%sinc/extensions/ext-%s.php", PATH, $ext_name);
57 // Does this extension exists?
58 if (FILE_READABLE($file)) {
59 // Extension was found so we can load it in registration mode
62 // And run possible updates
63 $EXT_LOAD_MODE = "update"; $EXT_UPDATE_DEPENDS = "";
64 foreach ($EXT_VER_HISTORY as $EXT_VER) {
65 // Load extension in update mode
68 // Do we have an update?
69 if (((GET_EXT_VERSION("sql_patches") != "") && ($_CONFIG['verbose_sql'] == "Y")) || (!EXT_IS_ACTIVE("sql_patches"))) {
70 if (!empty($UPDATE_NOTES)) {
72 $NOTES .= "<STRONG>v".$EXT_VER.":</STRONG><br />".$UPDATE_NOTES."<br /><br />\n";
74 } elseif (($EXT_VER == "0.0") || ($EXT_VER == "0.0.0")) {
76 $NOTES .= "<STRONG>v".$EXT_VER.":</STRONG><br />".INITIAL_RELEASE."<br /><br />\n";
78 // No update notes found!
79 $NOTES .= "<STRONG>v".$EXT_VER.":</STRONG><br /><I>".NO_UPDATE_NOTES."</I><br /><br />\n";
84 // Does this extension depends on an outstanding update of another update?
85 if (!empty($EXT_UPDATE_DEPENDS)) {
86 // Backup SQL commands and clear current
91 // Backup language as well
92 $LANG_BCK = $EXT_LANG_PREFIX;
93 $EXT_ALWAYS_ACTIVE = "N";
95 // Load required extension also in update mode
96 $file = sprintf("%sinc/extensions/ext-%s.php", PATH, $EXT_UPDATE_DEPENDS);
98 // Check for required file
99 if (FILE_READABLE($file)) {
100 // File exists so let's load it
101 $VER_BACKUP = $EXT_VERSION;
103 $EXT_VERSION = $VER_BACKUP;
105 // If versions mismatch update extension first
106 $ext_ver = GET_EXT_VERSION($EXT_UPDATE_DEPENDS);
108 // Extension version set?
109 if (empty($ext_ver)) {
110 // Extension not registered so far so first load task's ID...
111 $result = SQL_QUERY_ESC("SELECT id FROM "._MYSQL_PREFIX."_task_system WHERE task_type='EXTENSION' AND subject LIKE '[%s:]%%' LIMIT 1",
112 array($EXT_UPDATE_DEPENDS), __FILE__, __LINE__);
115 if (SQL_NUMROWS($result) == 1) {
116 // Task found so load task's ID and register extension...
117 list($task) = SQL_FETCHROW($result);
119 // Try to register the extension
120 $test = EXTENSION_REGISTER($EXT_UPDATE_DEPENDS, $task, $dry_run);
124 SQL_FREERESULT($result);
125 } elseif ($ext_ver != $EXT_VERSION) {
126 // Ok, update this extension now
127 EXTENSION_UPDATE(basename($file), $EXT_UPDATE_DEPENDS, $ext_ver, $dry_run);
132 // Nothing to register / update before...
136 // Required file for update does not exists!
138 // But this is fine for the first time...
141 // Finally restore previous SQLs
142 $SQLs = $SQLs2; unset($SQLs2);
143 $EXT_LANG_PREFIX = $LANG_BCK;
145 // Does not depend on an other extension
149 // Switch back to register mode
150 $EXT_LOAD_MODE = "register";
152 // Does everthing before wents ok?
154 // "Dry-run-mode" activated?
157 foreach ($SQLs as $sql) {
158 // Trim spaces away which we don't need
161 // Is there still an SQL query?
163 // Do we have an "ALTER TABLE" command?
164 if (substr(strtolower($sql), 0, 11) == "alter table") {
165 // Analyse the alteration command
166 SQL_ALTER_TABLE($sql, __FILE__, __LINE__);
168 // Run regular SQL command
169 $result = SQL_QUERY($sql, __FILE__, __LINE__, false);
174 // Remove cache file(s) if extension is active
175 if ((EXT_IS_ACTIVE("cache")) || (GET_EXT_VERSION("cache") != "")) {
176 //* DEBUG: */ echo __LINE__.": DESTROY!<br />\n";
177 // Remove cache files
178 if ($cacheInstance->cache_file("extensions", true)) $cacheInstance->cache_destroy();
179 if ($cacheInstance->cache_file("mod_reg", true)) $cacheInstance->cache_destroy();
180 if ($cacheInstance->cache_file("config", true)) $cacheInstance->cache_destroy();
183 // Check for added include files
184 if (count($INC_POOL > 0)) {
185 // Loads every include file
186 foreach ($INC_POOL as $inc) {
194 // Register extension
195 $result = SQL_QUERY_ESC("INSERT INTO "._MYSQL_PREFIX."_extensions (ext_name, ext_lang_file, ext_active, ext_version) VALUES ('%s','%s','%s','%s')",
196 array($ext_name, $EXT_LANG_PREFIX, $EXT_ALWAYS_ACTIVE, $EXT_VERSION), __FILE__, __LINE__);
198 // Update task management
199 ADMIN_SOLVE_TASK($id);
201 // @TODO This causes the whole (!) menu cache being purged
202 CACHE_PURGE_ADMIN_MENU();
204 // In normal mode return a true on success
210 // Rewrite SQL command to keep { and } inside
211 foreach ($SQLs as $key => $sql) {
212 $sql = str_replace('{', "{", str_replace('}', "}", $sql));
216 // In "dry-run" mode return array with all SQL commands
219 // Remove all SQL commands
223 // No, an error occurs while registering extension :-(
226 } elseif (($id > 0) && (!empty($ext_name))) {
227 // Remove task from system when id and extension's name is valid
228 $result = SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM "._MYSQL_PREFIX."_task_system WHERE id=%s AND status='NEW' LIMIT 1",
229 array(bigintval($id)), __FILE__, __LINE__);
232 // Is this the sql_patches?
233 //* DEBUG: */ echo __LINE__.":{$ext_name}/{$EXT_LOAD_MODE}<br />\n";
234 if (($ext_name == "sql_patches") && (($EXT_LOAD_MODE == "register") || ($EXT_LOAD_MODE == "remove")) && (!$dry_run) && ($test)) {
235 // Then redirect to logout
236 //* DEBUG: */ echo __LINE__.": LOAD!<br />\n";
237 LOAD_URL("modules.php?module=admin&logout=1&".$EXT_LOAD_MODE."=sql_patches");
240 // Return status code
244 function EXTENSION_RUN_SQLS($id, $EXT_LOAD_MODE) {
245 global $cacheInstance, $_CONFIG;
248 // By default no SQL has been executed
251 // This shall never do a non-admin user!
252 if (!IS_ADMIN()) return false;
254 // Get extension's name
255 $ext_name = GET_EXT_NAME($id);
256 if (empty($ext_name)) return false;
258 // Load extension in detected mode
259 //* DEBUG: */ echo __FUNCTION__.":ext_name[{$id}]={$ext_name}<br />\n";
260 $file = sprintf("%sinc/extensions/ext-%s.php", PATH, $ext_name);
261 if (FILE_READABLE($file)) {
266 //* DEBUG: */ echo __FUNCTION__.":SQLs::count=".count($SQLs)."<br />\n";
267 if ((is_array($SQLs) && (sizeof($SQLs) > 0))) {
268 // Run SQL commands...
269 foreach ($SQLs as $sql) {
270 // Trim spaces away which we don't need
273 // Is there still an SQL query?
275 // Do we have an "ALTER TABLE" command?
276 //* DEBUG: */ echo __FUNCTION__.":SQL={$SQL}<br />\n";
277 if (substr(strtolower($sql), 0, 11) == "alter table") {
278 // Analyse the alteration command
279 SQL_ALTER_TABLE($sql, __FILE__, __LINE__);
281 // Run regular SQL command
282 $result = SQL_QUERY($sql, __FILE__, __LINE__, false);
285 // An SQL has been executed
291 if ($EXT_LOAD_MODE == "remove") {
292 // Delete this extension (remember to remove it from your server *before* you click on welcome!
293 $result = SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM "._MYSQL_PREFIX."_extensions WHERE id=%s LIMIT 1",
294 array($id), __FILE__, __LINE__);
297 //* DEBUG: */ echo __FUNCTION__.":mode={$EXT_LOAD_MODE}<br />\n";
299 // Remove cache file(s) if extension is active
300 if (((EXT_IS_ACTIVE("cache")) || (GET_EXT_VERSION("cache") != "")) && (((SQL_AFFECTEDROWS() == 1) && ($EXT_LOAD_MODE == "remove")) || ($sqlRan === true))) {
301 //* DEBUG: */ echo __LINE__.": DESTROY!<br />\n";
302 // Remove cache files
303 if ($cacheInstance->cache_file("extensions", true)) $cacheInstance->cache_destroy();
304 if ($cacheInstance->cache_file("mod_reg", true)) $cacheInstance->cache_destroy();
305 if ($cacheInstance->cache_file("config", true)) $cacheInstance->cache_destroy();
307 // @TODO This causes the whole (!) menu cache being purged
308 CACHE_PURGE_ADMIN_MENU();
311 // Is this the sql_patches?
312 //* DEBUG: */ echo __LINE__.": {$id}/{$ext_name}/{$EXT_LOAD_MODE}<br />\n";
313 if (($ext_name == "sql_patches") && (($EXT_LOAD_MODE == "register") || ($EXT_LOAD_MODE == "remove"))) {
314 // Then redirect to logout
315 //* DEBUG: */ echo __LINE__.": LOAD!<br />\n";
316 LOAD_URL("modules.php?module=admin&logout=1&".$EXT_LOAD_MODE."=sql_patches");
320 // Check if given extension is active
321 function EXT_IS_ACTIVE ($ext_name) {
322 global $cacheArray, $_CONFIG;
324 // Extensions are all inactive during installation
325 if ((!isBooleanConstantAndTrue('mxchange_installed')) || (isBooleanConstantAndTrue('mxchange_installing')) || (empty($ext_name))) return false;
327 // Not active is the default
331 if (!empty($cacheArray['extensions']['ext_active'][$ext_name])) {
333 //* DEBUG: */ echo "CACHE! ext_name={$ext_name}<br />\n";
334 $active = $cacheArray['extensions']['ext_active'][$ext_name];
337 if (isset($_CONFIG['cache_hits'])) $_CONFIG['cache_hits']++;
338 } elseif (($ext_name == "cache") || (GET_EXT_VERSION("cache") == "")) {
339 //* DEBUG: */ echo "DB! ext_name={$ext_name}<br />\n";
340 // Load from database
341 $result = SQL_QUERY_ESC("SELECT ext_active FROM "._MYSQL_PREFIX."_extensions WHERE ext_name='%s' LIMIT 1",
342 array($ext_name), __FILE__, __LINE__);
343 if (SQL_NUMROWS($result) == 0) {
344 // Extension was not found!
347 list($active) = SQL_FETCHROW($result);
348 //* DEBUG: */ echo $ext_name."[DB]: {$active}<br />\n";
349 SQL_FREERESULT($result);
352 $cacheArray['extensions']['ext_active'][$ext_name] = $active;
354 // Extension not active!
355 //* DEBUG: */ echo $ext_name.": Not active!<br />\n";
356 $cacheArray['extensions']['ext_active'][$ext_name] = "N";
359 // Create FQFN for extension file
360 $inc = sprintf("%sinc/extensions/ext-%s.php", PATH, $ext_name);
363 //DEBUG_LOG(__FUNCTION__.": ext_name={$ext_name},active={$active}");
365 // Is this extension activated? (For admins we always have active extensions...)
366 return ($active == "Y");
368 // Get version from extensions
369 function GET_EXT_VERSION ($ext_name) {
370 global $cacheArray, $_CONFIG, $cacheInstance;
373 // Extensions are all inactive during installation
374 if ((!isBooleanConstantAndTrue('mxchange_installed')) || (isBooleanConstantAndTrue('mxchange_installing'))) return "";
375 //* DEBUG: */ echo __FUNCTION__.": ext_name={$ext_name}<br />\n";
377 // Is the cache written?
378 if (!empty($cacheArray['extensions']['ext_version'][$ext_name])) {
379 // Load data from cache
380 //* DEBUG: */ echo __FUNCTION__.": CACHE!<br />\n";
381 $ret = $cacheArray['extensions']['ext_version'][$ext_name];
384 if (isset($_CONFIG['cache_hits'])) $_CONFIG['cache_hits']++; else $_CONFIG['cache_hits'] = 1;
385 } elseif (!is_object($cacheInstance)) {
386 // Load from database
387 $result = SQL_QUERY_ESC("SELECT ext_version FROM "._MYSQL_PREFIX."_extensions WHERE ext_name='%s' LIMIT 1",
388 array($ext_name), __FILE__, __LINE__);
389 list($ret) = SQL_FETCHROW($result);
390 SQL_FREERESULT($result);
393 $cacheArray['extensions']['ext_version'][$ext_name] = $ret;
397 //* DEBUG: */ echo __FUNCTION__.": ret={$ret}<br />\n";
401 function EXTENSION_UPDATE($file, $ext, $EXT_VER, $dry_run=false)
403 // This shall never do a non-admin user!
404 global $cacheInstance, $_CONFIG, $NOTES; $SQLs = array();
405 if ((!IS_ADMIN()) || (empty($ext))) return false;
407 // Load extension in update mode
408 $EXT_LOAD_MODE = "update"; $EXT_UPDATE_DEPENDS = ""; $NOTES = "";
410 // Load extension file
411 include(sprintf("%sinc/extensions/%s", PATH, $file));
413 if (!empty($EXT_UPDATE_DEPENDS)) {
414 // Update another extension first!
415 $test = EXTENSION_UPDATE(("ext-".$EXT_UPDATE_DEPENDS.".php"), $EXT_UPDATE_DEPENDS, GET_EXT_VERSION($EXT_UPDATE_DEPENDS), $dry_run);
418 // Check if version is updated
419 if ((($EXT_VERSION != $EXT_VER) || ($dry_run)) && (is_array($EXT_VER_HISTORY)))
421 // Search for starting point
422 $start = array_search($EXT_VER, $EXT_VER_HISTORY);
425 // And load SQL queries in order of version history
426 for ($idx = ($start + 1); $idx < sizeof($EXT_VER_HISTORY); $idx++)
428 // Remove old SQLs array to prevent possible bugs
429 if (!$dry_run) { unset($SQLs); $SQLs = array(); }
432 $EXT_VER = $EXT_VER_HISTORY[$idx];
435 include(PATH."inc/extensions/".$file);
438 if ($_CONFIG['verbose_sql'] == "Y")
440 $EXT_VER = $EXT_VER_HISTORY[$idx];
441 if (!empty($UPDATE_NOTES))
443 // Update notes found
444 $NOTES .= "<STRONG>v".$EXT_VER.":</STRONG><br />".$UPDATE_NOTES."<br /><br />\n";
447 elseif ($EXT_VER == "0.0")
450 $NOTES .= "<STRONG>v".$EXT_VER.":</STRONG><br />".INITIAL_RELEASE."<br /><br />\n";
454 $NOTES .= "<STRONG>v".$EXT_VER.":</STRONG><br /><I>".NO_UPDATE_NOTES."</I><br /><br />\n";
459 if ((is_array($SQLs)) && (!$dry_run))
462 foreach ($SQLs as $sql)
467 // Do we have an "ALTER TABLE" command?
468 if (substr(strtolower($sql), 0, 11) == "alter table") {
469 // Analyse the alteration command
470 SQL_ALTER_TABLE($sql, __FILE__, __LINE__);
472 // Run regular SQL command
473 $result = SQL_QUERY($sql, __FILE__, __LINE__, false);
477 } elseif (GET_EXT_VERSION("sql_patches") == "") {
478 // Remove SQLs if extension is not installed
485 // In normal mode insert task and update extension's version...
486 $ext_subj = "[UPDATE-".$ext."-".$EXT_VERSION.":] ".ADMIN_UPDATE_EXT_SUBJ;
489 CREATE_EXTENSION_UPDATE_TASK(GET_ADMIN_ID(get_session('admin_login')), $ext_subj, addslashes($NOTES));
491 // Update extension's version
492 $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_extensions SET ext_version='%s' WHERE ext_name='%s' LIMIT 1",
493 array($EXT_VERSION, $ext), __FILE__, __LINE__);
496 if (EXT_IS_ACTIVE("cache")) {
497 if ($cacheInstance->cache_file("extensions", true) == true) $cacheInstance->cache_destroy();
498 if ($cacheInstance->cache_file("config", true) == true) $cacheInstance->cache_destroy();
499 if ($cacheInstance->cache_file("mod_reg", true) == true) $cacheInstance->cache_destroy();
505 // In "dry-run" mode return array with SQL commands
511 function EXTENSION_VERBOSE_TABLE($SQLs, $title = ADMIN_SQLS_EXECUTED_ON_REMOVAL, $dashed = "", $switch = false, $WIDTH = "480") {
514 $S = false; $SW = 2; $i = 1;
516 if ((is_array($SQLs)) && (GET_EXT_VERSION("sql_patches") >= "0.0.7") && ($_CONFIG['verbose_sql'] == "Y")) {
517 $OUT = "<DIV align=\"center\">
518 <TABLE border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"".$WIDTH."\" align=\"center\"".$dashed.">
520 <TD colspan=\"2\" align=\"center\" class=\"admin_title bottom2\" height=\"24\">
521 <STRONG>".$title.":</STRONG>
524 foreach ($SQLs as $idx => $sql) {
529 <TD class=\"switch_sw".(3 - $SW)." bottom2\" style=\"padding-left: 2px\" width=\"30\">".$i.".</TD>
530 <TD class=\"switch_sw".(3 - $SW)." bottom2\" style=\"padding-left: 5px; padding-right: 5px\">
534 if ($switch) $SW = 3 - $SW;
540 if ((!$S) && (GET_EXT_VERSION("sql_patches") >= "0.0.7") && ($_CONFIG['verbose_sql'] == "Y")) {
541 // No addional SQL commands to run
543 <TD colspan=\"2\" align=\"center\" class=\"switch_sw2 bottom2\" height=\"24\">
544 <FONT class=\"admin_note\">".ADMIN_NO_ADDIONAL_SQLS."</FONT>
550 // Add missing close-table tag
558 // Get extension name from id
559 function GET_EXT_NAME ($id) {
561 global $cacheArray, $_CONFIG;
562 if (!empty($cacheArray['extensions']['ext_name'][$id])) {
564 $ret = $cacheArray['extensions']['ext_name'][$id];
567 $_CONFIG['cache_hits']++;
568 } elseif (!EXT_IS_ACTIVE("cache")) {
569 // Load from database
570 $result = SQL_QUERY_ESC("SELECT ext_name FROM "._MYSQL_PREFIX."_extensions WHERE id=%s LIMIT 1",
571 array(bigintval($id)), __FILE__, __LINE__);
572 list($ret) = SQL_FETCHROW($result);
573 SQL_FREERESULT($result);
577 // Get extension id from name
578 function GET_EXT_ID($name) {
580 global $cacheArray, $_CONFIG;
581 if (isset($cacheArray['extensions']['ext_id'][$name])) {
583 $ret = $cacheArray['extensions']['ext_id'][$name];
586 $_CONFIG['cache_hits']++;
587 } elseif (!EXT_IS_ACTIVE("cache")) {
588 // Load from database
589 $result = SQL_QUERY_ESC("SELECT id FROM "._MYSQL_PREFIX."_extensions WHERE ext_name='%s' LIMIT 1",
590 array($name), __FILE__, __LINE__);
591 list($ret) = SQL_FETCHROW($result);
592 SQL_FREERESULT($result);
598 // Activate given extension
599 function ACTIVATE_EXTENSION($ext_name) {
600 // Activate the extension
601 $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_extensions SET ext_active='Y' WHERE ext_name='%s' LIMIT 1",
602 array($ext_name), __FILE__, __LINE__);
604 // Extension has been activated?
605 if (SQL_AFFECTEDROWS() == 1) {
606 // Then run all queries
607 EXTENSION_RUN_SQLS(GET_EXT_ID($ext_name), "activate");
610 // Checks wether the extension is older than given
611 function EXT_VERSION_IS_OLDER ($ext_name, $ext_ver) {
612 // Get current extension version
613 $currVersion = GET_EXT_VERSION($ext_name);
615 // Remove all dots from both versions
616 $currVersion = str_replace(".", "", $currVersion);
617 $ext_ver = str_replace(".", "", $ext_ver);
619 // Now compare both and return the result
620 return ($currVersion < $ext_ver);