<?
$page = "admin_backup.php";
include "admin_header.php";

if(isset($_POST['task'])) { $task = $_POST['task']; } else { $task = "main"; }


if($task == "dobackup") {

$nowtime = cdate('Md_G-i-s', time());

header("Content-Type: text/plain");
header("Content-Disposition: attachment; filename=bhost_backup_$nowtime.txt");
header("Pragma: no-cache");
header("Expires: 0");

// LOOP THROUGH ALL TABLES
$tables = mysql_list_tables($mysql_database);
while ($row = mysql_fetch_row($tables)) {
 $tablename = $row[0];

// LOOP THROUGH bhost TABLES
 if(strstr($tablename, "bhost_")) {
echo "<mysql_runqueryflag>
########################################################
# TABLE STRUCTURE AND DATA FOR TABLE $tablename
########################################################

<mysql_runqueryflag>
DROP TABLE IF EXISTS `$tablename`;
<mysql_runqueryflag>
CREATE TABLE `$tablename` (";

 // GET EACH COLUMN IN THIS TABLE
 $structure = "";
 $primary = "";
 $column_list = array();
 $fields = mysql_list_fields($mysql_database, $tablename, $mysql_connect);
 $columns = mysql_num_fields($fields);
 for($i = 0; $i<$columns; $i++) {
  if($i != $columns - 1) { $comma = ","; } else { $comma = ""; }
  $thisfield = mysql_field_name($fields, $i);
  $thisfieldtype = mysql_field_type($fields, $i);
  $thisfieldlength = mysql_field_len($fields, $i);
  $thisfieldflags = str_replace("blob", "", str_replace("not_null", "NOT NULL", mysql_field_flags($fields, $i)));
   if(strstr($thisfieldflags, "primary_key")) { $thisfieldflags = str_replace("primary_key", "", $thisfieldflags); $primary = ",PRIMARY KEY  (`$thisfield`)"; }
  $thisfieldtypelength = "$thisfieldtype($thisfieldlength)";
  $column_list[$i] = $thisfield;
   if($thisfieldtype == "string") { $thisfieldtypelength = "varchar($thisfieldlength)"; }
   if($thisfieldtype == "blob") { $thisfieldtype = "text"; $thisfieldtypelength = "$thisfieldtype"; }

$structure .= "`$thisfield` $thisfieldtypelength $thisfieldflags"."$comma
";
 }

// DUMP STRUCTURE
echo "
$structure$primary
) TYPE=MyISAM;
";

 // GET DATA FOR THIS TABLE
 $data = "";
 $rows = mysql_query("SELECT * FROM $tablename");
 $num_rows = mysql_num_rows($rows);
 while($row_info = mysql_fetch_assoc($rows)) {
$data .= "
<mysql_runqueryflag>INSERT INTO `$tablename` VALUES (";
  for($a=0;$a<count($column_list);$a++) {
   if($a != count($column_list) - 1) { $comma2 = ","; } else { $comma2 = ""; }
   $thisfield = $column_list[$a];
   $thisfielddata = str_replace("<mysql_runqueryflag>", "", str_replace("\r\n", "\\r\\n", str_replace("'", "\'", $row_info[$thisfield])));
$data .= "'$thisfielddata'$comma2";
  }
$data .= ");";
 }

// DUMP DATA
echo "

$data



";
 }
}



exit();
}







if($task == "dorestore") {
$sql_ext = str_replace(".", "", strtolower(strrchr($_FILES['sql']['name'], ".")));
$is_error = "no";
$error = "";

if($_FILES['sql']['type'] != "text/plain" | $sql_ext != "txt") {
$is_error = "yes";
$error = "$admin_backup1";
}

if($_FILES['sql']['name'] == "") {
$is_error = "yes";
$error = "$admin_backup2";
}

if($is_error == "yes") {
echo "
$head
<h2>$admin_backup3</h2>
$error<br><br>
$admin_backup4
$foot";
exit;
}

$handle = fopen($_FILES['sql']['tmp_name'], "r") or die("1");
$contents = fread($handle, filesize($_FILES['sql']['tmp_name'])) or die("2");
fclose($handle);

$queries = explode("<mysql_runqueryflag>", $contents);
for($q=0;$q<count($queries);$q++) {
if(str_replace(" ", "", $queries[$q]) != "") {
mysql_query("$queries[$q]") or die("$admin_backup5 $q");
}}

echo $head;
echo "
<h2>$admin_backup6</h2>
$admin_backup7
";
echo $foot;
exit();
}







if($task == "main") {
echo $head;
$date = cdate("", time());

echo "
<script language='JavaScript'> 
<!--
function warning() {
var agree = confirm('$admin_backup8');
if(agree)
 return true;
else
 return false;
}
// -->
</SCRIPT>



<h2>$admin_backup9</h2>
$admin_backup10
<br><br>

<form action='admin_backup.php' method='POST'>
<input type='submit' class='button' value='$admin_backup11'>
<input type='hidden' name='task' value='dobackup'>
</form>

<h2>$admin_backup12</h2>
$admin_backup13<br>
<font color='#FF0000'><i>$admin_backup14</i></font><br>
<br>

<form action='admin_backup.php' method='POST' enctype='multipart/form-data' onSubmit='return warning()'>
$admin_backup15<br>
<input type='hidden' name='MAX_FILE_SIZE' value='1000000'>
<input type='file' name='sql' size='40'><br><br>
<input type='submit' class='button' value='$admin_backup16'>
<input type='hidden' name='task' value='dorestore'>
</form>
";

echo $foot;
}
?>