File "sauvegarde.php"

Full Path: /home/analogde/www/Documents/sauvegarde.php
File size: 11.14 KB
MIME-type: text/x-php
Charset: utf-8

<?php

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

	header( 'content-type: text/html; charset=utf-8' );
	
	error_reporting(0);
	date_default_timezone_set('Europe/Paris');

	
	echo '<a href="admin.php">Retour...</a>';
	echo '<br><br>';
	
	if (isset($_GET['restauration'])) 
	{
		$jour = date('d-m-Y');
		$heure = date("H_i_s");
		$infos = $jour.'-'.$heure;
		$fichier = "backup_user-" .$infos .".txt"; 
		DB_to_file($fichier);
		echo " Un nouveau point a été crée : " .$fichier;
		echo '<br>';
		echo '<br>';
	
	}	
	
	if (isset($_GET['riri'])) 
	{
		
		//$source = '1formatik.zip';
		//$destination = 'dossier/copie_1formatik.zip';
		
		//chdir("backup");
		//echo " Path " .getcwd();
		//echo '<br><br>';
		
		// copier un fichier du répertoire backup -> insertion
		$source = getcwd() .'/backup/' .$_GET['original'];
	    $destination = getcwd() .'/insertion/' .$_GET['riri'];
		//echo $source . " --- " . $destination;
		//echo '<br><br>';
		
		if (!copy($source, $destination)) 
		{  
			echo "[Erreur] : Fichier non copié !.";
			echo '<br><br>';			
		}  
		else 
		{  
			echo "Le serveur a fait un copie de la sauvegarde " .$_GET['original'] ." dans le fichier temporaire " .$_GET['riri'] ." qui pourra être modifier.";
			echo '<br><br>';
			echo " Ce dernier accessible pour une insertion à partir de la page admin - bouton Import Modification.";
			echo '<br><br>';
		}  
		
	}	
	
	
	$path = "backup";
	
	$results = array();
	$cpt = 0;

	
	chdir($path);
	array_multisort(array_map('filemtime', ($files = glob("*.*"))), SORT_DESC, $files);
	foreach($files as $filename)
	{
		//echo "<li>".substr($filename, 0, -4)."</li>";
		//echo "<li>" .$filename    ."</li>";
		
		if(is_file($filename))
		{
			$results[$cpt][0] = $filename;
			$results[$cpt][1]= filesize($filename);
			$results[$cpt][2]= filemtime($filename);
			$results[$cpt][3]= date("F d Y H:i:s", filemtime($filename));
			$cpt = $cpt + 1;
		}
	}
	
	
	//$files = array_diff(scandir($path), array('.', '..'));
	
	//$results = array();
	//$cpt = 0;
	
	//foreach($files as $file) 
	//{
	//	if (!in_array($file,array(".",".."))) 
	//	{ 
	//		$results[$cpt][0] = $file;
	//		$results[$cpt][1]= filesize('backup/'.$file);
	//		$results[$cpt][2]= filemtime('backup/'.$file);
	//		$results[$cpt][3]= date("F d Y H:i:s.", filemtime('backup/'.$file));
	//		$cpt = $cpt + 1;
	//	}
	//}
	
	//echo "<pre>";
	//print_r($results);
	//echo "</pre>";
	
	//$tab = listing_fichier_repertoire('backup'); 
	
	//for($i=0; $i < count($tab); $i++)
	//{
	//	$tab_fichier = $tab[$i][0];
	//	$tab_ts[] = $tab[$i][1];
	//}
		
	//$tab_sort = $tab_ts;
	//sort($tab_sort);
		
	//$tab_reverse = array_reverse($tab_sort);

	//for($i=0; $i < count($tab); $i++)
	//{
	//	$element = $tab_reverse[$i];
	//	$position = array_search($element, $tab_ts);
		//echo '<br><br>';
	//	$file = $tab[$position][0];
		//echo $position ." " .$tab[$position][0]  ."  " .filesize($file);
		
	//	$tab_affiche[$i][0] = $tab[$position][0];
	//	$tab_affiche[$i][1] = filesize($file);
	//}
	
	
