<?php
if(!defined('CRON')) { header('Location: ../'); exit; }
print '<br /><br />Début de la sauvegarde de la base de données ; il est : '.date('d/m/Y H:i:s');

function dumpTable($fp, $nomtable) {
	global $config;
	include(_FCORE."../game/tables.php");
	$bdd = new bdd();

	if (!function_exists('gzopen')) $fwrite = 'fwrite';
	else $fwrite = 'gzwrite';

	$fwrite($fp, "-- Données de la table $nomtable\n\n");
	$bdd->connexion();
	$nomtable = ${'table_'.$nomtable};
	$res = $bdd->query('SELECT * FROM '.$nomtable.';');
	$bdd->deconnexion();

	for($i = 0; $i < $bdd->num_rows; $i++){
		$strFieldsNames = '';
		$strFieldsValues = '';

		foreach ($res[$i] as $field_name => $field_value){
			if ($strFieldsNames) $strFieldsNames .= ', ';
			$strFieldsNames .= "$field_name";

			if($strFieldsValues) $strFieldsValues .= ', ';
			$strFieldsValues .= "'" . addslashes($field_value) . "'";
		}
		$fwrite($fp, 'INSERT INTO '.$nomtable.' ('.$strFieldsNames.') VALUES ('.$strFieldsValues.");\n");
	}
	$fwrite($fp, "\n\n");
}

function dumpTableXML($fp, $nomtable) {
	global $config;
	include(_FCORE."../game/tables.php");
	$bdd = new bdd();
	$bdd->connexion();
	$nomtable = ${'table_'.$nomtable};
	$res = $bdd->query('SELECT * FROM '.$nomtable.';');
	$bdd->deconnexion();

	fwrite($fp, "<table name=\"$nomtable>\"\n");
	for($i = 0; $i < $bdd->num_rows; $i++){
		fwrite($fp, "\t<enregistrement i=\"$i\">\n");
		foreach ($res[$i] as $field_name => $field_value){
			fwrite($fp, "\t\t<$field_name>".htmlentities($field_value)."</$field_name>\n");
		}
		fwrite($fp, "\t</enregistrement>\n");
	}
	fwrite($fp, "</table>\n");
}

//Création du fichier de destination
$fname = _FCORE."backup/".date('Y-m-d H-i-s');
if (true || !function_exists('gzopen')) {
	$fp = fopen($fname.'.sql', 'w+');
	$ext = 'sql';
	$fwrite = 'fwrite';
}
else {
	$fp = gzopen($fname.'.gz', 'w+');
	$ext = 'gz';
	$fwrite = 'gzwrite';
}
$fpx = fopen($fname.'.xml', 'w+');

if ($fp) {
	$fwrite($fp, "-- Utilitaire de sauvegarde de base de données de HB\n-- Sauvegarde du ".date('d/m/Y H:i:s')."\n-- Version du jeu : ".$config['version']."\n\n");
	fwrite($fpx, '<?xml version="1.0" encoding="UTF-8" ?>'."\n<!DOCTYPE SavBDD_HB>\n");

	//Liste des tables à sauvegarder
	$list = array('alliances', 'alliances_attente', 'alliances_chat', 'alliances_creation', 'alliances_grade', 'flottes', 'flottes_combats', 'planete', 'user');

	foreach($list as $table) {
		dumpTable($fp, $table);
		dumpTableXML($fpx, $table);
	}
}

if (!function_exists('gzopen')) fclose($fp);
else gzclose($fp);

// TODO tester l'envoie de mail !!
if (@$envoyer_mail) {
	require_once(_FCORE."../game/Class/class.phpmailer.php");

	$mail = new PHPmailer();
	$mail->SetLanguage('fr', _FCORE."../game/Class/");
	$mail->IsSMTP();
	$mail->Host='s-fr.com';
	$mail->From='no-reply@halo-battle.s-fr.com';
	$mail->FromName='Halo-Battle';
	$mail->SMTPAuth=true;
	$mail->Username='no-reply@halo-battle.s-fr.com';
	$mail->Password='hD3e2nXu';

	$mail->AddAddress('sauv@gmail.com');
	$mail->AddReplyTo('no-reply@halo-battle.s-fr.com');
	$mail->Subject = 'Sauvegarde du '.date('d/m/Y à H:i:s');
	$mail->Body = 'Utilitaire de sauvegarde de base de données de HB version '.$config['version'];
	$mail->AddAttachment($fname.'.'.$ext);

	$return = $mail->Send();
	$mail->SmtpClose();
}

print '<br />Fin de la sauvegarde de la base de données ; il est : '.date('d/m/Y H:i:s').'<br />';
?>