2007-07-02 05:51:26 +00:00
|
|
|
<?php
|
2008-07-31 14:28:26 +00:00
|
|
|
function tar_file($filename, $contents) {
|
|
|
|
$return = pack("a100a8a8a8a12a12", $filename, 644, 0, 0, decoct(strlen($contents)), decoct(time()));
|
|
|
|
$checksum = 8*32; // space for checksum itself
|
|
|
|
for ($i=0; $i < strlen($return); $i++) {
|
|
|
|
$checksum += ord($return{$i});
|
|
|
|
}
|
|
|
|
$return .= sprintf("%06o", $checksum) . "\0 ";
|
2008-08-16 12:28:31 +00:00
|
|
|
return $return . str_repeat("\0", 512 - strlen($return)) . $contents . str_repeat("\0", 511 - (strlen($contents) + 511) % 512);
|
2008-07-31 14:28:26 +00:00
|
|
|
}
|
|
|
|
|
2009-06-15 21:47:29 +00:00
|
|
|
function dump_triggers($table, $style) {
|
|
|
|
global $dbh;
|
|
|
|
if ($_POST["format"] != "csv" && $style && $dbh->server_info >= 5) {
|
2009-06-22 00:53:51 +00:00
|
|
|
$result = $dbh->query("SHOW TRIGGERS LIKE " . $dbh->quote(addcslashes($table, "%_")));
|
2009-06-15 21:47:29 +00:00
|
|
|
if ($result->num_rows) {
|
|
|
|
echo "\nDELIMITER ;;\n";
|
|
|
|
while ($row = $result->fetch_assoc()) {
|
2009-06-16 05:49:10 +00:00
|
|
|
echo "\n" . ($style == 'CREATE+ALTER' ? "DROP TRIGGER IF EXISTS " . idf_escape($row["Trigger"]) . ";;\n" : "")
|
|
|
|
. "CREATE TRIGGER " . idf_escape($row["Trigger"]) . " $row[Timing] $row[Event] ON " . idf_escape($row["Table"]) . " FOR EACH ROW\n$row[Statement];;\n";
|
2009-06-15 21:47:29 +00:00
|
|
|
}
|
|
|
|
echo "\nDELIMITER ;\n";
|
|
|
|
}
|
|
|
|
$result->free();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-06-20 14:13:37 +00:00
|
|
|
if ($_POST) {
|
2009-06-01 12:53:19 +00:00
|
|
|
$ext = dump_headers((strlen($_GET["dump"]) ? $_GET["dump"] : $_GET["db"]), (!strlen($_GET["db"]) || count((array) $_POST["tables"] + (array) $_POST["data"]) > 1));
|
2008-08-16 12:28:31 +00:00
|
|
|
if ($_POST["format"] != "csv") {
|
|
|
|
echo "SET NAMES utf8;\n";
|
|
|
|
echo "SET foreign_key_checks = 0;\n";
|
2009-06-22 00:53:51 +00:00
|
|
|
echo "SET time_zone = " . $dbh->quote($dbh->result($dbh->query("SELECT @@time_zone"))) . ";\n";
|
2008-08-16 12:28:31 +00:00
|
|
|
echo "\n";
|
|
|
|
}
|
2008-06-20 14:13:37 +00:00
|
|
|
|
2009-06-01 12:53:19 +00:00
|
|
|
$style = $_POST["db_style"];
|
|
|
|
foreach ((strlen($_GET["db"]) ? array($_GET["db"]) : (array) $_POST["databases"]) as $db) {
|
2009-05-08 05:23:03 +00:00
|
|
|
if ($dbh->select_db($db)) {
|
|
|
|
if ($_POST["format"] != "csv" && ereg('CREATE', $style) && ($result = $dbh->query("SHOW CREATE DATABASE " . idf_escape($db)))) {
|
2009-06-01 12:53:19 +00:00
|
|
|
if ($style == "DROP+CREATE") {
|
2008-08-19 09:19:18 +00:00
|
|
|
echo "DROP DATABASE IF EXISTS " . idf_escape($db) . ";\n";
|
2008-08-18 15:18:16 +00:00
|
|
|
}
|
2009-05-08 05:23:03 +00:00
|
|
|
$create = $dbh->result($result, 1);
|
2009-06-01 12:53:19 +00:00
|
|
|
echo ($style == "CREATE+ALTER" ? preg_replace('~^CREATE DATABASE ~', '\\0IF NOT EXISTS ', $create) : $create) . ";\n";
|
2008-08-19 09:19:18 +00:00
|
|
|
$result->free();
|
|
|
|
}
|
|
|
|
if ($style && $_POST["format"] != "csv") {
|
2008-08-26 10:29:23 +00:00
|
|
|
echo "USE " . idf_escape($db) . ";\n\n";
|
2008-09-01 16:49:38 +00:00
|
|
|
$out = "";
|
2009-05-08 05:23:03 +00:00
|
|
|
if ($dbh->server_info >= 5) {
|
2008-08-19 09:19:18 +00:00
|
|
|
foreach (array("FUNCTION", "PROCEDURE") as $routine) {
|
2009-06-22 00:53:51 +00:00
|
|
|
$result = $dbh->query("SHOW $routine STATUS WHERE Db = " . $dbh->quote($db));
|
2008-08-19 09:19:18 +00:00
|
|
|
while ($row = $result->fetch_assoc()) {
|
2009-06-16 05:49:10 +00:00
|
|
|
$out .= ($style != 'DROP+CREATE' ? "DROP $routine IF EXISTS " . idf_escape($row["Name"]) . ";;\n" : "")
|
|
|
|
. $dbh->result($dbh->query("SHOW CREATE $routine " . idf_escape($row["Name"])), 2) . ";;\n\n";
|
2008-08-19 09:19:18 +00:00
|
|
|
}
|
|
|
|
$result->free();
|
|
|
|
}
|
|
|
|
}
|
2009-05-08 05:23:03 +00:00
|
|
|
if ($dbh->server_info >= 5.1) {
|
|
|
|
$result = $dbh->query("SHOW EVENTS");
|
2008-09-01 16:49:38 +00:00
|
|
|
while ($row = $result->fetch_assoc()) {
|
2009-06-16 05:49:10 +00:00
|
|
|
$out .= ($style != 'DROP+CREATE' ? "DROP EVENT IF EXISTS " . idf_escape($row["Name"]) . ";;\n" : "")
|
|
|
|
. $dbh->result($dbh->query("SHOW CREATE EVENT " . idf_escape($row["Name"])), 3) . ";;\n\n";
|
2008-09-01 16:49:38 +00:00
|
|
|
}
|
|
|
|
$result->free();
|
|
|
|
}
|
|
|
|
echo ($out ? "DELIMITER ;;\n\n$out" . "DELIMITER ;\n\n" : "");
|
2008-08-19 09:19:18 +00:00
|
|
|
}
|
|
|
|
|
2009-06-01 12:53:19 +00:00
|
|
|
if ($_POST["table_style"] || $_POST["data_style"]) {
|
2008-08-19 09:19:18 +00:00
|
|
|
$views = array();
|
2009-07-01 19:58:22 +00:00
|
|
|
foreach (table_status() as $row) {
|
2009-06-01 12:53:19 +00:00
|
|
|
$table = (!strlen($_GET["db"]) || in_array($row["Name"], (array) $_POST["tables"]));
|
|
|
|
$data = (!strlen($_GET["db"]) || in_array($row["Name"], (array) $_POST["data"]));
|
|
|
|
if ($table || $data) {
|
2008-08-19 09:19:18 +00:00
|
|
|
if (isset($row["Engine"])) {
|
|
|
|
if ($ext == "tar") {
|
|
|
|
ob_start();
|
|
|
|
}
|
2009-06-01 12:53:19 +00:00
|
|
|
dump_table($row["Name"], ($table ? $_POST["table_style"] : ""));
|
|
|
|
if ($data) {
|
|
|
|
dump_data($row["Name"], $_POST["data_style"]);
|
|
|
|
}
|
2009-06-15 21:47:29 +00:00
|
|
|
if ($table) {
|
|
|
|
dump_triggers($row["Name"], $_POST["table_style"]);
|
|
|
|
}
|
2008-08-19 09:19:18 +00:00
|
|
|
if ($ext == "tar") {
|
|
|
|
echo tar_file((strlen($_GET["db"]) ? "" : "$db/") . "$row[Name].csv", ob_get_clean());
|
2008-08-26 10:29:23 +00:00
|
|
|
} elseif ($_POST["format"] != "csv") {
|
|
|
|
echo "\n";
|
2008-08-19 09:19:18 +00:00
|
|
|
}
|
|
|
|
} elseif ($_POST["format"] != "csv") {
|
2009-06-01 12:53:19 +00:00
|
|
|
$views[] = $row["Name"];
|
2008-08-19 09:19:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-06-01 12:53:19 +00:00
|
|
|
foreach ($views as $view) {
|
|
|
|
dump_table($view, $_POST["table_style"], true);
|
2008-08-18 15:18:16 +00:00
|
|
|
}
|
2008-08-16 12:28:31 +00:00
|
|
|
}
|
2008-08-19 10:32:34 +00:00
|
|
|
|
2009-06-01 12:53:19 +00:00
|
|
|
if ($style == "CREATE+ALTER" && $_POST["format"] != "csv") {
|
2009-06-21 23:20:32 +00:00
|
|
|
// drop old tables
|
2008-08-19 10:32:34 +00:00
|
|
|
$query = "SELECT TABLE_NAME, ENGINE, TABLE_COLLATION, TABLE_COMMENT FROM information_schema.TABLES WHERE TABLE_SCHEMA = DATABASE()";
|
|
|
|
?>
|
|
|
|
DELIMITER ;;
|
2009-06-08 19:02:30 +00:00
|
|
|
CREATE PROCEDURE adminer_drop () BEGIN
|
2008-08-19 10:32:34 +00:00
|
|
|
DECLARE _table_name, _engine, _table_collation varchar(64);
|
|
|
|
DECLARE _table_comment varchar(64);
|
|
|
|
DECLARE done bool DEFAULT 0;
|
|
|
|
DECLARE tables CURSOR FOR <?php echo $query; ?>;
|
|
|
|
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
|
|
|
|
OPEN tables;
|
|
|
|
REPEAT
|
|
|
|
FETCH tables INTO _table_name, _engine, _table_collation, _table_comment;
|
|
|
|
IF NOT done THEN
|
|
|
|
CASE _table_name<?php
|
2009-05-08 05:23:03 +00:00
|
|
|
$result = $dbh->query($query);
|
2008-08-19 10:32:34 +00:00
|
|
|
while ($row = $result->fetch_assoc()) {
|
2009-06-22 00:53:51 +00:00
|
|
|
$comment = $dbh->quote($row["ENGINE"] == "InnoDB" ? preg_replace('~(?:(.+); )?InnoDB free: .*~', '\\1', $row["TABLE_COMMENT"]) : $row["TABLE_COMMENT"]);
|
2008-08-19 10:32:34 +00:00
|
|
|
echo "
|
2009-06-22 00:53:51 +00:00
|
|
|
WHEN " . $dbh->quote($row["TABLE_NAME"]) . " THEN
|
|
|
|
" . (isset($row["ENGINE"]) ? "IF _engine != '$row[ENGINE]' OR _table_collation != '$row[TABLE_COLLATION]' OR _table_comment != $comment THEN
|
|
|
|
ALTER TABLE " . idf_escape($row["TABLE_NAME"]) . " ENGINE=$row[ENGINE] COLLATE=$row[TABLE_COLLATION] COMMENT=$comment;
|
2008-08-19 10:32:34 +00:00
|
|
|
END IF" : "BEGIN END") . ";";
|
|
|
|
}
|
|
|
|
$result->free();
|
|
|
|
?>
|
|
|
|
|
|
|
|
ELSE
|
|
|
|
SET @alter_table = CONCAT('DROP TABLE `', REPLACE(_table_name, '`', '``'), '`');
|
|
|
|
PREPARE alter_command FROM @alter_table;
|
2008-10-06 15:08:55 +00:00
|
|
|
EXECUTE alter_command; -- returns "can't return a result set in the given context" with MySQL extension
|
2008-08-19 10:32:34 +00:00
|
|
|
DROP PREPARE alter_command;
|
|
|
|
END CASE;
|
|
|
|
END IF;
|
|
|
|
UNTIL done END REPEAT;
|
|
|
|
CLOSE tables;
|
|
|
|
END;;
|
|
|
|
DELIMITER ;
|
2009-06-08 19:02:30 +00:00
|
|
|
CALL adminer_drop;
|
|
|
|
DROP PROCEDURE adminer_drop;
|
2008-08-19 10:32:34 +00:00
|
|
|
<?php
|
|
|
|
}
|
2008-08-08 15:00:42 +00:00
|
|
|
}
|
2008-06-20 14:13:37 +00:00
|
|
|
}
|
|
|
|
exit;
|
|
|
|
}
|
2007-08-04 19:40:11 +00:00
|
|
|
|
2008-06-20 14:13:37 +00:00
|
|
|
page_header(lang('Export'), "", (strlen($_GET["export"]) ? array("table" => $_GET["export"]) : array()), $_GET["db"]);
|
|
|
|
?>
|
|
|
|
|
|
|
|
<form action="" method="post">
|
2009-06-01 12:53:19 +00:00
|
|
|
<table cellspacing="0">
|
2009-05-31 22:31:09 +00:00
|
|
|
<?php
|
2009-06-01 12:53:19 +00:00
|
|
|
$db_style = array('USE', 'DROP+CREATE', 'CREATE');
|
|
|
|
$table_style = array('DROP+CREATE', 'CREATE');
|
|
|
|
$data_style = array('TRUNCATE+INSERT', 'INSERT', 'INSERT+UPDATE');
|
|
|
|
if ($dbh->server_info >= 5) {
|
|
|
|
$db_style[] = 'CREATE+ALTER';
|
|
|
|
$table_style[] = 'CREATE+ALTER';
|
|
|
|
}
|
2009-05-31 22:31:09 +00:00
|
|
|
echo "<tr><th>" . lang('Output') . "</th><td>$dump_output</td></tr>\n";
|
|
|
|
echo "<tr><th>" . lang('Format') . "</th><td>$dump_format</td></tr>\n";
|
2009-06-01 12:53:19 +00:00
|
|
|
echo "<tr><th>" . lang('Database') . "</th><td><select name='db_style'><option></option>" . optionlist($db_style, (strlen($_GET["db"]) ? '' : 'CREATE')) . "</select></td></tr>\n";
|
|
|
|
echo "<tr><th>" . lang('Tables') . "</th><td><select name='table_style'><option></option>" . optionlist($table_style, 'DROP+CREATE') . "</select></td></tr>\n";
|
|
|
|
echo "<tr><th>" . lang('Data') . "</th><td><select name='data_style'><option></option>" . optionlist($data_style, 'INSERT') . "</select></td></tr>\n";
|
2009-05-31 22:31:09 +00:00
|
|
|
?>
|
|
|
|
</table>
|
|
|
|
<p><input type="submit" value="<?php echo lang('Export'); ?>" /></p>
|
2008-06-20 14:13:37 +00:00
|
|
|
|
2009-06-21 23:49:04 +00:00
|
|
|
<table cellspacing="0">
|
2008-06-20 14:13:37 +00:00
|
|
|
<?php
|
2009-05-31 22:31:09 +00:00
|
|
|
if (strlen($_GET["db"])) {
|
|
|
|
$checked = (strlen($_GET["dump"]) ? "" : " checked='checked'");
|
2009-06-03 09:17:20 +00:00
|
|
|
echo "<thead><tr>";
|
2009-06-01 13:06:11 +00:00
|
|
|
echo "<th align='left'><label><input type='checkbox' id='check-tables'$checked onclick='form_check(this, /^tables\\[/);' />" . lang('Tables') . "</label></th>";
|
|
|
|
echo "<th align='right'><label>" . lang('Data') . "<input type='checkbox' id='check-data'$checked onclick='form_check(this, /^data\\[/);' /></label></th>";
|
2009-05-31 22:31:09 +00:00
|
|
|
echo "</tr></thead>\n";
|
|
|
|
$views = "";
|
2009-07-01 19:58:22 +00:00
|
|
|
foreach (table_status() as $row) {
|
2009-05-31 22:31:09 +00:00
|
|
|
$checked = (strlen($_GET["dump"]) && $row["Name"] != $_GET["dump"] ? '' : " checked='checked'");
|
2009-06-01 13:06:11 +00:00
|
|
|
$print = '<tr><td><label><input type="checkbox" name="tables[]" value="' . htmlspecialchars($row["Name"]) . "\"$checked onclick=\"form_uncheck('check-tables');\" />" . htmlspecialchars($row["Name"]) . "</label></td>";
|
2009-05-31 22:31:09 +00:00
|
|
|
if (!$row["Engine"]) {
|
|
|
|
$views .= "$print</tr>\n";
|
|
|
|
} else {
|
2009-06-01 13:06:11 +00:00
|
|
|
echo "$print<td align='right'><label>" . ($row["Engine"] == "InnoDB" && $row["Rows"] ? lang('~ %s', $row["Rows"]) : $row["Rows"]) . '<input type="checkbox" name="data[]" value="' . htmlspecialchars($row["Name"]) . "\"$checked onclick=\"form_uncheck('check-data');\" /></label></td></tr>\n";
|
2008-07-31 14:28:26 +00:00
|
|
|
}
|
2008-06-20 14:13:37 +00:00
|
|
|
}
|
2009-06-21 23:49:04 +00:00
|
|
|
echo $views;
|
|
|
|
} else {
|
|
|
|
echo "<thead><tr><th align='left'><label><input type='checkbox' id='check-databases' checked='checked' onclick='form_check(this, /^databases\\[/);' />" . lang('Database') . "</label></th></tr></thead>\n";
|
|
|
|
foreach (get_databases() as $db) {
|
2009-06-22 09:58:58 +00:00
|
|
|
if (!information_schema($db)) {
|
2009-06-21 23:49:04 +00:00
|
|
|
echo '<tr><td><label><input type="checkbox" name="databases[]" value="' . htmlspecialchars($db) . '" checked="checked" onclick="form_uncheck(\'check-databases\');" />' . htmlspecialchars($db) . "</label></td></tr>\n";
|
|
|
|
}
|
|
|
|
}
|
2008-06-20 14:13:37 +00:00
|
|
|
}
|
|
|
|
?>
|
2009-06-21 23:49:04 +00:00
|
|
|
</table>
|
2008-06-20 14:13:37 +00:00
|
|
|
</form>
|