<? 
include ('logue.php'); 
include ('config.php');
$login = $_SESSION["login"];
$sqlid = "SELECT id FROM matgraph_users WHERE login='$login' AND id = idjoueur ORDER BY ID ASC limit 1";
$reqid = db_mysql_Query($sqlid) or die('Erreur SQL !<br>'.$sqlid.'<br>'.mysql_error());
$dataid = mysql_fetch_array($reqid);
$idlogin = $dataid['id'] ;

$sql = "SELECT id  FROM matgraph_banni WHERE login = '".$_SESSION["login"]."'";
$res = db_mysql_Query($sql)or die('erreur sql<br>'.$sql.'<br>'.mysql_error());
$num = mysql_num_rows($res);
if ( $num > 0 ) {
		echo '<script language="javascript" type="text/javascript">
		<!--
		window.location.replace("http://www.sg-univers.fr/");
		-->
		</script>';
		exit;
}


$sql = "SELECT id,date  FROM matgraph_vacance WHERE id != '0' ORDER by date ASC";
$res = db_mysql_Query($sql)or die('erreur sql<br>'.$sql.'<br>'.mysql_error());
$num = mysql_num_rows($res);
if ( $num > 0 ) {
  While ( $row = mysql_fetch_array($res) ) {
 	$datetime = $row['date'];

	ereg ("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2}) ([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})", $datetime, $thetime);
	$time = mktime($thetime[4],$thetime[5],$thetime[6],$thetime[2],$thetime[3],$thetime[1]);

	$diftemps = time() - $time;
	$difheure = round($diftemps / 3600,3); 

	if($difheure > 72) {
		$sql4 = "DELETE FROM matgraph_vacance WHERE id = '$row[id]'";
		$res4 = db_mysql_Query($sql4)or die('erreur sql<br>'.$sql4.'<br>'.mysql_error());
	}
	else{
	break;
	}
  }
}

// Récupération des données :

$llogin = addslashes($_POST["New_speudo"]);
$Ex_pass = addslashes($_POST["Ex_pass"]);
$password = addslashes($_POST["New_pass"]);
$password2 = addslashes($_POST["New_pass2"]);
$mail = addslashes($_POST["New_email"]);
$permmail = addslashes($_POST["New_permmail"]);
$vacance = addslashes($_POST["vacance"]);
$design = addslashes($_POST["design"]);
$suppression = addslashes($_POST["suppression"]);

// donnée requise de la db :

$sql = "SELECT mail, password, vacance FROM matgraph_users WHERE id='$idlogin'";
$res = db_mysql_Query($sql)or die('erreur sql<br>'.$sql.'<br>'.mysql_error());
$row = mysql_fetch_array($res); 

$actuel_pass = $row[password] ;
$actuel_mail = $row[mail] ;


//maj vacance :

$req = "SELECT id FROM matgraph_vacance WHERE idjoueur = $idlogin";
$res = db_mysql_Query($req) or die('Erreur SQL! <br>'.$req.'<br>'.mysql_error());
$num = mysql_num_rows($res);
if( $num == "0" ) {

	if($vacance == "OUI" AND $row[vacance] == "0000-00-00 00:00:00") {
		ereg ("([0-9]{4})-([0-9]{1,2})-([0-9]{1,2}) ([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})", $row[vacance], $thetime);
		$time = mktime($thetime[4],$thetime[5],$thetime[6],$thetime[2],$thetime[3],$thetime[1]);
		
		$requete = "UPDATE matgraph_users SET vacance = now() WHERE id='$idlogin'";
		$resultat = db_mysql_Query($requete)or die('erreur sql<br>'.$requete.'<br>'.mysql_error()); 
	
	}

else 
{	
	if($vacance == "OUI" AND $row[vacance] == "0000-00-00 00:00:00") {
	echo '<script language=javascript> alert ("Vous devez attendre au minimum 72h avant de pouvoir vous remettre en mode pause");</script>';
	echo '<script language="javascript" type="text/javascript">
	<!--
	window.location.replace("profil.php");
	-->
	</script>';
	}
}
}
// maj desgin :
if(is_numeric($design))
{
	$requete = "UPDATE matgraph_users SET design = '$design' WHERE id='$idlogin'";
	$resultat = db_mysql_Query($requete)or die('erreur sql<br>'.$requete.'<br>'.mysql_error());
}

