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;
45 // We want to register an extension and registration status is by default "failed" (= false)
46 $EXT_LOAD_MODE = "register"; $ret = false; $SQLs = array();
49 // This shall never do a non-admin user!
50 if (!IS_ADMIN()) return false;
52 // By default the language prefix is the extension's name
53 // @TODO: Do we really need this one anymore? Can't we just take $ext_name and done?
54 $EXT_LANG_PREFIX = $ext_name;
56 // By default we have no failtures
57 $EXT_REPORTS_FAILURE = false;
59 // Is this extension already installed?
60 if (EXT_IS_ACTIVE($ext_name)) return false;
63 $file = sprintf("%sinc/extensions/ext-%s.php", PATH, $ext_name);
65 // Does this extension exists?
66 if (FILE_READABLE($file)) {
67 // Extension was found so we can load it in registration mode
68 $EXT_ALWAYS_ACTIVE = "N";
71 // And run possible updates
72 $EXT_LOAD_MODE = "update"; $EXT_UPDATE_DEPENDS = "";
73 foreach ($EXT_VER_HISTORY as $EXT_VER) {
74 // Load extension in update mode
77 // Do we have an update?
78 if (((GET_EXT_VERSION("sql_patches") != "") && ($_CONFIG['verbose_sql'] == "Y")) || (!EXT_IS_ACTIVE("sql_patches"))) {
79 if (!empty($UPDATE_NOTES)) {
81 $NOTES .= "<STRONG>v".$EXT_VER.":</STRONG><br />".$UPDATE_NOTES."<br /><br />\n";
83 } elseif (($EXT_VER == "0.0") || ($EXT_VER == "0.0.0")) {
85 $NOTES .= "<STRONG>v".$EXT_VER.":</STRONG><br />".INITIAL_RELEASE."<br /><br />\n";
87 // No update notes found!
88 $NOTES .= "<STRONG>v".$EXT_VER.":</STRONG><br /><I>".NO_UPDATE_NOTES."</I><br /><br />\n";
93 // Does this extension depends on an outstanding update of another update?
94 if (!empty($EXT_UPDATE_DEPENDS)) {
95 // Backup SQL commands and clear current
100 // Load required extension also in update mode
101 $file = sprintf("%sinc/extensions/ext-%s.php", PATH, $EXT_UPDATE_DEPENDS);
103 // Check for required file
104 if (FILE_READABLE($file)) {
105 // Bacup version number
106 $VER_BACKUP = $EXT_VERSION;
108 // Backup language as well
109 $LANG_BCK = $EXT_LANG_PREFIX;
111 // Save the Parrent $EXT_ALWAYS_ACTIVE for later!
112 $EXT_ALWAYS_ACTIVE_PARRENT = $EXT_ALWAYS_ACTIVE;
114 // Set EXT_ALWAYS_ACTIVE for update
115 $EXT_ALWAYS_ACTIVE = "N";
117 // File exists so let's load it
120 // If versions mismatch update extension first
121 $ext_ver = GET_EXT_VERSION($EXT_UPDATE_DEPENDS);
123 // Extension version set?
124 if (empty($ext_ver)) {
125 // Extension not registered so far so first load task's ID...
126 $result = SQL_QUERY_ESC("SELECT id FROM "._MYSQL_PREFIX."_task_system WHERE task_type='EXTENSION' AND subject LIKE '[%s:]%%' LIMIT 1",
127 array($EXT_UPDATE_DEPENDS), __FILE__, __LINE__);
130 if (SQL_NUMROWS($result) == 1) {
131 // Task found so load task's ID and register extension...
132 list($task) = SQL_FETCHROW($result);
134 // Try to register the extension
135 $test = EXTENSION_REGISTER($EXT_UPDATE_DEPENDS, $task, $dry_run);
139 SQL_FREERESULT($result);
140 } elseif ($ext_ver != $EXT_VERSION) {
141 // Ok, update this extension now
142 EXTENSION_UPDATE(basename($file), $EXT_UPDATE_DEPENDS, $ext_ver, $dry_run);
147 // Nothing to register / update before...
151 // Restore version number
152 $EXT_VERSION = $VER_BACKUP;
154 // Restore language back
155 $EXT_LANG_PREFIX = $LANG_BCK;
157 // Restore $EXT_ALWAYS_ACTIVE with the value from parrent
158 $EXT_ALWAYS_ACTIVE = $EXT_ALWAYS_ACTIVE_PARRENT;
160 // Required file for update does not exists!
162 // But this is fine for the first time...
165 // Finally restore previous SQLs
166 $SQLs = $SQLs2; unset($SQLs2);
168 // Does not depend on an other extension
172 // Switch back to register mode
173 $EXT_LOAD_MODE = "register";
175 // Remains true if extension registration reports no failtures
176 $test = ($test && !$EXT_REPORTS_FAILURE);
178 // Does everthing before wents ok?
180 // "Dry-run-mode" activated?
183 foreach ($SQLs as $sql) {
184 // Trim spaces away which we don't need
187 // Is there still an SQL query?
189 // Do we have an "ALTER TABLE" command?
190 if (substr(strtolower($sql), 0, 11) == "alter table") {
191 // Analyse the alteration command
192 SQL_ALTER_TABLE($sql, __FILE__, __LINE__);
194 // Run regular SQL command
195 $result = SQL_QUERY($sql, __FILE__, __LINE__, false);
200 // Remove cache file(s) if extension is active
201 if ((EXT_IS_ACTIVE("cache")) || (GET_EXT_VERSION("cache") != "")) {
202 //* DEBUG: */ echo __LINE__.": DESTROY!<br />\n";
203 // Remove cache files
204 if ($cacheInstance->cache_file("extensions", true)) $cacheInstance->cache_destroy();
205 if ($cacheInstance->cache_file("mod_reg")) $cacheInstance->cache_destroy();
206 if ($cacheInstance->cache_file("config")) $cacheInstance->cache_destroy();
209 // Check for added include files
210 if (count($INC_POOL > 0)) {
211 // Loads every include file
212 foreach ($INC_POOL as $inc) {
220 // Register extension
221 $result = SQL_QUERY_ESC("INSERT INTO "._MYSQL_PREFIX."_extensions (ext_name, ext_lang_file, ext_active, ext_version) VALUES ('%s','%s','%s','%s')",
222 array($ext_name, $EXT_LANG_PREFIX, $EXT_ALWAYS_ACTIVE, $EXT_VERSION), __FILE__, __LINE__);
224 // Update task management
225 ADMIN_SOLVE_TASK($id);
227 // @TODO This causes the whole (!) menu cache being purged
228 CACHE_PURGE_ADMIN_MENU();
230 // In normal mode return a true on success
236 // Rewrite SQL command to keep { and } inside
237 foreach ($SQLs as $key => $sql) {
238 $sql = str_replace('{', "{", str_replace('}', "}", $sql));
242 // In "dry-run" mode return array with all SQL commands
245 // Remove all SQL commands
249 // No, an error occurs while registering extension :-(
252 } elseif (($id > 0) && (!empty($ext_name))) {
253 // Remove task from system when id and extension's name is valid
254 $result = SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM "._MYSQL_PREFIX."_task_system WHERE id=%s AND status='NEW' LIMIT 1",
255 array(bigintval($id)), __FILE__, __LINE__);
258 // Is this the sql_patches?
259 //* DEBUG: */ echo __LINE__.":{$ext_name}/{$EXT_LOAD_MODE}<br />\n";
260 if (($ext_name == "sql_patches") && (($EXT_LOAD_MODE == "register") || ($EXT_LOAD_MODE == "remove")) && (!$dry_run) && ($test)) {
261 // Then redirect to logout
262 //* DEBUG: */ echo __LINE__.": LOAD!<br />\n";
263 LOAD_URL("modules.php?module=admin&logout=1&".$EXT_LOAD_MODE."=sql_patches");
266 // Return status code
270 function EXTENSION_RUN_SQLS($id, $EXT_LOAD_MODE) {
271 global $cacheInstance, $_CONFIG;
274 // Extensions are never active by default
275 $EXT_ALWAYS_ACTIVE = "N";
277 // By default no SQL has been executed
280 // This shall never do a non-admin user!
281 if (!IS_ADMIN()) return false;
283 // Get extension's name
284 $ext_name = GET_EXT_NAME($id);
285 if (empty($ext_name)) return false;
287 // Load extension in detected mode
288 //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):ext_name[{$id}]={$ext_name}<br />\n";
289 $file = sprintf("%sinc/extensions/ext-%s.php", PATH, $ext_name);
290 if (FILE_READABLE($file)) {
295 //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):SQLs::count=".count($SQLs)."<br />\n";
296 if ((is_array($SQLs) && (sizeof($SQLs) > 0))) {
297 // Run SQL commands...
298 foreach ($SQLs as $sql) {
299 // Trim spaces away which we don't need
302 // Is there still an SQL query?
304 // Do we have an "ALTER TABLE" command?
305 //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):SQL={$SQL}<br />\n";
306 if (substr(strtolower($sql), 0, 11) == "alter table") {
307 // Analyse the alteration command
308 SQL_ALTER_TABLE($sql, __FILE__, __LINE__);
310 // Run regular SQL command
311 $result = SQL_QUERY($sql, __FILE__, __LINE__, false);
314 // An SQL has been executed
320 if ($EXT_LOAD_MODE == "remove") {
321 // Delete this extension (remember to remove it from your server *before* you click on welcome!
322 $result = SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM "._MYSQL_PREFIX."_extensions WHERE id=%s LIMIT 1",
323 array($id), __FILE__, __LINE__);
326 //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):mode={$EXT_LOAD_MODE}<br />\n";
328 // Is this the sql_patches?
329 //* DEBUG: */ echo __LINE__.": {$id}/{$ext_name}/{$EXT_LOAD_MODE}<br />\n";
330 if (($ext_name == "sql_patches") && (($EXT_LOAD_MODE == "register") || ($EXT_LOAD_MODE == "remove"))) {
331 // Then redirect to logout
332 //* DEBUG: */ echo __LINE__.": LOAD!<br />\n";
333 LOAD_URL("modules.php?module=admin&logout=1&".$EXT_LOAD_MODE."=sql_patches");
337 // Remove cache file(s) if extension is active
338 if (((EXT_IS_ACTIVE("cache")) || (GET_EXT_VERSION("cache") != "")) && (((SQL_AFFECTEDROWS() == 1)) || ($sqlRan === true) || ($EXT_LOAD_MODE == "activate") || ($EXT_LOAD_MODE == "deactivate"))) {
339 //* DEBUG: */ echo __LINE__.": DESTROY!<br />\n";
340 // Remove cache files
341 if ($cacheInstance->cache_file("extensions", true)) $cacheInstance->cache_destroy();
342 if ($cacheInstance->cache_file("mod_reg")) $cacheInstance->cache_destroy();
343 if ($cacheInstance->cache_file("config")) $cacheInstance->cache_destroy();
345 // @TODO This causes the whole (!) menu cache being purged
346 CACHE_PURGE_ADMIN_MENU();
349 // Check if given extension is active
350 function EXT_IS_ACTIVE ($ext_name) {
351 global $cacheArray, $_CONFIG;
353 // Extensions are all inactive during installation
354 if ((!isBooleanConstantAndTrue('mxchange_installed')) || (isBooleanConstantAndTrue('mxchange_installing')) || (empty($ext_name))) return false;
356 // Not active is the default
360 if (!empty($cacheArray['extensions']['ext_active'][$ext_name])) {
362 //* DEBUG: */ echo "CACHE! ext_name={$ext_name}<br />\n";
363 $active = $cacheArray['extensions']['ext_active'][$ext_name];
366 if (isset($_CONFIG['cache_hits'])) $_CONFIG['cache_hits']++;
367 } elseif (($ext_name == "cache") || (GET_EXT_VERSION("cache") == "")) {
368 //* DEBUG: */ echo "DB! ext_name={$ext_name}<br />\n";
369 // Load from database
370 $result = SQL_QUERY_ESC("SELECT ext_active FROM "._MYSQL_PREFIX."_extensions WHERE ext_name='%s' LIMIT 1",
371 array($ext_name), __FILE__, __LINE__);
372 if (SQL_NUMROWS($result) == 0) {
373 // Extension was not found!
378 list($active) = SQL_FETCHROW($result);
381 SQL_FREERESULT($result);
385 //* DEBUG: */ echo $ext_name."[DB]: {$active}<br />\n";
386 $cacheArray['extensions']['ext_active'][$ext_name] = $active;
388 // Extension not active!
389 //* DEBUG: */ echo $ext_name.": Not active!<br />\n";
390 $cacheArray['extensions']['ext_active'][$ext_name] = "N";
394 //* DEBUG: */ DEBUG_LOG(__FUNCTION__, __LINE__, " ext_name={$ext_name},active={$active}");
396 // Is this extension activated? (For admins we always have active extensions...)
397 return ($active == "Y");
399 // Get version from extensions
400 function GET_EXT_VERSION ($ext_name) {
401 global $cacheArray, $_CONFIG, $cacheInstance;
404 // Extensions are all inactive during installation
405 if ((!isBooleanConstantAndTrue('mxchange_installed')) || (isBooleanConstantAndTrue('mxchange_installing'))) return "";
406 //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>): ext_name={$ext_name}<br />\n";
408 // Is the cache written?
409 if (!empty($cacheArray['extensions']['ext_version'][$ext_name])) {
410 // Load data from cache
411 //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>): CACHE!<br />\n";
412 $ret = $cacheArray['extensions']['ext_version'][$ext_name];
415 if (isset($_CONFIG['cache_hits'])) $_CONFIG['cache_hits']++; else $_CONFIG['cache_hits'] = 1;
416 } elseif (!is_object($cacheInstance)) {
417 // Load from database
418 $result = SQL_QUERY_ESC("SELECT ext_version FROM "._MYSQL_PREFIX."_extensions WHERE ext_name='%s' LIMIT 1",
419 array($ext_name), __FILE__, __LINE__);
420 list($ret) = SQL_FETCHROW($result);
421 SQL_FREERESULT($result);
424 $cacheArray['extensions']['ext_version'][$ext_name] = $ret;
428 //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>): ret={$ret}<br />\n";
432 function EXTENSION_UPDATE($file, $ext, $EXT_VER, $dry_run=false)
434 // This shall never do a non-admin user!
435 global $cacheInstance, $_CONFIG, $NOTES;
438 $SQLs = array(); $INC_POOL = array();
440 // Only admins are allowed to update extensions
441 if ((!IS_ADMIN()) || (empty($ext))) return false;
443 // Load extension in update mode
444 $EXT_LOAD_MODE = "update"; $EXT_UPDATE_DEPENDS = ""; $NOTES = "";
446 // Load extension file
447 include(sprintf("%sinc/extensions/%s", PATH, $file));
449 if (!empty($EXT_UPDATE_DEPENDS)) {
450 // Update another extension first!
451 $test = EXTENSION_UPDATE(("ext-".$EXT_UPDATE_DEPENDS.".php"), $EXT_UPDATE_DEPENDS, GET_EXT_VERSION($EXT_UPDATE_DEPENDS), $dry_run);
454 // Check if version is updated
455 if ((($EXT_VERSION != $EXT_VER) || ($dry_run)) && (is_array($EXT_VER_HISTORY)))
457 // Search for starting point
458 $start = array_search($EXT_VER, $EXT_VER_HISTORY);
461 // And load SQL queries in order of version history
462 for ($idx = ($start + 1); $idx < sizeof($EXT_VER_HISTORY); $idx++)
464 // Remove old SQLs array to prevent possible bugs
465 if (!$dry_run) { unset($SQLs); $SQLs = array(); }
468 $EXT_VER = $EXT_VER_HISTORY[$idx];
471 include(PATH."inc/extensions/".$file);
474 if ($_CONFIG['verbose_sql'] == "Y")
476 $EXT_VER = $EXT_VER_HISTORY[$idx];
477 if (!empty($UPDATE_NOTES))
479 // Update notes found
480 $NOTES .= "<STRONG>v".$EXT_VER.":</STRONG><br />".$UPDATE_NOTES."<br /><br />\n";
483 elseif ($EXT_VER == "0.0")
486 $NOTES .= "<STRONG>v".$EXT_VER.":</STRONG><br />".INITIAL_RELEASE."<br /><br />\n";
490 $NOTES .= "<STRONG>v".$EXT_VER.":</STRONG><br /><I>".NO_UPDATE_NOTES."</I><br /><br />\n";
494 // In real-mode execute any existing includes
495 if ((!$dry_run) && (count($INC_POOL) > 0)) {
497 foreach ($INC_POOL as $fqfn) {
503 if ((is_array($SQLs)) && (!$dry_run)) {
505 foreach ($SQLs as $sql)
510 // Do we have an "ALTER TABLE" command?
511 if (substr(strtolower($sql), 0, 11) == "alter table") {
512 // Analyse the alteration command
513 SQL_ALTER_TABLE($sql, __FILE__, __LINE__);
515 // Run regular SQL command
516 $result = SQL_QUERY($sql, __FILE__, __LINE__, false);
520 } elseif (GET_EXT_VERSION("sql_patches") == "") {
521 // Remove SQLs if extension is not installed
528 // In normal mode insert task and update extension's version...
529 $ext_subj = "[UPDATE-".$ext."-".$EXT_VERSION.":] ".ADMIN_UPDATE_EXT_SUBJ;
532 CREATE_EXTENSION_UPDATE_TASK(GET_ADMIN_ID(get_session('admin_login')), $ext_subj, addslashes($NOTES));
534 // Update extension's version
535 $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_extensions SET ext_version='%s' WHERE ext_name='%s' LIMIT 1",
536 array($EXT_VERSION, $ext), __FILE__, __LINE__);
539 if (EXT_IS_ACTIVE("cache")) {
540 if ($cacheInstance->cache_file("extensions", true)) $cacheInstance->cache_destroy();
541 if ($cacheInstance->cache_file("config")) $cacheInstance->cache_destroy();
542 if ($cacheInstance->cache_file("mod_reg")) $cacheInstance->cache_destroy();
548 // In "dry-run" mode return array with SQL commands
554 function EXTENSION_VERBOSE_TABLE($SQLs, $title = ADMIN_SQLS_EXECUTED_ON_REMOVAL, $dashed = "", $switch = false, $WIDTH = "480") {
557 $S = false; $SW = 2; $i = 1;
559 if ((is_array($SQLs)) && (GET_EXT_VERSION("sql_patches") >= "0.0.7") && ($_CONFIG['verbose_sql'] == "Y")) {
560 $OUT = "<DIV align=\"center\">
561 <TABLE border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"".$WIDTH."\" align=\"center\"".$dashed.">
563 <TD colspan=\"2\" align=\"center\" class=\"admin_title bottom2\" height=\"24\">
564 <STRONG>".$title.":</STRONG>
567 foreach ($SQLs as $idx => $sql) {
572 <TD class=\"switch_sw".(3 - $SW)." bottom2\" style=\"padding-left: 2px\" width=\"30\">".$i.".</TD>
573 <TD class=\"switch_sw".(3 - $SW)." bottom2\" style=\"padding-left: 5px; padding-right: 5px\">
577 if ($switch) $SW = 3 - $SW;
583 if ((!$S) && (GET_EXT_VERSION("sql_patches") >= "0.0.7") && ($_CONFIG['verbose_sql'] == "Y")) {
584 // No addional SQL commands to run
586 <TD colspan=\"2\" align=\"center\" class=\"switch_sw2 bottom2\" height=\"24\">
587 <FONT class=\"admin_note\">".ADMIN_NO_ADDIONAL_SQLS."</FONT>
593 // Add missing close-table tag
601 // Get extension name from id
602 function GET_EXT_NAME ($id) {
604 global $cacheArray, $_CONFIG;
605 if (!empty($cacheArray['extensions']['ext_name'][$id])) {
607 $ret = $cacheArray['extensions']['ext_name'][$id];
610 if (isset($_CONFIG['cache_hits'])) { $_CONFIG['cache_hits']++; } else { $_CONFIG['cache_hits'] = 1; }
611 } elseif (!EXT_IS_ACTIVE("cache")) {
612 // Load from database
613 $result = SQL_QUERY_ESC("SELECT ext_name FROM "._MYSQL_PREFIX."_extensions WHERE id=%s LIMIT 1",
614 array(bigintval($id)), __FILE__, __LINE__);
615 list($ret) = SQL_FETCHROW($result);
616 SQL_FREERESULT($result);
620 // Get extension id from name
621 function GET_EXT_ID($name) {
623 global $cacheArray, $_CONFIG;
624 if (isset($cacheArray['extensions']['ext_id'][$name])) {
626 $ret = $cacheArray['extensions']['ext_id'][$name];
629 if (isset($_CONFIG['cache_hits'])) { $_CONFIG['cache_hits']++; } else { $_CONFIG['cache_hits'] = 1; }
630 } elseif (!EXT_IS_ACTIVE("cache")) {
631 // Load from database
632 $result = SQL_QUERY_ESC("SELECT id FROM "._MYSQL_PREFIX."_extensions WHERE ext_name='%s' LIMIT 1",
633 array($name), __FILE__, __LINE__);
634 list($ret) = SQL_FETCHROW($result);
635 SQL_FREERESULT($result);
641 // Activate given extension
642 function ACTIVATE_EXTENSION($ext_name) {
643 // Activate the extension
644 $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_extensions SET ext_active='Y' WHERE ext_name='%s' LIMIT 1",
645 array($ext_name), __FILE__, __LINE__);
647 // Extension has been activated?
648 if (SQL_AFFECTEDROWS() == 1) {
649 // Then run all queries
650 EXTENSION_RUN_SQLS(GET_EXT_ID($ext_name), "activate");
653 // Checks wether the extension is older than given
654 function EXT_VERSION_IS_OLDER ($ext_name, $ext_ver) {
655 // Get current extension version
656 $currVersion = GET_EXT_VERSION($ext_name);
658 // Remove all dots from both versions
659 $currVersion = str_replace(".", "", $currVersion);
660 $ext_ver = str_replace(".", "", $ext_ver);
662 // Now compare both and return the result
663 return ($currVersion < $ext_ver);