Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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