// regarde modif mail :

if($mail !== $actuel_mail) 
{
	//vérifie mail existant :
	$testmail = db_mysql_Query("SELECT * FROM matgraph_users WHERE mail ='$mail' AND id=idjoueur AND id!=$idlogin");
	if(mysql_num_rows( $testmail ) == 0)
	{
		//si ok on modifie :
		$requete = "UPDATE matgraph_users SET mail='$mail' WHERE id='$idlogin'";
		$resultat = db_mysql_Query($requete)or die('erreur sql<br>'.$requete.'<br>'.mysql_error());
	}
	else
	{
		echo '<script language=javascript> alert ("Cette adresse email est deja utilisée");</script>';
		echo '<script language="javascript" type="text/javascript">
		<!--
		window.location.replace("profil.php");
		-->
		</script>';
		exit;
	}
}


// regarde modif password :
//si l'ancien pass est pas remplit mais le nouveau oui :
if (empty($Ex_pass) && empty($password))
{
	echo '<script language=javascript> alert ("L\'ancien mot de pass n\'est pas valide");</script>';
	echo '<script language="javascript" type="text/javascript">
	<!--
	window.location.replace("profil.php");
	-->
	</script>';
	exit;
}
//si l'ancien pass est remplit :
if ($Ex_pass != "" && $password != "")
{
	//hachage md5 :
	$key = "8Z5q7?&Pu!?M!?5Xs";
	$the_ex_pass = md5($Ex_pass.$key);
	//$the_ex_pass = md5($Ex_pass);
	//vérifie si c'est le bon : 
	if($actuel_pass !== $the_ex_pass)
	{
		echo '<script language=javascript> alert ("L\'ancien mot de pass n\'est pas valide");</script>';
		echo '<script language="javascript" type="text/javascript">
		<!--
		window.location.replace("profil.php");
		-->
		</script>';
		exit;
	}
	//vérifie si les deux nouveaux pass sont identique : 
	if ( $password != $password2 ) {
		echo '<script language=javascript> alert ("Les deux mots de pass sont differents");</script>';
		echo '<script language="javascript" type="text/javascript">
		<!--
		window.location.replace("profil.php");
		-->
		</script>';
		exit;
	}
	//cryptage new pass :
	$key = "8Z5q7?&Pu!?M!?5Xs";
	$new_password = md5($password.$key);
	//$new_password = md5($password);
	//maj password :
	$requete = "UPDATE matgraph_users SET password='$new_password' WHERE id='$idlogin'";
	$resultat = db_mysql_Query($requete)or die('erreur sql<br>'.$requete.'<br>'.mysql_error());

}

//maj suppression
$req = "SELECT date FROM matgraph_suppression WHERE idlogin= $idlogin limit 1";
$res = db_mysql_Query($req) or die('Erreur SQL! <br>'.$req.'<br>'.mysql_error());
$num = mysql_num_rows($res);
if ( $num == 0 )
{
	if($suppression !== "NON")
	{
		$requete = "INSERT INTO matgraph_suppression VALUES ('$idlogin', now())";
		$resultat = db_mysql_Query($requete)or die('erreur sql<br>'.$requete.'<br>'.mysql_error());
	}

}
else
{
	if($suppression == "NON")
	{
		$requete = "DELETE FROM matgraph_suppression WHERE idlogin=$idlogin";
		$resultat = db_mysql_Query($requete)or die('erreur sql<br>'.$requete.'<br>'.mysql_error());
	}
}

echo '<script language=javascript> alert ("Votre profil a bien été sauvegardé.");</script>';

echo '<script language="javascript" type="text/javascript">
<!--
window.location.replace("profil.php");
-->
</script>';

?>


