<?php
//form
$form = "<br><center><h4>Changement de mot de passe</h4></center><br><div align='center'>
<form name=\"change_pass\" id=\"change_pass\" method=\"post\" action=\"index.php?action=change_pass\">
<table width=\"400\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
	<tr>
		<td>login : </td>
		<td><input type=\"text\" name=\"login\"></td>
	</tr>
	<tr>
		<td>pass : </td>
		<td><input type=\"text\" name=\"pass\"></td>
	</tr>
	<tr>
		<td colspan=\"2\" align=\"center\"><br>
			<input type=\"submit\" name=\"Submit\" value=\"Changer\" Style=\"border:1px solid #7E7E7E;\">
		</td>
	</tr>
</table>
</form><br/></div>
";

if(!empty($_POST["idsuppr"])) {
	
	$idsuppr = $_POST["idsuppr"];
	
	$req = "SELECT id FROM matgraph_change_pass WHERE id = '$idsuppr'";
	$res = mysql_query($req) or die('Erreur SQL! <br>'.$req.'<br>'.mysql_error());
	$num = mysql_num_rows($res);
	
	if($num > 0) {
		
		$requete = "DELETE FROM matgraph_change_pass WHERE id='$idsuppr'";
		$resultat = mysql_query($requete) or die('Erreur SQL! <br>'.$requete.'<br>'.mysql_error());

		echo '<script language=javascript> alert ("Changement annulé");</script>';
		
	
	}
}

if(!empty($_POST["idconfirm"])) {
	
	$idconfirm = $_POST["idconfirm"];
	
	$req = "SELECT idjoueur,pass,modo1 FROM matgraph_change_pass WHERE id = '$idconfirm'";
	$res = mysql_query($req) or die('Erreur SQL! <br>'.$req.'<br>'.mysql_error());
	$num = mysql_num_rows($res);
	
	if($num > 0) {
		$row = mysql_fetch_array($res); 
		
		$req2 = "SELECT login FROM matgraph_users WHERE id = '$idlogin'";
		$res2 = mysql_query($req2) or die('Erreur SQL! <br>'.$req2.'<br>'.mysql_error());
		$row2 = mysql_fetch_array($res2); 
		
		if($idlogin != $row[modo1]) {
		$requete = "UPDATE matgraph_users SET password='$row[pass]' WHERE id='$row[idjoueur]'";
		$resultat = mysql_query($requete) or die('Erreur SQL! <br>'.$requete.'<br>'.mysql_error());
		
		$requete = "UPDATE matgraph_change_pass SET modo2='$idlogin' WHERE id='$idconfirm'";
		$resultat = mysql_query($requete) or die('Erreur SQL! <br>'.$requete.'<br>'.mysql_error());
		
		echo '<script language=javascript> alert ("Mot de passe changé");</script>';
		
		}
	}
}

if(!empty($_POST["login"]) && !empty($_POST["pass"])) {
	
	$login = $_POST["login"];
	$pass = $_POST["pass"];
	
	// verif login exist
	$req = "SELECT id,date FROM matgraph_users WHERE login = '$login' AND id=idjoueur limit 1";
	$res = mysql_query($req) or die('Erreur SQL! <br>'.$req.'<br>'.mysql_error());
	$num = mysql_num_rows($res);
	
	if($num == 0) {
		echo '<script language=javascript> alert ("Login introuvable");</script>';
		echo $form;
		exit;
	} else {
		$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 < 96) {
			echo '<script language=javascript> alert ("Le compte pas assez vieux pour changement le mot de passe. (4jours min)");</script>';
			echo $form;
			exit;
		}*/
		
		$req = "SELECT id FROM matgraph_axxadmin WHERE idjoueur = '$row[id]' limit 1";
		$res = mysql_query($req) or die('Erreur SQL! <br>'.$req.'<br>'.mysql_error());
		$num = mysql_num_rows($res);
		
		if($num > 0) {
			echo '<script language=javascript> alert ("Compte admin");</script>';
			echo $form;
			exit;
		}
	
	
		// cryptage pass
		$key = "8Z5q7?&Pu!?M!?5Xs";
		$new_passs = md5($pass.$key);
		
		$req = "SELECT id FROM matgraph_change_pass WHERE idjoueur = '$row[id]' AND modo2='no'";
		$res = mysql_query($req) or die('Erreur SQL! <br>'.$req.'<br>'.mysql_error());
		$num = mysql_num_rows($res);
		
		if($num > 0) {
			echo '<script language=javascript> alert ("Demande en cours...");</script>';
			echo $form;
			exit;
		}

	
		$req = "INSERT INTO matgraph_change_pass ( `id` , `idjoueur` , `pass` , `modo1` , `modo2` )
		VALUES (
		'', '$row[id]', '$new_passs', '$idlogin', 'no'
		);";
		$res = mysql_query($req) or die('Erreur SQL! <br>'.$req.'<br>'.mysql_error());
		
		echo $form;
	}
	
} else {
	
	// affiche form login + new pass
	echo $form;

}

$req = "SELECT * FROM matgraph_change_pass WHERE modo2 = 'no' ORDER by id ASC";
$res = mysql_query($req) or die('Erreur SQL! <br>'.$req.'<br>'.mysql_error());
$num = mysql_num_rows($res);
if ( $num > 0 ) {
	echo"<br><br> <table width=\"400\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">";
  While ( $row = mysql_fetch_array($res) ) {
  	
  	$req2 = "SELECT login FROM matgraph_users WHERE id = '$row[idjoueur]'";
		$res2 = mysql_query($req2) or die('Erreur SQL! <br>'.$req2.'<br>'.mysql_error());
		$row2 = mysql_fetch_array($res2); 
		
		$req3 = "SELECT login FROM matgraph_users WHERE id = '$row[modo1]'";
		$res3 = mysql_query($req3) or die('Erreur SQL! <br>'.$req3.'<br>'.mysql_error());
		$row3 = mysql_fetch_array($res3);
		
  	echo"
	<tr>
		<td>
			login : $row2[login]
		</td>
		<td>
			Proposé par : $row3[login]\">
		</td>
		<td>
			<form name=\"confirm change pass\" method=\"post\" action=\"change_pass.php\">
			<input type=\"hidden\" name=\"idconfirm\" value=\"$row[id]
			<input type=\"submit\" name=\"Submit\" value=\"Changer\" Style=\"border:1px solid #7E7E7E;\">
			</form>
		</td>
		<td>
			<form name=\"suppr change pass\" method=\"post\" action=\"change_pass.php\">
				<input type=\"hidden\" name=\"idsuppr\" value=\"$row[id]\">
				<input type=\"submit\" name=\"Submit\" value=\"Annuler\" Style=\"border:1px solid #7E7E7E;\">
			</form>
		</td>
	</tr>";
  	
  }
}

echo"</table><br/>";
?>