2007-07-02 05:51:26 +00:00
< ? php
2008-07-31 14:28:26 +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 " ;
2008-08-16 12:28:31 +00:00
return $return . str_repeat ( " \0 " , 512 - strlen ( $return )) . $contents . str_repeat ( " \0 " , 511 - ( strlen ( $contents ) + 511 ) % 512 );
2008-07-31 14:28:26 +00:00
}
2008-06-20 14:13:37 +00:00
if ( $_POST ) {
2009-01-13 14:45:01 +00:00
$ext = dump_headers (( strlen ( $_GET [ " dump " ]) ? $_GET [ " dump " ] : $_GET [ " db " ]), ( ! strlen ( $_GET [ " db " ]) || count ( array_filter (( array ) $_POST [ " tables " ]) + array_filter (( array ) $_POST [ " data " ])) > 1 ));
2008-08-16 12:28:31 +00:00
if ( $_POST [ " format " ] != " csv " ) {
2009-04-02 09:46:42 +00:00
$max_packet = 1048576 ; // default, minimum is 1024
2008-08-16 12:28:31 +00:00
echo " SET NAMES utf8; \n " ;
echo " SET foreign_key_checks = 0; \n " ;
2009-05-08 05:23:03 +00:00
echo " SET time_zone = ' " . $dbh -> escape_string ( $dbh -> result ( $dbh -> query ( " SELECT @@time_zone " ))) . " '; \n " ;
2008-08-16 12:28:31 +00:00
echo " \n " ;
}
2008-06-20 14:13:37 +00:00
foreach ( $_POST [ " databases " ] as $db => $style ) {
$db = bracket_escape ( $db , " back " );
2009-05-08 05:23:03 +00:00
if ( $dbh -> select_db ( $db )) {
if ( $_POST [ " format " ] != " csv " && ereg ( 'CREATE' , $style ) && ( $result = $dbh -> query ( " SHOW CREATE DATABASE " . idf_escape ( $db )))) {
2008-08-19 09:19:18 +00:00
if ( $style == " DROP, CREATE " ) {
echo " DROP DATABASE IF EXISTS " . idf_escape ( $db ) . " ; \n " ;
2008-08-18 15:18:16 +00:00
}
2009-05-08 05:23:03 +00:00
$create = $dbh -> result ( $result , 1 );
2008-08-19 09:19:18 +00:00
echo ( $style == " CREATE, ALTER " ? preg_replace ( '~^CREATE DATABASE ~' , '\\0IF NOT EXISTS ' , $create ) : $create ) . " ; \n " ;
$result -> free ();
}
if ( $style && $_POST [ " format " ] != " csv " ) {
2008-08-26 10:29:23 +00:00
echo " USE " . idf_escape ( $db ) . " ; \n \n " ;
2008-09-01 16:49:38 +00:00
$out = " " ;
2009-05-08 05:23:03 +00:00
if ( $dbh -> server_info >= 5 ) {
2008-08-19 09:19:18 +00:00
foreach ( array ( " FUNCTION " , " PROCEDURE " ) as $routine ) {
2009-05-08 05:23:03 +00:00
$result = $dbh -> query ( " SHOW $routine STATUS WHERE Db = ' " . $dbh -> escape_string ( $db ) . " ' " );
2008-08-19 09:19:18 +00:00
while ( $row = $result -> fetch_assoc ()) {
2009-05-08 05:23:03 +00:00
$out .= $dbh -> result ( $dbh -> query ( " SHOW CREATE $routine " . idf_escape ( $row [ " Name " ])), 2 ) . " ;; \n \n " ;
2008-08-19 09:19:18 +00:00
}
$result -> free ();
}
}
2009-05-08 05:23:03 +00:00
if ( $dbh -> server_info >= 5.1 ) {
$result = $dbh -> query ( " SHOW EVENTS " );
2008-09-01 16:49:38 +00:00
while ( $row = $result -> fetch_assoc ()) {
2009-05-08 05:23:03 +00:00
$out .= $dbh -> result ( $dbh -> query ( " SHOW CREATE EVENT " . idf_escape ( $row [ " Name " ])), 3 ) . " ;; \n \n " ;
2008-09-01 16:49:38 +00:00
}
$result -> free ();
}
echo ( $out ? " DELIMITER ;; \n \n $out " . " DELIMITER ; \n \n " : " " );
2008-08-19 09:19:18 +00:00
}
2009-01-13 14:45:01 +00:00
if (( $style || strlen ( $_GET [ " db " ])) && ( array_filter (( array ) $_POST [ " tables " ]) || array_filter (( array ) $_POST [ " data " ]))) {
2008-08-19 09:19:18 +00:00
$views = array ();
2009-05-08 05:23:03 +00:00
$result = $dbh -> query ( " SHOW TABLE STATUS " );
2008-08-19 09:19:18 +00:00
while ( $row = $result -> fetch_assoc ()) {
$key = ( strlen ( $_GET [ " db " ]) ? bracket_escape ( $row [ " Name " ]) : 0 );
if ( $_POST [ " tables " ][ $key ] || $_POST [ " data " ][ $key ]) {
if ( isset ( $row [ " Engine " ])) {
if ( $ext == " tar " ) {
ob_start ();
}
dump_table ( $row [ " Name " ], $_POST [ " tables " ][ $key ]);
dump_data ( $row [ " Name " ], $_POST [ " data " ][ $key ]);
if ( $ext == " tar " ) {
echo tar_file (( strlen ( $_GET [ " db " ]) ? " " : " $db / " ) . " $row[Name] .csv " , ob_get_clean ());
2008-08-26 10:29:23 +00:00
} elseif ( $_POST [ " format " ] != " csv " ) {
echo " \n " ;
2008-08-19 09:19:18 +00:00
}
} elseif ( $_POST [ " format " ] != " csv " ) {
$views [ $row [ " Name " ]] = $_POST [ " tables " ][ $key ];
}
}
}
$result -> free ();
2008-08-19 10:32:34 +00:00
foreach ( $views as $view => $style1 ) {
dump_table ( $view , $style1 , true );
2008-08-18 15:18:16 +00:00
}
2008-08-16 12:28:31 +00:00
}
2008-08-19 10:32:34 +00:00
2009-05-08 05:23:03 +00:00
if ( $dbh -> server_info >= 5 && $style == " CREATE, ALTER " && $_POST [ " format " ] != " csv " ) {
2008-08-19 10:32:34 +00:00
$query = " SELECT TABLE_NAME, ENGINE, TABLE_COLLATION, TABLE_COMMENT FROM information_schema.TABLES WHERE TABLE_SCHEMA = DATABASE() " ;
?>
DELIMITER ;;
CREATE PROCEDURE phpminadmin_drop () BEGIN
DECLARE _table_name , _engine , _table_collation varchar ( 64 );
DECLARE _table_comment varchar ( 64 );
DECLARE done bool DEFAULT 0 ;
DECLARE tables CURSOR FOR < ? php echo $query ; ?> ;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1 ;
OPEN tables ;
REPEAT
FETCH tables INTO _table_name , _engine , _table_collation , _table_comment ;
IF NOT done THEN
CASE _table_name < ? php
2009-05-08 05:23:03 +00:00
$result = $dbh -> query ( $query );
2008-08-19 10:32:34 +00:00
while ( $row = $result -> fetch_assoc ()) {
2009-05-08 05:23:03 +00:00
$comment = $dbh -> escape_string ( $row [ " ENGINE " ] == " InnoDB " ? preg_replace ( '~(?:(.+); )?InnoDB free: .*~' , '\\1' , $row [ " TABLE_COMMENT " ]) : $row [ " TABLE_COMMENT " ]);
2008-08-19 10:32:34 +00:00
echo "
2009-05-08 05:23:03 +00:00
WHEN '" . $dbh->escape_string($row["TABLE_NAME"]) . "' THEN
2008-08-19 10:32:34 +00:00
" . (isset( $row["ENGINE"] ) ? " IF _engine != '$row[ENGINE]' OR _table_collation != '$row[TABLE_COLLATION]' OR _table_comment != '$comment' THEN
ALTER TABLE " . idf_escape( $row["TABLE_NAME"] ) . " ENGINE = $row [ ENGINE ] COLLATE = $row [ TABLE_COLLATION ] COMMENT = '$comment' ;
END IF " : " BEGIN END " ) . " ; " ;
}
$result -> free ();
?>
ELSE
SET @ alter_table = CONCAT ( 'DROP TABLE `' , REPLACE ( _table_name , '`' , '``' ), '`' );
PREPARE alter_command FROM @ alter_table ;
2008-10-06 15:08:55 +00:00
EXECUTE alter_command ; -- returns " can't return a result set in the given context " with MySQL extension
2008-08-19 10:32:34 +00:00
DROP PREPARE alter_command ;
END CASE ;
END IF ;
UNTIL done END REPEAT ;
CLOSE tables ;
END ;;
DELIMITER ;
CALL phpminadmin_drop ;
DROP PROCEDURE phpminadmin_drop ;
< ? php
}
2008-08-08 15:00:42 +00:00
}
2008-06-20 14:13:37 +00:00
}
exit ;
}
2007-08-04 19:40:11 +00:00
2008-06-20 14:13:37 +00:00
page_header ( lang ( 'Export' ), " " , ( strlen ( $_GET [ " export " ]) ? array ( " table " => $_GET [ " export " ]) : array ()), $_GET [ " db " ]);
?>
< form action = " " method = " post " >
2009-05-31 22:31:09 +00:00
< table border = " 0 " >
< ? php
echo " <tr><th> " . lang ( 'Output' ) . " </th><td> $dump_output </td></tr> \n " ;
echo " <tr><th> " . lang ( 'Format' ) . " </th><td> $dump_format </td></tr> \n " ;
echo " <tr><th> " . lang ( 'Database' ) . " </th><td><select name=''><option></option> " . optionlist ( array ( 'USE' , 'DROP, CREATE' , 'CREATE' , 'CREATE, ALTER' ), ( strlen ( $_GET [ " db " ]) ? '' : 'CREATE' )) . " </select></td></tr> \n " ;
echo " <tr><th> " . lang ( 'Tables' ) . " </th><td><select name=''><option></option> " . optionlist ( array ( 'DROP, CREATE' , 'CREATE' , 'CREATE, ALTER' ), 'DROP, CREATE' ) . " </select></td></tr> \n " ;
echo " <tr><th> " . lang ( 'Data' ) . " </th><td><select name=''><option></option> " . optionlist ( array ( 'TRUNCATE, INSERT' , 'INSERT' , 'UPDATE' ), 'INSERT' ) . " </select></td></tr> \n " ; // INSERT INTO ... ON DUPLICATE KEY UPDATE
?>
</ table >
< p >< input type = " submit " value = " <?php echo lang('Export'); ?> " /></ p >
2008-06-20 14:13:37 +00:00
< ? php
2009-05-31 22:31:09 +00:00
if ( ! strlen ( $_GET [ " db " ])) {
echo " <table cellspacing='0'> \n <thead><tr><th><input type='checkbox' id='check-databases' onclick='dump_check(this, /^databases \\ [/);' checked='checked' /> " . lang ( 'Database' ) . " </th></tr></thead> \n " ;
foreach ( get_databases () as $db ) {
if ( $db != " information_schema " || $dbh -> server_info < 5 ) {
echo '<tr><td><label><input type="checkbox" name="databases[' . htmlspecialchars ( bracket_escape ( $db )) . ']" checked="checked" value="1" onclick="dump_uncheck(\'check-databases\');" />' . htmlspecialchars ( $db ) . " </label></td></tr> \n " ; //! uncheck all
2008-06-20 14:13:37 +00:00
}
}
2009-05-31 22:31:09 +00:00
echo " </table> \n " ;
2008-06-20 14:13:37 +00:00
}
2009-05-31 22:31:09 +00:00
if ( strlen ( $_GET [ " db " ])) {
$checked = ( strlen ( $_GET [ " dump " ]) ? " " : " checked='checked' " );
echo " <table cellspacing='0'> \n <thead><tr> " ;
echo " <th style='text-align: left;'><label><input type='checkbox' id='check-tables' onclick='dump_check(this, /^tables \\ [/);' $checked /> " . lang ( 'Tables' ) . " </label></th> " ;
echo " <th><label><input type='checkbox' id='check-data' onclick='dump_check(this, /^data \\ [/);' $checked /> " . lang ( 'Data' ) . " </label></th> " ;
echo " </tr></thead> \n " ;
$views = " " ;
$result = $dbh -> query ( " SHOW TABLE STATUS " );
while ( $row = $result -> fetch_assoc ()) {
$checked = ( strlen ( $_GET [ " dump " ]) && $row [ " Name " ] != $_GET [ " dump " ] ? '' : " checked='checked' " );
$print = '<tr><td><label><input type="checkbox" name="tables[' . htmlspecialchars ( bracket_escape ( $row [ " Name " ])) . " ] \" $checked value='1' onclick= \" dump_uncheck('check-tables'); \" /> " . htmlspecialchars ( $row [ " Name " ]) . " </label></td> " ; //! uncheck all
if ( ! $row [ " Engine " ]) {
$views .= " $print </tr> \n " ;
} else {
echo $print . '<td><input type="checkbox" name="data[' . htmlspecialchars ( bracket_escape ( $row [ " Name " ])) . " ] \" $checked value='1' onclick= \" dump_uncheck('check-data'); \" /></td></tr> \n " ;
2008-07-31 14:28:26 +00:00
}
2008-06-20 14:13:37 +00:00
}
2009-05-31 22:31:09 +00:00
echo " $views </table> \n " ;
2008-06-20 14:13:37 +00:00
}
?>
2009-05-28 12:16:39 +00:00
< p >< input type = " submit " value = " <?php echo lang('Export'); ?> " /></ p >
2008-06-20 14:13:37 +00:00
</ form >