<?php
session_start();
include ('config.php');
$user_rg=$_SESSION['user_rg'];
if(isset($_POST['suppression']) && $_POST['suppression']=='true'){
	if(isset($_POST['activer_supp']) && $_POST['activer_supp']=='on'){
		$requete = "INSERT INTO matgraph_suppression VALUES ('".$_POST['id']."', now())";
		$resultat = mysql_query($requete)or die('erreur sql<br>'.$requete.'<br>'.mysql_error());
		echo htmlentities("Mode suppression activé...");
	}elseif(isset($_POST['retirer_supp']) && $_POST['retirer_supp']=='on'){
		$requete = "DELETE FROM matgraph_suppression WHERE idlogin=".$_POST['id']."";
		$resultat = mysql_query($requete)or die('erreur sql<br>'.$requete.'<br>'.mysql_error());
		echo htmlentities("Mode suppression retiré...");
	}else{
		$query3="SELECT * FROM matgraph_suppression WHERE idlogin='".$_POST['id']."'";
		$req=mysql_query($query3);
		$num=mysql_num_rows($req);
		echo "<form name='suppression' method=post>
		<input type='hidden' name='id' value='".$_POST['id']."' />";
		if($num!=0){
			echo"<input type=radio name='retirer_supp' />Retirer le mode suppression<br/>";
		}elseif($_SESSION['user_rg']>0 && $num==0){
			echo"<input type=radio name='activer_supp' />Activer le mode suppression<br/>";
		}else{
			echo htmlentities("Vous n'avez pas aassez d'accés pour activer le mode suppresion");
		}
		echo "<input type='button' onclick=\"AfficheForm('suppression','index.php?page=joueur&amp;suppression=true','result');return false;\" value='Valider'/></form>";
	}
}elseif(isset($_POST['editer_pl']) && $_POST['editer_pl']!=''){
	if(isset($_POST['planete-'.$_POST['editer_pl']]) && $_POST['planete-'.$_POST['editer_pl']]!=''){
		$tab=split(';',$_POST['champ_id']);
		array_pop($tab);
		foreach($tab as $value){
			$query="UPDATE matgraph_users SET planete='".htmlentities($_POST['planete-'.$value])."' WHERE id='".$value."'";
			mysql_query($query) or die('Erreur MySQL :'.mysql_error());
		}
		echo htmlentities("Modification terminé...");
	}else{
		echo "<form method=post name='planete'>";
		$query="SELECT id,idjoueur,planete FROM matgraph_users WHERE idjoueur='".$_POST['editer_pl']."' ORDER BY id ASC";
		$req=mysql_query($query);
		$champ_id='';
		while($row = mysql_fetch_array($req)){
			$champ_id.=$row['id'].';';
			if($row['id']==$row['idjoueur']){
				echo "Nom de la plan&egrave;te M&egrave;re :&nbsp;<input type='text' name='planete-".$row['id']."' value='".$row['planete']."' />";
				$i=0;
			}else{
				$i++;
				echo "<br/>Nom de la colonie n&deg;".$i." :&nbsp;<input type='text' name='planete-".$row['id']."' value='".$row['planete']."' />";
			}
		}
		echo "<br/><input type='hidden' name='champ_id' value='".$champ_id."' /><input type='button' onclick=\"AfficheForm('planete','index.php?page=joueur&amp;editer_pl=".$_POST['editer_pl']."','result');return false;\" value=\"Editer les noms\"></form>";
	}
}elseif(isset($_POST['editer_mdp']) && $_POST['editer_mdp']!=''){
	if(isset($_POST['mdp-'.$_POST['editer_mdp']]) && $_POST['mdp-'.$_POST['editer_mdp']]!=''){
		$key = "8Z5q7?&Pu!?M!?5Xs";
		$pass=$_POST['mdp-'.$_POST['editer_mdp']];
		$new_pass = md5($pass.$key);
		
		$req = "SELECT id FROM matgraph_change_pass WHERE idjoueur = '".$_POST['editer_mdp']."' 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 htmlentities('Demande déjà en cours...');
			exit;
		}	
		$req = "INSERT INTO matgraph_change_pass ( `id` , `idjoueur` , `pass` , `modo1` , `modo2` )
		VALUES (
		'', '".$_POST['editer_mdp']."', '".$new_pass."', '".$_SESSION['idlogin']."', 'no'
		);";
		$res = mysql_query($req) or die('Erreur SQL! <br>'.$req.'<br>'.mysql_error());
		echo htmlentities('Demande de modification de mot de passe accepté...');
		
	}else{
		echo "<form method=post name='mdp'>";
		echo "Mot de passe :&nbsp;<input type='text' name='mdp-".$_POST['editer_mdp']."' />";
		echo "<br/><input type='button' onclick=\"AfficheForm('mdp','index.php?page=joueur&amp;editer_mdp=".$_POST['editer_mdp']."','result');return false;\" value=\"Editer le mot de passe\"></form>";
	}
}elseif(isset($_POST['editer_log_mail']) && $_POST['editer_log_mail']!=''){
	if(isset($_POST['log-'.$_POST['editer_log_mail']]) && isset($_POST['mail-'.$_POST['editer_log_mail']]) ){
		$login=$_POST['log-'.$_POST['editer_log_mail']];
		$mail=$_POST['mail-'.$_POST['editer_log_mail']];
		$query="UPDATE matgraph_users SET login='".$login."', mail='".$mail."' WHERE idjoueur='".$_POST['editer_log_mail']."'";
		$req=mysql_query($query) or die ('Erreur MySQL :'.mysql_error());
		if($req){
			echo htmlentities('Login et/ou adresse email modifié...');
		}else{
			echo "Erreur d'action";
		}
	}else{
		$query="SELECT login,mail FROM matgraph_users WHERE id=idjoueur AND id='".$_POST['editer_log_mail']."'";
		$req=mysql_query($query) or die('Erreur MySQL :'.mysql_error());
		list($log,$mail)=mysql_fetch_array($req);
		echo "<script type=\"text/javascript\">
		function verif_champs(){
			login=document.forms['log_mail'].elements[0].value;
			mail=document.forms['log_mail'].elements[1].value;
			reg=new RegExp('^\s','g');
			reg1=new RegExp('^[a-zA-Z0-9\-_]+[a-zA-Z0-9\.\-_]*@[a-zA-Z0-9\-_]+\.[a-zA-Z\.\-_]{1,}[a-zA-Z\-_]+','g');
			if(login.length < 3 || login.match(reg)){
				alert('Le login entré a moins de 3 caratères ou contient un espace en début de login');
			}else if(mail.length <3  || !reg1.test(mail)){
				alert('L\email entré a moins de 3 caractères et/ou ne correspond pas à une adresse valide');
			}else{
				AfficheForm('log_mail','index.php?page=joueur&editer_log_mail=".$_POST['editer_log_mail']."','result');
				return false;
			}
		}
		</script>";
		echo "<form method=post name='log_mail'>";
		echo "Login :&nbsp;<input type='text' name='log-".$_POST['editer_log_mail']."' value='".$log."'/>"
	   ."<br/>Mail :&nbsp;<input type='text' name='mail-".$_POST['editer_log_mail']."' value='".$mail."'/>";
		echo "<br/><input type='button' onclick=\"verif_champs();\" value=\"Editer le login et/ou email\"></form>";
	}
}
?>