<?php
/****************************************************************************************
SMI (Services Maintenance Interventions) est une gestion libre de maintenance et de SAV

Copyright (C) 2006  Sylvain FATOME

This program 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.

This program 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 this program
(in the directory docs); 
if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 
Boston, MA  02110-1301, USA.

Contact : galawa@free.fr
****************************************************************************************/

session_start(); 
?>
<?php if ((@$HTTP_SESSION_VARS["etat_log"] <> "connecte") or (@$HTTP_SESSION_VARS["niv_log"] <> 9)) header("Location: ../index.php") ?>
<?php include ("../inc/config.inc.php") ?>
<?php require_once('../classes/mysqlbckup/mysql_backup.class.php'); ?>
<?PHP
	$code_session = @$HTTP_SESSION_VARS["code_log"];
	if ($code_session == "") // Test si pas encore connecté
		$code_session = "?";
	$datecourante = date("Y-m-d");
	$heurecourante = date("H:i:s");
	$urlpr = $_SERVER['HTTP_REFERER'];
	$racinesrv = $racine_web;
	$urlprecedent = ereg_replace($racinesrv,"",$urlpr); // "Nettoyage" si on était en domaine principal
	$urlcourant = $_SERVER['REQUEST_URI'];
	
	$log_ip = $_SERVER['REMOTE_ADDR']; // Récupération adresse IP

	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rechlog  = "INSERT INTO " .TBLOG;
	$rechlog .= " (log_code,log_date,log_heure,log_urlprec,log_url,log_ip) VALUES";
	$rechlog .= " ('$code_session','$datecourante','$heurecourante','$urlprecedent','$urlcourant','$log_ip')";
	// $rslog = mysql_query($rechlog) or die(mysql_error());
	$rslog = mysql_query($rechlog);
	// mysql_free_result($rslog);
	mysql_close($conn2);
?>
<?php
	function datefr($dateang) 
	{
		$split = split("-",$dateang);
		$annee = $split[0];
		$mois = $split[1];
		$jour = $split[2];
		return "$jour"."-"."$mois"."-"."$annee";
	}

	$typsvg = strip_tags(trim($_GET['td']));
	
	$titre = "SAUVEGARDE DES DONNEES";
	
	if (($typsvg == 1) OR ($typsvg == 2))
	{
	
		$backup_obj = new MySQL_Backup();

		$backup_obj->server = DBHOST;
		$backup_obj->port = 3306;
		$backup_obj->username = DBUSER;
		$backup_obj->password = DBPASS;
		$backup_obj->database = DBNAME;

		// On autorise le DROP TABLES IF EXISTS avant CREATE TABLE 
		$backup_obj->drop_tables = true;

		// On autorise les commentaires
		$backup_obj->comments = true;

		// Répertoire où l'on sauve le fichier (inutilisé car téléchargement)
		$backup_obj->backup_dir = '/';

		// Format par défaut du fichier
		$backup_obj->fname_format = 'd_m_Y';

		// On sauve toutes les tables
		$backup_obj->tables = array();

		// On dit que l'on veut envoyer en mode download
		$task = MSB_DOWNLOAD;


		// On compresse
		$use_gzip = true;

		switch ($typsvg)
		{
		case 1 :
				// Nom du fichier de sauvegarde
				$filename = 'svg_smi_struct_'.date(d_m_Y).'-'.date(H_i_s).'.sql.gz';

				// On prend que la structure
				$backup_obj->struct_only = TRUE;
		 	break;
			
		case 2 :
				// Nom du fichier de sauvegarde
				$filename = 'svg_smi_all_'.date(d_m_Y).'-'.date(H_i_s).'.sql.gz';

				// On prend la structure et les données
				$backup_obj->struct_only = FALSE;
			break;
			
		default : break;
		}
		
		if (!$backup_obj->Execute($task, $filename, $use_gzip))
		{
			 $msg = $backup_obj->error;
		}
		else
		{
			$msg = "Sauvegarde effectuée le <b>".datefr($datea)."</b>";
		}
		
	}
	else
	{
		header("Location: ../index.php");
	}
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>SMI - ESPACE PRIVE : Sauvegarde donn&eacute;es</title>

<!-- Feuille de style pour les affichages courants -->
<link href="../inc/smi.css" rel="stylesheet" type="text/css">

<!-- Feuille de style & Script pour le menu -->
<link href="../inc/menu.css" rel="stylesheet" type="text/css">
<script language="JavaScript" src="../coolmenus/coolmenus4.js"></script>

<!-- Scripts Javascript pour affichages -->
<script language="JavaScript" src="../inc/aff.js"></script>

</head>

<body>

<?php 
	include_once ("../inc/entete.php"); 
?>
<p align="center"> <span class="titrepage"><?PHP echo $titre ?></span></p>
<p align="center">&nbsp;</p>
<p align="center" class="annot_gras_it"><?PHP echo $msg ?></p>
<table width="100%"  border="0">
<tr>
	<td width="3%">&nbsp;</td>
	<td width="44%">&nbsp;</td>
	<td width="7%">&nbsp;</td>
	<td width="43%"><a href="index.php"><em><img src="../img/index.gif" width="48" height="48" border="0" align="absmiddle"> </em><span class="annot_gras_it">Revenir &agrave; l'accueil</span> </a></td>
	<td width="3%">&nbsp;</td>
</tr>
</table>
<?php 
	include_once ("pied.php")
?>
