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.
215 lines
5.7 KiB
215 lines
5.7 KiB
<?php
|
|
namespace Controleur;
|
|
|
|
use Model\QuoteModel;
|
|
use Model\CommentaryModel;
|
|
use Gateway\Connection;
|
|
use Gateway\QuoteGateway;
|
|
use Gateway\CommentaryGateway;
|
|
use Model\SearchModel;
|
|
use Gateway\UserGateway;
|
|
use Model\UserModel;
|
|
use Twig\Error\LoaderError;
|
|
use Twig\Error\RuntimeError;
|
|
use Twig\Error\SyntaxError;
|
|
use Gateway\AccueilGateway;
|
|
|
|
Class VisitorControler {
|
|
|
|
private QuoteModel $qMod;
|
|
private CommentaryModel $cMod;
|
|
private AccueilGateway $accueilGateway;
|
|
|
|
private SearchModel $sMod;
|
|
|
|
private UserGateway $gw;
|
|
|
|
private UserModel $mdl;
|
|
|
|
private array $errors;
|
|
|
|
/**
|
|
* @throws SyntaxError
|
|
* @throws RuntimeError
|
|
* @throws LoaderError
|
|
*/
|
|
public function __construct() {
|
|
global $co;
|
|
$this->qMod = new QuoteModel(new QuoteGateway($co));
|
|
$this->cMod = new CommentaryModel(new CommentaryGateway($co));
|
|
$this->accueilGateway = new AccueilGateway($co);
|
|
$this->gw = new UserGateway($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;
|
|
|
|
// Récupérer la citation du jour via AccueilGateway
|
|
$citationDuJour = $this->accueilGateway->getQuoteOfTheDay('fr');
|
|
$suggestions = $this->accueilGateway->getSuggestions(0, 'fr');
|
|
|
|
// Passer les données à la vue
|
|
require_once $vues['accueil'];
|
|
}
|
|
|
|
public function quote(array $arg){
|
|
global $vues;
|
|
$id= $arg['idQuote'] ?? 1;
|
|
$q = $this->qMod->searchId($id);
|
|
$c = $this->cMod->getComment($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: /");
|
|
}
|
|
}
|
|
|
|
public function favorite() {
|
|
global $vues;
|
|
require_once $vues['favorite'];
|
|
}
|
|
|
|
public function search(array $arg){
|
|
global $vues;
|
|
|
|
$type = ($_POST['type'] ?? "");
|
|
$search = ($_POST['search'] ?? NULL);
|
|
$filtre = ($arg['filtre'] ?? []);
|
|
|
|
|
|
$tq=$this->sMod->searchQuote($type,$search,$filtre);
|
|
|
|
require_once $vues['search'];
|
|
}
|
|
} |