//	echo '<a href="' .$_SERVER['HTTP_REFERER'] .'">retour ...</a> ; 

?>

<link rel="stylesheet" href="//code.jquery.com/ui/1.13.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="/resources/demos/style.css">
<script src="https://code.jquery.com/jquery-3.6.0.js"></script>
<script src="https://code.jquery.com/ui/1.13.1/jquery-ui.js"></script>

<script type="text/javascript">

function fct(fichier)
{
	
	
	//alert(fichier);
	
	$('#add_dialog').dialog({
  modal: true,
  overlay: {
    opacity: 0.7,
    background: "black"
  },


  buttons: {
    "OK": function() {
      $(this).dialog('close');
	  
	
	  
	   //var value = $("#name").val();
	
		//alert(value);
	
	  var filename = $("#add_dialog").find('input[name="name"]').val(); //get the value..
	  
      alert(" >>> " + filename );
	  
	  window.location.href = "sauvegarde.php?original="+fichier+"&riri="+filename;
	  /*if (boxInput === '' ) 
	  {
		alert("Please fill all fields...!!!!!!");
		e.preventDefault();
		} 
		else if (!(email).match(emailReg)) 
		{
			alert("Invalid Email...!!!!!!");
			e.preventDefault();
		} */
	  
    },
    "CANCEL": function() {
      $(this).dialog('close');
      alert("Select name from the list");
    }

  }


});
	
	//https://www.formget.com/jquery-dialog-form/
	// https://www.nicesnippets.com/blog/jquery-ui-dialog-modal-form-example
	// https://stackoverflow.com/questions/37565537/insert-a-form-inside-a-jquery-dialog-box  + ++++
}

function fct_creation_point_restauration()
{
	
	// on faut un submit de la page
	
	 window.location.href = "sauvegarde.php?restauration=yes";
	
}

function fct_importer(filename)
{
		//alert("Pipo");
	
		var folder = "backup";

		$(function () 
		{
			$( "#dialog" ).dialog({
								modal: true,
								title: "Info",
								closeOnEscape: false,
								buttons: {
											Oui: function()
											{
												$( this ).dialog( "close" );
												window.location.href = "import.php?filename=" + filename + "&folder=" + folder;
											},
											Non: function()
											{
												$( this ).dialog( "close" );
													
											}
										 }
			});
			
			$("#action").click(function () 
			{
				$('#dialog').dialog('open');
			});
		});
	
}

</script>

<div id="add_dialog" title="Fichier modifié : saisir un nom" style="display:none;" >
  <form>
    <p>
      Name:
      <input id="bidule" type="text" name="name" />
    </p>
    <!--<p>Group:
      <select>
        <option value="p">p</option>
        <option value="s">s</option>
      </select>
    </p>
	-->
  </form>
</div>

<?php
function DB_to_file($filename)
{
				// sauvegarde du classement dans un fichier
	
				//$sql = "SELECT * FROM parties ORDER by date asc"; 
				$sql = "SELECT * FROM " .$_SESSION['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;
				}
	
				$nom_fichier = $filename;
				chdir("backup");
											
				$fp = fopen($nom_fichier, 'w'); 
				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);
				
				
		
}
?>

