commit b4a429709f91fca9165e3d243708df94fabf58a7 Author: jakubvrana Date: Fri Jun 29 14:34:21 2007 +0000 Initial revision git-svn-id: https://adminer.svn.sourceforge.net/svnroot/adminer/trunk@1 7c3ca157-0c34-0410-bff1-cbf682f78f5c diff --git a/phpMinAdmin.php b/phpMinAdmin.php new file mode 100644 index 00000000..1206c0b6 --- /dev/null +++ b/phpMinAdmin.php @@ -0,0 +1,456 @@ + ':1', ']' => ':2'); + return strtr($idf, ($back ? array_flip($trans) : $trans)); +} + +function optionlist($options, $selected = array(), $not_vals = false) { + $return = ""; + foreach ($options as $key => $val) { + $checked = in_array(($not_vals ? $val : $key), (array) $selected); + $return .= '' . htmlspecialchars($val) . ''; + } + return $return; +} + +function fields($table) { + $return = array(); + $result = mysql_query("SHOW COLUMNS FROM " . idf_escape($table)); + while ($row = mysql_fetch_assoc($result)) { + preg_match('~^(.*?)(?:\\((.+)\\))?$~', $row["Type"], $match); + $return[$row["Field"]] = array( + "type" => $match[1], + "length" => $match[2], + "default" => $row["Default"], + "null" => ($row["Null"] != "NO"), + ); + } + mysql_free_result($result); + return $return; +} + +function indexes($table) { + $return = array(); + $result = mysql_query("SHOW INDEX FROM " . idf_escape($table)); + while ($row = mysql_fetch_assoc($result)) { + $type = ($row["Key_name"] == "PRIMARY" ? "PRIMARY" : ($row["Index_type"] == "FULLTEXT" ? "FULLTEXT" : ($row["Non_unique"] ? "INDEX" : "UNIQUE"))); + $return[$type][$row["Key_name"]][$row["Seq_in_index"]] = $row["Column_name"]; + } + mysql_free_result($result); + return $return; +} + +function foreign_keys($table) { + static $pattern = '~`((?:[^`]*|``)+)`~'; + $return = array(); + $create_table = mysql_result(mysql_query("SHOW CREATE TABLE " . idf_escape($table)), 0, 1); + preg_match_all('~FOREIGN KEY \\((.*)\\) REFERENCES (.*) \\((.*)\\)~', $create_table, $matches, PREG_SET_ORDER); + foreach ($matches as $match) { + preg_match_all($pattern, $match[1], $source); + preg_match_all($pattern, $match[3], $target); + foreach ($source[1] as $val) { + $return[idf_unescape($val)][] = array(idf_unescape(substr($match[2], 1, -1)), array_map('idf_unescape', $source[1]), array_map('idf_unescape', $target[1])); + } + } + return $return; +} + +function unique_idf($row, $indexes) { + foreach ($indexes as $type => $index) { + if ($type == "PRIMARY" || $type == "UNIQUE") { + foreach ($index as $columns) { + $return = array(); + foreach ($columns as $key) { + if (!isset($row[$key])) { + continue 2; + } + $return[] = urlencode("where[$key]") . "=" . urlencode($row[$key]); + } + return $return; + } + } + } + $return = array(); + foreach ($row as $key => $val) { + $return[] = (isset($val) ? urlencode("where[$key]") . "=" . urlencode($val) : "null%5B%5D=" . urlencode($key)); + } + return $return; +} + +if (get_magic_quotes_gpc()) { + $process = array(&$_GET, &$_POST); + while (list($key, $val) = each($process)) { + foreach ($val as $k => $v) { + unset($process[$key][$k]); + if (is_array($v)) { + $process[$key][stripslashes($k)] = $v; + $process[] = &$process[$key][stripslashes($k)]; + } else { + $process[$key][stripslashes($k)] = stripslashes($v); + } + } + } + unset($process); +} + +if (isset($_POST["server"])) { + $_SESSION["username"] = $_POST["username"]; + $_SESSION["password"] = $_POST["password"]; + header("Location: " . ($_GET["server"] == $_POST["server"] ? $_SERVER["REQUEST_URI"] : preg_replace('~^[^?]*/([^?]*).*~', '\\1' . (strlen($_POST["server"]) ? '?server=' . urlencode($_POST["server"]) : ''), $_SERVER["REQUEST_URI"]))); + exit; +} +?> + + + +<?php echo lang('phpMinAdmin'); ?> + + + + + + + +

+ " . lang('Invalid credentials.') . "

