PageRenderTime 51ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/system/application/third_party/adminer/adminer/sql.inc.php

https://gitlab.com/sylver.gocloud/gocloudasia-college-system-framework
PHP | 238 lines | 214 code | 22 blank | 2 comment | 75 complexity | 4b7bb1652c1e5c29c199afee45acedff MD5 | raw file
  1. <?php
  2. if (!$error && $_POST["export"]) {
  3. dump_headers("sql");
  4. $adminer->dumpTable("", "");
  5. $adminer->dumpData("", "table", $_POST["query"]);
  6. exit;
  7. }
  8. restart_session();
  9. $history_all = &get_session("queries");
  10. $history = &$history_all[DB];
  11. if (!$error && $_POST["clear"]) {
  12. $history = array();
  13. redirectx(remove_from_uri("history"));
  14. }
  15. page_header((isset($_GET["import"]) ? lang('Import') : lang('SQL command')), $error);
  16. if (!$error && $_POST) {
  17. $fp = false;
  18. if (!isset($_GET["import"])) {
  19. $query = $_POST["query"];
  20. } elseif ($_POST["webfile"]) {
  21. $fp = @fopen((file_exists("adminer.sql")
  22. ? "adminer.sql"
  23. : "compress.zlib://adminer.sql.gz"
  24. ), "rb");
  25. $query = ($fp ? fread($fp, 1e6) : false);
  26. } else {
  27. $query = get_file("sql_file", true);
  28. }
  29. if (is_string($query)) { // get_file() returns error as number, fread() as false
  30. if (function_exists('memory_get_usage')) {
  31. @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
  32. }
  33. if ($query != "" && strlen($query) < 1e6) { // don't add big queries
  34. $q = $query . (preg_match("~;[ \t\r\n]*\$~", $query) ? "" : ";"); //! doesn't work with DELIMITER |
  35. if (!$history || reset(end($history)) != $q) { // no repeated queries
  36. restart_session();
  37. $history[] = array($q, time()); //! add elapsed time
  38. set_session("queries", $history_all); // required because reference is unlinked by stop_session()
  39. stop_session();
  40. }
  41. }
  42. $space = "(?:\\s|/\\*.*\\*/|(?:#|-- )[^\n]*\n|--\r?\n)";
  43. $delimiter = ";";
  44. $offset = 0;
  45. $empty = true;
  46. $connection2 = connect(); // connection for exploring indexes and EXPLAIN (to not replace FOUND_ROWS()) //! PDO - silent error
  47. if (is_object($connection2) && DB != "") {
  48. $connection2->select_db(DB);
  49. }
  50. $commands = 0;
  51. $errors = array();
  52. $line = 0;
  53. $parse = '[\'"' . ($jush == "sql" ? '`#' : ($jush == "sqlite" ? '`[' : ($jush == "mssql" ? '[' : ''))) . ']|/\\*|-- |$' . ($jush == "pgsql" ? '|\\$[^$]*\\$' : '');
  54. $total_start = microtime(true);
  55. parse_str($_COOKIE["adminer_export"], $adminer_export);
  56. $dump_format = $adminer->dumpFormat();
  57. unset($dump_format["sql"]);
  58. while ($query != "") {
  59. if (!$offset && preg_match("~^$space*DELIMITER\\s+(\\S+)~i", $query, $match)) {
  60. $delimiter = $match[1];
  61. $query = substr($query, strlen($match[0]));
  62. } else {
  63. preg_match('(' . preg_quote($delimiter) . "\\s*|$parse)", $query, $match, PREG_OFFSET_CAPTURE, $offset); // should always match
  64. list($found, $pos) = $match[0];
  65. if (!$found && $fp && !feof($fp)) {
  66. $query .= fread($fp, 1e5);
  67. } else {
  68. if (!$found && rtrim($query) == "") {
  69. break;
  70. }
  71. $offset = $pos + strlen($found);
  72. if ($found && rtrim($found) != $delimiter) { // find matching quote or comment end
  73. while (preg_match('(' . ($found == '/*' ? '\\*/' : ($found == '[' ? ']' : (preg_match('~^-- |^#~', $found) ? "\n" : preg_quote($found) . "|\\\\."))) . '|$)s', $query, $match, PREG_OFFSET_CAPTURE, $offset)) { //! respect sql_mode NO_BACKSLASH_ESCAPES
  74. $s = $match[0][0];
  75. if (!$s && $fp && !feof($fp)) {
  76. $query .= fread($fp, 1e5);
  77. } else {
  78. $offset = $match[0][1] + strlen($s);
  79. if ($s[0] != "\\") {
  80. break;
  81. }
  82. }
  83. }
  84. } else { // end of a query
  85. $empty = false;
  86. $q = substr($query, 0, $pos);
  87. $commands++;
  88. $print = "<pre id='sql-$commands'><code class='jush-$jush'>" . shorten_utf8(trim($q), 1000) . "</code></pre>\n";
  89. if (!$_POST["only_errors"]) {
  90. echo $print;
  91. ob_flush();
  92. flush(); // can take a long time - show the running query
  93. }
  94. $start = microtime(true);
  95. //! don't allow changing of character_set_results, convert encoding of displayed query
  96. if ($connection->multi_query($q) && is_object($connection2) && preg_match("~^$space*USE\\b~isU", $q)) {
  97. $connection2->query($q);
  98. }
  99. do {
  100. $result = $connection->store_result();
  101. $time = " <span class='time'>(" . format_time($start) . ")</span>"
  102. . (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
  103. ;
  104. if ($connection->error) {
  105. echo ($_POST["only_errors"] ? $print : "");
  106. echo "<p class='error'>" . lang('Error in query') . ($connection->errno ? " ($connection->errno)" : "") . ": " . error() . "\n";
  107. $errors[] = " <a href='#sql-$commands'>$commands</a>";
  108. if ($_POST["error_stops"]) {
  109. break 2;
  110. }
  111. } elseif (is_object($result)) {
  112. $orgtables = select($result, $connection2);
  113. if (!$_POST["only_errors"]) {
  114. echo "<form action='' method='post'>\n";
  115. echo "<p>" . ($result->num_rows ? lang('%d row(s)', $result->num_rows) : "") . $time;
  116. $id = "export-$commands";
  117. $export = ", <a href='#$id' onclick=\"return !toggle('$id');\">" . lang('Export') . "</a><span id='$id' class='hidden'>: "
  118. . html_select("output", $adminer->dumpOutput(), $adminer_export["output"]) . " "
  119. . html_select("format", $dump_format, $adminer_export["format"])
  120. . "<input type='hidden' name='query' value='" . h($q) . "'>"
  121. . " <input type='submit' name='export' value='" . lang('Export') . "'><input type='hidden' name='token' value='$token'></span>\n"
  122. ;
  123. if ($connection2 && preg_match("~^($space|\\()*SELECT\\b~isU", $q) && ($explain = explain($connection2, $q))) {
  124. $id = "explain-$commands";
  125. echo ", <a href='#$id' onclick=\"return !toggle('$id');\">EXPLAIN</a>$export";
  126. echo "<div id='$id' class='hidden'>\n";
  127. select($explain, $connection2, $orgtables);
  128. echo "</div>\n";
  129. } else {
  130. echo $export;
  131. }
  132. echo "</form>\n";
  133. }
  134. } else {
  135. if (preg_match("~^$space*(CREATE|DROP|ALTER)$space+(DATABASE|SCHEMA)\\b~isU", $q)) {
  136. restart_session();
  137. set_session("dbs", null); // clear cache
  138. stop_session();
  139. }
  140. if (!$_POST["only_errors"]) {
  141. echo "<p class='message' title='" . h($connection->info) . "'>" . lang('Query executed OK, %d row(s) affected.', $connection->affected_rows) . "$time\n";
  142. }
  143. }
  144. $start = microtime(true);
  145. } while ($connection->next_result());
  146. $line += substr_count($q.$found, "\n");
  147. $query = substr($query, $offset);
  148. $offset = 0;
  149. }
  150. }
  151. }
  152. }
  153. if ($empty) {
  154. echo "<p class='message'>" . lang('No commands to execute.') . "\n";
  155. } elseif ($_POST["only_errors"]) {
  156. echo "<p class='message'>" . lang('%d query(s) executed OK.', $commands - count($errors));
  157. echo " <span class='time'>(" . format_time($total_start) . ")</span>\n";
  158. } elseif ($errors && $commands > 1) {
  159. echo "<p class='error'>" . lang('Error in query') . ": " . implode("", $errors) . "\n";
  160. }
  161. //! MS SQL - SET SHOWPLAN_ALL OFF
  162. } else {
  163. echo "<p class='error'>" . upload_error($query) . "\n";
  164. }
  165. }
  166. ?>
  167. <form action="" method="post" enctype="multipart/form-data" id="form">
  168. <?php
  169. $execute = "<input type='submit' value='" . lang('Execute') . "' title='Ctrl+Enter'>";
  170. if (!isset($_GET["import"])) {
  171. $q = $_GET["sql"]; // overwrite $q from if ($_POST) to save memory
  172. if ($_POST) {
  173. $q = $_POST["query"];
  174. } elseif ($_GET["history"] == "all") {
  175. $q = $history;
  176. } elseif ($_GET["history"] != "") {
  177. $q = $history[$_GET["history"]][0];
  178. }
  179. echo "<p>";
  180. textarea("query", $q, 20);
  181. echo ($_POST ? "" : "<script type='text/javascript'>focus(document.getElementsByTagName('textarea')[0]);</script>\n");
  182. echo "<p>$execute\n";
  183. } else {
  184. echo "<fieldset><legend>" . lang('File upload') . "</legend><div>";
  185. echo (ini_bool("file_uploads")
  186. ? "SQL (&lt; " . ini_get("upload_max_filesize") . "B): <input type='file' name='sql_file[]' multiple>\n$execute" // ignore post_max_size because it is for all form fields together and bytes computing would be necessary
  187. : lang('File uploads are disabled.')
  188. );
  189. echo "</div></fieldset>\n";
  190. echo "<fieldset><legend>" . lang('From server') . "</legend><div>";
  191. echo lang('Webserver file %s', "<code>adminer.sql" . (extension_loaded("zlib") ? "[.gz]" : "") . "</code>");
  192. echo ' <input type="submit" name="webfile" value="' . lang('Run file') . '">';
  193. echo "</div></fieldset>\n";
  194. echo "<p>";
  195. }
  196. echo checkbox("error_stops", 1, ($_POST ? $_POST["error_stops"] : isset($_GET["import"])), lang('Stop on error')) . "\n";
  197. echo checkbox("only_errors", 1, ($_POST ? $_POST["only_errors"] : isset($_GET["import"])), lang('Show only errors')) . "\n";
  198. echo "<input type='hidden' name='token' value='$token'>\n";
  199. if (!isset($_GET["import"]) && $history) {
  200. print_fieldset("history", lang('History'), $_GET["history"] != "");
  201. for ($val = end($history); $val; $val = prev($history)) { // not array_reverse() to save memory
  202. $key = key($history);
  203. list($q, $time, $elapsed) = $val;
  204. echo '<a href="' . h(ME . "sql=&history=$key") . '">' . lang('Edit') . "</a>"
  205. . " <span class='time' title='" . @date('Y-m-d', $time) . "'>" . @date("H:i:s", $time) . "</span>" // @ - time zone may be not set
  206. . " <code class='jush-$jush'>" . shorten_utf8(ltrim(str_replace("\n", " ", str_replace("\r", "", preg_replace('~^(#|-- ).*~m', '', $q)))), 80, "</code>")
  207. . ($elapsed ? " <span class='time'>($elapsed)</span>" : "")
  208. . "<br>\n"
  209. ;
  210. }
  211. echo "<input type='submit' name='clear' value='" . lang('Clear') . "'>\n";
  212. echo "<a href='" . h(ME . "sql=&history=all") . "'>" . lang('Edit all') . "</a>\n";
  213. echo "</div></fieldset>\n";
  214. }
  215. ?>
  216. </form>