<?php	
	
	echo '<div id="dialog" style="display: none" align = "center">Confirmation de l\'import ?.</div>';
	
	echo " Créer un point de restauration manuellement:  &nbsp &nbsp &nbsp &nbsp &nbsp  ";
	
	echo '<button id="action" onclick="fct_creation_point_restauration()">Creation</button>';
	echo '<br><br>';
	

	echo "Liste des points de restaurations de la database:";
	echo '<br><br>';
	
	
	echo "<table border=1 cellpadding=5 cellspacing=0>";
	echo "	<tr>
			<th>Id</th>
			<th>Fichier backup</th>
			<th>Date</th>
			<th>Taille en octets</th>
			<th>Voir</th>
			<th>Copier</th>
			<th>Importer</th>
			</tr>";
			
	//for($i=0; $i < count($tab_affiche); $i++)
	for($i=0; $i < $cpt; $i++)
		
	{
			$j = $i + 1;
			echo "<tr>
				  <td>" .$j ."</td>";	
			//echo '<td>';
			//echo '<a href="restauration.php?fichier=" .$tab_affiche[$i][0].</a>' .$tab_affiche[$i][0] .'</td>';
			echo '<td>'; 
				  //<a href="restauration.php?fichier='.$tab_affiche[$i][0].'" title = "Restauration">';
			//echo  $tab_affiche[$i][0];
			echo $results[$i][0];
			
			//echo '</a>
			echo	'</td>';
			
			$b = '<input type="button" value="Voir">';
			
			
			echo "<td align = 'center'>" .$results[$i][3] ."</td>";
			
			
			echo "<td align = 'center'>" .$results[$i][1] ."</td>";
			
			
			$blabla = $tab_affiche[$i][0];
			$blabla = $results[$i][0];

			
			
			echo "<td align = 'center'>"; 
			//echo "<button type=\"button\" title=\"Visualiser\" id=\"button_voir\" onclick=\"window.location.href='https://analog-design.net/CHESS_2022/visualiser.php?filename=$blabla'\"> Voir </>";
			echo "<button type=\"button\" title=\"Visualiser\" id=\"button_voir\" onclick=\"window.location.href='https://analog-design.net/CHESS_ON/visualiser.php?filename=$blabla&folder=backup'\"> Voir </>";
			echo "</td>";
			
			//echo "<td align = 'center'>"; 
			//echo "<button type=\"button\" title=\"Editer\" id=\"button_editer\" onclick=\"window.location.href='https://analog-design.net/CHESS_2022/editer.php?filename=$blabla'\"> Modifier </>";
			//echo "</td>";
			
			echo "<td align = 'center'>"; 
			echo "<button type=\"button\" title=\"Copie\" id=\"button_copier\" onclick=\"fct('$blabla')\"> Copier </>";
			echo "</td>";
		
	
			//echo "<td align = 'center'>"; 
			//echo "<button type=\"button\" title=\"Importer\" id=\"button_importer\" onclick=\"window.location.href='https://analog-design.net/CHESS_2022/importer.php?filename=$blabla&folder=backup'\"> Importer </>";
			//echo "</td>";
			
			echo "<td align = 'center'>"; 
			echo "<button type=\"button\" title=\"Impoter\" id=\"button_importer\" onclick=\"fct_importer('$blabla')\"> Importer </>";
			echo "</td>";
			
			
			echo '</td>
			      </tr>';
	}
	echo "</table>";
	
	
	?>
	
	<!--
	 <button onclick="window.location.href = 'https://waytolearnx.com';"> Cliquez ici </button>
    -->
	
<?php	 
	
function listing_fichier_repertoire($repertoire) 
{
    $results = array();
    $handler = opendir($repertoire);

	$cpt = 0;
		
	$ts_actuel = strtotime(date("d-m-Y H:i:s"));

	//sauvegarde_edition-13-09-2015-10_06_57.txt
	
    while ($file = readdir($handler)) 
	{
		if($file != "." && $file != ".." ) 
		{
			$file_name = explode(".",$file);
			$date_pieces = explode("-",$file_name[0]);
			$jour = $date_pieces[1];
			$mois = $date_pieces[2];
			$annee = $date_pieces[3];
			$heure_tab = explode("_",$date_pieces[4]);
			
			//echo "<pre>";
			//print_r($date_pieces);
			
			$timestamp = mktime ($heure_tab[0], $heure_tab[1], $heure_tab[2], $mois, $jour, $annee);
			
			$ts_difference = $ts_actuel - $timestamp;
			$ts_7jours = 7 * 24 * 3600;
			if($ts_difference >= $ts_7jours)
			{
				//echo $file ." ----  " .$format ."    " .$ts ."   trop ancien" ;
				//chdir("sauvegarde_echec");
				//unlink($file);
				 $coco = 5;
			}
			
			$results[$cpt][0] = $file;
			$results[$cpt][1]= $timestamp;
			$results[$cpt][2]= filesize($file);
			$cpt = $cpt + 1;
		}
    }
	
	chdir("backup");
	
    closedir($handler);
    return $results;
}

?>