#!/usr/local/bin/php
<?php
	header( 'content-type: text/html; charset=utf-8' );

	error_reporting(0);
	date_default_timezone_set('Europe/Paris');

	$site = $_SERVER["SERVER_NAME"];
	if( ($site == "localhost") || ($site == "127.0.0.1") ) 
	{

		$db_host    = "localhost";
		$db_user   = "root";
		$db_pass   = ""; 
		$db_name   = "test";
	}
	else
	{

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

	
		$db_host    = "analogdepat.mysql.db";
		$db_user   = "analogdepat";
		$db_pass   = "Un92pac007";
		$db_name   = "analogdepat";
		
		
		$jour = date('d-m-Y');
		$heure = date("H_i_s");
		$infos = $jour.'-'.$heure;
		$fichier = 'db-backup-'.$infos.'.sql';
		
		// Chemin absolu de l'emplacement du site
		$chemin_absolu_site = 'www' ;
		// Emplacement sur le FTP du répertoire des sauvegardes
		$repertoire_sauvegardes = $chemin_absolu_site.'/Massage/Backup/' ; 
		
		$fichier = '/homez.65/analogde/www/Massage/Backup/auto_db-backup-'.$infos.'.sql';
	
		system("mysqldump --host=analogdepat.mysql.db --user=analogdepat --password=Un92pac007 --default-character-set=utf8 analogdepat configuration client gestion reservation2015 > $fichier");

		
		include('database.php');
		DB_connexion(); 
		
		$jour = date('d-m-Y');
		$heure = date("H_i_s");
		$infos = $jour.'-'.$heure;
		$fichier = "backup_auto-" .$infos .".txt"; 
		chdir("backup/WORKAREA/WEBSITE/site_perso");
		DB_to_file($fichier);
	
function DB_to_file($fichier)
{
				//chdir($chemin);
				
				$sql = "SELECT * FROM parties ORDER by date asc"; 
				$results = mysql_query($sql);
				$nb_parties = mysql_num_rows($results);
							
				while($row = mysql_fetch_assoc($results))
				{
					$blanc = $row['w_name'];
					$noir = $row['b_name'];
					$date = $row['date'];
					$resultat = $row['result'];
					$date_partie = date('d/m/Y', $date);
					
					if($resultat == 1)
					{
						$_score_blanc = 1;
						$_score_noir = 0;
					}
					if($resultat == 2)
					{
						$_score_blanc = 0;
						$_score_noir = 1;
					}
					if($resultat == 3)
					{
						$_score_blanc = 0.5;
						$_score_noir = 0.5;
					}
				
					$table_csv[0] = $date_partie;
					$table_csv[1] = $blanc;
					$table_csv[2] = $noir;
					$table_csv[3] = $_score_blanc;
					$table_csv[4] = $_score_noir;

					$table_fichier[] = $table_csv;
				}
				
				//echo getcwd();
				
										
				$fp = fopen($fichier, 'a+'); 
				for ($i=0; $i < $nb_parties; $i++)
				{
					$str_comma_separated = implode(";", $table_fichier[$i]);
					fputs ($fp, $str_comma_separated);
					if($i != $nb_parties - 1)
					fputs($fp, "\n");
				}
						
				fclose($fp);
				
				//echo "Done";
				
			
}
	

	
	
	
	

	

?>