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 (ereg(basename(__FILE__), $_SERVER['PHP_SELF']))
37 $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))
60 // Extension was found so we can load it in registration mode
63 // And run possible updates
64 $EXT_LOAD_MODE = "update"; $EXT_UPDATE_DEPENDS = "";
65 foreach ($EXT_VER_HISTORY as $EXT_VER)
68 if (((GET_EXT_VERSION("sql_patches") != '') && ($_CONFIG['verbose_sql'] == "Y")) || (!EXT_IS_ACTIVE("sql_patches")))
70 if (!empty($UPDATE_NOTES))
73 $NOTES .= "<STRONG>v".$EXT_VER.":</STRONG><br />".$UPDATE_NOTES."<br /><br />\n";
76 elseif (($EXT_VER == "0.0") || ($EXT_VER == "0.0.0"))
79 $NOTES .= "<STRONG>v".$EXT_VER.":</STRONG><br />".INITIAL_RELEASE."<br /><br />\n";
83 $NOTES .= "<STRONG>v".$EXT_VER.":</STRONG><br /><I>".NO_UPDATE_NOTES."</I><br /><br />\n";
88 // Does this extension depends on an outstanding update of another update?
89 if (!empty($EXT_UPDATE_DEPENDS))
91 // Backup SQL commands and clear current
92 $SQLs2 = $SQLs; $SQLs = array(); $test = false;
94 // Backup language as well
95 $LANG_BCK = $EXT_LANG_PREFIX; $EXT_ALWAYS_ACTIVE = "N";
97 // Load required extension also in update mode
98 $file = sprintf("%sinc/extensions/ext-%s.php", PATH, $EXT_UPDATE_DEPENDS);
100 // Check for required file
101 if (FILE_READABLE($file))
103 // File exists so let's load it
104 $VER_BACKUP = $EXT_VERSION;
106 $EXT_VERSION = $VER_BACKUP;
108 // If versions mismatch update extension first
109 $ext_ver = GET_EXT_VERSION($EXT_UPDATE_DEPENDS);
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__);
115 if (SQL_NUMROWS($result) == 1)
117 // Task found so load task's ID and register extension...
118 list($task) = SQL_FETCHROW($result);
119 SQL_FREERESULT($result);
120 $test = EXTENSION_REGISTER($EXT_UPDATE_DEPENDS, $task, $dry_run);
123 elseif ($ext_ver != $EXT_VERSION)
125 // Ok, update this extension now
126 EXTENSION_UPDATE(basename($file), $EXT_UPDATE_DEPENDS, $ext_ver, $dry_run);
133 // Nothing to register / update before...
139 // Required file for update does not exists!
141 // But this is fine for the first time...
144 // Finally restore previous SQLs
145 $SQLs = $SQLs2; unset($SQLs2);
146 $EXT_LANG_PREFIX = $LANG_BCK;
150 // Does not depend on an other extension
154 // Switch back to register mode
155 $EXT_LOAD_MODE = "register";
157 // Does everthing before wents ok?
159 // "Dry-run-mode" activated?
162 foreach ($SQLs as $sql) {
163 // Trim spaces away which we don't need
166 // Is there still an SQL query?
168 // Do we have an "ALTER TABLE" command?
169 if (substr(strtolower($sql), 0, 11) == "alter table") {
170 // Analyse the alteration command
171 SQL_ALTER_TABLE($sql, __FILE__, __LINE__);
173 // Run regular SQL command
174 $result = SQL_QUERY($sql, __FILE__, __LINE__, false);
179 // Remove cache file(s) if extension is active
180 if ((EXT_IS_ACTIVE("cache")) || (GET_EXT_VERSION("cache") != "")) {
181 //* DEBUG: */ echo __LINE__.": DESTROY!<br />\n";
182 // Remove cache files
183 if ($cacheInstance->cache_file("extensions", true)) $cacheInstance->cache_destroy();
184 if ($cacheInstance->cache_file("mod_reg", true)) $cacheInstance->cache_destroy();
185 if ($cacheInstance->cache_file("config", true)) $cacheInstance->cache_destroy();
188 // Check for added include files
189 if (count($INC_POOL > 0)) {
190 // Loads every include file
191 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 // In normal mode return a true on success
204 $ret = true; unset($SQLs);
206 // Rewrite SQL command to keep { and } inside
207 foreach ($SQLs as $key => $sql) {
208 $sql = str_replace('{', "{", str_replace('}', "}", $sql));
212 // In "dry-run" mode return array with all SQL commands
215 // Remove all SQL commands
219 // No, an error occurs while registering extension :-(
222 } elseif (($id > 0) && (!empty($ext_name))) {
223 // Remove task from system when id and extension's name is valid
224 $result = SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM "._MYSQL_PREFIX."_task_system WHERE id=%s AND status='NEW' LIMIT 1",
225 array(bigintval($id)), __FILE__, __LINE__);
228 // Is this the sql_patches?
229 //* DEBUG: */ echo __LINE__.":{$ext_name}/{$EXT_LOAD_MODE}<br />\n";
230 if (($ext_name == "sql_patches") && (($EXT_LOAD_MODE == "register") || ($EXT_LOAD_MODE == "remove")) && (!$dry_run) && ($test)) {
231 // Then redirect to logout
232 //* DEBUG: */ echo __LINE__.": LOAD!<br />\n";
233 LOAD_URL("modules.php?module=admin&logout=1&".$EXT_LOAD_MODE."=sql_patches");
236 // Return status code
240 function EXTENSION_RUN_SQLS($id, $EXT_LOAD_MODE) {
241 global $cacheInstance;
244 // By default no SQL has been executed
247 // This shall never do a non-admin user!
248 if (!IS_ADMIN()) return false;
250 // Get extension's name
251 $ext_name = GET_EXT_NAME($id);
252 if (empty($ext_name)) return false;
254 // Load extension in detected mode
255 //* DEBUG: */ echo __FUNCTION__.":ext_name[{$id}]={$ext_name}<br />\n";
256 $file = sprintf("%sinc/extensions/ext-%s.php", PATH, $ext_name);
257 if (FILE_READABLE($file)) {
262 //* DEBUG: */ echo __FUNCTION__.":SQLs::count=".count($SQLs)."<br />\n";
263 if ((is_array($SQLs) && (sizeof($SQLs) > 0))) {
264 // Run SQL commands...
265 foreach ($SQLs as $sql) {
266 // Trim spaces away which we don't need
269 // Is there still an SQL query?
271 // Do we have an "ALTER TABLE" command?
272 //* DEBUG: */ echo __FUNCTION__.":SQL={$SQL}<br />\n";
273 if (substr(strtolower($sql), 0, 11) == "alter table") {
274 // Analyse the alteration command
275 SQL_ALTER_TABLE($sql, __FILE__, __LINE__);
277 // Run regular SQL command
278 $result = SQL_QUERY($sql, __FILE__, __LINE__, false);
281 // An SQL has been executed
287 if ($EXT_LOAD_MODE == "remove") {
288 // Delete this extension (remember to remove it from your server *before* you click on welcome!
289 $result = SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM "._MYSQL_PREFIX."_extensions WHERE id=%s LIMIT 1",
290 array($id), __FILE__, __LINE__);
293 //* DEBUG: */ echo __FUNCTION__.":mode={$EXT_LOAD_MODE}<br />\n";
295 // Remove cache file(s) if extension is active
296 if (((EXT_IS_ACTIVE("cache")) || (GET_EXT_VERSION("cache") != "")) && (((SQL_AFFECTEDROWS() == 1) && ($EXT_LOAD_MODE == "remove")) || ($sqlRan === true))) {
297 //* DEBUG: */ echo __LINE__.": DESTROY!<br />\n";
298 // Remove cache files
299 if ($cacheInstance->cache_file("extensions", true)) $cacheInstance->cache_destroy();
300 if ($cacheInstance->cache_file("mod_reg", true)) $cacheInstance->cache_destroy();
301 if ($cacheInstance->cache_file("config", true)) $cacheInstance->cache_destroy();
304 // Is this the sql_patches?
305 //* DEBUG: */ echo __LINE__.": {$id}/{$ext_name}/{$EXT_LOAD_MODE}<br />\n";
306 if (($ext_name == "sql_patches") && (($EXT_LOAD_MODE == "register") || ($EXT_LOAD_MODE == "remove"))) {
307 // Then redirect to logout
308 //* DEBUG: */ echo __LINE__.": LOAD!<br />\n";
309 LOAD_URL("modules.php?module=admin&logout=1&".$EXT_LOAD_MODE."=sql_patches");
313 // Check if given extension is active
314 function EXT_IS_ACTIVE ($ext_name, $ignore_admin = false, $ignore_cache = false) {
315 global $cacheArray, $_CONFIG;
317 // Extensions are all inactive during installation
318 if ((!isBooleanConstantAndTrue('mxchange_installed')) || (isBooleanConstantAndTrue('mxchange_installing')) || (empty($ext_name))) return false;
320 // Extension's file name will also be checked
321 $file = sprintf("%sinc/extensions/ext-%s.php", PATH, $ext_name);
322 if (!FILE_READABLE($file)) return false;
323 //* DEBUG: */ echo "*".$ext_name."(".count($cacheArray).")<br />";
325 // Not active is the default
329 if ((!empty($cacheArray['extensions']['ext_active'][$ext_name])) && (!$ignore_cache)) {
331 //* DEBUG: */ echo "CACHE!<br />\n";
332 $active = $cacheArray['extensions']['ext_active'][$ext_name];
335 if (isset($_CONFIG['cache_hits'])) $_CONFIG['cache_hits']++;
336 } elseif (($ext_name == "cache") || (GET_EXT_VERSION("cache") == "")) {
337 //* DEBUG: */ echo "DB!<br />\n";
338 // Load from database
339 $result = SQL_QUERY_ESC("SELECT ext_active FROM "._MYSQL_PREFIX."_extensions WHERE ext_name='%s' LIMIT 1",
340 array($ext_name), __FILE__, __LINE__);
341 if (SQL_NUMROWS($result) == 0) {
342 // Extension was not found!
345 list($active) = SQL_FETCHROW($result);
346 //* DEBUG: */ echo $ext_name."[DB]: {$active}<br />\n";
347 SQL_FREERESULT($result);
350 $cacheArray['extensions']['ext_active'][$ext_name] = $active;
352 // Extension not active!
353 //* DEBUG: */ echo $ext_name.": Not active!<br />\n";
354 $cacheArray['extensions']['ext_active'][$ext_name] = "N";
357 // Create FQFN for extension file
358 $inc = sprintf("%sinc/extensions/ext-%s.php", PATH, $ext_name);
361 //DEBUG_LOG(__FUNCTION__.": ext_name={$ext_name},active={$active}");
363 // Is this extension activated? (For admins we always have active extensions...)
372 // Get version from extensions
373 function GET_EXT_VERSION ($ext_name) {
374 global $cacheArray, $_CONFIG, $cacheInstance;
377 // Extensions are all inactive during installation
378 if ((!isBooleanConstantAndTrue('mxchange_installed')) || (isBooleanConstantAndTrue('mxchange_installing'))) return "";
379 //* DEBUG: */ echo __FUNCTION__.": ext_name={$ext_name}<br />\n";
381 // Is the cache written?
382 if (!empty($cacheArray['extensions']['ext_version'][$ext_name])) {
383 // Load data from cache
384 //* DEBUG: */ echo __FUNCTION__.": CACHE!<br />\n";
385 $ret = $cacheArray['extensions']['ext_version'][$ext_name];
388 if (isset($_CONFIG['cache_hits'])) $_CONFIG['cache_hits']++; else $_CONFIG['cache_hits'] = 1;
389 } elseif (!is_object($cacheInstance)) {
390 // Load from database
391 $result = SQL_QUERY_ESC("SELECT ext_version FROM "._MYSQL_PREFIX."_extensions WHERE ext_name='%s' LIMIT 1",
392 array($ext_name), __FILE__, __LINE__);
393 list($ret) = SQL_FETCHROW($result);
394 SQL_FREERESULT($result);
397 $cacheArray['extensions']['ext_version'][$ext_name] = $ret;
401 //* DEBUG: */ echo __FUNCTION__.": ret={$ret}<br />\n";
405 function EXTENSION_UPDATE($file, $ext, $EXT_VER, $dry_run=false)
407 // This shall never do a non-admin user!
408 global $cacheInstance, $_CONFIG, $NOTES; $SQLs = array();
409 if ((!IS_ADMIN()) || (empty($ext))) return false;
411 // Load extension in update mode
412 $EXT_LOAD_MODE = "update"; $EXT_UPDATE_DEPENDS = ""; $NOTES = "";
413 include(PATH."inc/extensions/".$file);
414 if (!empty($EXT_UPDATE_DEPENDS))
416 // Update another extension first!
417 $test = EXTENSION_UPDATE(("ext-".$EXT_UPDATE_DEPENDS.".php"), $EXT_UPDATE_DEPENDS, GET_EXT_VERSION($EXT_UPDATE_DEPENDS), $dry_run);
420 // Check if version is updated
421 if ((($EXT_VERSION != $EXT_VER) || ($dry_run)) && (is_array($EXT_VER_HISTORY)))
423 // Search for starting point
424 $start = array_search($EXT_VER, $EXT_VER_HISTORY);
427 // And load SQL queries in order of version history
428 for ($idx = ($start + 1); $idx < sizeof($EXT_VER_HISTORY); $idx++)
430 // Remove old SQLs array to prevent possible bugs
431 if (!$dry_run) { unset($SQLs); $SQLs = array(); }
434 $EXT_VER = $EXT_VER_HISTORY[$idx];
437 include(PATH."inc/extensions/".$file);
440 if ($_CONFIG['verbose_sql'] == "Y")
442 $EXT_VER = $EXT_VER_HISTORY[$idx];
443 if (!empty($UPDATE_NOTES))
445 // Update notes found
446 $NOTES .= "<STRONG>v".$EXT_VER.":</STRONG><br />".$UPDATE_NOTES."<br /><br />\n";
449 elseif ($EXT_VER == "0.0")
452 $NOTES .= "<STRONG>v".$EXT_VER.":</STRONG><br />".INITIAL_RELEASE."<br /><br />\n";
456 $NOTES .= "<STRONG>v".$EXT_VER.":</STRONG><br /><I>".NO_UPDATE_NOTES."</I><br /><br />\n";
461 if ((is_array($SQLs)) && (!$dry_run))
464 foreach ($SQLs as $sql)
469 // Do we have an "ALTER TABLE" command?
470 if (substr(strtolower($sql), 0, 11) == "alter table") {
471 // Analyse the alteration command
472 SQL_ALTER_TABLE($sql, __FILE__, __LINE__);
474 // Run regular SQL command
475 $result = SQL_QUERY($sql, __FILE__, __LINE__, false);
479 } elseif (GET_EXT_VERSION("sql_patches") == "") {
480 // Remove SQLs if extension is not installed
487 // In normal mode insert task and update extension's version...
488 $ext_subj = "[UPDATE-".$ext."-".$EXT_VERSION.":] ".ADMIN_UPDATE_EXT_SUBJ;
491 CREATE_EXTENSION_UPDATE_TASK(GET_ADMIN_ID(get_session('admin_login')), $ext_subj, addslashes($NOTES));
493 // Update extension's version
494 $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_extensions SET ext_version='%s' WHERE ext_name='%s' LIMIT 1",
495 array($EXT_VERSION, $ext), __FILE__, __LINE__);
498 if (EXT_IS_ACTIVE("cache")) {
499 if ($cacheInstance->cache_file("extensions", true) == true) $cacheInstance->cache_destroy();
500 if ($cacheInstance->cache_file("config", true) == true) $cacheInstance->cache_destroy();
501 if ($cacheInstance->cache_file("mod_reg", true) == true) $cacheInstance->cache_destroy();
507 // In "dry-run" mode return array with SQL commands
513 function EXTENSION_VERBOSE_TABLE($SQLs, $title = ADMIN_SQLS_EXECUTED_ON_REMOVAL, $dashed = "", $switch = false, $WIDTH = "480") {
516 $S = false; $SW = 2; $i = 1;
518 if ((is_array($SQLs)) && (GET_EXT_VERSION("sql_patches")) && ($_CONFIG['verbose_sql'] == "Y")) {
519 $OUT = "<DIV align=\"center\">
520 <TABLE border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"".$WIDTH."\" align=\"center\"".$dashed.">
522 <TD colspan=\"2\" align=\"center\" class=\"admin_title bottom2\" height=\"24\">
523 <STRONG>".$title.":</STRONG>
526 foreach ($SQLs as $idx => $sql) {
531 <TD class=\"switch_sw".(3 - $SW)." bottom2\" style=\"padding-left: 2px\" width=\"30\">".$i.".</TD>
532 <TD class=\"switch_sw".(3 - $SW)." bottom2\" style=\"padding-left: 5px; padding-right: 5px\">
536 if ($switch) $SW = 3 - $SW;
542 if ((!$S) && (GET_EXT_VERSION("sql_patches")) && ($_CONFIG['verbose_sql'] == "Y")) {
543 // No addional SQL commands to run
545 <TD colspan=\"2\" align=\"center\" class=\"switch_sw2 bottom2\" height=\"24\">
546 <FONT class=\"admin_note\">".ADMIN_NO_ADDIONAL_SQLS."</FONT>
552 // Add missing close-table tag
560 // Get extension name from id
561 function GET_EXT_NAME ($id) {
563 global $cacheArray, $_CONFIG;
564 if (!empty($cacheArray['extensions']['ext_name'][$id])) {
566 $ret = $cacheArray['extensions']['ext_name'][$id];
569 $_CONFIG['cache_hits']++;
570 } elseif (!EXT_IS_ACTIVE("cache")) {
571 // Load from database
572 $result = SQL_QUERY_ESC("SELECT ext_name FROM "._MYSQL_PREFIX."_extensions WHERE id=%s LIMIT 1",
573 array(bigintval($id)), __FILE__, __LINE__);
574 list($ret) = SQL_FETCHROW($result);
575 SQL_FREERESULT($result);
579 // Get extension id from name
580 function GET_EXT_ID($name) {
582 global $cacheArray, $_CONFIG;
583 if (isset($cacheArray['extensions']['ext_id'][$name])) {
585 $ret = $cacheArray['extensions']['ext_id'][$name];
588 $_CONFIG['cache_hits']++;
589 } elseif (!EXT_IS_ACTIVE("cache")) {
590 // Load from database
591 $result = SQL_QUERY_ESC("SELECT id FROM "._MYSQL_PREFIX."_extensions WHERE ext_name='%s' LIMIT 1",
592 array($name), __FILE__, __LINE__);
593 list($ret) = SQL_FETCHROW($result);
594 SQL_FREERESULT($result);