<?php

include("db.php");
$cur_date=date('d-m-Y');
$db_base="PS DATABASE ".$cur_date;
function get_para_value($name, $default_value) {
    global $HTTP_GET_VARS, $HTTP_POST_VARS, $_GET, $_POST;
    if( isset($_GET[$name])) {
        return $_GET[$name];
    }
    if( isset($_POST[$name])) {
        return $_POST[$name];
    }
    return $default_value;
}

$action=get_para_value('action', 'ask');


if ($action=='ask') {
    ?>
    <style type="text/css">
	.sync_button{ background-image:url(image/data-backup-xxl.png); padding-left:100px; padding-top:75px; padding-bottom:150px; padding-right:80px;}
	</style>
    <SCRIPT LANGUAGE="JavaScript">
    var checkflag = "false";
    function check(form) {
		alert(form);
        var objCheckBoxes = form.elements;
        if (checkflag == "false") {
            for (i = 0; i < objCheckBoxes.length; i++) {
                objCheckBoxes[i].checked = true;
            }
            checkflag = "true";
            return "Uncheck all";
        } else {
            for (i = 0; i < objCheckBoxes.length; i++) {
                objCheckBoxes[i].checked = false;
            }
            checkflag = "false";
            return "Check all";
        }
    }
    </script>

    <center>

    <form name="dump_form"  method="post" enctype="multipart/form-data" accept-charset="utf-8">
    
    <p>&nbsp;      </p>
    <p>&nbsp;</p>
    <p>
      <input type=button value="Check all" style='visibility:hidden'  onClick="this.value=check(this.form)">
      <?php
    $sql_tables = "SHOW TABLES";
    $req_tables = mysql_query($sql_tables);
    while (list($table) = mysql_fetch_row($req_tables)) {
        echo "<input checked='checked' style='visibility:hidden' type=checkbox name=".$table.">";
    }
    ?>
      
      <input type="hidden" name="action" value="dump">
    </p>
    <div align="center"><input type="image" class="sync_button" readonly value="DB Backup" style="text-align:center"   onClick="this.form.action.value='dump_to_disk';this.form.submit();"></div>
   
    </form>
    </center>
    <?php
} else if ($action=='dump') {
	
    header("Content-type: application/octet-stream");
    header("Content-Disposition: attachment;filename=\"$db_base.sql\"");
    header("Content-Transfer-Encoding: binary");
    echo "--\n";
    echo "-- Dump of database $db_base\n";
    echo "--\n";
    $sql_tables = "SHOW TABLES";
    $req_tables = mysql_query($sql_tables);
    while (list($table) = mysql_fetch_row($req_tables)) {
        if (get_para_value($table,'no')=='no') continue;
        echo "\n--\n-- Table $table\n\n";
        echo "DROP TABLE IF EXISTS $table;\n";
        $sql_create_table = "SHOW CREATE TABLE $table";
        $req_create_table = mysql_query($sql_create_table);
        $create_table = mysql_fetch_array($req_create_table);
        echo $create_table[1].";\n";
        echo "\n--\n-- Filling de $table\n\n";
        $sql_fill_table = "SELECT * FROM $table";
        $req_fill_table = mysql_query($sql_fill_table);
        while ($row = mysql_fetch_assoc($req_fill_table)) {
        $line_insert = "INSERT INTO $table (";
        $l_value = ") VALUES (";
        foreach ($row as $field => $value) {
            $line_insert .= "`$field`, ";
            $l_value .= "'".mysql_real_escape_string($value)."', ";
        }
        $line_insert = substr($line_insert, 0, -2);
        $l_value = substr($l_value, 0, -2);
        echo $line_insert.$l_value.");\n";
       }
    }
    echo "--\n";
    echo "-- Dump of base $db_base finished )\n";
    echo "--\n";
} else if ($action=='dump_to_disk') {
    $file = fopen($db_base.".sql", 'w') or die('bug!');
    fwrite($file, "--\n");
    fwrite($file,  "-- Dump of the base $db_base\n");
    fwrite($file,  "--\n");
    $sql_tables = "SHOW TABLES";
    $req_tables = mysql_query($sql_tables);
    while (list($table) = mysql_fetch_row($req_tables)) {
        if (get_para_value($table,'no')=='no') continue;
        fwrite($file,  "\n--\n-- Table $table\n\n");
        fwrite($file,  "DROP TABLE IF EXISTS $table;\n");
        $sql_create_table = "SHOW CREATE TABLE $table";
        $req_create_table = mysql_query($sql_create_table);
        $create_table = mysql_fetch_array($req_create_table);
        fwrite($file,  $create_table[1].";\n");
        fwrite($file,  "\n--\n-- Remplissage de $table\n\n");
        $sql_fill_table = "SELECT * FROM $table";
        $req_fill_table = mysql_query($sql_fill_table);
        while ($row = mysql_fetch_assoc($req_fill_table)) {
        $line_insert = "INSERT INTO $table (";
        $l_value = ") VALUES (";
        foreach ($row as $field => $value) {
            $line_insert .= "`$field`, ";
            $l_value .= "'".mysql_real_escape_string($value)."', ";
        }
        $line_insert = substr($line_insert, 0, -2);
        $l_value = substr($l_value, 0, -2);
        fwrite($file,  $line_insert.$l_value.");\n");
       }
    }
    fwrite($file, "--\n");
    fwrite($file, "-- Dump of base $db_base finished (not even a crash!)\n");
    fwrite($file, "--\n");
    fclose($file);
    echo "dump done";
}
?>