<?php include "core/menu.php" ?>
<div></div>
							
			</div>
<?php include "config/config.php" ?>	
<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>
<h1>Inscription</h1>
<?php
if(isset($_SESSION['username']))
{
	echo "Vous avez d&egrave;ja un compte, vous ne pouvez donc pas en cr&eacuteer un autre.";
}
else
{
define('USER_LENGHT', 3);
define('PASSWORD_LENGHT', 3);
class Inscription
{
	private $inscriptionId;
	private $username;
	private $password;
	private $passwordRepeat;
	private $cryptedPassword;
	private $email;
	private $inscriptionDate;
	private $extension;
	private $errorMsg;
	private $error;
	private $injection;	
	
	private function construct()
	{
		$this->inscriptionDate = date('Y-m-d H:i:s');
		$this->error = false;
		$this->errorMsg = '';
		$this->injection = false;
		$this->cryptedPassword = '';
	}
	private function verifyUsername()
	{
		if(strlen($this->username) <= USER_LENGHT)
		{
			$this->error = true;
			$this->errorMsg .= ' - Le nom d\'utilisateur choisi est trop court.<br />';
		}
		$sqlUsername = mysql_query('SELECT COUNT(*) AS nombre FROM account WHERE username=\'' . $this->username.'\'') or die('Comparaison des noms d\'utilisateur : '.mysql_error());
		$datasUsername = mysql_fetch_assoc($sqlUsername);
		if($datasUsername['nombre'] == 1)
		{
			$this->error = true;
			$this->errorMsg .= ' - Ce nom d\'utilisateur est deja pris.<br />';
		}
		$this->sqlDetect($this->username);
	}
	private function verifyPassword()
	{
		if(strlen($this->password) <= PASSWORD_LENGHT)
		{
			$this->error = true;
			$this->errorMsg .= ' - Le mot de passe choisi est trop court.<br />';
		}
		if($this->password !== $this->passwordRepeat)
		{
			$this->error = true;
			$this->errorMsg .= ' - Les mots de passe ne sont pas identiques.<br />';			
		}
		$this->sqlDetect($this->password);
	}
	private function verifyEmail()
	{
		if(!preg_match('#^[a-z0-9.-_]+@[a-z0-9.-_]{2,}\.[a-z]{2,4}$#', $this->email))
		{
			$this->error = true;
			$this->errorMsg .= ' - L\'email n\'est pas correct.<br />';
		}
		$sqlEmail = mysql_query('SELECT COUNT(*) AS nombre FROM account WHERE email=\''.$this->email.'\'') or die('Comparaison des emails : '.mysql_error());;
		$datasEmail = mysql_fetch_assoc($sqlEmail);

		if($datasEmail['nombre'] != 0)
		{
			$this->error = true;
			$this->errorMsg .= ' - L\'email est deja utilise.<br />';
		}
		$this->sqlDetect($this->email);
	}

	public function verifyAll()
	{
		$this->verifyUsername();
		$this->verifyEmail();
		$this->verifyPassword();
		
		if(!$this->error)
		{
			$this->cryptedPassword = sha1(strtoupper($this->username).':'.strtoupper($this->password));
			mysql_query('INSERT INTO account(username,
											sha_pass_hash,
											email,
											joindate,
											last_ip,
											expansion)
									VALUES(\''.$this->username.'\',
											\''.$this->cryptedPassword.'\',
											\''.$this->email.'\',
											\''.$this->inscriptionDate.'\',
											\''.$_SERVER['REMOTE_ADDR'].'\',
											\''.$this->extension.'\')') or die('Enregistrement du compte : '.mysql_error());
			echo '<p class="info"><br /><br /><br />Votre enregistrement a ete effectue avec succes !<br /><a href="index.php">Retour a l\'index</a></p>';
		}
		else
		{
			if($this->injection)
			{
				echo '<p class="info"><br /><br />On ne fait pas ça !</p>';
			}
			else
			{
				echo '<p class="info">Des erreurs ont ete trouvees durant l\'enregistrement :<br /><br />',$this->errorMsg,'<br /><a href="../index.php">Reessayer</a></p>';
			}
		}
	}
	private function sqlDetect($string)
	{
		if (preg_match('#INSERT|SELECT|UNION|FROM|WHERE|DELETE#', $string))
		{
			$this->error = true;
			$this->errorMsg .= ' - Injection sql detectee !<br />';
			$this->injection = true;
		}
	}
	public function setUsername($username)
	{
		$this->username = mysql_real_escape_string($username);
	}
	
	public function setPassword($password)
	{
		$this->password = mysql_real_escape_string($password);
	}
	
	public function setPasswordRepeat($password)
	{
		$this->passwordRepeat = mysql_real_escape_string($password);
	}
	
	public function setEmail($email)
	{
		$this->email = mysql_real_escape_string($email);
	}
	
	public function setExtension($extension)
	{
		$this->extension = $extension;
	}
}
?>
<?php
include('config/config.php');
if(isset($_POST['ok']))
{
	mysql_connect($host, $user, $pass) or die(mysql_error());
	mysql_select_db($realmd);
	$joueur = new Inscription();
	$joueur->setUsername($_POST['username']);
	$joueur->setPassword($_POST['password']);
	$joueur->setPasswordRepeat($_POST['passwordRepeat']);
	$joueur->setEmail($_POST['email']);
	$joueur->setExtension($_POST['extension']);
	$joueur->verifyAll();
}
else
{
?>
				<p>
                <form method="post">
					Votre inscription sera effective des que vous aurez rempli ce formulaire :<br />
					<br />
                    <table>
					<tr><td><label for="label1">Nom de compte :</label></td> <td><input type="text" name="username" id="label1"/></td></tr>
					<tr><td><label for="label2">Adresse email :</label></td> <td><input type="text" name="email" id="label2" /></td></tr>
					<tr><td><label for="label3">Mot de passe :</label></td> <td><input type="password" name="password" id="label3" /></td></tr>
					<tr><td><label for="label4">Repetez le mot de passe :</label></td> <td><input type="password" name="passwordRepeat" id="label4" /></td></tr>
					<tr><td><label for="label5">Extension :</label></td> <td>
										<select name="extension" id="label5">
											<option value="3">Cataclysm</option>
											<option value="2">Wrath of the Lich King</option>
											<option value="1">Burning Crusade</option>
											<option value="0">World of Warcraft</option>
										</select></td></tr></table>
					<br />
					<br />
					<input type="submit" name="ok" value="Envoyer" />
				</p>
			</form>
<?php
}
?>
<?php
require_once ( 'config/config.php');
echo "set realmlist 78.226.17.69<br>";
echo "Version $version <br>";
}
?>
</div>
					<div class="spacer"></div>
				</div>
			</div>

<?php include "core/end.php" ?>
