<?php

	session_start ();

	include('database.php');
	DB_connexion(); 

	define('_PATH', dirname(__FILE__));
	
	header( 'content-type: text/html; charset=utf-8' );
		
	echo '<a href="blitz_restauration_tables.php">retour ...</a>  
	<br>
	<br>';
	
	$_SESSION['classement'] = "BLITZ_classement";
	$_SESSION['parties'] = "BLITZ_parties";
	$_SESSION['evolution'] = "BLITZ_evolution";
	$_SESSION['elo_affectation'] = "BLITZ_affectation";

function unzip_file($file_selected)
{
	
	$chemin_source = _PATH. "/backup_blitz/" .$file_selected;
	$chemin_destination = _PATH. "/unzip/" .$file_selected;

	//echo $chemin_source;
	//echo "<br/>";
	//echo "<br/>";
	//echo $chemin_destination;
	
	copy($chemin_source, $chemin_destination);

	$path = _PATH ."/unzip/" .$file_selected;	
	
	//echo " +++++++ " .$path;
	//echo "<br/>";
	//echo "<br/>";
	
	chdir("unzip");
	
	//echo "<br/>";
	//echo "<br/>";
	
	//echo getcwd() . "\n";
	
	//echo "<br/>";
	//echo "<br/>";
	
	
	$zip = new ZipArchive;
	if ($zip->open($file_selected) === TRUE) 
	{
		$zip->extractTo(_PATH ."/unzip/");
		$zip->close();
		//echo "******** Unzip du fichier : ok (répertoire unzip) ********";
		//echo "<br/>";
		//echo "<br/>";
	} 
	else 
	{
		//echo 'Unzip fail !.';
	}
}

function folder_clean($file_selected)
{
   
	// List of name of files inside specified folder 
	$files = glob($file_selected.'/*');  
   
	// Deleting all the files in the list 
	foreach($files as $file) 
	{ 
   		if(is_file($file))
		{	
			unlink($file);  
			//echo $file ."<br/>";
		}	
	}
}	


