<?PHP 
require_once('core.php');
if(isset($_SESSION['id']))
{
header("Location: home.php");
exit();
}
$erreur = false;
if (isset($_POST['submit'])) {
$pseudo = htmlspecialchars(htmlentities(trim($_POST['pseudo'])));
$mdp = htmlspecialchars(htmlentities(trim($_POST['mdp'])));
$remdp = htmlspecialchars(htmlentities(trim($_POST['remdp'])));
$mail = htmlspecialchars(htmlentities(trim($_POST['mail'])));
$captcha = htmlspecialchars(htmlentities(trim($_POST['captcha'])));
if ($pseudo != "" && $mdp != "" && $remdp != "" && $mail != "" && $captcha != "") {
    $VPseudo = $bdd->prepare('SELECT * FROM users WHERE username = ?');
    $VPseudo->execute(array($pseudo));
    $VEmail = $bdd->prepare('SELECT * FROM users WHERE mail = ?');
    $VEmail->execute(array($mail));
    if ($VEmail->rowCount() == 0) {
    if ($VPseudo->rowCount() == 0) {
        if (!ctype_alnum($pseudo)) {
            $erreur = true;
            $message = "Le pseudonyme choisis contient des caractères non-autorisées !";
        }elseif (strlen($pseudo) <= 3) {
            $erreur = true;
            $message = "Le pseudonyme choisis est trop court !";
        }
        if (!filter_var($mail, FILTER_VALIDATE_EMAIL)) {
            $erreur = true;
            $message = "L'adresse e-mail contient des caractères non-autorisées !";
        } elseif (strlen($mail) <= 8) {
            $erreur = true;
            $message = "L'adresse e-mail doit faire plus de 8 caractères !";
        }
        if ($captcha != $_SESSION['captcha']) { 
            $erreur = true;
            $message = "Le code de sécurité n'est pas correct !";
        }
        if ($mdp != $remdp) {
            $erreur = true;
            $message = "Les mots de passe ne correspondent pas !";
        } elseif (strlen($mdp) <= 5) {
            $erreur = true;
            $message = "Le mot de passe doit contenir minimum 6 caractères !";
        }
    } else {
        $erreur = true;
        $message = "Désolé, le Pseudonyme est déjà utilisé !";
    }
    } else {
        $erreur = true;
        $message = "Désolé, Cette adresse e-mail est déjà utilisé !"; 
    }
    } else {
        $erreur = true;
        $message = "Merci de remplir tout les champs.";
    }

    // Insertion dans la base de donnée du membre.
    if($erreur == false) {
      $HashMDP = sha1($mdp);
      $AjoutUtilisateur = $bdd->prepare('INSERT INTO users (username,password,mail,rank,credits,activity_points,look,motto,account_created,last_online,ip_reg,home_room) VALUES (?,?,?,?,?,?,?,?,?,?,?)');
      $AjoutUtilisateur->execute(array($pseudo,$HashMDP,$mail,$cms['rank'],$cms['credits'],$cms['activity_points'],$cms['look'],$cms['motto'],time(),time(),$_SERVER["REMOTE_ADDR"],$cms['room']));
      //Creation de la Session
      $CreerSession = $bdd->prepare("SELECT * FROM users WHERE mail = :mail && password = :pass");
      $CreerSession->execute(array('mail' => $mail, 'pass' => $HashMDP));
      $CS = $CreerSession->fetch();
      $_SESSION['id'] = $CS['id'];
      //Connexion Effectuer on redirectionne sur le site
      header ("Location: home.php");
    }
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
    <title><?PHP echo htmlentities(trim($cms['nom_hotel'])); ?> - Un monde étrange !</title>
    <link rel="stylesheet" type="text/css" href="web/css/index.css">
</head>
<body>
 <header>
     <img src="http://hsource.fr/font/habbo_new/Habbo.gif" class="logo">
     <?PHP if ($erreur == false) { ?>
     <a style="margin-left: 10px;" href="<?PHP echo htmlentities(trim($cms['lien'])); ?>"><button class="connexion <?PHP echo htmlentities(trim($cms['themes'])); ?>">Déjà membre de <?PHP echo htmlentities(trim($cms['nom_hotel'])); ?> ? Connecte toi !</button></a>
     <?PHP } else { ?> 
     <a style="margin-left: 10px;"><button class="connexion noel"><?PHP echo $message; ?></button></a>
     <?PHP } ?>
 </header>
 <div class="context <?PHP echo htmlentities(trim($cms['themes'])); ?>" style="background-image: url(web/img/<?PHP echo htmlentities(trim($cms['themes'])); ?>.png);">
    <div class="text">
        <p style="font-size: 25px;">Bienvenue sur <?PHP echo htmlentities(trim($cms['nom_hotel'])); ?> Hôtel !</p>
        <i>Ici des personnes étrange y résident, seras-tu devenir membre de cette grande famille ? Mais attention tu risque de t'attacher très vite à cette aventure !</i>
    </div>
 </div>
 <div class="news" style="padding: 20px;">
 <form method="post">
 <div style="width: 300px;float: left;">
     <input class="<?PHP echo htmlentities(trim($cms['themes'])); ?>Border <?PHP echo htmlentities(trim($cms['themes'])); ?>Color" style="top: 0;left: 0;width: 300px;margin-bottom: 10px;" type="text" name="pseudo" placeholder="Pseudonyme">

     <input class="<?PHP echo htmlentities(trim($cms['themes'])); ?>Border <?PHP echo htmlentities(trim($cms['themes'])); ?>Color" style="top: 0;left: 0;width: 300px;margin-bottom: 10px;" type="password" name="mdp" placeholder= "Mot de passe">

     <input class="<?PHP echo htmlentities(trim($cms['themes'])); ?>Border <?PHP echo htmlentities(trim($cms['themes'])); ?>Color" style="top: 0;left: 0;width: 300px;margin-bottom: 10px;" type="password" name="remdp" placeholder= "Re-tape ton mot de passe">

     <input class="<?PHP echo htmlentities(trim($cms['themes'])); ?>Border <?PHP echo htmlentities(trim($cms['themes'])); ?>Color" style="top: 0;left: 0;width: 300px;margin-bottom: 10px;" type="mail" name="mail" placeholder= "Email">
 </div>
  <div style="width: 300px;float: left;margin-left: 50px;">
     <input class="<?PHP echo htmlentities(trim($cms['themes'])); ?>Border <?PHP echo htmlentities(trim($cms['themes'])); ?>Color" style="top: 0;left: 0;width: 300px;margin-bottom: 10px;" type="text" name="motto" value="<?PHP echo htmlentities(trim($cms['motto'])); ?>" disabled>
     <img style="margin-bottom: 8px;" src="app/captcha/code.php">
     <input class="<?PHP echo htmlentities(trim($cms['themes'])); ?>Border <?PHP echo htmlentities(trim($cms['themes'])); ?>Color" style="top: 0;left: 0;width: 300px;margin-bottom: 10px;" type="text" name="captcha" placeholder= "Tape le code au dessus">
     <button style="left: 0;top: 0;width: 324px;" type="submit" name="submit" class="connexion <?PHP echo htmlentities(trim($cms['themes'])); ?>">C'est parti !</button>
 </div>
 </form>
 <img style="float: right;" src="web/img/frank.png">
 </div>
 <footer style="clear: both;position: relative;margin-left: auto;margin-right: auto;width: 940px;top: 30px;padding:10px;box-shadow: 0 2px 5px 0 rgba(0, 0, 0, .10), 0 2px 10px 0 rgba(0, 0, 0, .16);">
     <b class="<?PHP echo htmlentities(trim($cms['themes'])); ?>Color" style="margin: 20px;"><?PHP echo htmlentities(trim($cms['nom_hotel'])); ?> est propulsé par PlusCMS par Dodone</b>
 </footer>
</body>
</html>