You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
WF-Website/src/Controleur/VisitorControler.php

180 lines
4.6 KiB

<?php
namespace Controleur;
use Gateway\Connection;
use Gateway\UserGateway;
use Model\UserModel;
use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
use Twig\Error\SyntaxError;
Class VisitorControler {
private Connection $co;
private UserGateway $gw;
private UserModel $mdl;
private array $errors;
/**
* @throws SyntaxError
* @throws RuntimeError
* @throws LoaderError
*/
public function __construct(Connection $co, string $action) {
$this->co = $co;
$this->gw = new UserGateway($this->co);
$this->mdl = new UserModel($this->gw);
$this -> getAction($action);
}
/**
* @throws SyntaxError
* @throws RuntimeError
* @throws LoaderError
*/
public function getAction(string $action): void
{
switch ($action) {
case "login":
$this -> visitorLogIn();
break;
case "signin":
$this -> visitorSignIn();
break;
case "validsignin":
$this -> toSignIn();
break;
case "validlogin":
$this -> toLogIn();
break;
default:
break;
}
}
public function accueil(){
global $vues;
require_once $vues['accueil'];
}
public function quote(array $arg){
global $vues;
$id=$arg['idQuote'] ?? 1;
//echo "{$id}";
require_once $vues['quote'];
}
/**
* @throws SyntaxError
* @throws RuntimeError
* @throws LoaderError
*/
public function visitorLogIn(): void
{
global $twig;
echo $twig->render("login.html.twig");
$this -> toLogIn();
}
public function toLogIn() : void
{
if ($_POST)
{
$pseudo = $_POST['pseudo'] ?? null;
$mdp = $_POST['mdp'] ?? null;
$user = $this -> mdl -> getUsername($pseudo);
if ($user)
{
if (password_verify($mdp, $user->getPassword()))
{
$_SESSION['user'] = $pseudo;
$_SESSION['role'] = 'user';
header("Location: /");
exit();
}else
{
global $twig;
$this -> errors = ["Identifiant ou mot de passe incorrect"];
echo $twig -> render("login.html.twig", ['error' => $this -> errors[0]]);
exit();
}
}
else
{
global $twig;
$this -> errors = ["Identifiant ou mot de passe incorrect"];
echo $twig -> render("login.html.twig", ['error' => $this -> errors[0]]);
exit();
}
}
}
/**
* @throws SyntaxError
* @throws RuntimeError
* @throws LoaderError
*/
public function visitorSignIn(): void
{
global $twig;
echo $twig->render("signin.html.twig");
}
/**
* @throws RuntimeError
* @throws SyntaxError
* @throws LoaderError
*/
public function toSignIn() : void
{
global $twig;
$this -> errors = [null, null, null];
if ($_POST) {
$pseudo = $_POST['pseudo'] ?? null;
$email = $_POST['email'] ?? null;
$mdp = $_POST['mdp'] ?? null;
$cmdp = $_POST['cmdp'] ?? null;
if ($mdp != $cmdp) {
$this->errors[2] = "Mots de passe incorrects";
//$this->visitorSignIn();
echo $twig->render("signin.html.twig", ['error' => $this->errors]);
exit();
}
$option = ['cost' => 12];
$hmdp = password_hash($mdp, PASSWORD_BCRYPT, $option);
$isUserAlreadyUsed = $this -> mdl -> getUsername($pseudo);
$isEmailAlreadyUsed = $this -> mdl -> getEmail($email);
if ($isUserAlreadyUsed and !$isEmailAlreadyUsed) {
$this->errors[0] = "Pseudo déjà utilisé";
echo $twig->render("signin.html.twig", ['error' => $this->errors]);
exit();
}
else if ($isEmailAlreadyUsed and !$isUserAlreadyUsed) {
$this->errors[1] = "Email déjà utilisé";
echo $twig->render("signin.html.twig", ['error' => $this->errors]);
exit();
}
else echo $this->mdl->insertUser($pseudo, $email, $hmdp);
$_SESSION["pseudo"] = $pseudo;
header("Location: /");
}
}
}