<?php
include("common_db.php");
include("functions.php");
dbconnect($host, $username, $password); //from common_db.php
$strings = loadStrings($lang, 'REXPORT');
headers();
html();
head($strings['REX_TITLE'], array('js/rex.php'));
menu();
navpane();
//get database and table
if(isset($_REQUEST['database'])) {
$db = mysql_real_escape_string($_REQUEST['database']);
} else {
//TODO: panic
}
if($_REQUEST['database']=='HypatiaDB') {
printAuthError(); die();
}
if(isset($_REQUEST['table'])) {
$table = mysql_real_escape_string($_REQUEST['table']);
} else {
//TODO: panic
}
//get a nice temp filename
$tfname = dirname(__FILE__) . '/database-export';
//Now, that file shouldn't exist (because the user should've deleted
//it after the last delete. Consequently, we can safely call unlink
//on it, just in case. This is done in the switch statements, because
//there's an extension that still needs to be appended.)
?>
<div id="mainpane">
<?php
switch($_REQUEST['type']) {
case 'dbdump':
//This mode returns a database dump (i.e., a file containing SQL statements)
//in the specified format. To achieve this, we use mysqldump, which comes with
//all mysql distros that have been released this decade or so.
$tfname .= '.sql';
@unlink($tfname);
//validate the mode
$mode = $_REQUEST['dbd_mode'];
switch($mode) {
case 'mysql40':
case 'mysql323':
case 'ansi':
case 'postgresql':
case 'oracle':
case 'mssql':
case 'db2':
case 'maxdb':
break;
default:
$mode = 'mysql40';
break;
}
$command = "mysqldump -h $host -u $username";
if($password != "")
$command .= " -p $password";
$command .= " --compatible=$mode $db $table > $tfname";
exec($command);
break;
case 'csv':
//CSVs are supported natively by mysql
//See the docs for the SELECT statement
$tfname .= '.csv';
@unlink($tfname);
mysql_select_db($db);
$query = "SELECT * INTO OUTFILE '$tfname' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\\n' FROM $table";
(mysql_query($query)) or die(mysql_error()); //TODO: L10N
break;
case 'xml':
//The xml version is much the same as the regular database dump, except
//we need to pass the --xml parameter to mysqldump, and there is no
//mode argument.
$tfname .= '.xml';
@unlink($tfname);
$command = "mysqldump -h $host -u $username";
if($password != "")
$command .= " -p $password";
$command .= " --xml $db $table > $tfname";
exec($command);
break;
default:
//TODO: panic
break;
}
echo("<p>$strings[REX_INFO]</p>");
echo('<p><a href="' . basename($tfname) . '">' . $strings['REX_DOWNLOAD'] . '</a></p>');
echo('<p><span class="ll" id="dela" onclick="deleteFromServer(\'' . $tfname . '\');">' . $strings['REX_DELETE'] . '</span></p>');
?>
</div>
<?php
endhtml();
?>