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;
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(PATH."inc/extensions/ext-%s.php", $ext_name);
57 // Does this extension exists?
58 if (file_exists($file) && is_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;
97 // Load required extension also in update mode
98 $file = sprintf(PATH."inc/extensions/ext-%s.php", $EXT_UPDATE_DEPENDS);
100 // Check for required file
101 if (file_exists($file) && is_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);
131 // Nothing to register / update before...
137 // Required file for update does not exists!
139 // But this is fine for the first time...
142 // Finally restore previous SQLs
143 $SQLs = $SQLs2; unset($SQLs2);
144 $EXT_LANG_PREFIX = $LANG_BCK;
148 // Does not depend on an other extension
152 // Does everthing before wents ok?
155 // "Dry-run-mode" activated?
158 // Are there any SQL commands to run?
159 if (count($SQLs) > 0)
161 foreach ($SQLs as $sql)
167 // Run SQLs without compiling them...
168 $result = SQL_QUERY($sql, __FILE__, __LINE__, false);
173 // Check for added include files
174 if (count($INC_POOL > 0))
176 // Loads every include file
177 foreach ($INC_POOL as $inc)
183 // Register extension
184 $result = SQL_QUERY_ESC("INSERT INTO "._MYSQL_PREFIX."_extensions (ext_name, ext_lang_file, ext_active, ext_version) VALUES ('%s', '%s', 'N', '%s')",
185 array($ext_name, $EXT_LANG_PREFIX, $EXT_VERSION), __FILE__, __LINE__);
187 // Update task management
188 $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_task_system SET status='SOLVED' WHERE id=%d LIMIT 1",
189 array(bigintval($id)), __FILE__, __LINE__);
191 // In normal mode return a true on success
192 $ret = true; unset($SQLs);
196 // Rewrite SQL command to keep { and } inside
197 foreach ($SQLs as $key=>$sql)
199 $sql = str_replace('{', "{", str_replace('}', "}", $sql));
203 // In "dry-run" mode return array with all SQL commands
206 // Remove all SQL commands
212 // No, an error occurs while registering extension :-(
216 elseif (($id > 0) && (!empty($ext_name)))
218 // Remove task from system when id and extension's name is valid
219 $result = SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM "._MYSQL_PREFIX."_task_system WHERE id=%d AND status='NEW' LIMIT 1",
220 array(bigintval($id)), __FILE__, __LINE__);
222 // Return status code
226 function EXTENSION_RUN_SQLS($id, $EXT_LOAD_MODE)
228 global $cacheInstance;
229 // This shall never do a non-admin user!
230 if (!IS_ADMIN()) return false;
232 // Get extension's name
233 $result = SQL_QUERY_ESC("SELECT ext_name FROM "._MYSQL_PREFIX."_extensions WHERE id=%d LIMIT 1",
234 array(bigintval($id)), __FILE__, __LINE__);
235 list($ext_name) = SQL_FETCHROW($result);
236 SQL_FREERESULT($result);
237 if (empty($ext_name)) return false;
239 // Load extension in detected mode
240 $file = sprintf(PATH."inc/extensions/ext-%s.php", $ext_name); $SQLs = array();
241 if (file_exists($file) && is_readable($file)) require($file);
242 if ((is_array($SQLs) && (sizeof($SQLs) > 0)))
244 // Run SQL commands...
245 foreach ($SQLs as $sql) {
246 // Trim spaces away which we don't need
249 // Is there still an SQL query?
252 $result = SQL_QUERY($sql, __FILE__, __LINE__, false);
256 // Remove cache file(s) if extension is active
257 if (EXT_IS_ACTIVE("cache")) {
258 // Remove cache filer
259 if ($cacheInstance->cache_file("extensions", true)) $cacheInstance->cache_destroy();
260 if ($cacheInstance->cache_file("mod_reg", true)) $cacheInstance->cache_destroy();
261 if ($cacheInstance->cache_file("config", true)) $cacheInstance->cache_destroy();
266 function EXT_IS_ACTIVE ($ext_name, $ignore_admin=false, $ignore_cache=false)
268 global $cacheArray, $_CONFIG;
270 // Extensions are all inactive during installation
271 if ((!isBooleanConstantAndTrue('mxchange_installed')) || (isBooleanConstantAndTrue('mxchange_installing')) || (empty($ext_name))) return false;
273 // Extension's file name will also be checked
274 $file = PATH."inc/extensions/ext-".$ext_name.".php";
275 if ((!file_exists($file)) && (!is_readable($file))) return false;
276 //* DEBUG: */ echo "*".$ext_name."*<br />";
278 // Failed is the default
280 if ((!empty($cacheArray['extensions']['ext_active'][$ext_name])) && (!$ignore_cache))
283 $active = $cacheArray['extensions']['ext_active'][$ext_name];
286 if (isset($_CONFIG['cache_hits'])) $_CONFIG['cache_hits']++;
290 // Load from database
291 $result = SQL_QUERY_ESC("SELECT ext_active FROM "._MYSQL_PREFIX."_extensions WHERE ext_name='%s' LIMIT 1",
292 array($ext_name), __FILE__, __LINE__);
293 if (SQL_NUMROWS($result) == 0)
295 // Extension was not found!
298 list($active) = SQL_FETCHROW($result);
299 SQL_FREERESULT($result);
302 $cacheArray['extensions']['ext_active'][$ext_name] = $active;
305 // Is this extension activated? (For admins we always have active extensions...)
306 $inc = sprintf(PATH."inc/extensions/ext-%s.php", $ext_name);
310 ($active == "Y") || (
322 // Get version from extensions
323 function GET_EXT_VERSION ($ext_name) {
324 global $cacheArray, $_CONFIG, $cacheInstance;
327 // Extensions are all inactive during installation
328 if ((!isBooleanConstantAndTrue('mxchange_installed')) || (isBooleanConstantAndTrue('mxchange_installing'))) return "";
330 // Is the cache written?
331 if (!empty($cacheArray['extensions']['ext_version'][$ext_name])) {
332 // Load data from cache
333 $ret = $cacheArray['extensions']['ext_version'][$ext_name];
336 if (isset($_CONFIG['cache_hits'])) $_CONFIG['cache_hits']++; else $_CONFIG['cache_hits'] = 1;
337 } elseif (!is_object($cacheInstance)) {
338 // Load from database
339 $result = SQL_QUERY_ESC("SELECT ext_version FROM "._MYSQL_PREFIX."_extensions WHERE ext_name='%s' LIMIT 1",
340 array($ext_name), __FILE__, __LINE__);
341 list($ret) = SQL_FETCHROW($result);
342 SQL_FREERESULT($result);
345 $cacheArray['extensions']['ext_version'][$ext_name] = $ret;
350 function EXTENSION_UPDATE($file, $ext, $EXT_VER, $dry_run=false)
352 // This shall never do a non-admin user!
353 global $cacheInstance, $_CONFIG, $NOTES; $SQLs = array();
354 if ((!IS_ADMIN()) || (empty($ext))) return false;
356 // Load extension in update mode
357 $EXT_LOAD_MODE = "update"; $EXT_UPDATE_DEPENDS = ""; $NOTES = "";
358 include(PATH."inc/extensions/".$file);
359 if (!empty($EXT_UPDATE_DEPENDS))
361 // Update another extension first!
362 $test = EXTENSION_UPDATE(("ext-".$EXT_UPDATE_DEPENDS.".php"), $EXT_UPDATE_DEPENDS, GET_EXT_VERSION($EXT_UPDATE_DEPENDS), $dry_run);
365 // Check if version is updated
366 if ((($EXT_VERSION != $EXT_VER) || ($dry_run)) && (is_array($EXT_VER_HISTORY)))
368 // Search for starting point
369 $start = array_search($EXT_VER, $EXT_VER_HISTORY);
372 // And load SQL queries in order of version history
373 for ($idx = ($start + 1); $idx < sizeof($EXT_VER_HISTORY); $idx++)
375 // Remove old SQLs array to prevent possible bugs
376 if (!$dry_run) { unset($SQLs); $SQLs = array(); }
379 $EXT_VER = $EXT_VER_HISTORY[$idx];
382 include(PATH."inc/extensions/".$file);
385 if ($_CONFIG['verbose_sql'] == "Y")
387 $EXT_VER = $EXT_VER_HISTORY[$idx];
388 if (!empty($UPDATE_NOTES))
390 // Update notes found
391 $NOTES .= "<STRONG>v".$EXT_VER.":</STRONG><br />".$UPDATE_NOTES."<br /><br />\n";
394 elseif ($EXT_VER == "0.0")
397 $NOTES .= "<STRONG>v".$EXT_VER.":</STRONG><br />".INITIAL_RELEASE."<br /><br />\n";
401 $NOTES .= "<STRONG>v".$EXT_VER.":</STRONG><br /><I>".NO_UPDATE_NOTES."</I><br /><br />\n";
406 if ((is_array($SQLs)) && (!$dry_run))
409 foreach ($SQLs as $sql)
414 // Run SQL command...
415 $result = SQL_QUERY($sql, __FILE__, __LINE__, false);
418 } elseif (GET_EXT_VERSION("sql_patches") == "") {
419 // Remove SQLs if extension is not installed
426 // In normal mode insert task and update extension's version...
427 $ext_subj = "[UPDATE-".$ext."-".$EXT_VERSION.":] ".ADMIN_UPDATE_EXT_SUBJ;
430 $result = SQL_QUERY_ESC("SELECT id FROM "._MYSQL_PREFIX."_task_system WHERE subject='%s' LIMIT 1",
431 array($ext_subj), __FILE__, __LINE__);
432 if (SQL_NUMROWS($result) == 0)
434 // Task not created so it's a brand-new extension which we need to register and create a task for!
435 $result = SQL_QUERY_ESC("INSERT INTO "._MYSQL_PREFIX."_task_system (assigned_admin, userid, status, task_type, subject, text, task_created) VALUES ('%s', '0', 'NEW', 'EXTENSION_UPDATE', '%s', '%s', UNIX_TIMESTAMP())",
436 array(GET_ADMIN_ID(SQL_ESCAPE(get_session('admin_login'))), $ext_subj, addslashes($NOTES)), __FILE__, __LINE__);
440 SQL_FREERESULT($result);
442 // Update extension's version
443 $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_extensions SET ext_version='%s' WHERE ext_name='%s' LIMIT 1",
444 array($EXT_VERSION, $ext), __FILE__, __LINE__);
447 if (EXT_IS_ACTIVE("cache"))
449 if ($cacheInstance->cache_file("extensions", true) == true) $cacheInstance->cache_destroy();
450 if ($cacheInstance->cache_file("config", true) == true) $cacheInstance->cache_destroy();
451 if ($cacheInstance->cache_file("mod_reg", true) == true) $cacheInstance->cache_destroy();
459 // In "dry-run" mode return array with SQL commands
465 function EXTENSION_VERBOSE_TABLE($SQLs, $title=ADMIN_SQLS_EXECUTED_ON_REMOVAL, $dashed="", $switch=false, $WIDTH="480")
469 $S = false; $SW = 2; $i = 1;
471 if ((is_array($SQLs)) && (GET_EXT_VERSION("sql_patches")) && ($_CONFIG['verbose_sql'] == "Y"))
473 $OUT = "<DIV align=\"center\">
474 <TABLE border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"".$WIDTH."\" align=\"center\"".$dashed.">
476 <TD colspan=\"2\" align=\"center\" class=\"admin_title bottom2\" height=\"24\">
477 <STRONG>".$title.":</STRONG>
480 foreach ($SQLs as $idx=>$sql)
487 <TD class=\"switch_sw".(3 - $SW)." bottom2\" style=\"padding-left: 2px\" width=\"30\">".$i.".</TD>
488 <TD class=\"switch_sw".(3 - $SW)." bottom2\" style=\"padding-left: 5px; padding-right: 5px\">
492 if ($switch) $SW = 3 - $SW;
498 if ((!$S) && (GET_EXT_VERSION("sql_patches")) && ($_CONFIG['verbose_sql'] == "Y"))
500 // No addional SQL commands to run
502 <TD colspan=\"2\" align=\"center\" class=\"switch_sw2 bottom2\" height=\"24\">
503 <FONT class=\"admin_note\">".ADMIN_NO_ADDIONAL_SQLS."</FONT>
509 // Add missing close-table tag
516 function GET_EXT_NAME($id)
519 global $cacheArray, $_CONFIG;
520 if (!empty($cacheArray['extensions']['ext_id'][$id]))
523 $ret = $cacheArray['extensions']['ext_id'][$id];
526 $_CONFIG['cache_hits']++;
530 // Load from database
531 $result = SQL_QUERY_ESC("SELECT ext_name FROM "._MYSQL_PREFIX."_extensions WHERE id=%d LIMIT 1",
532 array(bigintval($id)), __FILE__, __LINE__);
533 list($ret) = SQL_FETCHROW($result);
534 SQL_FREERESULT($result);
539 function GET_EXT_ID($name)
542 global $cacheArray, $_CONFIG;
543 if ((isset($cacheArray['extensions']['ext_id'])) && (is_array($cacheArray['extensions']['ext_id'])))
546 $ret = array_search($name, $cacheArray['extensions']['ext_id']);
549 $_CONFIG['cache_hits']++;
553 // Load from database
554 $result = SQL_QUERY_ESC("SELECT id FROM "._MYSQL_PREFIX."_extensions WHERE ext_name='%s' LIMIT 1",
555 array($name), __FILE__, __LINE__);
556 list($ret) = SQL_FETCHROW($result);
557 SQL_FREERESULT($result);