2007-07-02 05:51:26 +00:00
< ? php
2011-02-15 16:22:50 +00:00
if ( ! $error && $_POST [ " export " ]) {
2011-02-17 10:43:21 +00:00
dump_headers ( " sql " );
2011-02-15 16:22:50 +00:00
$adminer -> dumpTable ( " " , " " );
$adminer -> dumpData ( " " , " table " , $_POST [ " query " ]);
exit ;
}
2009-11-03 10:55:57 +00:00
restart_session ();
2010-10-15 08:58:08 +00:00
$history_all = & get_session ( " queries " );
2010-04-21 12:01:32 +00:00
$history = & $history_all [ DB ];
2009-07-17 13:53:10 +00:00
if ( ! $error && $_POST [ " clear " ]) {
$history = array ();
redirect ( remove_from_uri ( " history " ));
}
2013-08-02 18:45:55 +00:00
page_header (( isset ( $_GET [ " import " ]) ? lang ( 'Import' ) : lang ( 'SQL command' )), $error );
2007-07-02 05:51:26 +00:00
2008-09-30 12:22:24 +00:00
if ( ! $error && $_POST ) {
2009-09-01 13:09:00 +00:00
$fp = false ;
2013-08-02 18:45:55 +00:00
if ( ! isset ( $_GET [ " import " ])) {
$query = $_POST [ " query " ];
} elseif ( $_POST [ " webfile " ]) {
2017-09-27 16:09:14 +00:00
$sql_file_path = $adminer -> importServerPath ();
$fp = @ fopen (( file_exists ( $sql_file_path )
? $sql_file_path
2018-01-23 13:10:21 +00:00
: " compress.zlib:// $sql_file_path .gz "
2013-04-29 21:00:35 +00:00
), " rb " );
2009-09-01 13:09:00 +00:00
$query = ( $fp ? fread ( $fp , 1e6 ) : false );
2013-08-02 18:45:55 +00:00
} else {
2009-08-28 11:49:57 +00:00
$query = get_file ( " sql_file " , true );
2009-08-26 11:55:45 +00:00
}
2013-07-24 23:26:41 +00:00
2009-09-01 13:09:00 +00:00
if ( is_string ( $query )) { // get_file() returns error as number, fread() as false
2010-02-24 21:00:36 +00:00
if ( function_exists ( 'memory_get_usage' )) {
2011-08-24 12:16:11 +00:00
@ 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
2010-02-24 21:00:36 +00:00
}
2013-07-24 23:26:41 +00:00
2011-03-22 14:34:34 +00:00
if ( $query != " " && strlen ( $query ) < 1e6 ) { // don't add big queries
2013-07-24 23:26:41 +00:00
$q = $query . ( preg_match ( " ~;[ \t \r \n ]* \$ ~ " , $query ) ? " " : " ; " ); //! doesn't work with DELIMITER |
2012-03-10 10:28:13 +00:00
if ( ! $history || reset ( end ( $history )) != $q ) { // no repeated queries
2012-09-07 18:54:02 +00:00
restart_session ();
2014-03-07 17:33:37 +00:00
$history [] = array ( $q , time ()); //! add elapsed time
2012-09-07 18:54:02 +00:00
set_session ( " queries " , $history_all ); // required because reference is unlinked by stop_session()
stop_session ();
2011-03-22 14:34:34 +00:00
}
2009-09-09 15:51:26 +00:00
}
2013-07-24 23:26:41 +00:00
2016-06-01 16:05:16 +00:00
$space = " (?: \\ s|/ \\ *[ \ s \ S]*? \\ */|(?:#|-- )[^ \n ]* \n ?|-- \r ? \n ) " ;
2008-09-30 12:22:24 +00:00
$delimiter = " ; " ;
$offset = 0 ;
$empty = true ;
2010-04-21 12:01:32 +00:00
$connection2 = connect (); // connection for exploring indexes and EXPLAIN (to not replace FOUND_ROWS()) //! PDO - silent error
if ( is_object ( $connection2 ) && DB != " " ) {
2009-09-22 10:51:40 +00:00
$connection2 -> select_db ( DB );
2009-06-03 18:34:57 +00:00
}
2010-10-15 08:58:08 +00:00
$commands = 0 ;
2010-10-22 12:31:46 +00:00
$errors = array ();
2011-08-04 20:58:25 +00:00
$parse = '[\'"' . ( $jush == " sql " ? '`#' : ( $jush == " sqlite " ? '`[' : ( $jush == " mssql " ? '[' : '' ))) . ']|/\\*|-- |$' . ( $jush == " pgsql " ? '|\\$[^$]*\\$' : '' );
2013-08-09 00:18:39 +00:00
$total_start = microtime ( true );
2011-02-15 16:22:50 +00:00
parse_str ( $_COOKIE [ " adminer_export " ], $adminer_export );
$dump_format = $adminer -> dumpFormat ();
unset ( $dump_format [ " sql " ]);
2013-07-24 23:26:41 +00:00
2010-01-09 23:33:41 +00:00
while ( $query != " " ) {
2016-06-01 16:05:16 +00:00
if ( ! $offset && preg_match ( " ~^ $space *+DELIMITER \\ s+( \\ S+)~i " , $query , $match )) {
2008-10-02 15:23:30 +00:00
$delimiter = $match [ 1 ];
2008-09-30 12:22:24 +00:00
$query = substr ( $query , strlen ( $match [ 0 ]));
2009-08-21 12:43:46 +00:00
} else {
2012-04-16 06:37:05 +00:00
preg_match ( '(' . preg_quote ( $delimiter ) . " \\ s*| $parse ) " , $query , $match , PREG_OFFSET_CAPTURE , $offset ); // should always match
list ( $found , $pos ) = $match [ 0 ];
2009-09-01 13:09:00 +00:00
if ( ! $found && $fp && ! feof ( $fp )) {
2009-10-02 12:21:55 +00:00
$query .= fread ( $fp , 1e5 );
2009-09-01 13:09:00 +00:00
} else {
2010-01-09 23:33:41 +00:00
if ( ! $found && rtrim ( $query ) == " " ) {
2009-09-01 13:09:00 +00:00
break ;
}
2012-04-30 00:54:18 +00:00
$offset = $pos + strlen ( $found );
2013-07-24 23:26:41 +00:00
2012-04-16 06:37:05 +00:00
if ( $found && rtrim ( $found ) != $delimiter ) { // find matching quote or comment end
2013-07-24 23:26:41 +00:00
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
2010-10-22 12:31:46 +00:00
$s = $match [ 0 ][ 0 ];
if ( ! $s && $fp && ! feof ( $fp )) {
2011-08-04 21:49:38 +00:00
$query .= fread ( $fp , 1e5 );
2011-08-29 13:58:53 +00:00
} else {
$offset = $match [ 0 ][ 1 ] + strlen ( $s );
if ( $s [ 0 ] != " \\ " ) {
break ;
}
2010-10-22 12:31:46 +00:00
}
}
2013-07-24 23:26:41 +00:00
2010-10-22 12:31:46 +00:00
} else { // end of a query
2009-09-01 13:09:00 +00:00
$empty = false ;
2012-04-16 06:37:05 +00:00
$q = substr ( $query , 0 , $pos );
2010-10-15 08:58:08 +00:00
$commands ++ ;
2015-03-11 16:52:11 +00:00
$print = " <pre id='sql- $commands '><code class='jush- $jush '> " . $adminer -> sqlCommandQuery ( $q ) . " </code></pre> \n " ;
2016-06-01 16:05:16 +00:00
if ( $jush == " sqlite " && preg_match ( " ~^ $space *+ATTACH \\ b~i " , $q , $match )) {
2016-02-06 19:00:19 +00:00
// PHP doesn't support setting SQLITE_LIMIT_ATTACHED
2010-10-22 12:31:46 +00:00
echo $print ;
2016-02-06 19:00:19 +00:00
echo " <p class='error'> " . lang ( 'ATTACH queries are not supported.' ) . " \n " ;
$errors [] = " <a href='#sql- $commands '> $commands </a> " ;
if ( $_POST [ " error_stops " ]) {
break ;
}
} else {
if ( ! $_POST [ " only_errors " ]) {
echo $print ;
ob_flush ();
flush (); // can take a long time - show the running query
}
$start = microtime ( true );
//! don't allow changing of character_set_results, convert encoding of displayed query
2016-06-01 16:05:16 +00:00
if ( $connection -> multi_query ( $q ) && is_object ( $connection2 ) && preg_match ( " ~^ $space *+USE \\ b~i " , $q )) {
2016-02-06 19:00:19 +00:00
$connection2 -> query ( $q );
}
2013-07-24 23:26:41 +00:00
2016-02-06 19:00:19 +00:00
do {
$result = $connection -> store_result ();
if ( $connection -> error ) {
echo ( $_POST [ " only_errors " ] ? $print : " " );
echo " <p class='error'> " . lang ( 'Error in query' ) . ( $connection -> errno ? " ( $connection->errno ) " : " " ) . " : " . error () . " \n " ;
$errors [] = " <a href='#sql- $commands '> $commands </a> " ;
if ( $_POST [ " error_stops " ]) {
break 2 ;
}
2013-07-24 23:26:41 +00:00
2018-01-31 16:28:12 +00:00
} else {
$time = " <span class='time'>( " . format_time ( $start ) . " )</span> "
. ( 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
;
2018-02-05 08:23:07 +00:00
$affected = $connection -> affected_rows ; // getting warnigns overwrites this
2018-02-01 10:43:44 +00:00
$warnings = ( $_POST [ " only_errors " ] ? " " : $driver -> warnings ());
2018-02-01 11:27:22 +00:00
$warnings_id = " warnings- $commands " ;
2018-02-01 10:43:44 +00:00
if ( $warnings ) {
2018-01-31 16:28:12 +00:00
$time .= " , <a href='# $warnings_id '> " . lang ( 'Warnings' ) . " </a> " . script ( " qsl('a').onclick = partial(toggle, ' $warnings_id '); " , " " );
2009-09-12 04:44:18 +00:00
}
2018-01-31 19:25:11 +00:00
$explain = null ;
$explain_id = " explain- $commands " ;
2018-01-31 16:28:12 +00:00
if ( is_object ( $result )) {
$limit = $_POST [ " limit " ];
$orgtables = select ( $result , $connection2 , array (), $limit );
if ( ! $_POST [ " only_errors " ]) {
echo " <form action='' method='post'> \n " ;
$num_rows = $result -> num_rows ;
echo " <p> " . ( $num_rows ? ( $limit && $num_rows > $limit ? lang ( '%d / ' , $limit ) : " " ) . lang ( '%d row(s)' , $num_rows ) : " " );
echo $time ;
2018-01-31 19:25:11 +00:00
if ( $connection2 && preg_match ( " ~^( $space | \\ ()*+SELECT \\ b~i " , $q ) && ( $explain = explain ( $connection2 , $q ))) {
2018-01-31 19:25:41 +00:00
echo " , <a href='# $explain_id '>Explain</a> " . script ( " qsl('a').onclick = partial(toggle, ' $explain_id '); " , " " );
2018-01-31 19:25:11 +00:00
}
2018-01-31 16:28:12 +00:00
$id = " export- $commands " ;
2018-01-31 19:25:11 +00:00
echo " , <a href='# $id '> " . lang ( 'Export' ) . " </a> " . script ( " qsl('a').onclick = partial(toggle, ' $id '); " , " " ) . " <span id=' $id ' class='hidden'>: "
2018-01-31 16:28:12 +00:00
. html_select ( " output " , $adminer -> dumpOutput (), $adminer_export [ " output " ]) . " "
. html_select ( " format " , $dump_format , $adminer_export [ " format " ])
. " <input type='hidden' name='query' value=' " . h ( $q ) . " '> "
. " <input type='submit' name='export' value=' " . lang ( 'Export' ) . " '><input type='hidden' name='token' value=' $token '></span> \n "
2018-01-31 19:25:11 +00:00
. " </form> \n "
2018-01-31 16:28:12 +00:00
;
}
2013-07-24 23:26:41 +00:00
2018-01-31 16:28:12 +00:00
} else {
if ( preg_match ( " ~^ $space *+(CREATE|DROP|ALTER) $space ++(DATABASE|SCHEMA) \\ b~i " , $q )) {
restart_session ();
set_session ( " dbs " , null ); // clear cache
stop_session ();
}
if ( ! $_POST [ " only_errors " ]) {
2018-02-05 08:23:07 +00:00
echo " <p class='message' title=' " . h ( $connection -> info ) . " '> " . lang ( 'Query executed OK, %d row(s) affected.' , $affected ) . " $time\n " ;
2018-01-31 16:28:12 +00:00
}
2016-02-06 19:00:19 +00:00
}
2018-02-01 10:43:44 +00:00
echo ( $warnings ? " <div id=' $warnings_id ' class='hidden'> \n $warnings </div> \n " : " " );
2018-01-31 19:25:11 +00:00
if ( $explain ) {
echo " <div id=' $explain_id ' class='hidden'> \n " ;
select ( $explain , $connection2 , $orgtables );
echo " </div> \n " ;
}
2008-09-30 12:22:24 +00:00
}
2013-07-24 23:26:41 +00:00
2016-02-06 19:00:19 +00:00
$start = microtime ( true );
} while ( $connection -> next_result ());
}
2013-07-24 23:26:41 +00:00
2009-09-01 13:09:00 +00:00
$query = substr ( $query , $offset );
$offset = 0 ;
2009-08-26 11:55:45 +00:00
}
2013-07-24 23:26:41 +00:00
2007-07-10 13:30:42 +00:00
}
2007-07-02 05:51:26 +00:00
}
}
2013-07-24 23:26:41 +00:00
2008-09-30 12:22:24 +00:00
if ( $empty ) {
2009-07-11 20:30:40 +00:00
echo " <p class='message'> " . lang ( 'No commands to execute.' ) . " \n " ;
2010-10-22 12:31:46 +00:00
} elseif ( $_POST [ " only_errors " ]) {
2013-04-26 06:41:46 +00:00
echo " <p class='message'> " . lang ( '%d query(s) executed OK.' , $commands - count ( $errors ));
2014-03-11 16:37:56 +00:00
echo " <span class='time'>( " . format_time ( $total_start ) . " )</span> \n " ;
2010-10-22 12:31:46 +00:00
} elseif ( $errors && $commands > 1 ) {
echo " <p class='error'> " . lang ( 'Error in query' ) . " : " . implode ( " " , $errors ) . " \n " ;
2008-09-30 12:22:24 +00:00
}
2010-04-21 12:01:32 +00:00
//! MS SQL - SET SHOWPLAN_ALL OFF
2013-07-24 23:26:41 +00:00
2008-09-30 12:22:24 +00:00
} else {
2009-07-16 13:46:49 +00:00
echo " <p class='error'> " . upload_error ( $query ) . " \n " ;
2007-07-02 05:51:26 +00:00
}
}
?>
2007-07-09 14:45:11 +00:00
2011-06-14 09:37:02 +00:00
< form action = " " method = " post " enctype = " multipart/form-data " id = " form " >
2013-08-02 18:45:55 +00:00
< ? php
$execute = " <input type='submit' value=' " . lang ( 'Execute' ) . " ' title='Ctrl+Enter'> " ;
if ( ! isset ( $_GET [ " import " ])) {
$q = $_GET [ " sql " ]; // overwrite $q from if ($_POST) to save memory
if ( $_POST ) {
$q = $_POST [ " query " ];
} elseif ( $_GET [ " history " ] == " all " ) {
$q = $history ;
} elseif ( $_GET [ " history " ] != " " ) {
$q = $history [ $_GET [ " history " ]][ 0 ];
}
echo " <p> " ;
textarea ( " query " , $q , 20 );
2018-01-12 14:27:44 +00:00
echo ( $_POST ? " " : script ( " qs('textarea').focus(); " ));
2013-08-02 18:45:55 +00:00
echo " <p> $execute\n " ;
2014-12-06 19:11:34 +00:00
echo lang ( 'Limit rows' ) . " : <input type='number' name='limit' class='size' value=' " . h ( $_POST ? $_POST [ " limit " ] : $_GET [ " limit " ]) . " '> \n " ;
2013-08-02 18:45:55 +00:00
} else {
echo " <fieldset><legend> " . lang ( 'File upload' ) . " </legend><div> " ;
echo ( ini_bool ( " file_uploads " )
2014-06-15 10:06:01 +00:00
? " SQL (< " . 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
2013-08-02 18:45:55 +00:00
: lang ( 'File uploads are disabled.' )
);
echo " </div></fieldset> \n " ;
echo " <fieldset><legend> " . lang ( 'From server' ) . " </legend><div> " ;
2017-09-27 16:09:14 +00:00
echo lang ( 'Webserver file %s' , " <code> " . h ( $adminer -> importServerPath ()) . ( extension_loaded ( " zlib " ) ? " [.gz] " : " " ) . " </code> " );
2013-08-02 18:45:55 +00:00
echo ' <input type="submit" name="webfile" value="' . lang ( 'Run file' ) . '">' ;
echo " </div></fieldset> \n " ;
echo " <p> " ;
2009-09-24 14:17:26 +00:00
}
2013-05-02 01:28:04 +00:00
2013-08-02 18:45:55 +00:00
echo checkbox ( " error_stops " , 1 , ( $_POST ? $_POST [ " error_stops " ] : isset ( $_GET [ " import " ])), lang ( 'Stop on error' )) . " \n " ;
2014-01-11 20:17:25 +00:00
echo checkbox ( " only_errors " , 1 , ( $_POST ? $_POST [ " only_errors " ] : isset ( $_GET [ " import " ])), lang ( 'Show only errors' )) . " \n " ;
2013-10-25 02:10:50 +00:00
echo " <input type='hidden' name='token' value=' $token '> \n " ;
2009-06-15 09:07:05 +00:00
2013-08-02 18:45:55 +00:00
if ( ! isset ( $_GET [ " import " ]) && $history ) {
2010-02-15 13:42:12 +00:00
print_fieldset ( " history " , lang ( 'History' ), $_GET [ " history " ] != " " );
2013-04-16 16:50:32 +00:00
for ( $val = end ( $history ); $val ; $val = prev ( $history )) { // not array_reverse() to save memory
$key = key ( $history );
2014-03-07 17:33:37 +00:00
list ( $q , $time , $elapsed ) = $val ;
echo '<a href="' . h ( ME . " sql=&history= $key " ) . '">' . lang ( 'Edit' ) . " </a> "
. " <span class='time' title=' " . @ date ( 'Y-m-d' , $time ) . " '> " . @ date ( " H:i:s " , $time ) . " </span> " // @ - time zone may be not set
. " <code class='jush- $jush '> " . shorten_utf8 ( ltrim ( str_replace ( " \n " , " " , str_replace ( " \r " , " " , preg_replace ( '~^(#|-- ).*~m' , '' , $q )))), 80 , " </code> " )
. ( $elapsed ? " <span class='time'>( $elapsed )</span> " : " " )
. " <br> \n "
;
2009-06-15 09:07:05 +00:00
}
2009-07-17 13:53:10 +00:00
echo " <input type='submit' name='clear' value=' " . lang ( 'Clear' ) . " '> \n " ;
2011-03-22 13:10:25 +00:00
echo " <a href=' " . h ( ME . " sql=&history=all " ) . " '> " . lang ( 'Edit all' ) . " </a> \n " ;
2010-02-15 13:42:12 +00:00
echo " </div></fieldset> \n " ;
2009-06-15 09:07:05 +00:00
}
?>
2007-07-02 05:51:26 +00:00
</ form >