function restaure_mysql_table_dump($restore_file)
{

		$mysqlDatabaseName = "analogdepat";
		$mysqlUserName = "analogdepat";
		$mysqlPassword ="Un92pac007";
		$mysqlHostName = "analogdepat.mysql.db";
		$mysqlExportPath ='nom-du-fichier-dexport.sql';

/*
		echo "Export les 3 tables.";
		echo "<br/>";
		echo "<br/>";

		$command='mysqldump --opt -h' .$mysqlHostName .' -u' .$mysqlUserName .' -p' .$mysqlPassword .' ' .$mysqlDatabaseName  .' BLITZ_classement BLITZ_evolution BLITZ_parties '  .' > ' .$mysqlExportPath;


		echo " >>> " .$command;
		echo "<br/>";
		echo "<br/>";

		exec($command,$output=array(),$worked);

		switch($worked)
		{
			case 0:
				echo 'La base de données <b>' .$mysqlDatabaseName .'</b> a été stockée avec succès dans le chemin suivant '.getcwd().'/' .$mysqlExportPath .'</b>';
				break;
			case 1:
				echo 'Une erreur s est produite lors de la exportation de <b>' .$mysqlDatabaseName .'</b> vers'.getcwd().'/' .$mysqlExportPath .'</b>';
				break;
			case 2:
				echo 'Une erreur d exportation s est produite, veuillez vérifier les informations suivantes : <br/><br/><table><tr><td>MySQL Database Name:</td><td><b>' .$mysqlDatabaseName .'</b></td></tr><tr><td>MySQL User Name:</td><td><b>' .$mysqlUserName .'</b></td></tr><tr><td>MySQL Password:</td><td><b>NOTSHOWN</b></td></tr><tr><td>MySQL Host Name:</td><td><b>' .$mysqlHostName .'</b></td></tr></table>';
				break;
		}
*/

/*
		echo "******** Purger les tables qui vont être restaurées : ********";
		echo "<br/>";
		echo "<br/>";

		$str1 = "TRUNCATE TABLE " .$_SESSION['classement'];
		$str2 = "TRUNCATE TABLE " .$_SESSION['evolution'];
		$str3 = "TRUNCATE TABLE " .$_SESSION['parties'];

		echo $str1;
		echo '<br>';
		echo $str2;
		echo '<br>';
		echo $str3;
		echo '<br>';
		echo '<br>';

		mysql_query($str1);
		mysql_query($str2);
		mysql_query($str3);
*/

		echo "******** Restauration du fichier dump : ********";
		echo "<br/>";
		echo "<br/>";

		$command='mysql -h' .$mysqlHostName .' -u' .$mysqlUserName .' -p' .$mysqlPassword .' ' .$mysqlDatabaseName .' < ' .$restore_file;
		exec($command,$output=array(),$worked);

		switch($worked)
		{
			case 0:
				echo 'Les données du fichier <b>' .$restore_file .'</b> ont été importées avec succès dans la base de données <b>' .$mysqlDatabaseName .'</b>';
				break;
			case 1:
				echo 'Une erreur s est produite lors de l importation. Veuillez vérifier si le fichier se trouve dans le même dossier que ce script. Vérifiez également à nouveau les données suivantes.:<br/><br/><table><tr><td>MySQL Database Name:</td><td><b>' .$mysqlDatabaseName .'</b></td></tr><tr><td>MySQL User Name:</td><td><b>' .$mysqlUserName .'</b></td></tr><tr><td>MySQL Password:</td><td><b>NOTSHOWN</b></td></tr><tr><td>MySQL Host Name:</td><td><b>' .$mysqlHostName .'</b></td></tr><tr><td>MySQL Import Dateiname:</td><td><b>' .$mysqlImportFilename .'</b></td></tr></table>';
				break;
		}





/*



	$db_host    = "analogdepat.mysql.db";
   	$db_user   = "analogdepat";
 	$db_pass   = "Un92pac007";
	$db_name   = "analogdepat";

	//$file_selected = "blitz_db-backup-09.02.2020_18:25.zip";
	//$restore_file = _PATH ."/unzip/" .$file_selected;	
	
	// purger les tables avant la restauration
	$str0 = "TRUNCATE TABLE " .$_SESSION['elo_affectation'];
	$str1 = "TRUNCATE TABLE " .$_SESSION['classement'];
	$str2 = "TRUNCATE TABLE " .$_SESSION['evolution'];
	$str3 = "TRUNCATE TABLE " .$_SESSION['parties'];

	echo $str0;
	echo '<br>';
	echo $str1;
	echo '<br>';
	echo $str2;
	echo '<br>';
	echo $str3;
	echo '<br>';

	//mysql_query($str0);
	//mysql_query($str1);
	//mysql_query($str2);
	//mysql_query($str3);
	
	//$cmd = "mysql -h {'$analogdepat.mysql.db'} -u {'$db_user'} -p{'$db_pass'} {'$analogdepat'} < '$restore_file'";
	
	
		$mysqlDatabaseName = "analogdepat";
		$mysqlUserName = "analogdepat";
		$mysqlPassword ="Un92pac007";
		$mysqlHostName = "analogdepat.mysql.db";
		$mysqlExportPath ='nom-du-fichier-dexport.sql';

		//Veuillez ne pas modifier les points suivants
		//Exportation de la base de données et résultat
		
		//	//system("mysqldump --host=analogdepat.mysql.db --user=analogdepat --password=Un92pac007 --default-character-set=utf8 analogdepat configuration client gestion reservation2015 > $fichier");

		
		//$cmd = 'mysql -h ' .$mysqlHostName .' -u ' .$mysqlUserName .' -p ' .$mysqlPassword .' ' .$mysqlDatabaseName .' < ' .$fichier;
		
		//$command_update = 'mysql -h' .$DB_HOST .' -u' .$DB_USER .' -p' .$DB_PASSWORD .' ' .$DB_NAME .' < ' .$import_update;
		
		$toto = "blitz_db-backup-10.02.2020_20_24.sql";
		
		$cmd = 'mysql -h analogdepat.mysql.db -u analogdepat -p Un92pac007  analogdepat < ' .$toto;		
		
		// $cmd = $mySQLDir.' -h '.$dbhost.' --user='.$dbuser.' --password='.$dbpass.' < "'.$file.'"';

		//$cmd = $mySQLDir.' -h '.$dbhost.' --user='.$dbuser.' < "'.$file.'"';

		//$cmd = 'mysql -h analogdepat.mysql.db -u analogdepat -p Un92pac007  analogdepat < ' .$toto;		

    

//$cmd = "$mysql --host=analogdepat.mysql.db --user=analogdepat --password=$pass -D $dbname -e 'source $base_sql_file'";
//exec($cmd, $output, $retvar);
 
		
	//	echo $cmd;
		
	//	exec('"'.$cmd.'"',$out ,$retval);
		
		//exec($command,$output=array(),$worked);
	
	//mysql -u admin -p`cat /etc/psa/.psa.shadow` dbname < dbname.sql
	
	//exec($cmd);

    //echo "Command returned $return, and output:\n";
    //var_dump($output);

	//echo "Traitement réussi.";
	
	$command='mysql -h ' .$mysqlHostName .' -u ' .$mysqlUserName .' -p ' .$mysqlPassword .' ' .$mysqlDatabaseName .' < ' .$toto;

	

	echo $command;
	
	echo '<br>';
	

exec($command,$output=array(),$worked);
switch($worked){
case 0:
echo 'Les données du fichier <b>' .$mysqlImportFilename .'</b>ont été importées avec succès dans la base de données. <b>' .$mysqlDatabaseName .'</b>';
break;
case 1:
echo 'Une erreur s est produite lors de l importation. Veuillez vérifier si le fichier se trouve dans le même dossier que ce script. Vérifiez également à nouveau les données suivantes.:<br/><br/><table><tr><td>MySQL Database Name:</td><td><b>' .$mysqlDatabaseName .'</b></td></tr><tr><td>MySQL User Name:</td><td><b>' .$mysqlUserName .'</b></td></tr><tr><td>MySQL Password:</td><td><b>NOTSHOWN</b></td></tr><tr><td>MySQL Host Name:</td><td><b>' .$mysqlHostName .'</b></td></tr><tr><td>MySQL Import Dateiname:</td><td><b>' .$mysqlImportFilename .'</b></td></tr></table>';
break;
}

// https://www.ionos.fr/assistance/hebergement/utiliser-la-base-de-donnees-mysql-pour-un-projet-web/sauvegarder-et-restaurer-une-base-de-donnees-mysql-a-laide-de-php/


*/

}

	
/***********************************************************************************************************/	
	
	$_SESSION["filename_restauration"] = $_GET['fichier'];
	
	echo "******** Fichier archive choisi : ********";
	echo '<br>';
	echo $_GET['fichier'];
	
	echo '<br>';
	echo '<br>';
	
	//echo "Fichier choisi: " .$_SESSION["filename_restauration"];
	//echo '<br>';
	//echo '<br>';


	folder_clean("unzip");

	unzip_file($_SESSION["filename_restauration"]);
	
	$path_parts = pathinfo($_SESSION["filename_restauration"]);

	//echo $path_parts['dirname'], "\n";
	//echo $path_parts['basename'], "\n";
	//echo $path_parts['extension'], "\n";
	//echo $path_parts['filename'], "\n"; // depuis PHP 5.2.0
	
	$fichier = $path_parts['filename'].".sql";

	echo "******** Fichier à traiter : ********";
	echo '<br>';
	echo $fichier;
	
	echo '<br>';
	echo '<br>';


	$restore_file = _PATH ."/unzip/" .$fichier;	
	
	
	echo "******** Restauration des tables contenues dans  : " .$restore_file ." ********";
	echo '<br>';
	echo '<br>';
	
	echo '<br>';
	echo '<br>';
	
	$fn = fopen($restore_file,"r");
  
	while(! feof($fn))  
	{
		$result = fgets($fn);
		echo $result;
		echo '<br/>';
	}

	fclose($fn);
	
	//$fichier = "blitz_db-backup-10.02.2020_20_24.sql";

	//restaure_mysql_table_dump($restore_file);

	echo '<br>';
	echo '<br>';

	echo "Done"

?>