<?php
	require_once ('../common/scripts/templatize.inc.php');
	require_once ('../common/scripts/connect.inc.php');
	require_once ('../common/scripts/bdd.inc.php');
	require_once ('../common/scripts/emblemize.inc.php');
	
/***********************/
/*traitement           */
/***********************/
	
	session_start();
	/*
	//debug :
	echo '<pre>';
	print_r($_SESSION);
	print_r($_POST);
	echo '</pre>';
	*/
	$stringResult = '';
	
	$newlogin = $_POST['login'];//nouveau login
	$newpass = $_POST['password']==''?$_SESSION['password']:md5($_POST['password']);//nouveau pass avec cryptage
	
	$test = '';
	if ($newlogin == $_SESSION['login']) //si le login n'a pas change 
		$test = checkPassword($_POST['password']==''?'ok':$_POST['password']);//verifie uniquement le pass
	else //si le login a change
		$test = checkLoginAndPassword($dbconn,$newlogin,$_POST['password']==''?'ok':$_POST['password']);//verifie le login et le pass
	
	//si le login et le password sont toujours corrects
	if ($test=='') {
		
		//upload du nouvel embleme
		$res = uploadOrGetEmblem($_FILES['monfichier']);
		$imageEmblemFile = $res[0];
		$stringResult = '<p>'.$res[1].'</p>';

		$folderImage = 'http://'.$_SERVER['HTTP_HOST'].substr($_SERVER['REQUEST_URI'],0,strrpos($_SERVER['REQUEST_URI'],'/'));

		//mise a jour de la base de donnees
		updatePlayer($dbconn,
			$_SESSION['id'],
			$newlogin,
			$newpass,
			$_POST['mail'],
			$_POST['guildName'],
			$_POST['guildColor'],
			$_POST['guildInfos'],
			$folderImage.'/'.$imageEmblemFile);
		
		//la nouvelle session utilise le nouveau login et le nouveau password
		$_SESSION['login'] = $newlogin;
		$_SESSION['password'] = $newpass;
		
		//affichage des infos
		$stringResult .= getPlayerInfos($dbconn, $newlogin);
	}
	else { //sinon pas de modification de la base, juste un message d'erreur
		$stringResult .= '<p>'.$test.'</p>';
	}
	
/**************/
/*affichage   */
/**************/
	
	$arrayVars = array('VALIDATION_RESULT'=>$stringResult);
	
	templatizeWithVars('../common/header2.tpl','../common/menuInterieur.tpl', 'compteFini.tpl','../common/footer.tpl',$arrayVars);
?>