\n"; + } +?> + +
+ + + + + +
:" maxlength="60" />
:" maxlength="16" />
:
$val) { // expired session + echo ''; +} +?> +
+
+ +
+

+

+ +
+

+ +
+ " . lang('Invalid database.') . "

\n"; + } else { + mysql_query("SET CHARACTER SET utf8"); + + if (isset($_GET["database"])) { + $result = mysql_query("SHOW TABLES"); + if (!mysql_num_rows($result)) { + echo "

" . lang('No tables.') . "

\n"; + } else { + echo "

\n"; + while ($row = mysql_fetch_row($result)) { + echo "" . lang('select') . " " . htmlspecialchars($row[0]) . "
\n"; + } + echo "

\n"; + } + mysql_free_result($result); + } + ?> +
+ +
+ " . lang('SQL command') . "\n"; + if ($_SESSION["message"]) { + echo "

$_SESSION[message]

\n"; + $_SESSION["message"] = ""; + } + if ($_POST) { + $result = mysql_query($_POST["query"]); + if (!$result) { + echo "

" . lang('Error in query') . ": " . mysql_error() . "

\n"; + } elseif (mysql_num_rows($result)) { + while ($row = mysql_fetch_assoc($result)) { + //! select + } + mysql_free_result($result); + } else { + mysql_free_result($result); + $_SESSION["message"] = sprintf(lang('Query executed OK, %d row(s) affected.'), mysql_affected_rows()); + header("Location: " . $SELF . "sql="); + exit; + } + } + ?> +
+

+

+
+ " . lang('Table') . ": " . htmlspecialchars($_GET["table"]) . "\n"; + $result = mysql_query("SHOW FULL COLUMNS FROM " . idf_escape($_GET["table"])); + echo "\n"; + while ($row = mysql_fetch_assoc($result)) { + echo "\n"; + } + echo "
" . htmlspecialchars($row["Field"]) . "$row[Type]" . ($row["Null"] == "NO" ? " NOT NULL" : "") . "
\n"; + mysql_free_result($result); + + $indexes = indexes($_GET["table"]); + if ($indexes) { + echo "

" . lang('Indexes') . "

\n"; + echo "\n"; + foreach ($indexes as $type => $index) { + foreach ($index as $columns) { + sort($columns); + echo "\n"; + } + } + echo "
$type" . implode(", ", $columns) . "
\n"; + } + + } elseif (isset($_GET["select"])) { + ob_end_flush(); + echo "

" . lang('Select') . ": " . htmlspecialchars($_GET["select"]) . "

\n"; + if ($_SESSION["message"]) { + echo "

$_SESSION[message]

\n"; + $_SESSION["message"] = ""; + } + echo "

" . lang('New item') . "

\n"; + $limit = 30; + + echo "
\n"; + if (strlen($_GET["server"])) { + echo ''; + } + echo ''; + echo ''; + + $where = array(); + $columns = array(); + foreach (fields($_GET["select"]) as $name => $field) { + $columns[] = $name; + } + $operators = array("=", "<", ">", "<=", ">=", "!=", "IS NULL"); //! IS NULL - hide input + $i = 0; + foreach ((array) $_GET["where"] as $val) { + if ($val["col"] && in_array($val["op"], $operators)) { + $where[] = idf_escape($val["col"]) . " $val[op]" . ($val["op"] != "IS NULL" ? " '" . mysql_real_escape_string($val["val"]) . "'" : ""); + echo ""; + echo ""; + echo "
\n"; + $i++; + } + } + echo ""; + echo ""; + echo "
\n"; //! JavaScript for adding next + + //! sort, limit + + echo "\n"; + echo "
\n"; + $result = mysql_query("SELECT SQL_CALC_FOUND_ROWS * FROM " . idf_escape($_GET["select"]) . ($where ? " WHERE " . implode(" AND ", $where) : "") . " LIMIT $limit OFFSET " . ($limit * $_GET["page"])); + $found_rows = mysql_result(mysql_query(" SELECT FOUND_ROWS()"), 0); + if (!mysql_num_rows($result)) { + echo "

" . lang('No rows.') . "

\n"; + } else { + $indexes = indexes($_GET["select"]); + $foreign_keys = foreign_keys($_GET["select"]); + + echo "\n"; + $first = true; + while ($row = mysql_fetch_assoc($result)) { + if ($first) { + echo "\n"; + $first = false; + } + echo ""; + foreach ($row as $key => $val) { + if (!isset($val)) { + $val = "NULL"; + } else { + $val = htmlspecialchars($val); + if (count($foreign_keys[$key]) == 1) { + $foreign_key = $foreign_keys[$key][0]; + $val = '">' . "$val"; + foreach ($foreign_key[1] as $i => $source) { + $val = "&where[$i][col]=" . urlencode($foreign_key[2][$i]) . "&where[$i][op]=%3D&where[$i][val]=" . urlencode($row[$source]) . $val; + } + $val = '$val"; + } + echo ""; //! links to referencing tables + echo "\n"; + } + echo "
" . implode("", array_map('htmlspecialchars', array_keys($row))) . "" . lang('Action') . "
edit
\n"; + if ($found_rows > $limit) { + echo "

" . lang('Page') . ":\n"; + for ($i=0; $i < $found_rows / $limit; $i++) { + echo ($i == $_GET["page"] ? $i + 1 : "" . ($i + 1) . "") . "\n"; + } + echo "

\n"; + } + } + mysql_free_result($result); + + } elseif (isset($_GET["edit"])) { + echo "

" . lang('Edit') . ": " . htmlspecialchars($_GET["edit"]) . "

\n"; + $where = array(); + if (is_array($_GET["where"])) { + foreach ($_GET["where"] as $key => $val) { + $where[] = idf_escape($key) . " = BINARY '" . mysql_real_escape_string($val) . "'"; + } + } + if (is_array($_GET["null"])) { + foreach ($_GET["null"] as $key) { + $where[] = idf_escape($key) . " IS NULL"; + } + } + $fields = fields($_GET["edit"]); + if ($_POST) { + if (isset($_POST["delete"])) { + $query = "DELETE FROM " . idf_escape($_GET["edit"]) . " WHERE " . implode(" AND ", $where) . " LIMIT 1"; + $message = lang('Item has been deleted.'); + } else { + $set = array(); + foreach ($fields as $key => $field) { + if (preg_match('~char|text|set~', $field["type"]) ? $_POST["null"][$key] : !strlen($_POST["fields"][$key])) { + $value = "NULL"; + } elseif ($field["type"] == "enum") { + $value = intval($_POST["fields"][$key]); + } elseif ($field["type"] == "set") { + $value = array_sum((array) $_POST["fields"][$key]); + } else { + $value = "'" . mysql_real_escape_string($_POST["fields"][$key]) . "'"; + } + $set[] = idf_escape(bracket_escape($key, "back")) . " = $value"; + } + if ($where) { + $query = "UPDATE " . idf_escape($_GET["edit"]) . " SET " . implode(", ", $set) . " WHERE " . implode(" AND ", $where) . " LIMIT 1"; + $message = lang('Item has been updated.'); + } else { + $query = "INSERT INTO " . idf_escape($_GET["edit"]) . " SET " . implode(", ", $set); + $message = lang('Item has been inserted.'); + } + } + if (mysql_query($query)) { + $_SESSION["message"] = $message; + header("Location: " . $SELF . "select=" . urlencode($_GET["edit"])); + exit; + } else { + echo "

" . lang('Error during saving') . ": " . htmlspecialchars(mysql_error()) . "

\n"; + } + } + if ($_POST) { + $data = $_POST["fields"]; + } elseif ($where) { + $select = array("*"); + foreach ($fields as $name => $field) { + if ($field["type"] == "enum" || $field["type"] == "set") { + $select[] = "1*" . idf_escape($name) . " AS " . idf_escape($name); + } + } + $data = mysql_fetch_assoc(mysql_query("SELECT " . implode(", ", $select) . " FROM " . idf_escape($_GET["edit"]) . " WHERE " . implode(" AND ", $where) . " LIMIT 1")); + } else { + $data = array(); + } + ?> +
+ + $field) { + echo "\n"; + } + echo "\n"; + ?> +
" . htmlspecialchars($name) . ""; + $value = ($data ? $data[$name] : $field["default"]); + $name = htmlspecialchars(bracket_escape($name)); + if ($field["type"] == "enum") { + echo ''; + preg_match_all("~'((?:[^']*|'')+)'~", $field["length"], $matches); + foreach ($matches[1] as $i => $val) { + $id = "field-$name-" . ($i+1); + echo ' '; + } + if ($field["null"]) { + $id = "field-$name-"; + echo ' '; + } + } elseif ($field["type"] == "set") { //! 64 bits + preg_match_all("~'((?:[^']*|'')+)'~", $field["length"], $matches); + foreach ($matches[1] as $i => $val) { + $id = "$name-" . ($i+1); + echo ' '; + } + } elseif (strpos($field["type"], "text") !== false) { + echo ''; + } else { //! numbers, date, binary + echo ''; + } + if ($field["null"] && preg_match('~char|text|set~', $field["type"])) { + echo ''; + } + echo "
" . ($where ? " " : "") . "
+
+ +
+ + + +