Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 214 lines (205 sloc) 9.507 kb
667bfec Decomposition
jakubvrana authored
1 <?php
15715b3 @vrana Export SQL command result (bug #3116854)
authored
2 if (!$error && $_POST["export"]) {
8ab6fff @vrana Centralize dump_headers
authored
3 dump_headers("sql");
15715b3 @vrana Export SQL command result (bug #3116854)
authored
4 $adminer->dumpTable("", "");
5 $adminer->dumpData("", "table", $_POST["query"]);
6 exit;
7 }
8
be49e08 Improve session restarting
jakubvrana authored
9 restart_session();
49565e0 @vrana Rename variables to avoid conflict with Adminer 2 sessions and enabled r...
authored
10 $history_all = &get_session("queries");
3f5b683 Reintegrate sqlite branch
jakubvrana authored
11 $history = &$history_all[DB];
5a0d809 Clear history (thanks to paranoiq)
jakubvrana authored
12 if (!$error && $_POST["clear"]) {
13 $history = array();
14 redirect(remove_from_uri("history"));
15 }
16
a71b990 @vrana Remove CodeMirror
authored
17 page_header(lang('SQL command'), $error);
667bfec Decomposition
jakubvrana authored
18
4bee1c0 Upload error only if not other error
jakubvrana authored
19 if (!$error && $_POST) {
03ba5f5 Read adminer.sql progressively to save memory
jakubvrana authored
20 $fp = false;
1f09dd3 Save memory
jakubvrana authored
21 $query = $_POST["query"];
22 if ($_POST["webfile"]) {
03ba5f5 Read adminer.sql progressively to save memory
jakubvrana authored
23 $fp = @fopen((file_exists("adminer.sql") ? "adminer.sql"
0a2d81d Bzip2 import
jakubvrana authored
24 : (file_exists("adminer.sql.gz") ? "compress.zlib://adminer.sql.gz"
25 : "compress.bzip2://adminer.sql.bz2"
03ba5f5 Read adminer.sql progressively to save memory
jakubvrana authored
26 )), "rb");
27 $query = ($fp ? fread($fp, 1e6) : false);
9d944c8 @vrana Respect original memory_limit
authored
28 } elseif ($_FILES && $_FILES["sql_file"]["error"] != UPLOAD_ERR_NO_FILE) {
25f01d3 Compress export and import
jakubvrana authored
29 $query = get_file("sql_file", true);
1f09dd3 Save memory
jakubvrana authored
30 }
03ba5f5 Read adminer.sql progressively to save memory
jakubvrana authored
31 if (is_string($query)) { // get_file() returns error as number, fread() as false
2211258 Check memory_get_usage availability (bug #2957708)
jakubvrana authored
32 if (function_exists('memory_get_usage')) {
9d944c8 @vrana Respect original memory_limit
authored
33 @ini_set("memory_limit", max(ini_bytes("memory_limit"), 2 * strlen($query) + memory_get_usage() + 8e6)); // @ - may be disabled, 2 - substr and trim, 8e6 - other variables
2211258 Check memory_get_usage availability (bug #2957708)
jakubvrana authored
34 }
2e6ad20 @vrana Display ; in history (thanks to Jan Cerny)
authored
35 if ($query != "" && strlen($query) < 1e6) { // don't add big queries
998e2f5 @vrana Ignore whitespace after semicolon
authored
36 $q = $query . (ereg(";[ \t\r\n]*\$", $query) ? "" : ";"); //! doesn't work with DELIMITER |
24c51fe @vrana Print current time next to executed SQL queries
authored
37 if (!$history || reset(end($history)) != $q) { // no repeated queries
38 $history[] = array($q, time());
2e6ad20 @vrana Display ; in history (thanks to Jan Cerny)
authored
39 }
9ef7666 Save to history before session close
jakubvrana authored
40 }
3535d0a @vrana Parse comment before delimiter
authored
41 $space = "(?:\\s|/\\*.*\\*/|(?:#|-- )[^\n]*\n|--\n)";
4bee1c0 Upload error only if not other error
jakubvrana authored
42 $delimiter = ";";
43 $offset = 0;
44 $empty = true;
3f5b683 Reintegrate sqlite branch
jakubvrana authored
45 $connection2 = connect(); // connection for exploring indexes and EXPLAIN (to not replace FOUND_ROWS()) //! PDO - silent error
46 if (is_object($connection2) && DB != "") {
64d616c Rename get_dbh to connection
jakubvrana authored
47 $connection2->select_db(DB);
fbebb08 Use separate connection for exploring indexes
jakubvrana authored
48 }
49565e0 @vrana Rename variables to avoid conflict with Adminer 2 sessions and enabled r...
authored
49 $commands = 0;
18e158f @vrana Option to show only errors in SQL command
authored
50 $errors = array();
047ce18 @vrana Highlight line with error in SQL command
authored
51 $line = 0;
323f4ab @vrana MySQL specific characters
authored
52 $parse = '[\'"' . ($jush == "sql" ? '`#' : ($jush == "sqlite" ? '`[' : ($jush == "mssql" ? '[' : ''))) . ']|/\\*|-- |$' . ($jush == "pgsql" ? '|\\$[^$]*\\$' : '');
0060c60 @vrana Save bytes
authored
53 $total_start = microtime();
15715b3 @vrana Export SQL command result (bug #3116854)
authored
54 parse_str($_COOKIE["adminer_export"], $adminer_export);
55 $dump_format = $adminer->dumpFormat();
56 unset($dump_format["sql"]);
7352c28 Replace strlen() by != ""
jakubvrana authored
57 while ($query != "") {
7fcfb0d @vrana Don't include trailing space to delimiter
authored
58 if (!$offset && preg_match("~^$space*DELIMITER\\s+(\\S+)~i", $query, $match)) {
d06a9ad Escape delimiter in right place (thanks to Peter R)
jakubvrana authored
59 $delimiter = $match[1];
4bee1c0 Upload error only if not other error
jakubvrana authored
60 $query = substr($query, strlen($match[0]));
1ccc5c2 Fix long SQL query crash (bug #2839231)
jakubvrana authored
61 } else {
047ce18 @vrana Highlight line with error in SQL command
authored
62 preg_match('(' . preg_quote($delimiter) . "\\s*|$parse)", $query, $match, PREG_OFFSET_CAPTURE, $offset); // should always match
63 list($found, $pos) = $match[0];
03ba5f5 Read adminer.sql progressively to save memory
jakubvrana authored
64 if (!$found && $fp && !feof($fp)) {
e40612a Use radio in export
jakubvrana authored
65 $query .= fread($fp, 1e5);
03ba5f5 Read adminer.sql progressively to save memory
jakubvrana authored
66 } else {
7352c28 Replace strlen() by != ""
jakubvrana authored
67 if (!$found && rtrim($query) == "") {
03ba5f5 Read adminer.sql progressively to save memory
jakubvrana authored
68 break;
69 }
b16bb54 @vrana Save assignment if not necessary
authored
70 $offset = $pos + strlen($found);
047ce18 @vrana Highlight line with error in SQL command
authored
71 if ($found && rtrim($found) != $delimiter) { // find matching quote or comment end
b69f9b4 @vrana Parse MS SQL identifiers
authored
72 while (preg_match('(' . ($found == '/*' ? '\\*/' : ($found == '[' ? ']' : (ereg('^-- |^#', $found) ? "\n" : preg_quote($found) . "|\\\\."))) . '|$)s', $query, $match, PREG_OFFSET_CAPTURE, $offset)) { //! respect sql_mode NO_BACKSLASH_ESCAPES
18e158f @vrana Option to show only errors in SQL command
authored
73 $s = $match[0][0];
74 if (!$s && $fp && !feof($fp)) {
f0d0bdb @vrana Parse 'a\';' on webserver file separation
authored
75 $query .= fread($fp, 1e5);
dd85aa5 @vrana Error in case of found string at end of chunk (thanks to simonik)
authored
76 } else {
77 $offset = $match[0][1] + strlen($s);
78 if ($s[0] != "\\") {
79 break;
80 }
18e158f @vrana Option to show only errors in SQL command
authored
81 }
82 }
83 } else { // end of a query
03ba5f5 Read adminer.sql progressively to save memory
jakubvrana authored
84 $empty = false;
047ce18 @vrana Highlight line with error in SQL command
authored
85 $q = substr($query, 0, $pos);
49565e0 @vrana Rename variables to avoid conflict with Adminer 2 sessions and enabled r...
authored
86 $commands++;
9802869 @vrana Merge from master
authored
87 $print = "<pre id='sql-$commands'><code class='jush-$jush'>" . shorten_utf8(trim($q), 1000) . "</code></pre>\n";
18e158f @vrana Option to show only errors in SQL command
authored
88 if (!$_POST["only_errors"]) {
89 echo $print;
90 ob_flush();
91 flush(); // can take a long time - show the running query
92 }
0060c60 @vrana Save bytes
authored
93 $start = microtime(); // microtime(true) is available since PHP 5
03ba5f5 Read adminer.sql progressively to save memory
jakubvrana authored
94 //! don't allow changing of character_set_results, convert encoding of displayed query
5576d84 @vrana Display "EXPLAIN SELECT invalid_column" error (thanks to Michal Vrabel)
authored
95 if ($connection->multi_query($q) && is_object($connection2) && preg_match("~^$space*USE\\b~isU", $q)) {
96 $connection2->query($q);
97 }
98 do {
99 $result = $connection->store_result();
0060c60 @vrana Save bytes
authored
100 $end = microtime();
5576d84 @vrana Display "EXPLAIN SELECT invalid_column" error (thanks to Michal Vrabel)
authored
101 $time = format_time($start, $end) . (strlen($q) < 1000 ? " <a href='" . h(ME) . "sql=" . urlencode(trim($q)) . "'>" . lang('Edit') . "</a>" : ""); // 1000 - maximum length of encoded URL in IE is 2083 characters
102 if ($connection->error) {
103 echo ($_POST["only_errors"] ? $print : "");
104 echo "<p class='error'>" . lang('Error in query') . ": " . error() . "\n";
105 $errors[] = " <a href='#sql-$commands'>$commands</a>";
106 if ($_POST["error_stops"]) {
52c15bf @vrana Fix Stop on error
authored
107 break 2;
5576d84 @vrana Display "EXPLAIN SELECT invalid_column" error (thanks to Michal Vrabel)
authored
108 }
109 } elseif (is_object($result)) {
48df7aa @vrana Link to original table in EXPLAIN of SELECT * FROM table t
authored
110 $orgtables = select($result, $connection2);
5576d84 @vrana Display "EXPLAIN SELECT invalid_column" error (thanks to Michal Vrabel)
authored
111 if (!$_POST["only_errors"]) {
112 echo "<form action='' method='post'>\n";
113 echo "<p>" . ($result->num_rows ? lang('%d row(s)', $result->num_rows) : "") . $time;
114 $id = "export-$commands";
115 $export = ", <a href='#$id' onclick=\"return !toggle('$id');\">" . lang('Export') . "</a><span id='$id' class='hidden'>: "
116 . html_select("output", $adminer->dumpOutput(), $adminer_export["output"]) . " "
117 . html_select("format", $dump_format, $adminer_export["format"])
118 . "<input type='hidden' name='query' value='" . h($q) . "'>"
f498219 @vrana Remove eventStop() used by AJAXification in past
authored
119 . " <input type='submit' name='export' value='" . lang('Export') . "'><input type='hidden' name='token' value='$token'></span>\n"
5576d84 @vrana Display "EXPLAIN SELECT invalid_column" error (thanks to Michal Vrabel)
authored
120 ;
121 if ($connection2 && preg_match("~^($space|\\()*SELECT\\b~isU", $q) && ($explain = explain($connection2, $q))) {
122 $id = "explain-$commands";
6c1598e @vrana Save bytes
authored
123 echo ", <a href='#$id' onclick=\"return !toggle('$id');\">EXPLAIN</a>$export";
5576d84 @vrana Display "EXPLAIN SELECT invalid_column" error (thanks to Michal Vrabel)
authored
124 echo "<div id='$id' class='hidden'>\n";
48df7aa @vrana Link to original table in EXPLAIN of SELECT * FROM table t
authored
125 select($explain, $connection2, ($jush == "sql" ? "http://dev.mysql.com/doc/refman/" . substr($connection->server_info, 0, 3) . "/en/explain-output.html#explain_" : ""), $orgtables);
5576d84 @vrana Display "EXPLAIN SELECT invalid_column" error (thanks to Michal Vrabel)
authored
126 echo "</div>\n";
127 } else {
6c1598e @vrana Save bytes
authored
128 echo $export;
380b1da Display EXPLAIN in SQL query
jakubvrana authored
129 }
5576d84 @vrana Display "EXPLAIN SELECT invalid_column" error (thanks to Michal Vrabel)
authored
130 echo "</form>\n";
131 }
132 } else {
133 if (preg_match("~^$space*(CREATE|DROP|ALTER)$space+(DATABASE|SCHEMA)\\b~isU", $q)) {
134 restart_session();
135 set_session("dbs", null); // clear cache
5e0038b @vrana Improve session management
authored
136 stop_session();
5576d84 @vrana Display "EXPLAIN SELECT invalid_column" error (thanks to Michal Vrabel)
authored
137 }
138 if (!$_POST["only_errors"]) {
139 echo "<p class='message' title='" . h($connection->info) . "'>" . lang('Query executed OK, %d row(s) affected.', $connection->affected_rows) . "$time\n";
4bee1c0 Upload error only if not other error
jakubvrana authored
140 }
8c10fc0 @vrana Don't print error message with no error (bug #3118538)
authored
141 }
5576d84 @vrana Display "EXPLAIN SELECT invalid_column" error (thanks to Michal Vrabel)
authored
142 $start = $end;
143 } while ($connection->next_result());
047ce18 @vrana Highlight line with error in SQL command
authored
144 $line += substr_count($q.$found, "\n");
03ba5f5 Read adminer.sql progressively to save memory
jakubvrana authored
145 $query = substr($query, $offset);
146 $offset = 0;
1f09dd3 Save memory
jakubvrana authored
147 }
ae372aa Database abstraction
jakubvrana authored
148 }
667bfec Decomposition
jakubvrana authored
149 }
150 }
4bee1c0 Upload error only if not other error
jakubvrana authored
151 if ($empty) {
ace55ed HTML instead of XHTML
jakubvrana authored
152 echo "<p class='message'>" . lang('No commands to execute.') . "\n";
18e158f @vrana Option to show only errors in SQL command
authored
153 } elseif ($_POST["only_errors"]) {
0060c60 @vrana Save bytes
authored
154 echo "<p class='message'>" . lang('%d query(s) executed OK.', $commands - count($errors)) . format_time($total_start, microtime()) . "\n";
18e158f @vrana Option to show only errors in SQL command
authored
155 } elseif ($errors && $commands > 1) {
156 echo "<p class='error'>" . lang('Error in query') . ": " . implode("", $errors) . "\n";
4bee1c0 Upload error only if not other error
jakubvrana authored
157 }
3f5b683 Reintegrate sqlite branch
jakubvrana authored
158 //! MS SQL - SET SHOWPLAN_ALL OFF
4bee1c0 Upload error only if not other error
jakubvrana authored
159 } else {
e2a03c0 Show upload_max_filesize
jakubvrana authored
160 echo "<p class='error'>" . upload_error($query) . "\n";
667bfec Decomposition
jakubvrana authored
161 }
162 }
163 ?>
73e8631 Move stuff to functions.inc.php
jakubvrana authored
164
d9c2e80 @vrana Show only errors with Webserver file
authored
165 <form action="" method="post" enctype="multipart/form-data" id="form">
6fffbbd Disable spell checking in SQL textareas
jakubvrana authored
166 <p><?php
fc23e5a Comment
jakubvrana authored
167 $q = $_GET["sql"]; // overwrite $q from if ($_POST) to save memory
eb2a39c Set memory_limit (thanks to kahi)
jakubvrana authored
168 if ($_POST) {
169 $q = $_POST["query"];
ddaa894 @vrana History: edit all
authored
170 } elseif ($_GET["history"] == "all") {
ef9d6d4 @vrana Save memory
authored
171 $q = $history;
7352c28 Replace strlen() by != ""
jakubvrana authored
172 } elseif ($_GET["history"] != "") {
24c51fe @vrana Print current time next to executed SQL queries
authored
173 $q = $history[$_GET["history"]][0];
eb2a39c Set memory_limit (thanks to kahi)
jakubvrana authored
174 }
a71b990 @vrana Remove CodeMirror
authored
175 textarea("query", $q, 20);
176 echo ($_POST ? "" : "<script type='text/javascript'>document.getElementsByTagName('textarea')[0].focus();</script>\n");
63f1972 @vrana Show only errors with file upload by default
authored
177 echo "<p>" . (ini_bool("file_uploads")
178 ? lang('File upload') . ': <input type="file" name="sql_file"' . ($_FILES && $_FILES["sql_file"]["error"] != 4 ? '' : ' onchange="this.form[\'only_errors\'].checked = true;"') . '> (&lt; ' . ini_get("upload_max_filesize") . 'B)' // ignore post_max_size because it is for all form fields together and bytes computing would be necessary
179 : lang('File uploads are disabled.')
180 );
9929cc6 @vrana Allow to insert Tab in SQL textareas and send the form by Ctrl+Enter
authored
181
6fffbbd Disable spell checking in SQL textareas
jakubvrana authored
182 ?>
de5e18c Syntax highlighting
jakubvrana authored
183 <p>
2e284c9 @vrana Show keyboard shortcut (thanks to Ondrej Valka)
authored
184 <input type="submit" value="<?php echo lang('Execute'); ?>" title="Ctrl+Enter">
740ae10 @vrana Don't send incomplete forms
authored
185 <input type="hidden" name="token" value="<?php echo $token; ?>">
430034c Cottage homework
jakubvrana authored
186 <?php
b3c8d71 @vrana Print data in only_errors mode (important for ALTER export)
authored
187 echo checkbox("error_stops", 1, $_POST["error_stops"], lang('Stop on error')) . "\n";
188 echo checkbox("only_errors", 1, $_POST["only_errors"], lang('Show only errors')) . "\n";
cd07584 Save SQL query to history
jakubvrana authored
189
d9c2e80 @vrana Show only errors with Webserver file
authored
190 print_fieldset("webfile", lang('From server'), $_POST["webfile"], "document.getElementById('form')['only_errors'].checked = true; ");
8625e91 Declare support for adminer.sql.gz
jakubvrana authored
191 $compress = array();
622531f PHP 4 compatibility (bug #2948187)
jakubvrana authored
192 foreach (array("gz" => "zlib", "bz2" => "bz2") as $key => $val) {
193 if (extension_loaded($val)) {
8625e91 Declare support for adminer.sql.gz
jakubvrana authored
194 $compress[] = ".$key";
195 }
196 }
197 echo lang('Webserver file %s', "<code>adminer.sql" . ($compress ? "[" . implode("|", $compress) . "]" : "") . "</code>");
4fe9b60 @vrana Don't send SQL commands by AJAX (can change navigation and flushes outpu...
authored
198 echo ' <input type="submit" name="webfile" value="' . lang('Run file') . '">';
8f0c260 @vrana Simplify SQL command user interface
authored
199 echo "</div></fieldset>\n";
b2a7c7a Execute SQL file stored on server disk
jakubvrana authored
200
cd07584 Save SQL query to history
jakubvrana authored
201 if ($history) {
5f6594e Collapse history
jakubvrana authored
202 print_fieldset("history", lang('History'), $_GET["history"] != "");
cd07584 Save SQL query to history
jakubvrana authored
203 foreach ($history as $key => $val) {
24c51fe @vrana Print current time next to executed SQL queries
authored
204 list($q, $time) = $val;
205 echo '<a href="' . h(ME . "sql=&history=$key") . '">' . lang('Edit') . "</a> <span class='time'>" . @date("H:i:s", $time) . "</span> <code class='jush-$jush'>" . shorten_utf8(ltrim(str_replace("\n", " ", str_replace("\r", "", preg_replace('~^(#|-- ).*~m', '', $q)))), 80, "</code>") . "<br>\n"; // @ - time zone may be not set
cd07584 Save SQL query to history
jakubvrana authored
206 }
5a0d809 Clear history (thanks to paranoiq)
jakubvrana authored
207 echo "<input type='submit' name='clear' value='" . lang('Clear') . "'>\n";
04e5250 @vrana Better translation
authored
208 echo "<a href='" . h(ME . "sql=&history=all") . "'>" . lang('Edit all') . "</a>\n";
5f6594e Collapse history
jakubvrana authored
209 echo "</div></fieldset>\n";
cd07584 Save SQL query to history
jakubvrana authored
210 }
211 ?>
212
667bfec Decomposition
jakubvrana authored
213 </form>
Something went wrong with that request. Please try again.