diff --git a/dump.inc.php b/dump.inc.php index 047e01f4..98213333 100644 --- a/dump.inc.php +++ b/dump.inc.php @@ -16,18 +16,28 @@ function dump($db) { } } - echo "CREATE DATABASE IF NOT EXISTS " . idf_escape($db) . ";\n"; + $result = mysql_query("SHOW CREATE DATABASE " . idf_escape($db)); + if ($result) { + echo mysql_result($result, 0) . ";\n"; + mysql_free_result($result); + } echo "USE " . idf_escape($db) . ";\n"; echo "SET CHARACTER SET utf8;\n\n"; $result = mysql_query("SHOW TABLES"); while ($row = mysql_fetch_row($result)) { - echo mysql_result(mysql_query("SHOW CREATE TABLE " . idf_escape($row[0])), 0, 1) . ";\n"; - $result1 = mysql_query("SELECT * FROM " . idf_escape($row[0])); //! except views //! enum and set as numbers - while ($row1 = mysql_fetch_row($result1)) { - echo "INSERT INTO " . idf_escape($row[0]) . " VALUES ('" . implode("', '", array_map('mysql_real_escape_string', $row1)) . "');\n"; + $result1 = mysql_query("SHOW CREATE TABLE " . idf_escape($row[0])); + if ($result1) { + echo mysql_result($result1, 0, 1) . ";\n"; + mysql_free_result($result1); + $result1 = mysql_query("SELECT * FROM " . idf_escape($row[0])); //! except views //! enum and set as numbers + if ($result1) { + while ($row1 = mysql_fetch_row($result1)) { + echo "INSERT INTO " . idf_escape($row[0]) . " VALUES ('" . implode("', '", array_map('mysql_real_escape_string', $row1)) . "');\n"; + } + mysql_free_result($result1); + } + echo "\n"; } - mysql_free_result($result1); - echo "\n"; } mysql_free_result($result); @@ -36,6 +46,7 @@ function dump($db) { echo "CREATE TRIGGER " . idf_escape($row["Trigger"]) . " $row[Timing] $row[Event] ON " . idf_escape($row["Table"]) . " FOR EACH ROW $row[Statement];\n\n"; //! delimiter } mysql_free_result($result); + echo "\n\n"; echo implode("", (array) $routines[$db]); } diff --git a/functions.inc.php b/functions.inc.php index d35f4412..b9fce472 100644 --- a/functions.inc.php +++ b/functions.inc.php @@ -32,20 +32,23 @@ function optionlist($options, $selected = array(), $not_vals = false) { function fields($table) { $return = array(); $result = mysql_query("SHOW FULL COLUMNS FROM " . idf_escape($table)); - while ($row = mysql_fetch_assoc($result)) { - preg_match('~^([^(]+)(?:\\((.+)\\))?( unsigned)?( zerofill)?$~', $row["Type"], $match); - $return[$row["Field"]] = array( - "field" => $row["Field"], - "type" => $match[1], - "length" => $match[2], - "unsigned" => ltrim($match[3] . $match[4]), - "default" => $row["Default"], - "null" => ($row["Null"] != "NO"), - "extra" => $row["Extra"], - "collation" => $row["Collation"], - ); + if ($result) { + while ($row = mysql_fetch_assoc($result)) { + preg_match('~^([^(]+)(?:\\((.+)\\))?( unsigned)?( zerofill)?$~', $row["Type"], $match); + $return[$row["Field"]] = array( + "field" => $row["Field"], + "type" => $match[1], + "length" => $match[2], + "unsigned" => ltrim($match[3] . $match[4]), + "default" => $row["Default"], + "null" => ($row["Null"] != "NO"), + "extra" => $row["Extra"], + "collation" => $row["Collation"], + "privileges" => explode(",", $row["Privileges"]), + ); + } + mysql_free_result($result); } - mysql_free_result($result); return $return; } @@ -63,12 +66,16 @@ function indexes($table) { 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[4], $target); - $return[] = array(idf_unescape($match[2]), idf_unescape($match[3]), array_map('idf_unescape', $source[1]), array_map('idf_unescape', $target[1])); + $result = mysql_query("SHOW CREATE TABLE " . idf_escape($table)); + if ($result) { + $create_table = mysql_result($result, 0, 1); + mysql_free_result($result); + 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[4], $target); + $return[] = array(idf_unescape($match[2]), idf_unescape($match[3]), array_map('idf_unescape', $source[1]), array_map('idf_unescape', $target[1])); + } } return $return; } diff --git a/select.inc.php b/select.inc.php index 0d1c6a26..9b3451d3 100644 --- a/select.inc.php +++ b/select.inc.php @@ -1,30 +1,39 @@ ' . lang('New item') . "

\n"; -echo "
\n
" . lang('Search') . "\n"; -if (strlen($_GET["server"])) { - echo ''; -} -echo ''; -echo ''; -echo "\n"; - -$where = array(); -$columns = array_keys(fields($_GET["select"])); -$operators = array("=", "<", ">", "<=", ">=", "!=", "LIKE", "REGEXP", "IS NULL"); -$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++; +$fields = fields($_GET["select"]); +$columns = array(); +foreach ($fields as $key => $field) { + if (in_array("select", $field["privileges"])) { + $columns[] = $key; } } -?> + +if (!$columns) { + echo "

" . lang('Unable to select the table') . ($fields ? "" : ": " . mysql_error()) . ".

\n"; +} else { + echo "\n
" . lang('Search') . "\n"; + if (strlen($_GET["server"])) { + echo ''; + } + echo ''; + echo ''; + echo "\n"; + + $where = array(); + $operators = array("=", "<", ">", "<=", ">=", "!=", "LIKE", "REGEXP", "IS NULL"); + $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 "\n"; //! JavaScript for adding next -//! fulltext search -echo "
\n"; - -echo "
" . lang('Sort') . "\n"; -$order = array(); -$i = 0; -foreach ((array) $_GET["order"] as $key => $val) { - if (in_array($val, $columns, true)) { - $desc = in_array($key, (array) $_GET["desc"]); - $order[] = idf_escape($val) . ($desc ? " DESC" : ""); - echo "
"; - echo "
\n"; - $i++; - } -} -echo "
"; -echo "
\n"; -echo "
\n"; - -echo "
" . lang('Limit') . "\n"; -$limit = (isset($_GET["limit"]) ? $_GET["limit"] : "30"); -echo '
'; -echo "
\n"; - -echo "
" . lang('Action') . "
\n"; -echo "\n"; -echo "
\n"; - -$result = mysql_query("SELECT SQL_CALC_FOUND_ROWS * FROM " . idf_escape($_GET["select"]) . ($where ? " WHERE " . implode(" AND ", $where) : "") . ($order ? " ORDER BY " . implode(", ", $order) : "") . (strlen($limit) ? " LIMIT " . intval($limit) . " OFFSET " . ($limit * $_GET["page"]) : "")); -$found_rows = mysql_result(mysql_query(" SELECT FOUND_ROWS()"), 0); // space for mysql.trace_mode -if (!mysql_num_rows($result)) { - echo "

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

\n"; -} else { - $foreign_keys = array(); - foreach (foreign_keys($_GET["select"]) as $foreign_key) { - foreach ($foreign_key[2] as $val) { - $foreign_keys[$val][] = $foreign_key; - } - } - $childs = array(); - $result1 = mysql_query("SELECT * FROM information_schema.KEY_COLUMN_USAGE WHERE REFERENCED_TABLE_SCHEMA = '" . mysql_real_escape_string($_GET["db"]) . "' AND REFERENCED_TABLE_NAME = '" . mysql_real_escape_string($_GET["select"]) . "' ORDER BY ORDINAL_POSITION"); - while ($row1 = mysql_fetch_assoc($result1)) { - $childs[$row1["CONSTRAINT_NAME"]][0] = $row1["TABLE_SCHEMA"]; - $childs[$row1["CONSTRAINT_NAME"]][1] = $row1["TABLE_NAME"]; - $childs[$row1["CONSTRAINT_NAME"]][2][] = $row1["REFERENCED_COLUMN_NAME"]; - $childs[$row1["CONSTRAINT_NAME"]][3][] = $row1["COLUMN_NAME"]; - } - mysql_free_result($result1); + echo "
"; + echo ""; + echo "
\n"; //! JavaScript for adding next + //! fulltext search + echo "
\n"; - echo "\n"; - for ($j=0; $row = mysql_fetch_assoc($result); $j++) { - if (!$j) { - echo "\n"; + echo "
" . lang('Sort') . "\n"; + $order = array(); + $i = 0; + foreach ((array) $_GET["order"] as $key => $val) { + if (in_array($val, $columns, true)) { + $desc = in_array($key, (array) $_GET["desc"]); + $order[] = idf_escape($val) . ($desc ? " DESC" : ""); + echo "
"; + echo "
\n"; + $i++; } - echo "
"; - foreach ($row as $key => $val) { - if (!isset($val)) { - $val = "NULL"; - } else { - $val = (strlen(trim($val)) ? nl2br(htmlspecialchars($val)) : " "); - foreach ((array) $foreign_keys[$key] as $foreign_key) { - if (count($foreign_keys[$key]) == 1 || count($foreign_key[2]) == 1) { - $val = '">' . "$val"; - foreach ($foreign_key[2] as $i => $source) { - $val = "&where[$i][col]=" . urlencode($foreign_key[3][$i]) . "&where[$i][op]=%3D&where[$i][val]=" . urlencode($row[$source]) . $val; + } + echo "
"; + echo "
\n"; + echo "\n"; + + echo "
" . lang('Limit') . "\n"; + $limit = (isset($_GET["limit"]) ? $_GET["limit"] : "30"); + echo '
'; + echo "
\n"; + + echo "
" . lang('Action') . "
\n"; + echo "\n"; + echo "
\n"; + + $result = mysql_query("SELECT SQL_CALC_FOUND_ROWS " . implode(", ", array_map('idf_escape', $columns)) . " FROM " . idf_escape($_GET["select"]) . ($where ? " WHERE " . implode(" AND ", $where) : "") . ($order ? " ORDER BY " . implode(", ", $order) : "") . (strlen($limit) ? " LIMIT " . intval($limit) . " OFFSET " . ($limit * $_GET["page"]) : "")); + if (!mysql_num_rows($result)) { + echo "

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

\n"; + } else { + $found_rows = mysql_result(mysql_query(" SELECT FOUND_ROWS()"), 0); // space for mysql.trace_mode + $indexes = indexes($_GET["select"]); + $foreign_keys = array(); + foreach (foreign_keys($_GET["select"]) as $foreign_key) { + foreach ($foreign_key[2] as $val) { + $foreign_keys[$val][] = $foreign_key; + } + } + $childs = array(); + $result1 = mysql_query("SELECT * FROM information_schema.KEY_COLUMN_USAGE WHERE REFERENCED_TABLE_SCHEMA = '" . mysql_real_escape_string($_GET["db"]) . "' AND REFERENCED_TABLE_NAME = '" . mysql_real_escape_string($_GET["select"]) . "' ORDER BY ORDINAL_POSITION"); + while ($row1 = mysql_fetch_assoc($result1)) { + $childs[$row1["CONSTRAINT_NAME"]][0] = $row1["TABLE_SCHEMA"]; + $childs[$row1["CONSTRAINT_NAME"]][1] = $row1["TABLE_NAME"]; + $childs[$row1["CONSTRAINT_NAME"]][2][] = $row1["REFERENCED_COLUMN_NAME"]; + $childs[$row1["CONSTRAINT_NAME"]][3][] = $row1["COLUMN_NAME"]; + } + mysql_free_result($result1); + + echo "
" . implode("", array_map('htmlspecialchars', array_keys($row))) . "" . lang('Action') . "
\n"; + for ($j=0; $row = mysql_fetch_assoc($result); $j++) { + if (!$j) { + echo "\n"; + } + echo ""; + foreach ($row as $key => $val) { + if (!isset($val)) { + $val = "NULL"; + } else { + $val = (strlen(trim($val)) ? nl2br(htmlspecialchars($val)) : " "); + foreach ((array) $foreign_keys[$key] as $foreign_key) { + if (count($foreign_keys[$key]) == 1 || count($foreign_key[2]) == 1) { + $val = '">' . "$val"; + foreach ($foreign_key[2] as $i => $source) { + $val = "&where[$i][col]=" . urlencode($foreign_key[3][$i]) . "&where[$i][op]=%3D&where[$i][val]=" . urlencode($row[$source]) . $val; + } + $val = '' . lang('edit') . ''; //! views can be unupdatable - foreach ($childs as $child) { - echo ' ' . lang('edit') . ''; //! views can be unupdatable + foreach ($childs as $child) { + echo ' ' . htmlspecialchars($child[1]) . ''; } - echo '">' . htmlspecialchars($child[1]) . ''; + echo ""; + echo "\n"; } - echo ""; - echo "\n"; - } - echo "
" . implode("", array_map('htmlspecialchars', array_keys($row))) . "" . lang('Action') . "
\n"; - if (intval($limit) && $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"; + if (intval($limit) && $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"; } - echo "

\n"; } + mysql_free_result($result); } -mysql_free_result($result); diff --git a/table.inc.php b/table.inc.php index 093335db..4f96938b 100644 --- a/table.inc.php +++ b/table.inc.php @@ -2,34 +2,38 @@ page_header(lang('Table') . ": " . htmlspecialchars($_GET["table"])); $result = mysql_query("SHOW 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" ? "" : " NULL") . "
\n"; -mysql_free_result($result); -echo '

' . lang('Alter table') . "

\n"; - -echo "

" . lang('Indexes') . "

\n"; -$indexes = indexes($_GET["table"]); -if ($indexes) { +if (!$result) { + echo "

" . lang('Unable to show the table definition') . ": " . mysql_error() . ".

\n"; +} else { echo "\n"; - foreach ($indexes as $index) { - ksort($index["columns"]); - echo "\n"; + while ($row = mysql_fetch_assoc($result)) { + echo "\n"; } echo "
$index[type]" . implode(", ", $index["columns"]) . "
" . htmlspecialchars($row["Field"]) . "$row[Type]" . ($row["Null"] == "NO" ? "" : " NULL") . "
\n"; -} -echo '

' . lang('Alter indexes') . "

\n"; - -$foreign_keys = foreign_keys($_GET["table"]); -if ($foreign_keys) { - echo "

" . lang('Foreign keys') . "

\n"; - echo "\n"; - foreach ($foreign_keys as $foreign_key) { - echo "\n"; + mysql_free_result($result); + echo '

' . lang('Alter table') . "

\n"; + + echo "

" . lang('Indexes') . "

\n"; + $indexes = indexes($_GET["table"]); + if ($indexes) { + echo "
" . implode(", ", $foreign_key[2]) . "" . (strlen($foreign_key[0]) && $foreign_key[0] !== $_GET["db"] ? "" . htmlspecialchars($foreign_key[0]) . "." : "") . htmlspecialchars($foreign_key[1]) . "(" . implode(", ", $foreign_key[3]) . ")
\n"; + foreach ($indexes as $index) { + ksort($index["columns"]); + echo "\n"; + } + echo "
$index[type]" . implode(", ", $index["columns"]) . "
\n"; + } + echo '

' . lang('Alter indexes') . "

\n"; + + $foreign_keys = foreign_keys($_GET["table"]); + if ($foreign_keys) { + echo "

" . lang('Foreign keys') . "

\n"; + echo "\n"; + foreach ($foreign_keys as $foreign_key) { + echo "\n"; + } + echo "
" . implode(", ", $foreign_key[2]) . "" . (strlen($foreign_key[0]) && $foreign_key[0] !== $_GET["db"] ? "" . htmlspecialchars($foreign_key[0]) . "." : "") . htmlspecialchars($foreign_key[1]) . "(" . implode(", ", $foreign_key[3]) . ")
\n"; } - echo "\n"; } if (mysql_get_server_info() >= 5) {