diff --git a/adminer/call.inc.php b/adminer/call.inc.php index 5c1ba945..eb3a1bb6 100644 --- a/adminer/call.inc.php +++ b/adminer/call.inc.php @@ -57,8 +57,13 @@ if ($in) { $name = $field["field"]; echo "" . $adminer->fieldName($field); $value = $_POST["fields"][$name]; - if ($value != "" && ereg("enum|set", $field["type"])) { - $value = intval($value); + if ($value != "") { + if ($field["type"] == "enum") { + $value = +$value; + } + if ($field["type"] == "set") { + $value = array_sum($value); + } } input($field, $value, (string) $_POST["function"][$name]); // param name can be empty echo "\n"; diff --git a/adminer/create.inc.php b/adminer/create.inc.php index fcce1e28..e1128e6f 100644 --- a/adminer/create.inc.php +++ b/adminer/create.inc.php @@ -65,7 +65,7 @@ if ($_POST && !$error && !$_POST["add"] && !$_POST["drop_col"] && !$_POST["up"] } $partitioning .= "\nPARTITION BY $_POST[partition_by]($_POST[partition])" . ($partitions // $_POST["partition"] can be expression, not only column ? " (" . implode(",", $partitions) . "\n)" - : ($_POST["partitions"] ? " PARTITIONS " . intval($_POST["partitions"]) : "") + : ($_POST["partitions"] ? " PARTITIONS " . (+$_POST["partitions"]) : "") ); } elseif ($TABLE != "" && support("partitioning")) { $partitioning .= "\nREMOVE PARTITIONING"; @@ -83,7 +83,7 @@ if ($_POST && !$error && !$_POST["add"] && !$_POST["drop_col"] && !$_POST["up"] $_POST["Comment"], ($_POST["Engine"] && $_POST["Engine"] != $orig_status["Engine"] ? $_POST["Engine"] : ""), ($_POST["Collation"] && $_POST["Collation"] != $orig_status["Collation"] ? $_POST["Collation"] : ""), - ($_POST["Auto_increment"] != "" ? preg_replace('~\\D+~', '', $_POST["Auto_increment"]) : ""), + ($_POST["Auto_increment"] != "" ? +$_POST["Auto_increment"] : ""), $partitioning )); } diff --git a/adminer/database.inc.php b/adminer/database.inc.php index 83e7ffb3..e0d91003 100644 --- a/adminer/database.inc.php +++ b/adminer/database.inc.php @@ -57,10 +57,11 @@ if ($_POST) {

' . h($name) . '
' - : '' + ? '
' + : '' ) . "\n" . ($collations ? html_select("collation", array("" => "(" . lang('collation') . ")") + $collations, $collate) : ""); ?> + "MySQL") + $drivers; -} +$drivers = array("server" => "MySQL") + $drivers; if (!defined("DRIVER")) { + $possible_drivers = array("MySQLi", "MySQL", "PDO_MySQL"); define("DRIVER", "server"); // server - backwards compatibility // MySQLi supports everything, MySQL doesn't support multiple result sets, PDO_MySQL doesn't support orgtable if (extension_loaded("mysqli")) { @@ -164,7 +160,7 @@ if (!defined("DRIVER")) { class Min_Result { var $num_rows, ///< @var int number of rows in the result - $_result ///< @access private + $_result, $_offset = 0 ///< @access private ; /** Constructor @@ -193,7 +189,7 @@ if (!defined("DRIVER")) { * @return object properties: name, type, orgtable, orgname, charsetnr */ function fetch_field() { - $return = mysql_fetch_field($this->_result); + $return = mysql_fetch_field($this->_result, $this->_offset++); // offset required under certain conditions $return->orgtable = $return->table; $return->orgname = $return->name; $return->charsetnr = ($return->blob ? 63 : 0); @@ -212,7 +208,7 @@ if (!defined("DRIVER")) { var $extension = "PDO_MySQL"; function connect($server, $username, $password) { - $this->dsn("mysql:host=" . str_replace(":", ";unix_socket=", preg_replace('~:([0-9])~', ';port=\\1', $server)), $username, $password); + $this->dsn("mysql:host=" . str_replace(":", ";unix_socket=", preg_replace('~:(\\d)~', ';port=\\1', $server)), $username, $password); $this->query("SET NAMES utf8"); // charset in DSN is ignored return true; } @@ -397,10 +393,9 @@ if (!defined("DRIVER")) { /** Get information about fields * @param string - * @param bool display hidden table columns * @return array array($name => array("field" => , "full_type" => , "type" => , "length" => , "unsigned" => , "default" => , "null" => , "auto_increment" => , "on_update" => , "collation" => , "privileges" => , "comment" => , "primary" => )) */ - function fields($table, $hidden = false) { + function fields($table) { $return = array(); foreach (get_rows("SHOW FULL COLUMNS FROM " . table($table)) as $row) { preg_match('~^([^( ]+)(?:\\((.+)\\))?( unsigned)?( zerofill)?$~', $row["Type"], $match); @@ -820,7 +815,7 @@ if (!defined("DRIVER")) { global $connection; $return = $connection->result("SHOW CREATE TABLE " . table($table), 1); if (!$auto_increment) { - $return = preg_replace('~ AUTO_INCREMENT=[0-9]+~', '', $return); //! skip comments + $return = preg_replace('~ AUTO_INCREMENT=\\d+~', '', $return); //! skip comments } return $return; } diff --git a/adminer/drivers/oracle.inc.php b/adminer/drivers/oracle.inc.php index cbdb61d5..1b001f0a 100644 --- a/adminer/drivers/oracle.inc.php +++ b/adminer/drivers/oracle.inc.php @@ -1,11 +1,8 @@ 0") . " -ORDER BY a.attnum < 0, a.attnum" +AND a.attnum > 0 +ORDER BY a.attnum" ) as $row) { //! collation, primary ereg('(.*)(\\((.*)\\))?', $row["full_type"], $match); diff --git a/adminer/drivers/sqlite.inc.php b/adminer/drivers/sqlite.inc.php index 5418ac6c..87498ae1 100644 --- a/adminer/drivers/sqlite.inc.php +++ b/adminer/drivers/sqlite.inc.php @@ -1,18 +1,83 @@ _link = new SQLite3($filename); + $version = $this->_link->version(); + $this->server_info = $version["versionString"]; + } + + function query($query) { + $result = @$this->_link->query($query); + if (!$result) { + $this->error = $this->_link->lastErrorMsg(); + return false; + } elseif ($result->numColumns()) { + return new Min_Result($result); + } + $this->affected_rows = $this->_link->changes(); + return true; + } + + function quote($string) { + return "'" . $this->_link->escapeString($string) . "'"; + } + + function store_result() { + return $this->_result; + } + + function result($query, $field = 0) { + $result = $this->query($query); + if (!is_object($result)) { + return false; + } + $row = $result->_result->fetchArray(); + return $row[$field]; + } + } + + class Min_Result { + var $_result, $_offset = 0, $num_rows; + + function Min_Result($result) { + $this->_result = $result; + } + + function fetch_assoc() { + return $this->_result->fetchArray(SQLITE3_ASSOC); + } + + function fetch_row() { + return $this->_result->fetchArray(SQLITE3_NUM); + } + + function fetch_field() { + $column = $this->_offset++; + $type = $this->_result->columnType($column); + return (object) array( + "name" => $this->_result->columnName($column), + "type" => $type, + "charsetnr" => ($type == SQLITE3_BLOB ? 63 : 0), // 63 - binary + ); + } + + function __desctruct() { + return $this->_result->finalize(); + } + } + + } else { class Min_SQLite { var $extension = "SQLite", $server_info, $affected_rows, $error, $_link; @@ -95,77 +160,6 @@ if (isset($_GET["sqlite"]) || isset($_GET["sqlite2"])) { } - } else { - - class Min_SQLite { - var $extension = "SQLite3", $server_info, $affected_rows, $error, $_link; - - function Min_SQLite($filename) { - $this->_link = new SQLite3($filename); - $version = $this->_link->version(); - $this->server_info = $version["versionString"]; - } - - function query($query) { - $result = @$this->_link->query($query); - if (!$result) { - $this->error = $this->_link->lastErrorMsg(); - return false; - } elseif ($result->numColumns()) { - return new Min_Result($result); - } - $this->affected_rows = $this->_link->changes(); - return true; - } - - function quote($string) { - return "'" . $this->_link->escapeString($string) . "'"; - } - - function store_result() { - return $this->_result; - } - - function result($query, $field = 0) { - $result = $this->query($query); - if (!is_object($result)) { - return false; - } - $row = $result->_result->fetchArray(); - return $row[$field]; - } - } - - class Min_Result { - var $_result, $_offset = 0, $num_rows; - - function Min_Result($result) { - $this->_result = $result; - } - - function fetch_assoc() { - return $this->_result->fetchArray(SQLITE3_ASSOC); - } - - function fetch_row() { - return $this->_result->fetchArray(SQLITE3_NUM); - } - - function fetch_field() { - $column = $this->_offset++; - $type = $this->_result->columnType($column); - return (object) array( - "name" => $this->_result->columnName($column), - "type" => $type, - "charsetnr" => ($type == SQLITE3_BLOB ? 63 : 0), // 63 - binary - ); - } - - function __desctruct() { - return $this->_result->finalize(); - } - } - } } elseif (extension_loaded("pdo_sqlite")) { @@ -269,7 +263,7 @@ if (isset($_GET["sqlite"]) || isset($_GET["sqlite2"])) { return !$connection->result("SELECT sqlite_compileoption_used('OMIT_FOREIGN_KEY')"); } - function fields($table, $hidden = false) { + function fields($table) { $return = array(); foreach (get_rows("PRAGMA table_info(" . table($table) . ")") as $row) { $type = strtolower($row["type"]); diff --git a/adminer/dump.inc.php b/adminer/dump.inc.php index b813f87a..fb9f5188 100644 --- a/adminer/dump.inc.php +++ b/adminer/dump.inc.php @@ -7,8 +7,9 @@ if ($_POST) { $cookie .= "&$key=" . urlencode($_POST[$key]); } cookie("adminer_export", substr($cookie, 1)); - $ext = dump_headers(($TABLE != "" ? $TABLE : DB), (DB == "" || count((array) $_POST["tables"] + (array) $_POST["data"]) > 1)); - if ($_POST["format"] == "sql") { + $ext = $adminer->dumpHeaders(($TABLE != "" ? $TABLE : DB), (DB == "" || count((array) $_POST["tables"] + (array) $_POST["data"]) > 1)); + $is_sql = ($_POST["format"] == "sql"); + if ($is_sql) { echo "-- Adminer $VERSION " . $drivers[DRIVER] . " dump " . ($jush != "sql" ? "" : "SET NAMES utf8; @@ -29,13 +30,13 @@ SET sql_mode = 'NO_AUTO_VALUE_ON_ZERO'; } foreach ((array) $databases as $db) { if ($connection->select_db($db)) { - if ($_POST["format"] == "sql" && ereg('CREATE', $style) && ($create = $connection->result("SHOW CREATE DATABASE " . idf_escape($db), 1))) { + if ($is_sql && ereg('CREATE', $style) && ($create = $connection->result("SHOW CREATE DATABASE " . idf_escape($db), 1))) { if ($style == "DROP+CREATE") { echo "DROP DATABASE IF EXISTS " . idf_escape($db) . ";\n"; } echo ($style == "CREATE+ALTER" ? preg_replace('~^CREATE DATABASE ~', '\\0IF NOT EXISTS ', $create) : $create) . ";\n"; } - if ($_POST["format"] == "sql") { + if ($is_sql) { if ($style) { echo use_sql($db) . ";\n\n"; } @@ -72,11 +73,11 @@ SET sql_mode = 'NO_AUTO_VALUE_ON_ZERO'; if ($ext == "tar") { ob_start(); } - dump_table($row["Name"], ($table ? $_POST["table_style"] : "")); + $adminer->dumpTable($row["Name"], ($table ? $_POST["table_style"] : "")); if ($data) { - dump_data($row["Name"], $_POST["data_style"]); + $adminer->dumpData($row["Name"], $_POST["data_style"], "SELECT * FROM " . table($row["Name"])); } - if ($_POST["format"] == "sql" && $_POST["triggers"]) { + if ($is_sql && $_POST["triggers"]) { $triggers = trigger_sql($row["Name"], $_POST["table_style"]); if ($triggers) { echo "\nDELIMITER ;;\n$triggers\nDELIMITER ;\n"; @@ -84,23 +85,23 @@ SET sql_mode = 'NO_AUTO_VALUE_ON_ZERO'; } if ($ext == "tar") { echo tar_file((DB != "" ? "" : "$db/") . "$row[Name].csv", ob_get_clean()); - } elseif ($_POST["format"] == "sql") { + } elseif ($is_sql) { echo "\n"; } - } elseif ($_POST["format"] == "sql") { + } elseif ($is_sql) { $views[] = $row["Name"]; } } } foreach ($views as $view) { - dump_table($view, $_POST["table_style"], true); + $adminer->dumpTable($view, $_POST["table_style"], true); } if ($ext == "tar") { echo pack("x512"); } } - if ($style == "CREATE+ALTER" && $_POST["format"] == "sql") { + if ($style == "CREATE+ALTER" && $is_sql) { // drop old tables $query = "SELECT TABLE_NAME, ENGINE, TABLE_COLLATION, TABLE_COMMENT FROM information_schema.TABLES WHERE TABLE_SCHEMA = DATABASE()"; echo "DELIMITER ;; @@ -136,11 +137,14 @@ CALL adminer_alter(@adminer_alter); DROP PROCEDURE adminer_alter; "; } - if (in_array("CREATE+ALTER", array($style, $_POST["table_style"])) && $_POST["format"] == "sql") { + if (in_array("CREATE+ALTER", array($style, $_POST["table_style"])) && $is_sql) { echo "SELECT @adminer_alter;\n"; } } } + if ($is_sql) { + echo "-- " . $connection->result("SELECT NOW()") . "\n"; + } exit; } @@ -163,8 +167,8 @@ if (!$row) { $row = array("output" => "text", "format" => "sql", "db_style" => (DB != "" ? "" : "CREATE"), "table_style" => "DROP+CREATE", "data_style" => "INSERT"); } $checked = ($_GET["dump"] == ""); -echo "" . lang('Output') . "" . $adminer->dumpOutput(0, $row["output"]) . "\n"; -echo "" . lang('Format') . "" . $adminer->dumpFormat(0, $row["format"]) . "\n"; +echo "" . lang('Output') . "" . html_select("output", $adminer->dumpOutput(), $row["output"], 0) . "\n"; // 0 - radio +echo "" . lang('Format') . "" . html_select("format", $adminer->dumpFormat(), $row["format"], 0) . "\n"; // 0 - radio echo ($jush == "sqlite" ? "" : "" . lang('Database') . "" . html_select('db_style', $db_style, $row["db_style"]) . (support("routine") ? checkbox("routines", 1, $checked, lang('Routines')) : "") . (support("event") ? checkbox("events", 1, $checked, lang('Events')) : "") diff --git a/adminer/edit.inc.php b/adminer/edit.inc.php index 1d23d12c..6e419ba4 100644 --- a/adminer/edit.inc.php +++ b/adminer/edit.inc.php @@ -72,7 +72,7 @@ if ($fields) { echo "" . $adminer->fieldName($field); $default = $_GET["set"][bracket_escape($name)]; $value = (isset($row) - ? ($row[$name] != "" && ereg("enum|set", $field["type"]) ? intval($row[$name]) : $row[$name]) + ? ($row[$name] != "" && ereg("enum|set", $field["type"]) ? +$row[$name] : $row[$name]) : (!$update && $field["auto_increment"] ? "" : (isset($_GET["select"]) ? false : (isset($default) ? $default : $field["default"]))) ); if (!$_POST["save"] && is_string($value)) { diff --git a/adminer/foreign.inc.php b/adminer/foreign.inc.php index 801586ef..f6e84f9d 100644 --- a/adminer/foreign.inc.php +++ b/adminer/foreign.inc.php @@ -60,8 +60,8 @@ foreach (table_status() as $name => $table_status) { $j = 0; foreach ($row["source"] as $key => $val) { echo ""; - echo "" . html_select("source[" . intval($key) . "]", array(-1 => "") + $source, $val, ($j == count($row["source"]) - 1 ? "foreignAddRow(this);" : 1)); - echo "" . html_select("target[" . intval($key) . "]", $target, $row["target"][$key]); + echo "" . html_select("source[" . (+$key) . "]", array(-1 => "") + $source, $val, ($j == count($row["source"]) - 1 ? "foreignAddRow(this);" : 1)); + echo "" . html_select("target[" . (+$key) . "]", $target, $row["target"][$key]); $j++; } ?> diff --git a/adminer/include/adminer.inc.php b/adminer/include/adminer.inc.php index 804f5958..420d2faf 100644 --- a/adminer/include/adminer.inc.php +++ b/adminer/include/adminer.inc.php @@ -36,7 +36,8 @@ class Adminer { * @return null */ function headers() { - header("X-Frame-Options: deny"); // ClickJacking protection in IE8, Safari 4, Chrome 2, Firefox NoScript plugin + header("X-Frame-Options: deny"); // ClickJacking protection in IE8, Safari 4, Chrome 2, Firefox 3.6.9 + header("X-XSS-Protection: 0"); // prevents introducing XSS in IE8 by removing safe parts of the page } /** Print login form @@ -107,6 +108,14 @@ document.getElementById('username').focus(); echo "\n"; } + /** Get foreign keys for table + * @param string + * @return array same format as foreign_keys() + */ + function foreignKeys($table) { + return foreign_keys($table); + } + /** Find backward keys for table * @param string * @param string @@ -130,7 +139,7 @@ document.getElementById('username').focus(); */ function selectQuery($query) { global $jush; - return "

>> " . h(str_replace("\n", " ", $query)) . " " . lang('Edit') . "\n"; + return "

>> " . h(str_replace("\n", " ", $query)) . " " . lang('Edit') . "\n"; } /** Description of a row in a table @@ -157,7 +166,7 @@ document.getElementById('username').focus(); * @return string */ function selectVal($val, $link, $field) { - $return = ($val != "NULL" && ereg("^char|binary", $field["type"]) ? "$val" : $val); + $return = ($val != "NULL" && ereg("char|binary", $field["type"]) && !ereg("var", $field["type"]) ? "$val" : $val); if (ereg('blob|bytea|raw|file', $field["type"]) && !is_utf8($val)) { $return = lang('%d byte(s)', strlen(html_entity_decode($val, ENT_QUOTES))); } @@ -429,8 +438,10 @@ document.getElementById('username').focus(); */ function editInput($table, $field, $attrs, $value) { if ($field["type"] == "enum") { - return ($field["null"] ? " " : "") + return (isset($_GET["select"]) ? " " : "") + . ($field["null"] ? " " : "") . "" + . enum_input("radio", $attrs, $field, $value) ; } return ""; @@ -452,7 +463,7 @@ document.getElementById('username').focus(); } elseif (ereg('^([+-]|\\|\\|)$', $function)) { $return = idf_escape($name) . " $function $return"; } elseif (ereg('^[+-] interval$', $function)) { - $return = idf_escape($name) . " $function " . (preg_match("~^([0-9]+|'[0-9.: -]') [A-Z_]+$~i", $value) ? $value : $return); + $return = idf_escape($name) . " $function " . (preg_match("~^(\\d+|'[0-9.: -]') [A-Z_]+$~i", $value) ? $value : $return); } elseif (ereg('^(addtime|subtime|concat)$', $function)) { $return = "$function(" . idf_escape($name) . ", $return)"; } elseif (ereg('^(md5|sha1|password|encrypt|hex)$', $function)) { @@ -465,11 +476,9 @@ document.getElementById('username').focus(); } /** Returns export output options - * @param bool generate select (otherwise radio) - * @param string - * @return string + * @return array */ - function dumpOutput($select, $value = "") { + function dumpOutput() { $return = array('text' => lang('open'), 'file' => lang('save')); if (function_exists('gzencode')) { $return['gz'] = 'gzip'; @@ -478,16 +487,199 @@ document.getElementById('username').focus(); $return['bz2'] = 'bzip2'; } // ZipArchive requires temporary file, ZIP can be created by gzcompress - see PEAR File_Archive - return html_select("output", $return, $value, $select); + return $return; } /** Returns export format options - * @param bool generate select (otherwise radio) - * @param string - * @return string + * @return array */ - function dumpFormat($select, $value = "") { - return html_select("format", array('sql' => 'SQL', 'csv' => 'CSV,', 'csv;' => 'CSV;'), $value, $select); + function dumpFormat() { + return array('sql' => 'SQL', 'csv' => 'CSV,', 'csv;' => 'CSV;', 'tsv' => 'TSV'); + } + + /** Export table structure + * @param string + * @param string + * @param bool + * @return null prints data + */ + function dumpTable($table, $style, $is_view = false) { + if ($_POST["format"] != "sql") { + echo "\xef\xbb\xbf"; // UTF-8 byte order mark + if ($style) { + dump_csv(array_keys(fields($table))); + } + } elseif ($style) { + $create = create_sql($table, $_POST["auto_increment"]); + if ($create) { + if ($style == "DROP+CREATE") { + echo "DROP " . ($is_view ? "VIEW" : "TABLE") . " IF EXISTS " . table($table) . ";\n"; + } + if ($is_view) { + // remove DEFINER with current user + $create = preg_replace('~^([A-Z =]+) DEFINER=`' . str_replace("@", "`@`", logged_user()) . '`~', '\\1', $create); //! proper escaping of user + } + echo ($style != "CREATE+ALTER" ? $create : ($is_view ? substr_replace($create, " OR REPLACE", 6, 0) : substr_replace($create, " IF NOT EXISTS", 12, 0))) . ";\n\n"; + } + if ($style == "CREATE+ALTER" && !$is_view) { + // create procedure which iterates over original columns and adds new and removes old + $query = "SELECT COLUMN_NAME, COLUMN_DEFAULT, IS_NULLABLE, COLLATION_NAME, COLUMN_TYPE, EXTRA, COLUMN_COMMENT FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = " . q($table) . " ORDER BY ORDINAL_POSITION"; + echo "DELIMITER ;; +CREATE PROCEDURE adminer_alter (INOUT alter_command text) BEGIN + DECLARE _column_name, _collation_name, after varchar(64) DEFAULT ''; + DECLARE _column_type, _column_default text; + DECLARE _is_nullable char(3); + DECLARE _extra varchar(30); + DECLARE _column_comment varchar(255); + DECLARE done, set_after bool DEFAULT 0; + DECLARE add_columns text DEFAULT '"; + $fields = array(); + $after = ""; + foreach (get_rows($query) as $row) { + $default = $row["COLUMN_DEFAULT"]; + $row["default"] = (isset($default) ? q($default) : "NULL"); + $row["after"] = q($after); //! rgt AFTER lft, lft AFTER id doesn't work + $row["alter"] = escape_string(idf_escape($row["COLUMN_NAME"]) + . " $row[COLUMN_TYPE]" + . ($row["COLLATION_NAME"] ? " COLLATE $row[COLLATION_NAME]" : "") + . (isset($default) ? " DEFAULT " . ($default == "CURRENT_TIMESTAMP" ? $default : $row["default"]) : "") + . ($row["IS_NULLABLE"] == "YES" ? "" : " NOT NULL") + . ($row["EXTRA"] ? " $row[EXTRA]" : "") + . ($row["COLUMN_COMMENT"] ? " COMMENT " . q($row["COLUMN_COMMENT"]) : "") + . ($after ? " AFTER " . idf_escape($after) : " FIRST") + ); + echo ", ADD $row[alter]"; + $fields[] = $row; + $after = $row["COLUMN_NAME"]; + } + echo "'; + DECLARE columns CURSOR FOR $query; + DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1; + SET @alter_table = ''; + OPEN columns; + REPEAT + FETCH columns INTO _column_name, _column_default, _is_nullable, _collation_name, _column_type, _extra, _column_comment; + IF NOT done THEN + SET set_after = 1; + CASE _column_name"; + foreach ($fields as $row) { + echo " + WHEN " . q($row["COLUMN_NAME"]) . " THEN + SET add_columns = REPLACE(add_columns, ', ADD $row[alter]', ''); + IF NOT (_column_default <=> $row[default]) OR _is_nullable != '$row[IS_NULLABLE]' OR _collation_name != '$row[COLLATION_NAME]' OR _column_type != " . q($row["COLUMN_TYPE"]) . " OR _extra != '$row[EXTRA]' OR _column_comment != " . q($row["COLUMN_COMMENT"]) . " OR after != $row[after] THEN + SET @alter_table = CONCAT(@alter_table, ', MODIFY $row[alter]'); + END IF;"; //! don't replace in comment + } + echo " + ELSE + SET @alter_table = CONCAT(@alter_table, ', DROP ', _column_name); + SET set_after = 0; + END CASE; + IF set_after THEN + SET after = _column_name; + END IF; + END IF; + UNTIL done END REPEAT; + CLOSE columns; + IF @alter_table != '' OR add_columns != '' THEN + SET alter_command = CONCAT(alter_command, 'ALTER TABLE " . table($table) . "', SUBSTR(CONCAT(add_columns, @alter_table), 2), ';\\n'); + END IF; +END;; +DELIMITER ; +CALL adminer_alter(@adminer_alter); +DROP PROCEDURE adminer_alter; + +"; + //! indexes + } + } + } + + /** Export table data + * @param string + * @param string + * @param string + * @return null prints data + */ + function dumpData($table, $style, $query) { + global $connection, $jush; + $max_packet = ($jush == "sqlite" ? 0 : 1048576); // default, minimum is 1024 + if ($style) { + if ($_POST["format"] == "sql" && $style == "TRUNCATE+INSERT") { + echo truncate_sql($table) . ";\n"; + } + $fields = fields($table); + $result = $connection->query($query, 1); // 1 - MYSQLI_USE_RESULT //! enum and set as numbers + if ($result) { + $insert = ""; + $buffer = ""; + while ($row = $result->fetch_assoc()) { + if ($_POST["format"] != "sql") { + dump_csv($row); + } else { + if (!$insert) { + $insert = "INSERT INTO " . table($table) . " (" . implode(", ", array_map('idf_escape', array_keys($row))) . ") VALUES"; + } + foreach ($row as $key => $val) { + $row[$key] = (isset($val) ? (ereg('int|float|double|decimal', $fields[$key]["type"]) ? $val : q($val)) : "NULL"); //! columns looking like functions + } + $s = implode(",\t", $row); + if ($style == "INSERT+UPDATE") { + $set = array(); + foreach ($row as $key => $val) { + $set[] = idf_escape($key) . " = $val"; + } + echo "$insert ($s) ON DUPLICATE KEY UPDATE " . implode(", ", $set) . ";\n"; + } else { + $s = ($max_packet ? "\n" : " ") . "($s)"; + if (!$buffer) { + $buffer = $insert . $s; + } elseif (strlen($buffer) + 2 + strlen($s) < $max_packet) { // 2 - separator and terminator length + $buffer .= ",$s"; + } else { + $buffer .= ";\n"; + echo $buffer; + $buffer = $insert . $s; + } + } + } + } + if ($_POST["format"] == "sql" && $style != "INSERT+UPDATE" && $buffer) { + $buffer .= ";\n"; + echo $buffer; + } + } elseif ($_POST["format"] == "sql") { + echo "-- " . str_replace("\n", " ", $connection->error) . "\n"; + } + } + } + + /** Send headers for export + * @param string + * @param bool + * @return string extension + */ + function dumpHeaders($identifier, $multi_table = false) { + $filename = ($identifier != "" ? friendly_url($identifier) : "adminer"); + $output = $_POST["output"]; + $ext = ($_POST["format"] == "sql" ? "sql" : ($multi_table ? "tar" : "csv")); // multiple CSV packed to TAR + header("Content-Type: " . + ($output == "bz2" ? "application/x-bzip" : + ($output == "gz" ? "application/x-gzip" : + ($ext == "tar" ? "application/x-tar" : + ($ext == "sql" || $output != "file" ? "text/plain" : "text/csv") . "; charset=utf-8" + )))); + if ($output != "text") { + header("Content-Disposition: attachment; filename=$filename.$ext" . ($output != "file" && !ereg('[^0-9a-z]', $output) ? ".$output" : "")); + } + session_write_close(); + if ($_POST["output"] == "bz2") { + ob_start('bzcompress', 1e6); + } + if ($_POST["output"] == "gz") { + ob_start('gzencode', 1e6); + } + return $ext; } /** Prints navigation after Adminer title @@ -524,13 +716,13 @@ document.getElementById('username').focus();

" . bold(lang('SQL command'), isset($_GET["sql"])) . "\n"; - if (support("dump")) { - echo "" . bold(lang('Dump'), isset($_GET["dump"])) . "\n"; - } -} -?> + if (DB == "" || !$missing) { + echo "" . bold(lang('SQL command'), isset($_GET["sql"])) . "\n"; + if (support("dump")) { + echo "" . bold(lang('Dump'), isset($_GET["dump"])) . "\n"; + } + } + ?>

@@ -549,6 +741,7 @@ if (DB == "" || !$missing) { } } if ($_GET["ns"] !== "" && !$missing) { + echo '

' . bold(lang('Create new table'), $_GET["create"] === "") . "\n"; $tables = tables_list(); if (!$tables) { echo "

" . lang('No tables.') . "\n"; @@ -559,13 +752,12 @@ if (DB == "" || !$missing) { $links[] = preg_quote($table, '/'); } echo "\n"; } - echo '

' . bold(lang('Create new table'), $_GET["create"] === "") . "\n"; } } echo (isset($_GET["sql"]) ? '' diff --git a/adminer/include/auth.inc.php b/adminer/include/auth.inc.php index 156593bc..590b4c23 100644 --- a/adminer/include/auth.inc.php +++ b/adminer/include/auth.inc.php @@ -1,12 +1,6 @@ login($_GET["username"], get_session("pwds"))) { diff --git a/adminer/include/bootstrap.inc.php b/adminer/include/bootstrap.inc.php index fc3ae784..70966859 100644 --- a/adminer/include/bootstrap.inc.php +++ b/adminer/include/bootstrap.inc.php @@ -39,11 +39,12 @@ if (!defined("SID")) { // disable magic quotes to be able to use database escaping function remove_slashes(array(&$_GET, &$_POST, &$_COOKIE)); -if (function_exists("set_magic_quotes_runtime")) { +if (function_exists("set_magic_quotes_runtime")) { // removed in PHP 6 set_magic_quotes_runtime(false); } @set_time_limit(0); // @ - can be disabled @ini_set("zend.ze1_compatibility_mode", false); // @ - deprecated +@ini_set("precision", 20); // @ - can be disabled include "../adminer/include/lang.inc.php"; include "../adminer/lang/$LANG.inc.php"; @@ -70,7 +71,6 @@ include "../adminer/include/xxtea.inc.php"; include "../adminer/include/auth.inc.php"; include "./include/connect.inc.php"; include "./include/editing.inc.php"; -include "./include/export.inc.php"; session_cache_limiter(""); // to allow restarting session if (!ini_bool("session.use_cookies") || @ini_set("session.use_cookies", false) !== false) { // @ - may be disabled diff --git a/adminer/include/connect.inc.php b/adminer/include/connect.inc.php index 37a090b3..01009712 100644 --- a/adminer/include/connect.inc.php +++ b/adminer/include/connect.inc.php @@ -23,6 +23,9 @@ function connect_error() { } echo "

" . lang('%s version: %s through PHP extension %s', $drivers[DRIVER], "$connection->server_info", "$connection->extension") . "\n"; echo "

" . lang('Logged as: %s', "" . h(logged_user()) . "") . "\n"; + if ($_GET["refresh"]) { + set_session("dbs", null); + } $databases = get_databases(); if ($databases) { $scheme = support("scheme"); @@ -40,6 +43,7 @@ function connect_error() { } echo "\n"; echo "

\n"; + echo "" . lang('Refresh') . "\n"; echo "

\n"; } } diff --git a/adminer/include/design.inc.php b/adminer/include/design.inc.php index aa14ea02..5d821fe5 100644 --- a/adminer/include/design.inc.php +++ b/adminer/include/design.inc.php @@ -15,7 +15,7 @@ function page_header($title, $error = "", $breadcrumb = array(), $title2 = "") { $protocol = ($HTTPS ? "https" : "http"); ?> - + @@ -26,7 +26,7 @@ function page_header($title, $error = "", $breadcrumb = array(), $title2 = "") { -', '');"> +', '');"> diff --git a/adminer/include/editing.inc.php b/adminer/include/editing.inc.php index cf4ba18b..a26d1594 100644 --- a/adminer/include/editing.inc.php +++ b/adminer/include/editing.inc.php @@ -333,3 +333,18 @@ function drop_create($drop, $create, $location, $message_drop, $message_alter, $ } return $dropped; } + +/** Get string to add a file in TAR +* @param string +* @param string +* @return string +*/ +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 "; + return $return . str_repeat("\0", 512 - strlen($return)) . $contents . str_repeat("\0", 511 - (strlen($contents) + 511) % 512); +} diff --git a/adminer/include/export.inc.php b/adminer/include/export.inc.php deleted file mode 100644 index 250557b7..00000000 --- a/adminer/include/export.inc.php +++ /dev/null @@ -1,176 +0,0 @@ - $row[default]) OR _is_nullable != '$row[IS_NULLABLE]' OR _collation_name != '$row[COLLATION_NAME]' OR _column_type != " . q($row["COLUMN_TYPE"]) . " OR _extra != '$row[EXTRA]' OR _column_comment != " . q($row["COLUMN_COMMENT"]) . " OR after != $row[after] THEN - SET @alter_table = CONCAT(@alter_table, ', MODIFY $row[alter]'); - END IF;"; //! don't replace in comment - } - echo " - ELSE - SET @alter_table = CONCAT(@alter_table, ', DROP ', _column_name); - SET set_after = 0; - END CASE; - IF set_after THEN - SET after = _column_name; - END IF; - END IF; - UNTIL done END REPEAT; - CLOSE columns; - IF @alter_table != '' OR add_columns != '' THEN - SET alter_command = CONCAT(alter_command, 'ALTER TABLE " . table($table) . "', SUBSTR(CONCAT(add_columns, @alter_table), 2), ';\\n'); - END IF; -END;; -DELIMITER ; -CALL adminer_alter(@adminer_alter); -DROP PROCEDURE adminer_alter; - -"; - //! indexes - } - } -} - -function dump_data($table, $style, $select = "") { - global $connection, $jush; - $max_packet = ($jush == "sqlite" ? 0 : 1048576); // default, minimum is 1024 - if ($style) { - if ($_POST["format"] == "sql" && $style == "TRUNCATE+INSERT") { - echo truncate_sql($table) . ";\n"; - } - $fields = fields($table); - $result = $connection->query(($select ? $select : "SELECT * FROM " . table($table)), 1); // 1 - MYSQLI_USE_RESULT //! enum and set as numbers - if ($result) { - $insert = ""; - $buffer = ""; - while ($row = $result->fetch_assoc()) { - if ($_POST["format"] != "sql") { - dump_csv($row); - } else { - if (!$insert) { - $insert = "INSERT INTO " . table($table) . " (" . implode(", ", array_map('idf_escape', array_keys($row))) . ") VALUES"; - } - foreach ($row as $key => $val) { - $row[$key] = (isset($val) ? (ereg('int|float|double|decimal', $fields[$key]["type"]) ? $val : q($val)) : "NULL"); //! columns looking like functions - } - $s = implode(",\t", $row); - if ($style == "INSERT+UPDATE") { - $set = array(); - foreach ($row as $key => $val) { - $set[] = idf_escape($key) . " = $val"; - } - echo "$insert ($s) ON DUPLICATE KEY UPDATE " . implode(", ", $set) . ";\n"; - } else { - $s = ($max_packet ? "\n" : " ") . "($s)"; - if (!$buffer) { - $buffer = $insert . $s; - } elseif (strlen($buffer) + 2 + strlen($s) < $max_packet) { // 2 - separator and terminator length - $buffer .= ",$s"; - } else { - $buffer .= ";\n"; - echo $buffer; - $buffer = $insert . $s; - } - } - } - } - if ($_POST["format"] == "sql" && $style != "INSERT+UPDATE" && $buffer) { - $buffer .= ";\n"; - echo $buffer; - } - } - } -} - -function dump_headers($identifier, $multi_table = false) { - $filename = ($identifier != "" ? friendly_url($identifier) : "adminer"); - $output = $_POST["output"]; - $ext = ($_POST["format"] == "sql" ? "sql" : ($multi_table ? "tar" : "csv")); // multiple CSV packed to TAR - header("Content-Type: " . - ($output == "bz2" ? "application/x-bzip" : - ($output == "gz" ? "application/x-gzip" : - ($ext == "tar" ? "application/x-tar" : - ($ext == "sql" || $output != "file" ? "text/plain" : "text/csv") . "; charset=utf-8" - )))); - if ($output != "text") { - header("Content-Disposition: attachment; filename=$filename.$ext" . ($output != "file" && !ereg('[^0-9a-z]', $output) ? ".$output" : "")); - } - session_write_close(); - if ($_POST["output"] == "bz2") { - ob_start('bzcompress', 1e6); - } - if ($_POST["output"] == "gz") { - ob_start('gzencode', 1e6); - } - return $ext; -} diff --git a/adminer/include/functions.inc.php b/adminer/include/functions.inc.php index 550fd999..ac3b7c55 100644 --- a/adminer/include/functions.inc.php +++ b/adminer/include/functions.inc.php @@ -143,6 +143,14 @@ function confirm($count = "") { return " onclick=\"return confirm('" . lang('Are you sure?') . ($count ? " (' + $count + ')" : "") . "');\""; } +/** Escape string for JavaScript apostrophes +* @param string +* @return string +*/ +function js_escape($string) { + return addcslashes($string, "\r\n'\\/"); // slash for