2008-08-16 12:28:31 +00:00
< ? php
2009-09-09 11:03:03 +00:00
function tar_file ( $filename , $contents ) {
$return = pack ( " a100a8a8a8a12a12 " , $filename , 644 , 0 , 0 , decoct ( strlen ( $contents )), decoct ( time ()));
$checksum = 8 * 32 ; // space for checksum itself
for ( $i = 0 ; $i < strlen ( $return ); $i ++ ) {
$checksum += ord ( $return { $i });
}
$return .= sprintf ( " %06o " , $checksum ) . " \0 " ;
return $return . str_repeat ( " \0 " , 512 - strlen ( $return )) . $contents . str_repeat ( " \0 " , 511 - ( strlen ( $contents ) + 511 ) % 512 );
}
2008-08-19 10:32:34 +00:00
function dump_table ( $table , $style , $is_view = false ) {
2009-09-22 10:51:40 +00:00
global $connection ;
2010-04-21 12:01:32 +00:00
if ( $_POST [ " format " ] != " sql " ) {
2009-10-02 12:31:28 +00:00
echo " \xef \xbb \xbf " ; // UTF-8 byte order mark
2008-08-18 22:40:45 +00:00
if ( $style ) {
dump_csv ( array_keys ( fields ( $table )));
}
} elseif ( $style ) {
2010-04-21 12:01:32 +00:00
$create = create_sql ( $table );
if ( $create ) {
2009-06-01 12:53:19 +00:00
if ( $style == " DROP+CREATE " ) {
2010-05-27 14:10:17 +00:00
echo " DROP " . ( $is_view ? " VIEW " : " TABLE " ) . " IF EXISTS " . table ( $table ) . " ; \n " ;
2008-08-18 22:40:45 +00:00
}
2009-10-02 12:31:28 +00:00
echo ( $style != " CREATE+ALTER " ? $create : ( $is_view ? substr_replace ( $create , " OR REPLACE " , 6 , 0 ) : substr_replace ( $create , " IF NOT EXISTS " , 12 , 0 ))) . " ; \n \n " ;
2008-08-18 22:40:45 +00:00
}
2009-06-01 12:53:19 +00:00
if ( $style == " CREATE+ALTER " && ! $is_view ) {
2009-06-21 23:20:32 +00:00
// create procedure which iterates over original columns and adds new and removes old
2009-09-22 10:51:40 +00:00
$query = " SELECT COLUMN_NAME, COLUMN_DEFAULT, IS_NULLABLE, COLLATION_NAME, COLUMN_TYPE, EXTRA, COLUMN_COMMENT FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = " . $connection -> quote ( $table ) . " ORDER BY ORDINAL_POSITION " ;
2009-10-02 12:31:28 +00:00
echo " DELIMITER ;;
2009-08-29 13:51:32 +00:00
CREATE PROCEDURE adminer_alter ( INOUT alter_command text ) BEGIN
2009-10-13 13:05:10 +00:00
DECLARE _column_name , _collation_name , after varchar ( 64 ) DEFAULT '' ;
DECLARE _column_type , _column_default text ;
2008-08-19 10:50:04 +00:00
DECLARE _is_nullable char ( 3 );
2009-10-13 13:05:10 +00:00
DECLARE _extra varchar ( 30 );
2008-08-19 10:50:04 +00:00
DECLARE _column_comment varchar ( 255 );
DECLARE done , set_after bool DEFAULT 0 ;
2009-10-02 12:31:28 +00:00
DECLARE add_columns text DEFAULT ' " ;
2009-06-22 00:53:51 +00:00
$fields = array ();
2009-09-22 10:51:40 +00:00
$result = $connection -> query ( $query );
2009-06-22 00:53:51 +00:00
$after = " " ;
while ( $row = $result -> fetch_assoc ()) {
2009-10-10 21:33:22 +00:00
$default = $row [ " COLUMN_DEFAULT " ];
2009-10-13 13:05:10 +00:00
$row [ " default " ] = ( isset ( $default ) ? $connection -> quote ( $default ) : " NULL " );
2009-09-22 10:51:40 +00:00
$row [ " after " ] = $connection -> quote ( $after ); //! rgt AFTER lft, lft AFTER id doesn't work
2009-06-22 00:53:51 +00:00
$row [ " alter " ] = escape_string ( idf_escape ( $row [ " COLUMN_NAME " ])
. " $row[COLUMN_TYPE] "
. ( $row [ " COLLATION_NAME " ] ? " COLLATE $row[COLLATION_NAME] " : " " )
2009-10-13 13:05:10 +00:00
. ( isset ( $default ) ? " DEFAULT " . ( $default == " CURRENT_TIMESTAMP " ? $default : $row [ " default " ]) : " " )
2009-06-22 00:53:51 +00:00
. ( $row [ " IS_NULLABLE " ] == " YES " ? " " : " NOT NULL " )
. ( $row [ " EXTRA " ] ? " $row[EXTRA] " : " " )
2009-09-22 10:51:40 +00:00
. ( $row [ " COLUMN_COMMENT " ] ? " COMMENT " . $connection -> quote ( $row [ " COLUMN_COMMENT " ]) : " " )
2009-06-22 00:53:51 +00:00
. ( $after ? " AFTER " . idf_escape ( $after ) : " FIRST " )
);
2009-10-02 12:31:28 +00:00
echo " , ADD $row[alter] " ;
2009-06-22 00:53:51 +00:00
$fields [] = $row ;
$after = $row [ " COLUMN_NAME " ];
}
2009-10-02 12:31:28 +00:00
echo " ';
2009-08-28 11:49:57 +00:00
DECLARE columns CURSOR FOR $query ;
2008-08-19 10:50:04 +00:00
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1 ;
SET @ alter_table = '' ;
OPEN columns ;
REPEAT
FETCH columns INTO _column_name , _column_default , _is_nullable , _collation_name , _column_type , _extra , _column_comment ;
IF NOT done THEN
SET set_after = 1 ;
2009-10-02 12:31:28 +00:00
CASE _column_name " ;
2009-06-22 00:53:51 +00:00
foreach ( $fields as $row ) {
2009-10-02 12:31:28 +00:00
echo "
2009-09-22 10:51:40 +00:00
WHEN " . $connection->quote ( $row["COLUMN_NAME"] ) . " THEN
2008-08-19 10:50:04 +00:00
SET add_columns = REPLACE ( add_columns , ', ADD $row[alter]' , '' );
2009-09-23 10:05:06 +00:00
IF NOT ( _column_default <=> $row [ default ]) OR _is_nullable != '$row[IS_NULLABLE]' OR _collation_name != '$row[COLLATION_NAME]' OR _column_type != " . $connection->quote ( $row["COLUMN_TYPE"] ) . " OR _extra != '$row[EXTRA]' OR _column_comment != " . $connection->quote ( $row["COLUMN_COMMENT"] ) . " OR after != $row [ after ] THEN
2008-08-19 10:50:04 +00:00
SET @ alter_table = CONCAT ( @ alter_table , ', MODIFY $row[alter]' );
2009-10-02 12:31:28 +00:00
END IF ; " ; //! don't replace in comment
2009-06-22 00:53:51 +00:00
}
2009-10-02 12:31:28 +00:00
echo "
2008-08-19 10:50:04 +00:00
ELSE
SET @ alter_table = CONCAT ( @ alter_table , ', DROP ' , _column_name );
SET set_after = 0 ;
END CASE ;
IF set_after THEN
SET after = _column_name ;
END IF ;
END IF ;
UNTIL done END REPEAT ;
CLOSE columns ;
IF @ alter_table != '' OR add_columns != '' THEN
2010-05-27 14:10:17 +00:00
SET alter_command = CONCAT ( alter_command , 'ALTER TABLE " . table($table) . "' , SUBSTR ( CONCAT ( add_columns , @ alter_table ), 2 ), ';\\n' );
2008-08-19 10:50:04 +00:00
END IF ;
END ;;
DELIMITER ;
2009-08-29 13:51:32 +00:00
CALL adminer_alter ( @ adminer_alter );
2009-06-08 19:02:30 +00:00
DROP PROCEDURE adminer_alter ;
2008-08-19 10:50:04 +00:00
2009-10-02 12:31:28 +00:00
" ;
2009-06-01 12:53:19 +00:00
//! indexes
}
2008-08-18 22:40:45 +00:00
}
}
2009-06-13 17:20:29 +00:00
function dump_data ( $table , $style , $select = " " ) {
2010-05-07 13:44:22 +00:00
global $connection , $jush ;
$max_packet = ( $jush == " sqlite " ? 0 : 1048576 ); // default, minimum is 1024
2008-08-16 12:28:31 +00:00
if ( $style ) {
2010-04-21 12:01:32 +00:00
if ( $_POST [ " format " ] == " sql " && $style == " TRUNCATE+INSERT " ) {
2010-05-27 14:10:17 +00:00
echo truncate_sql ( $table ) . " ; \n " ;
2008-08-16 12:28:31 +00:00
}
2009-09-11 18:53:39 +00:00
$fields = fields ( $table );
2010-05-27 14:10:17 +00:00
$result = $connection -> query (( $select ? $select : " SELECT * FROM " . table ( $table )), 1 ); // 1 - MYSQLI_USE_RESULT //! enum and set as numbers, microtime
2008-08-16 12:28:31 +00:00
if ( $result ) {
2009-08-28 13:16:54 +00:00
$insert = " " ;
$buffer = " " ;
2008-08-16 12:28:31 +00:00
while ( $row = $result -> fetch_assoc ()) {
2010-04-21 12:01:32 +00:00
if ( $_POST [ " format " ] != " sql " ) {
2008-08-16 12:28:31 +00:00
dump_csv ( $row );
} else {
2009-08-28 13:16:54 +00:00
if ( ! $insert ) {
2010-05-27 14:10:17 +00:00
$insert = " INSERT INTO " . table ( $table ) . " ( " . implode ( " , " , array_map ( 'idf_escape' , array_keys ( $row ))) . " ) VALUES " ;
2009-08-28 13:16:54 +00:00
}
2008-08-16 12:28:31 +00:00
foreach ( $row as $key => $val ) {
2009-09-22 10:51:40 +00:00
$row [ $key ] = ( isset ( $val ) ? ( ereg ( 'int|float|double|decimal' , $fields [ $key ][ " type " ]) ? $val : $connection -> quote ( $val )) : " NULL " ); //! columns looking like functions
2008-08-16 12:28:31 +00:00
}
2009-09-11 18:53:39 +00:00
$s = implode ( " , \t " , $row );
2009-06-13 17:20:29 +00:00
if ( $style == " INSERT+UPDATE " ) {
$set = array ();
2009-09-11 18:53:39 +00:00
foreach ( $row as $key => $val ) {
2009-08-04 11:13:35 +00:00
$set [] = idf_escape ( $key ) . " = $val " ;
2009-06-13 17:20:29 +00:00
}
2009-10-02 12:31:28 +00:00
echo " $insert ( $s ) ON DUPLICATE KEY UPDATE " . implode ( " , " , $set ) . " ; \n " ;
2008-08-16 12:28:31 +00:00
} else {
2010-04-21 12:01:32 +00:00
$s = ( $max_packet ? " \n " : " " ) . " ( $s ) " ;
2009-08-28 13:16:54 +00:00
if ( ! $buffer ) {
$buffer = $insert . $s ;
2009-09-24 14:16:47 +00:00
} elseif ( strlen ( $buffer ) + 2 + strlen ( $s ) < $max_packet ) { // 2 - separator and terminator length
2009-08-28 14:44:43 +00:00
$buffer .= " , $s " ;
2009-06-13 17:20:29 +00:00
} else {
2009-08-28 14:44:43 +00:00
$buffer .= " ; \n " ;
2009-10-02 12:31:28 +00:00
echo $buffer ;
2009-08-28 14:44:43 +00:00
$buffer = $insert . $s ;
2008-08-16 12:28:31 +00:00
}
}
}
}
2010-04-21 12:01:32 +00:00
if ( $_POST [ " format " ] == " sql " && $style != " INSERT+UPDATE " && $buffer ) {
2009-08-28 14:44:43 +00:00
$buffer .= " ; \n " ;
2009-10-02 12:31:28 +00:00
echo $buffer ;
2008-08-16 12:28:31 +00:00
}
}
}
}
2008-08-16 21:15:46 +00:00
2008-08-18 15:18:16 +00:00
function dump_headers ( $identifier , $multi_table = false ) {
2010-02-17 16:47:50 +00:00
$filename = ( $identifier != " " ? friendly_url ( $identifier ) : " adminer " );
2009-10-02 12:21:55 +00:00
$output = $_POST [ " output " ];
2009-06-21 23:20:32 +00:00
$ext = ( $_POST [ " format " ] == " sql " ? " sql " : ( $multi_table ? " tar " : " csv " )); // multiple CSV packed to TAR
2009-08-28 14:44:43 +00:00
header ( " Content-Type: " .
2009-10-02 12:21:55 +00:00
( $output == " bz2 " ? " application/x-bzip " :
( $output == " gz " ? " application/x-gzip " :
2009-08-28 14:44:43 +00:00
( $ext == " tar " ? " application/x-tar " :
2009-10-02 12:21:55 +00:00
( $ext == " sql " || $output != " file " ? " text/plain " : " text/csv " ) . " ; charset=utf-8 "
2009-08-28 14:44:43 +00:00
))));
2009-10-02 12:21:55 +00:00
if ( $output != " text " ) {
header ( " Content-Disposition: attachment; filename= $filename . $ext " . ( $output != " file " && ! ereg ( '[^0-9a-z]' , $output ) ? " . $output " : " " ));
2009-06-15 10:57:43 +00:00
}
2009-09-01 13:05:50 +00:00
session_write_close ();
2009-10-02 12:31:28 +00:00
if ( $_POST [ " output " ] == " bz2 " ) {
ob_start ( 'bzcompress' , 1e6 );
}
if ( $_POST [ " output " ] == " gz " ) {
ob_start ( 'gzencode' , 1e6 );
}
2008-08-18 15:18:16 +00:00
return $ext ;
}