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
60 $EXT_ALWAYS_ACTIVE = "N";
63 // And run possible updates
64 $EXT_LOAD_MODE = "update"; $EXT_UPDATE_DEPENDS = "";
65 foreach ($EXT_VER_HISTORY as $EXT_VER) {
66 // Load extension in update mode
69 // Do we have an update?
70 if (((GET_EXT_VERSION("sql_patches") != "") && ($_CONFIG['verbose_sql'] == "Y")) || (!EXT_IS_ACTIVE("sql_patches"))) {
71 if (!empty($UPDATE_NOTES)) {
73 $NOTES .= "<STRONG>v".$EXT_VER.":</STRONG><br />".$UPDATE_NOTES."<br /><br />\n";
75 } elseif (($EXT_VER == "0.0") || ($EXT_VER == "0.0.0")) {
77 $NOTES .= "<STRONG>v".$EXT_VER.":</STRONG><br />".INITIAL_RELEASE."<br /><br />\n";
79 // No update notes found!
80 $NOTES .= "<STRONG>v".$EXT_VER.":</STRONG><br /><I>".NO_UPDATE_NOTES."</I><br /><br />\n";
85 // Does this extension depends on an outstanding update of another update?
86 if (!empty($EXT_UPDATE_DEPENDS)) {
87 // Backup SQL commands and clear current
92 // Backup language as well
93 $LANG_BCK = $EXT_LANG_PREFIX;
94 $EXT_ALWAYS_ACTIVE = "N";
96 // Load required extension also in update mode
97 $file = sprintf("%sinc/extensions/ext-%s.php", PATH, $EXT_UPDATE_DEPENDS);
99 // Check for required file
100 if (FILE_READABLE($file)) {
101 // File exists so let's load it
102 $VER_BACKUP = $EXT_VERSION;
103 $EXT_ALWAYS_ACTIVE = "N";
105 $EXT_VERSION = $VER_BACKUP;
107 // If versions mismatch update extension first
108 $ext_ver = GET_EXT_VERSION($EXT_UPDATE_DEPENDS);
110 // Extension version set?
111 if (empty($ext_ver)) {
112 // Extension not registered so far so first load task's ID...
113 $result = SQL_QUERY_ESC("SELECT id FROM "._MYSQL_PREFIX."_task_system WHERE task_type='EXTENSION' AND subject LIKE '[%s:]%%' LIMIT 1",
114 array($EXT_UPDATE_DEPENDS), __FILE__, __LINE__);
117 if (SQL_NUMROWS($result) == 1) {
118 // Task found so load task's ID and register extension...
119 list($task) = SQL_FETCHROW($result);
121 // Try to register the extension
122 $test = EXTENSION_REGISTER($EXT_UPDATE_DEPENDS, $task, $dry_run);
126 SQL_FREERESULT($result);
127 } elseif ($ext_ver != $EXT_VERSION) {
128 // Ok, update this extension now
129 EXTENSION_UPDATE(basename($file), $EXT_UPDATE_DEPENDS, $ext_ver, $dry_run);
134 // Nothing to register / update before...
138 // Required file for update does not exists!
140 // But this is fine for the first time...
143 // Finally restore previous SQLs
144 $SQLs = $SQLs2; unset($SQLs2);
145 $EXT_LANG_PREFIX = $LANG_BCK;
147 // Does not depend on an other extension
151 // Switch back to register mode
152 $EXT_LOAD_MODE = "register";
154 // Does everthing before wents ok?
156 // "Dry-run-mode" activated?
159 foreach ($SQLs as $sql) {
160 // Trim spaces away which we don't need
163 // Is there still an SQL query?
165 // Do we have an "ALTER TABLE" command?
166 if (substr(strtolower($sql), 0, 11) == "alter table") {
167 // Analyse the alteration command
168 SQL_ALTER_TABLE($sql, __FILE__, __LINE__);
170 // Run regular SQL command
171 $result = SQL_QUERY($sql, __FILE__, __LINE__, false);
176 // Remove cache file(s) if extension is active
177 if ((EXT_IS_ACTIVE("cache")) || (GET_EXT_VERSION("cache") != "")) {
178 //* DEBUG: */ echo __LINE__.": DESTROY!<br />\n";
179 // Remove cache files
180 if ($cacheInstance->cache_file("extensions", true)) $cacheInstance->cache_destroy();
181 if ($cacheInstance->cache_file("mod_reg", true)) $cacheInstance->cache_destroy();
182 if ($cacheInstance->cache_file("config", true)) $cacheInstance->cache_destroy();
185 // Check for added include files
186 if (count($INC_POOL > 0)) {
187 // Loads every include file
188 foreach ($INC_POOL as $inc) {
196 // Register extension
197 $result = SQL_QUERY_ESC("INSERT INTO "._MYSQL_PREFIX."_extensions (ext_name, ext_lang_file, ext_active, ext_version) VALUES ('%s','%s','%s','%s')",
198 array($ext_name, $EXT_LANG_PREFIX, $EXT_ALWAYS_ACTIVE, $EXT_VERSION), __FILE__, __LINE__);
200 // Update task management
201 ADMIN_SOLVE_TASK($id);
203 // @TODO This causes the whole (!) menu cache being purged
204 CACHE_PURGE_ADMIN_MENU();
206 // In normal mode return a true on success
212 // Rewrite SQL command to keep { and } inside
213 foreach ($SQLs as $key => $sql) {
214 $sql = str_replace('{', "{", str_replace('}', "}", $sql));
218 // In "dry-run" mode return array with all SQL commands
221 // Remove all SQL commands
225 // No, an error occurs while registering extension :-(
228 } elseif (($id > 0) && (!empty($ext_name))) {
229 // Remove task from system when id and extension's name is valid
230 $result = SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM "._MYSQL_PREFIX."_task_system WHERE id=%s AND status='NEW' LIMIT 1",
231 array(bigintval($id)), __FILE__, __LINE__);
234 // Is this the sql_patches?
235 //* DEBUG: */ echo __LINE__.":{$ext_name}/{$EXT_LOAD_MODE}<br />\n";
236 if (($ext_name == "sql_patches") && (($EXT_LOAD_MODE == "register") || ($EXT_LOAD_MODE == "remove")) && (!$dry_run) && ($test)) {
237 // Then redirect to logout
238 //* DEBUG: */ echo __LINE__.": LOAD!<br />\n";
239 LOAD_URL("modules.php?module=admin&logout=1&".$EXT_LOAD_MODE."=sql_patches");
242 // Return status code
246 function EXTENSION_RUN_SQLS($id, $EXT_LOAD_MODE) {
247 global $cacheInstance, $_CONFIG;
250 // Extensions are never active by default
251 $EXT_ALWAYS_ACTIVE = "N";
253 // By default no SQL has been executed
256 // This shall never do a non-admin user!
257 if (!IS_ADMIN()) return false;
259 // Get extension's name
260 $ext_name = GET_EXT_NAME($id);
261 if (empty($ext_name)) return false;
263 // Load extension in detected mode
264 //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):ext_name[{$id}]={$ext_name}<br />\n";
265 $file = sprintf("%sinc/extensions/ext-%s.php", PATH, $ext_name);
266 if (FILE_READABLE($file)) {
271 //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):SQLs::count=".count($SQLs)."<br />\n";
272 if ((is_array($SQLs) && (sizeof($SQLs) > 0))) {
273 // Run SQL commands...
274 foreach ($SQLs as $sql) {
275 // Trim spaces away which we don't need
278 // Is there still an SQL query?
280 // Do we have an "ALTER TABLE" command?
281 //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):SQL={$SQL}<br />\n";
282 if (substr(strtolower($sql), 0, 11) == "alter table") {
283 // Analyse the alteration command
284 SQL_ALTER_TABLE($sql, __FILE__, __LINE__);
286 // Run regular SQL command
287 $result = SQL_QUERY($sql, __FILE__, __LINE__, false);
290 // An SQL has been executed
296 if ($EXT_LOAD_MODE == "remove") {
297 // Delete this extension (remember to remove it from your server *before* you click on welcome!
298 $result = SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM "._MYSQL_PREFIX."_extensions WHERE id=%s LIMIT 1",
299 array($id), __FILE__, __LINE__);
302 //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>):mode={$EXT_LOAD_MODE}<br />\n";
304 // Remove cache file(s) if extension is active
305 if (((EXT_IS_ACTIVE("cache")) || (GET_EXT_VERSION("cache") != "")) && (((SQL_AFFECTEDROWS() == 1) && ($EXT_LOAD_MODE == "remove")) || ($sqlRan === true))) {
306 //* DEBUG: */ echo __LINE__.": DESTROY!<br />\n";
307 // Remove cache files
308 if ($cacheInstance->cache_file("extensions", true)) $cacheInstance->cache_destroy();
309 if ($cacheInstance->cache_file("mod_reg", true)) $cacheInstance->cache_destroy();
310 if ($cacheInstance->cache_file("config", true)) $cacheInstance->cache_destroy();
312 // @TODO This causes the whole (!) menu cache being purged
313 CACHE_PURGE_ADMIN_MENU();
316 // Is this the sql_patches?
317 //* DEBUG: */ echo __LINE__.": {$id}/{$ext_name}/{$EXT_LOAD_MODE}<br />\n";
318 if (($ext_name == "sql_patches") && (($EXT_LOAD_MODE == "register") || ($EXT_LOAD_MODE == "remove"))) {
319 // Then redirect to logout
320 //* DEBUG: */ echo __LINE__.": LOAD!<br />\n";
321 LOAD_URL("modules.php?module=admin&logout=1&".$EXT_LOAD_MODE."=sql_patches");
325 // Check if given extension is active
326 function EXT_IS_ACTIVE ($ext_name) {
327 global $cacheArray, $_CONFIG;
329 // Extensions are all inactive during installation
330 if ((!isBooleanConstantAndTrue('mxchange_installed')) || (isBooleanConstantAndTrue('mxchange_installing')) || (empty($ext_name))) return false;
332 // Not active is the default
336 if (!empty($cacheArray['extensions']['ext_active'][$ext_name])) {
338 //* DEBUG: */ echo "CACHE! ext_name={$ext_name}<br />\n";
339 $active = $cacheArray['extensions']['ext_active'][$ext_name];
342 if (isset($_CONFIG['cache_hits'])) $_CONFIG['cache_hits']++;
343 } elseif (($ext_name == "cache") || (GET_EXT_VERSION("cache") == "")) {
344 //* DEBUG: */ echo "DB! ext_name={$ext_name}<br />\n";
345 // Load from database
346 $result = SQL_QUERY_ESC("SELECT ext_active FROM "._MYSQL_PREFIX."_extensions WHERE ext_name='%s' LIMIT 1",
347 array($ext_name), __FILE__, __LINE__);
348 if (SQL_NUMROWS($result) == 0) {
349 // Extension was not found!
352 list($active) = SQL_FETCHROW($result);
353 //* DEBUG: */ echo $ext_name."[DB]: {$active}<br />\n";
354 SQL_FREERESULT($result);
357 $cacheArray['extensions']['ext_active'][$ext_name] = $active;
359 // Extension not active!
360 //* DEBUG: */ echo $ext_name.": Not active!<br />\n";
361 $cacheArray['extensions']['ext_active'][$ext_name] = "N";
365 //DEBUG_LOG(__FUNCTION__.": ext_name={$ext_name},active={$active}");
367 // Is this extension activated? (For admins we always have active extensions...)
368 return ($active == "Y");
370 // Get version from extensions
371 function GET_EXT_VERSION ($ext_name) {
372 global $cacheArray, $_CONFIG, $cacheInstance;
375 // Extensions are all inactive during installation
376 if ((!isBooleanConstantAndTrue('mxchange_installed')) || (isBooleanConstantAndTrue('mxchange_installing'))) return "";
377 //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>): ext_name={$ext_name}<br />\n";
379 // Is the cache written?
380 if (!empty($cacheArray['extensions']['ext_version'][$ext_name])) {
381 // Load data from cache
382 //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>): CACHE!<br />\n";
383 $ret = $cacheArray['extensions']['ext_version'][$ext_name];
386 if (isset($_CONFIG['cache_hits'])) $_CONFIG['cache_hits']++; else $_CONFIG['cache_hits'] = 1;
387 } elseif (!is_object($cacheInstance)) {
388 // Load from database
389 $result = SQL_QUERY_ESC("SELECT ext_version FROM "._MYSQL_PREFIX."_extensions WHERE ext_name='%s' LIMIT 1",
390 array($ext_name), __FILE__, __LINE__);
391 list($ret) = SQL_FETCHROW($result);
392 SQL_FREERESULT($result);
395 $cacheArray['extensions']['ext_version'][$ext_name] = $ret;
399 //* DEBUG: */ print __FUNCTION__."(<font color=\"#0000aa\">".__LINE__."</font>): ret={$ret}<br />\n";
403 function EXTENSION_UPDATE($file, $ext, $EXT_VER, $dry_run=false)
405 // This shall never do a non-admin user!
406 global $cacheInstance, $_CONFIG, $NOTES;
409 $SQLs = array(); $INC_POOL = array();
411 // Only admins are allowed to update extensions
412 if ((!IS_ADMIN()) || (empty($ext))) return false;
414 // Load extension in update mode
415 $EXT_LOAD_MODE = "update"; $EXT_UPDATE_DEPENDS = ""; $NOTES = "";
417 // Load extension file
418 include(sprintf("%sinc/extensions/%s", PATH, $file));
420 if (!empty($EXT_UPDATE_DEPENDS)) {
421 // Update another extension first!
422 $test = EXTENSION_UPDATE(("ext-".$EXT_UPDATE_DEPENDS.".php"), $EXT_UPDATE_DEPENDS, GET_EXT_VERSION($EXT_UPDATE_DEPENDS), $dry_run);
425 // Check if version is updated
426 if ((($EXT_VERSION != $EXT_VER) || ($dry_run)) && (is_array($EXT_VER_HISTORY)))
428 // Search for starting point
429 $start = array_search($EXT_VER, $EXT_VER_HISTORY);
432 // And load SQL queries in order of version history
433 for ($idx = ($start + 1); $idx < sizeof($EXT_VER_HISTORY); $idx++)
435 // Remove old SQLs array to prevent possible bugs
436 if (!$dry_run) { unset($SQLs); $SQLs = array(); }
439 $EXT_VER = $EXT_VER_HISTORY[$idx];
442 include(PATH."inc/extensions/".$file);
445 if ($_CONFIG['verbose_sql'] == "Y")
447 $EXT_VER = $EXT_VER_HISTORY[$idx];
448 if (!empty($UPDATE_NOTES))
450 // Update notes found
451 $NOTES .= "<STRONG>v".$EXT_VER.":</STRONG><br />".$UPDATE_NOTES."<br /><br />\n";
454 elseif ($EXT_VER == "0.0")
457 $NOTES .= "<STRONG>v".$EXT_VER.":</STRONG><br />".INITIAL_RELEASE."<br /><br />\n";
461 $NOTES .= "<STRONG>v".$EXT_VER.":</STRONG><br /><I>".NO_UPDATE_NOTES."</I><br /><br />\n";
465 // In real-mode execute any existing includes
466 if ((!$dry_run) && (count($INC_POOL) > 0)) {
468 foreach ($INC_POOL as $fqfn) {
474 if ((is_array($SQLs)) && (!$dry_run)) {
476 foreach ($SQLs as $sql)
481 // Do we have an "ALTER TABLE" command?
482 if (substr(strtolower($sql), 0, 11) == "alter table") {
483 // Analyse the alteration command
484 SQL_ALTER_TABLE($sql, __FILE__, __LINE__);
486 // Run regular SQL command
487 $result = SQL_QUERY($sql, __FILE__, __LINE__, false);
491 } elseif (GET_EXT_VERSION("sql_patches") == "") {
492 // Remove SQLs if extension is not installed
499 // In normal mode insert task and update extension's version...
500 $ext_subj = "[UPDATE-".$ext."-".$EXT_VERSION.":] ".ADMIN_UPDATE_EXT_SUBJ;
503 CREATE_EXTENSION_UPDATE_TASK(GET_ADMIN_ID(get_session('admin_login')), $ext_subj, addslashes($NOTES));
505 // Update extension's version
506 $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_extensions SET ext_version='%s' WHERE ext_name='%s' LIMIT 1",
507 array($EXT_VERSION, $ext), __FILE__, __LINE__);
510 if (EXT_IS_ACTIVE("cache")) {
511 if ($cacheInstance->cache_file("extensions", true) == true) $cacheInstance->cache_destroy();
512 if ($cacheInstance->cache_file("config", true) == true) $cacheInstance->cache_destroy();
513 if ($cacheInstance->cache_file("mod_reg", true) == true) $cacheInstance->cache_destroy();
519 // In "dry-run" mode return array with SQL commands
525 function EXTENSION_VERBOSE_TABLE($SQLs, $title = ADMIN_SQLS_EXECUTED_ON_REMOVAL, $dashed = "", $switch = false, $WIDTH = "480") {
528 $S = false; $SW = 2; $i = 1;
530 if ((is_array($SQLs)) && (GET_EXT_VERSION("sql_patches") >= "0.0.7") && ($_CONFIG['verbose_sql'] == "Y")) {
531 $OUT = "<DIV align=\"center\">
532 <TABLE border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"".$WIDTH."\" align=\"center\"".$dashed.">
534 <TD colspan=\"2\" align=\"center\" class=\"admin_title bottom2\" height=\"24\">
535 <STRONG>".$title.":</STRONG>
538 foreach ($SQLs as $idx => $sql) {
543 <TD class=\"switch_sw".(3 - $SW)." bottom2\" style=\"padding-left: 2px\" width=\"30\">".$i.".</TD>
544 <TD class=\"switch_sw".(3 - $SW)." bottom2\" style=\"padding-left: 5px; padding-right: 5px\">
548 if ($switch) $SW = 3 - $SW;
554 if ((!$S) && (GET_EXT_VERSION("sql_patches") >= "0.0.7") && ($_CONFIG['verbose_sql'] == "Y")) {
555 // No addional SQL commands to run
557 <TD colspan=\"2\" align=\"center\" class=\"switch_sw2 bottom2\" height=\"24\">
558 <FONT class=\"admin_note\">".ADMIN_NO_ADDIONAL_SQLS."</FONT>
564 // Add missing close-table tag
572 // Get extension name from id
573 function GET_EXT_NAME ($id) {
575 global $cacheArray, $_CONFIG;
576 if (!empty($cacheArray['extensions']['ext_name'][$id])) {
578 $ret = $cacheArray['extensions']['ext_name'][$id];
581 $_CONFIG['cache_hits']++;
582 } elseif (!EXT_IS_ACTIVE("cache")) {
583 // Load from database
584 $result = SQL_QUERY_ESC("SELECT ext_name FROM "._MYSQL_PREFIX."_extensions WHERE id=%s LIMIT 1",
585 array(bigintval($id)), __FILE__, __LINE__);
586 list($ret) = SQL_FETCHROW($result);
587 SQL_FREERESULT($result);
591 // Get extension id from name
592 function GET_EXT_ID($name) {
594 global $cacheArray, $_CONFIG;
595 if (isset($cacheArray['extensions']['ext_id'][$name])) {
597 $ret = $cacheArray['extensions']['ext_id'][$name];
600 $_CONFIG['cache_hits']++;
601 } elseif (!EXT_IS_ACTIVE("cache")) {
602 // Load from database
603 $result = SQL_QUERY_ESC("SELECT id FROM "._MYSQL_PREFIX."_extensions WHERE ext_name='%s' LIMIT 1",
604 array($name), __FILE__, __LINE__);
605 list($ret) = SQL_FETCHROW($result);
606 SQL_FREERESULT($result);
612 // Activate given extension
613 function ACTIVATE_EXTENSION($ext_name) {
614 // Activate the extension
615 $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_extensions SET ext_active='Y' WHERE ext_name='%s' LIMIT 1",
616 array($ext_name), __FILE__, __LINE__);
618 // Extension has been activated?
619 if (SQL_AFFECTEDROWS() == 1) {
620 // Then run all queries
621 EXTENSION_RUN_SQLS(GET_EXT_ID($ext_name), "activate");
624 // Checks wether the extension is older than given
625 function EXT_VERSION_IS_OLDER ($ext_name, $ext_ver) {
626 // Get current extension version
627 $currVersion = GET_EXT_VERSION($ext_name);
629 // Remove all dots from both versions
630 $currVersion = str_replace(".", "", $currVersion);
631 $ext_ver = str_replace(".", "", $ext_ver);
633 // Now compare both and return the result
634 return ($currVersion < $ext_ver);