<?PHP
$f = '../app/php/pdo.php';

if (file_exists($f)) {
require_once('../app/php/pdo.php');
}
$Staff = $bdd->prepare('SELECT * FROM users WHERE rank >= ?');
$Staff->execute(array(9));
if ($Staff->rowCount() >= 1) {
  header('Refresh: 0;URL=fini.php');
}
$erreur = false;
if (isset($_POST['valider'])) {
  if (!empty($_POST['pseudo']) && !empty($_POST['mail']) && !empty($_POST['mdp'])) {
    $pseudo = htmlspecialchars(trim($_POST['pseudo']));
    $mail = htmlspecialchars(trim($_POST['mail']));
    $mdp = htmlspecialchars(trim($_POST['mdp']));
    $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 (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.";
    }
        if($erreur == false) {
      $HashMDP = sha1($mdp);
      $rank = 9;
      $credits = 100000;
      $activity_points = 50000;
      $look = "ch-255-73.sh-290-62.lg-280-82.hr-165-34.hd-190-3";
      $motto = "PlusCMS";
      $AjoutUtilisateur = $bdd->prepare('INSERT INTO users (username,password,mail,rank,credits,activity_points,look,motto,account_created) VALUES (?,?,?,?,?,?,?,?,?)');
      $AjoutUtilisateur->execute(array($pseudo,$HashMDP,$mail,$rank,$credits,$activity_points,$look,$motto,time()));
      $CreerSession = $bdd->prepare("SELECT * FROM users WHERE mail = :mail && password = :pass");
      $CreerSession->execute(array('mail' => $mail, 'pass' => $HashMDP));
      $CS = $CreerSession->rowCount();
      if ($CS == 1) {
        header('Location: fini.php');
      } else {
        $message = "Impossible de créer le compte, vérifier d'avoir mise la base de donnée de l'émulateur.";
      }
      
    }
    
  }


?>
<!DOCTYPE html>
<html lang="en">

  <head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">

    <title>PlusCMS - Installation</title>

    <!-- Bootstrap core CSS -->
    <link href="vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">

    <!-- Custom styles for this template -->
    <style>
      body {
        padding-top: 54px;
      }
      @media (min-width: 992px) {
        body {
          padding-top: 56px;
        }
      }

    </style>

  </head>

  <body>

    <!-- Navigation -->
    <nav class="navbar navbar-expand-lg navbar-dark bg-dark fixed-top">
      <div class="container">
        <a class="navbar-brand" href="#">PlusCMS</a>
        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">
          <span class="navbar-toggler-icon"></span>
        </button>
      </div>
    </nav>

    <!-- Page Content -->
    <div class="container">
      <div class="row">
        <div class="col-lg-12 text-center">
          <h1 class="mt-5">Etape 3 de l'installation</h1>
          <p class="lead">Compte Administrateur</p>
          <?PHP if(isset($message)) { ?><p class="lead" style="color: green;"><?PHP echo $message; ?></p><?PHP } ?>
          <div class="well" style="width: 500px;position: relative;margin-left: auto;margin-right: auto;">
          <form method="post">
            <div class="form-group">
              <label class="control-label" for="inputLarge">Pseudonyme</label>
              <input class="form-control input-lg" name="pseudo" placeholder="Pseudonyme" type="text" id="inputLarge">
            </div>
            <div class="form-group">
              <label class="control-label" for="inputLarge">Email </label>
              <input class="form-control input-lg" name="mail" placeholder="Habbo@gmail.com" type="email" id="inputLarge">
            </div>
            <div class="form-group">
              <label class="control-label" for="inputLarge">Mot de passe </label>
              <input class="form-control input-lg" name="mdp" placeholder="********" type="password" id="inputLarge">
            </div>
            <button name="valider" type="submit" class="btn btn-primary">Valider</button>
            </form>
          </div>
          <ul class="list-unstyled">
            <li>Ce système d'installation est destiné à PlusCMS par Dodone.</li>
          </ul>
        </div>
      </div>
    </div>

    <!-- Bootstrap core JavaScript -->
    <script src="vendor/jquery/jquery.min.js"></script>
    <script src="vendor/bootstrap/js/bootstrap.bundle.min.js"></script>

  </body>

</html>
