Advertisement
Guest User

cisty kod

a guest
Mar 17th, 2011
1,260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.67 KB | None | 0 0
  1. if (!$error && $_POST) {
  2.     $fp = false;
  3.     $query = $_POST["query"];
  4.     if ($_POST["webfile"]) {
  5.         $fp = @fopen((file_exists("adminer.sql") ? "adminer.sql"
  6.             : (file_exists("adminer.sql.gz") ? "compress.zlib://adminer.sql.gz"
  7.             : "compress.bzip2://adminer.sql.bz2"
  8.         )), "rb");
  9.         $query = ($fp ? fread($fp, 1e6) : false);
  10.     } elseif ($_FILES && $_FILES["sql_file"]["error"] != 4) { // 4 - UPLOAD_ERR_NO_FILE
  11.         $query = get_file("sql_file", true);
  12.     }
  13.     if (is_string($query)) { // get_file() returns error as number, fread() as false
  14.         if (function_exists('memory_get_usage')) {
  15.             @ini_set("memory_limit", 2 * strlen($query) + memory_get_usage() + 8e6); // @ - may be disabled, 2 - substr and trim, 8e6 - other variables
  16.         }
  17.         if ($query != "" && strlen($query) < 1e6 && (!$history || end($history) != $query)) { // don't add repeated and big queries
  18.             $history[] = $query;
  19.         }
  20.         $space = "(\\s|/\\*.*\\*/|(#|-- )[^\n]*\n|--\n)";
  21.         if (!ini_bool("session.use_cookies")) {
  22.             session_write_close();
  23.         }
  24.         $delimiter = ";";
  25.         $offset = 0;
  26.         $empty = true;
  27.         $connection2 = connect(); // connection for exploring indexes and EXPLAIN (to not replace FOUND_ROWS()) //! PDO - silent error
  28.         if (is_object($connection2) && DB != "") {
  29.             $connection2->select_db(DB);
  30.         }
  31.         $commands = 0;
  32.         $errors = array();
  33.         $parse = '[\'`"]' . ($jush == "pgsql" ? '|\\$[^$]*\\$' : ($jush == "mssql" || $jush == "sqlite" ? '|\\[' : '')) . '|/\\*|-- |#'; //! ` and # not everywhere
  34.         $total_start = explode(" ", microtime());
  35.         parse_str($_COOKIE["adminer_export"], $adminer_export);
  36.         $dump_format = $adminer->dumpFormat();
  37.         unset($dump_format["sql"]);
  38.         while ($query != "") {
  39.             if (!$offset && $jush == "sql" && preg_match('~^\\s*DELIMITER\\s+(.+)~i', $query, $match)) {
  40.                 $delimiter = $match[1];
  41.                 $query = substr($query, strlen($match[0]));
  42.             } else {
  43.                 preg_match('(' . preg_quote($delimiter) . "|$parse|\$)", $query, $match, PREG_OFFSET_CAPTURE, $offset); // should always match
  44.                 $found = $match[0][0];
  45.                 $offset = $match[0][1] + strlen($found);
  46.                 if (!$found && $fp && !feof($fp)) {
  47.                     $query .= fread($fp, 1e5);
  48.                 } else {
  49.                     if (!$found && rtrim($query) == "") {
  50.                         break;
  51.                     }
  52.                     if ($found && $found != $delimiter) { // find matching quote or comment end
  53.                         while (preg_match('(' . ($found == '/*' ? '\\*/' : ($found == '[' ? ']' : (ereg('^-- |^#', $found) ? "\n" : preg_quote($found) . "|\\\\."))) . '|$)s', $query, $match, PREG_OFFSET_CAPTURE, $offset)) { //! respect sql_mode NO_BACKSLASH_ESCAPES
  54.                             $s = $match[0][0];
  55.                             $offset = $match[0][1] + strlen($s);
  56.                             if (!$s && $fp && !feof($fp)) {
  57.                                 $query .= fread($fp, 1e6);
  58.                             } elseif ($s[0] != "\\") {
  59.                                 break;
  60.                             }
  61.                         }
  62.                     } else { // end of a query
  63.                         $empty = false;
  64.                         $q = substr($query, 0, $match[0][1]);
  65.                         $commands++;
  66.                         $print = "<pre id='sql-$commands'><code class='jush-$jush'>" . shorten_utf8(trim($q), 1000) . "</code></pre>\n";
  67.                         if (!$_POST["only_errors"]) {
  68.                             echo $print;
  69.                             ob_flush();
  70.                             flush(); // can take a long time - show the running query
  71.                         }
  72.                         $start = explode(" ", microtime()); // microtime(true) is available since PHP 5
  73.                         //! don't allow changing of character_set_results, convert encoding of displayed query
  74.                         if ($connection->multi_query($q)) {
  75.                             if (is_object($connection2) && preg_match("~^$space*(USE)\\b~isU", $q)) {
  76.                                 $connection2->query($q);
  77.                             }
  78.                             do {
  79.                                 $result = $connection->store_result();
  80.                                 $end = explode(" ", microtime());
  81.                                 $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
  82.                                 if (!is_object($result)) {
  83.                                     if (preg_match("~^$space*(CREATE|DROP|ALTER)$space+(DATABASE|SCHEMA)\\b~isU", $q)) {
  84.                                         restart_session();
  85.                                         set_session("dbs", null); // clear cache
  86.                                         session_write_close();
  87.                                     }
  88.                                     if (!$_POST["only_errors"]) {
  89.                                         echo "<p class='message' title='" . h($connection->info) . "'>" . lang('Query executed OK, %d row(s) affected.', $connection->affected_rows) . "$time\n";
  90.                                     }
  91.                                 } else {
  92.                                     if ($_POST["only_errors"]) {
  93.                                         echo $print;
  94.                                         $print = "";
  95.                                     }
  96.                                     select($result, $connection2);
  97.                                     echo "<form action='' method='post'>\n";
  98.                                     echo "<p>" . ($result->num_rows ? lang('%d row(s)', $result->num_rows) : "") . $time;
  99.                                     $id = "export-$commands";
  100.                                     $export = ", <a href='#$id' onclick=\"return !toggle('$id');\">" . lang('Export') . "</a><span id='$id' class='hidden'>: "
  101.                                         . html_select("output", $adminer->dumpOutput(), $adminer_export["output"]) . " "
  102.                                         . html_select("format", $dump_format, $adminer_export["format"])
  103.                                         . "<input type='hidden' name='query' value='" . h($q) . "'>"
  104.                                         . " <input type='submit' name='export' value='" . lang('Export') . "' onclick='eventStop(event);'><input type='hidden' name='token' value='$token'></span>"
  105.                                     ;
  106.                                     if ($connection2 && preg_match("~^($space|\\()*SELECT\\b~isU", $q) && ($explain = explain($connection2, $q))) {
  107.                                         $id = "explain-$commands";
  108.                                         echo ", <a href='#$id' onclick=\"return !toggle('$id');\">EXPLAIN</a>$export\n";
  109.                                         echo "<div id='$id' class='hidden'>\n";
  110.                                         select($explain, $connection2, ($jush == "sql" ? "http://dev.mysql.com/doc/refman/" . substr($connection->server_info, 0, 3) . "/en/explain-output.html#" : ""));
  111.                                         echo "</div>\n";
  112.                                     } else {
  113.                                         echo "$export\n";
  114.                                     }
  115.                                     echo "</form>\n";
  116.                                 }
  117.                                 $start = $end;
  118.                             } while ($connection->next_result());
  119.                         } elseif ($connection->error) {
  120.                             echo ($_POST["only_errors"] ? $print : "");
  121.                             echo "<p class='error'>" . lang('Error in query') . ": " . error() . "\n";
  122.                             $errors[] = " <a href='#sql-$commands'>$commands</a>";
  123.                             if ($_POST["error_stops"]) {
  124.                                 break;
  125.                             }
  126.                         }
  127.                         $query = substr($query, $offset);
  128.                         $offset = 0;
  129.                     }
  130.                 }
  131.             }
  132.         }
  133.         if ($empty) {
  134.             echo "<p class='message'>" . lang('No commands to execute.') . "\n";
  135.         } elseif ($_POST["only_errors"]) {
  136.             echo "<p class='message'>" . lang('%d query(s) executed OK.', $commands - count($errors)) . format_time($total_start, explode(" ", microtime())) . "\n";
  137.         } elseif ($errors && $commands > 1) {
  138.             echo "<p class='error'>" . lang('Error in query') . ": " . implode("", $errors) . "\n";
  139.         }
  140.         //! MS SQL - SET SHOWPLAN_ALL OFF
  141.     } else {
  142.         echo "<p class='error'>" . upload_error($query) . "\n";
  143.     }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement