|
|
<?php
|
|
|
namespace Controleur;
|
|
|
|
|
|
use Gateway\Connection;
|
|
|
|
|
|
Class FrontControler{
|
|
|
|
|
|
private $listAction;
|
|
|
|
|
|
private string $role = 'admin'; //Mettre en admin le temps de créer les comptes
|
|
|
|
|
|
private Connection $co;
|
|
|
|
|
|
public function __construct($co){
|
|
|
global $twig;
|
|
|
|
|
|
$this->listAction = ['visitor' => array('accueil','search','quote','login','signin'),
|
|
|
'user' => array('quiz','commentary','logout','addComment','favorite'),
|
|
|
'admin' => array('null')];
|
|
|
|
|
|
$dVueEreur = [];
|
|
|
|
|
|
$router = new \AltoRouter();
|
|
|
|
|
|
$router->setBasePath('/~kemondejar/WF-Website');
|
|
|
|
|
|
$router->map('GET', '/', 'VisitorControler','accueil');
|
|
|
|
|
|
/*
|
|
|
'i' => '[0-9]++'
|
|
|
'a' => '[0-9A-Za-z]++'
|
|
|
'h' => '[0-9A-Fa-f]++'
|
|
|
'*' => '.+?'
|
|
|
'**' => '.++'
|
|
|
'' => '[^/\.]++'
|
|
|
*/
|
|
|
|
|
|
$router->map('GET|POST', '/quote/[i:idQuote]', 'VisitorControler','quote');
|
|
|
$router->map('GET|POST', '/login', 'VisitorControler','login');
|
|
|
$router->map('GET|POST', '/signin', 'VisitorControler','signin');
|
|
|
$router->map('GET|POST', '/addComment', 'UserControler','addComment');
|
|
|
$router->map('GET|POST', '/favorite', 'VisitorControler','favorite');
|
|
|
$router->map('GET|POST', '/quiz/[i:id]?', 'QuizControler','quiz');
|
|
|
|
|
|
|
|
|
$match = $router->match();
|
|
|
$action = NULL;
|
|
|
|
|
|
if(!$match){
|
|
|
$dVueEreur[] = "Requête introuvable";
|
|
|
$this->vueErreur($dVueEreur);
|
|
|
}
|
|
|
else{
|
|
|
$controller=$match['target'] ?? null;
|
|
|
|
|
|
$action = $match['name'];
|
|
|
|
|
|
//Si existe, on l’appelle
|
|
|
if(!$this->ifExisteAction($action)){
|
|
|
$dVueEreur[] = "Action introuvable";
|
|
|
$this->vueErreur($dVueEreur);
|
|
|
}
|
|
|
|
|
|
else if(!$this->verifDroit($action)){
|
|
|
$action='login';
|
|
|
$controller='VisitorControler';
|
|
|
}
|
|
|
|
|
|
else {
|
|
|
$controller = '\\Controleur\\' . $controller;
|
|
|
$controller = new $controller($co);
|
|
|
if (is_callable(array($controller, $action))) {
|
|
|
call_user_func_array(array($controller, $action),
|
|
|
array($match['params']));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
private function ifExisteAction(string $action):bool {
|
|
|
if( in_array($action , $this->listAction['admin']) ||
|
|
|
in_array($action , $this->listAction['user']) ||
|
|
|
in_array($action , $this->listAction['visitor']) ) {
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
private function verifDroit(string $action):bool {
|
|
|
if( in_array($action , $this->listAction['admin']) && $this->role == 'admin') return true;
|
|
|
elseif( in_array($action , $this->listAction['user']) && ($this->role == 'admin' || $this->role == 'user') ) return true;
|
|
|
elseif(in_array($action , $this->listAction['visitor']) && ($this->role == 'admin'|| $this->role == 'user'|| $this->role == 'visitor')) return true;
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
private function vueErreur(array $dVueErreur){
|
|
|
global $vues;
|
|
|
echo "{$dVueErreur[0]}";
|
|
|
require_once $vues['erreur'];
|
|
|
}
|
|
|
|
|
|
}
|