<?php
#=
/****************************************************************************
Copyright 2002, Daniel Deycard, Jacques Messines, Stéphanie Roussel, Jean-Marie Treins
(CATICE Académie de Bordeaux)

This file is part of GiBii/Melia.

GiBii/Melia is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

GiBii/Melia is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with GiBii/Melia; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

****************************************************************************/

include("../include/vars_adm.php");
include("../include/util.inc.php");
include("../include/utiljs.php");
include("../sadmin/backup.inc.php");

opensession('Sadmin',false);
if ($sess_login=='admstats') {
	aneantir_session();
	goto("./index.php");
	}

$filenamegz=urldecode(@$_GET['f']);
if (file_exists($filenamegz)) {
	$tcomplet=explode('/',$filenamegz);
	$nom=array_pop($tcomplet);
	prepare_download($nom);
	header("Content-Transfer-Encoding: application/x-gzip");
	readfile($filenamegz);
	exit();
	}

?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
<title>Backup complet de la base</title>
<link rel="stylesheet" href="../include/general.css" />
<base target="_self" />
</head>

<body class="arial_sadmin" bgcolor="white" text="black" link="blue" vlink="purple" alink="red">

<?php

//voir est vrai sauf pour les INSERT où on ne voit que le compteur et pas les contenus des lignes
function ecr($fp,$fz,$s,$voir) {
static $compteur=0;
//fwrite($fp,$s);
gzwrite($fz,$s);
//print "<div style=\"color:red\">$s</div>";
if ($voir) {
	if ($compteur) print $compteur;
	print "<br /><p>".nl2br($s)."</p><br />\n";
	$compteur=0;
	}
else {
	$compteur++;
	if ($compteur%1000==0) print $compteur.' - ';
	if ($compteur%2000==0) superflush();
	if ($compteur%10000==0) voircetendroit();
	
	}
}

function mon_hook($lignesql) {
    global $fp,$fz;
    $ligne=$lignesql.$GLOBALS['crlf'];
 	ecr($fp,$fz,$ligne,false);
}

///////////////////////
//Programme principal//
///////////////////////

connecte($host,$user,$mdp,$db);

print "<h2>Sauvegarde de la base...</h2><hr />\n";

$crlf        = bon_crlf();
$filename    = '../data/sadmin/'.$MODULE.'_'.$VERSION_GIBII.'_'.$db.'_'.date("Y.M.d").".sql";
$filenamegz=$filename.'.gz';
creertousrep($filename);

$restables  = mysql_list_tables($db);
$num_tables = mysql_numrows($restables);
for ($i=0; $i<$num_tables; $i++)
    $tables[$i]=mysql_tablename($restables, $i);

// Envoi des en-têtes

$fp=fopen($filename,'w');
$fz=gzopen($filenamegz,'w9');
$dump_buffer       	 = '';
if (function_exists("apache_get_version"))
	$vapache=@apache_get_version();
else $vapache='non trouvée';
$dump_buffer       	.= "# Backup complet pour $MODULE version $VERSION_GIBII.". $crlf
                   	.  '# DD - JM - JMT, décembre 2001 - mai 2007'. $crlf
                   	.  '#' . $crlf;
$dump_buffer       	.= $crlf
                   	.  '# Créé le        : ' . date('F j, Y, g:i a') . $crlf
                   	.  '# IP             : ' . $sess_ip . $crlf
               		.  '# nom machine    : ' . $sess_machinecliente. $crlf
               		.  '# Version Apache : ' . $vapache. $crlf

                   	.  '# Version PHP    : ' . phpversion() . $crlf
               		.  '# Version MySQL  : ' . mysql_get_server_info().', sur '
                                        	 . mysql_get_host_info() . $crlf
               		.  '# MeliGiBii      : ' . $MODULE.' : ' . $VERSION_GIBII . $crlf
                   	.  '# Base           : ' . $db . $crlf
					.  '# Nb tables		 : ' . $num_tables . $crlf;

ecr($fp,$fz,$dump_buffer,true);
		
for ($i=0; $i<$num_tables; $i++) {
    $table = $tables[$i];
    if (is_table_temp($table)) continue;
    $dump_buffer= $crlf.'# ------------------------------'
                .  $crlf . '#' . $crlf
                .  '# Structure de la table '. $table . $crlf
                .  '#' . $crlf . $crlf
                .  get_table_def($db, $table, $crlf) . $crlf;
    $dump_buffer .= $crlf . '#' . $crlf
                 .  '# Contenu de la table '. $table . $crlf
                 .  '#' . $crlf ;
	ecr($fp,$fz,$dump_buffer,true);
    get_table_content($db, $table, '','mon_hook');
	} // while

$dump_buffer = $crlf;
$dump_buffer .="# FIN backup$crlf";

ecr($fp,$fz,$dump_buffer,true);
fclose($fp);
gzclose($fz);
print "<hr />";
print "<h3><a href=\"../sadmin/sback_traiter.php?f=".urlencode($filenamegz)."\">Télécharger le backup</a>";
print "<br /><br /><a href=\"delbackup.php?f=$filenamegz\">Supprimer le ficher sur le serveur</a>";
print "<br /><br /><a href=\"sadmin_bas2.php\">Retour</a></h3>";

voircetendroit();


/*
$azip=new zipfile();
$xml=file($nomfictxt);
$contenutxt='';
foreach($txt as $ligne) $contenutxt.=$ligne;
$azip->addFile($contenutxt, "$basefic/$basefic.txt");
$contenuzip=$azip->file();
$h=fopen($nomficzip,"w");
fputs($h,$contenuzip);
fclose($h);
*/
?>
</body></html>
