From d6dcb15c7bc19f443c3236ca34a7b7a573748955 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?No=C3=A9=20Garnier?= Date: Mon, 5 Dec 2022 12:39:08 +0100 Subject: [PATCH] Changement du MVC --- WEB/Config/Autoload.php | 2 +- WEB/Config/Config.php | 2 +- WEB/Config/Connection.php | 39 +++--- WEB/Controller/EnigmeGateway.php | 9 +- WEB/Controller/FrontController.php | 1 - WEB/Controller/PartieGateway.php | 9 +- WEB/Controller/UserController.php | 168 +++----------------------- WEB/Controller/UtilisateurGateway.php | 10 +- WEB/Factory/PartieFactory.php | 2 - WEB/{Model => Metier}/Enigme.php | 0 WEB/{Model => Metier}/Partie.php | 0 WEB/{Model => Metier}/Utilisateur.php | 0 WEB/Model/Model.php | 146 ++++++++++++++++++++++ 13 files changed, 216 insertions(+), 172 deletions(-) rename WEB/{Model => Metier}/Enigme.php (100%) rename WEB/{Model => Metier}/Partie.php (100%) rename WEB/{Model => Metier}/Utilisateur.php (100%) create mode 100644 WEB/Model/Model.php diff --git a/WEB/Config/Autoload.php b/WEB/Config/Autoload.php index c67bf2f5..d745eebc 100644 --- a/WEB/Config/Autoload.php +++ b/WEB/Config/Autoload.php @@ -34,7 +34,7 @@ class Autoload { global $rep; $filename = $class.'.php'; - $dir =array('Model/','./','Config/','Controller/'); + $dir =array('Model/','./','Config/','Controller/','Metier/', 'Factory/'); foreach ($dir as $d){ $file=$rep.$d.$filename; //echo $file; diff --git a/WEB/Config/Config.php b/WEB/Config/Config.php index 9853af16..0f773a6a 100644 --- a/WEB/Config/Config.php +++ b/WEB/Config/Config.php @@ -23,7 +23,7 @@ $sel = "JeSuisUnSeldeHashageEtJeSuisUniqueEtTresSecuriseEtJeSuisTresLong"; //Page $vues['main'] = 'View/src/pages/Main.php'; $vues['presentation'] = 'View/src/pages/Presentation.html'; -$vues['login'] = '../View/src/pages/LogSign/Login.php'; +$vues['login'] = '/View/src/pages/LogSign/Login.php'; $vues['signUp'] = 'View/src/pages/LogSign/SignUp.php'; $vues['enigme'] = 'View/src/pages/Palindrome.html'; diff --git a/WEB/Config/Connection.php b/WEB/Config/Connection.php index 987a2594..936fdcf3 100644 --- a/WEB/Config/Connection.php +++ b/WEB/Config/Connection.php @@ -19,26 +19,37 @@ class Connection extends SQLite3 { - private $stmt; - private $result; - function __construct($dsn) + private $stmt; + private $result; + function __construct($dsn) { $this->open($dsn); $this->enableExceptions(true); } - public function executeQuery(string $query, array $parameters = []) :bool { - $this->stmt = parent::prepare($query); - foreach ($parameters as $name => $value) { - $this->stmt->bindValue($name, $value[0], $value[1]); - } - $this->result=$this->stmt->execute(); - return $this->result; - } + public function executeQuery(string $query, array $parameters = []): bool + { + $this->stmt = parent::prepare($query); + foreach ($parameters as $name => $value) { + $this->stmt->bindValue($name, $value[0], $value[1]); + } + $this->result = $this->stmt->execute(); + if ($this->result == false) { + return false; + } else { + return true; + } + } - public function getResults(): array { - return $this->result->fetchArray(); - } + public function getResults(): array + { + var_dump($this->result->fetchArray(SQLITE3_ASSOC)); + if ($this->result->fetchArray(SQLITE3_ASSOC) == false) { + return array(); + } else { + return $this->result->fetchArray(SQLITE3_ASSOC); + } + } } // class Connection extends SQLite3{ diff --git a/WEB/Controller/EnigmeGateway.php b/WEB/Controller/EnigmeGateway.php index 68679e16..bd13b6e0 100644 --- a/WEB/Controller/EnigmeGateway.php +++ b/WEB/Controller/EnigmeGateway.php @@ -7,9 +7,16 @@ class EnigmeGateway * @param Connection $con */ - public function __construct(Connection $con) + public function __construct() { + global $dsn, $rep, $vues, $error; + try{ + $con = new Connection($dsn); $this->con = $con; + } catch (Exception $e) { + $error = $e->getMessage(); + require($rep . $vues['erreur']); + } } /** diff --git a/WEB/Controller/FrontController.php b/WEB/Controller/FrontController.php index 8870beaf..ebe2bc55 100644 --- a/WEB/Controller/FrontController.php +++ b/WEB/Controller/FrontController.php @@ -7,7 +7,6 @@ class FrontController session_start(); // $role=$_SESSION['role']; // $action=$_REQUEST['action']; - echo "frontController"; try { new UserController(); } catch (Exception $e) { diff --git a/WEB/Controller/PartieGateway.php b/WEB/Controller/PartieGateway.php index 05a7c8f8..7fb6498f 100644 --- a/WEB/Controller/PartieGateway.php +++ b/WEB/Controller/PartieGateway.php @@ -6,9 +6,16 @@ class PartieGateway /** * @param Connection $con */ - public function __construct(Connection $con) + public function __construct() { + global $dsn, $rep, $vues, $error; + try{ + $con = new Connection($dsn); $this->con = $con; + } catch (Exception $e) { + $error = $e->getMessage(); + require($rep . $vues['erreur']); + } } /** diff --git a/WEB/Controller/UserController.php b/WEB/Controller/UserController.php index 31f41663..9ac080a6 100644 --- a/WEB/Controller/UserController.php +++ b/WEB/Controller/UserController.php @@ -6,177 +6,47 @@ class UserController { private Connection $con; - function __construct() { - try{ - echo "UserController"; + function __construct() + { + try { global $dsn, $rep, $vues, $error; - $this->con=new Connection($dsn); - echo "USconection"; - $action=$_REQUEST['action']; - switch($action) { + $model = new Model(); + $action = $_REQUEST['action']; + switch ($action) { case NULL: - $this->goToHome(); + $model->goToHome(); break; case "signUp": - $this->signUp(); + $model->signUp(); break; case "login": - $this->login(); + $model->login(); break; case "goToPresentation": - $this->goToPresentation(); + $model->goToPresentation(); break; case "goToHome": - $this->goToHome(); + $model->goToHome(); break; case "goToLogin": - $this->goToLogin(); + $model->goToLogin(); break; case "goToSignUp": - $this->goToSignUp(); + $model->goToSignUp(); break; case "goToEnigme": - $this->goToEnigme(); + $model->goToEnigme(); break; case "goToQueue": - $this->goToQueue(); + $model->goToQueue(); break; } - } catch (PDOException $e) - { + } catch (PDOException $e) { $error = $e->getMessage(); - require ($rep.$vues['erreur']); - }catch (Exception $e2) - { + require($rep . $vues['erreur']); + } catch (Exception $e2) { $error = $e2->getMessage(); - require ($rep.$vues['erreur']); - } - } - - private function signUp() { - global $rep, $vues, $sel, $error; - try { - $gateway = new UtilisateurGateway($this->con); - $validation = new Validation(); - if (! $validation->ValidateEmail($_REQUEST['email'])) { - $error = "Email invalides."; - throw (new Exception("Email non valide")); - } - if(! $validation->ValidateUsername($_REQUEST['username'])){ - $error = "Nom d'utilisateur invalides. Il ne doit pas contenir de caractère spéciaux."; - throw(new Exception("Pseudo non valide")); - } - if(! $validation->ValidatePassword($_REQUEST['password'])){ - $error = "Mots de passe invalides. Il ne doit pas dépasser 100 caractères."; - throw(new Exception("Mot de passe non valide")); - } - $j = $gateway->getUtilisateurByEmail($_REQUEST['email']); - if ($j->getEmail() != "null") { - $error = "Email déjà utilisé."; - throw (new Exception("Email déjà utilisé")); - } - $password = password_hash($_REQUEST['password'], PASSWORD_DEFAULT); - $estAdmin = $gateway->getEstAdminByEmail($_REQUEST['email']); - $Utilisateur = new Utilisateur($_REQUEST['email'], $_REQUEST['username'], $password, $estAdmin); - $gateway->insert($Utilisateur); - $_SESSION['connected'] = 'true'; - $_SESSION['role'] = 'utilisateur'; - require ($rep.$vues['main']); - }catch (PDOException $e) - { - $error = "Erreur de connexion à la base de données."; - require ($rep.$vues['erreur']); - } - catch (Exception $e){ - require($rep.$vues['erreur']); - } - } - private function login(){ - global $rep, $vues, $sel, $error; - try { - $gateway = new UtilisateurGateway($this->con); - $Utilisateur = $gateway->getUtilisateurByEmail($_REQUEST['email']); - if ($Utilisateur->getEmail() == null){ - $error = "Utilisateur non trouvé."; - throw new Exception("Utilisateur introuvable"); - } - $mdp = $gateway->getMdpByEmail($_REQUEST['email']); - if (password_verify($mdp, $_REQUEST['password'])){ - $error = "Mot de passe incorrect."; - throw new Exception("Mot de passe invalide"); - } - $estAdmin =$gateway->getEstAdminByEmail($_REQUEST['email']); - if ($estAdmin == true) { - $_SESSION['role'] = "admin"; - } - else{ - $_SESSION['role'] = "utilisateur"; - } - - $_SESSION['connected'] = 'true'; - require ($rep.$vues['main']); - }catch (Exception $e){ - require($rep.$vues['erreur']); - } - } - - private function goToPresentation() { - global $rep, $vues, $error; - try { - require ($rep.$vues['presentation']); - }catch (Exception $e){ - $error = "Erreur Inconnue"; - require($rep.$vues['erreur']); - } - } - - private function goToHome() { - global $rep, $vues, $error; - try { - require ($rep.$vues['main']); - }catch (Exception $e){ - $error = "404"; - require($rep.$vues['erreur']); - } - } - - private function goToLogin() { - global $rep, $vues, $error; - try { - require ($rep.$vues['login']); - }catch (Exception $e){ - $error = "404"; - require($rep.$vues['erreur']); - } - } - - private function goToSignUp() { - global $rep, $vues, $error; - try { - require ($rep.$vues['signUp']); - }catch (Exception $e){ - $error = "404"; - require($rep.$vues['erreur']); - } - } - - private function goToEnigme() { - global $rep, $vues, $error; - try { - require ($rep.$vues['enigme']); - }catch (Exception $e){ - $error = "404"; - require($rep.$vues['erreur']); - } - } - - private function goToQueue() { - global $rep, $vues, $error; - try { - require ($rep.$vues['Queue']); - }catch (Exception $e){ - $error = "404"; - require($rep.$vues['erreur']); + require($rep . $vues['erreur']); } } } \ No newline at end of file diff --git a/WEB/Controller/UtilisateurGateway.php b/WEB/Controller/UtilisateurGateway.php index e9c2ec01..9306480c 100644 --- a/WEB/Controller/UtilisateurGateway.php +++ b/WEB/Controller/UtilisateurGateway.php @@ -1,5 +1,4 @@ con = $con; + } catch (Exception $e) { + $error = $e->getMessage(); + require($rep . $vues['erreur']); + } } /** diff --git a/WEB/Factory/PartieFactory.php b/WEB/Factory/PartieFactory.php index 58f3bb53..de23d7c0 100644 --- a/WEB/Factory/PartieFactory.php +++ b/WEB/Factory/PartieFactory.php @@ -1,6 +1,4 @@ enigme_gateway = new EnigmeGateway(); + $this->partie_gateway = new PartieGateway(); + $this->utilisateur_gateway = new UtilisateurGateway(); + } catch (Exception $e) { + $error = $e->getMessage(); + require($rep . $view['erreur']); + } + } + + public function signUp() { + global $rep, $vues, $sel, $error; + try { + $validation = new Validation(); + if (! $validation->ValidateEmail($_REQUEST['email'])) { + $error = "Email invalides."; + throw (new Exception("Email non valide")); + } + if(! $validation->ValidateUsername($_REQUEST['username'])){ + $error = "Nom d'utilisateur invalides. Il ne doit pas contenir de caractère spéciaux."; + throw(new Exception("Pseudo non valide")); + } + if(! $validation->ValidatePassword($_REQUEST['password'])){ + $error = "Mots de passe invalides. Il ne doit pas dépasser 100 caractères."; + throw(new Exception("Mot de passe non valide")); + } + $j = $this->utilisateur_gateway->getUtilisateurByEmail($_REQUEST['email']); + if ($j->getEmail() != "null") { + $error = "Email déjà utilisé."; + throw (new Exception("Email déjà utilisé")); + } + $password = password_hash($_REQUEST['password'], PASSWORD_DEFAULT); + $estAdmin = $this->utilisateur_gateway->getEstAdminByEmail($_REQUEST['email']); + $Utilisateur = new Utilisateur($_REQUEST['email'], $_REQUEST['username'], $password, $estAdmin); + $this->utilisateur_gateway->insert($Utilisateur); + $_SESSION['connected'] = 'true'; + $_SESSION['role'] = 'utilisateur'; + require ($rep.$vues['main']); + }catch (PDOException $e) + { + $error = "Erreur de connexion à la base de données."; + require ($rep.$vues['erreur']); + } + catch (Exception $e){ + require($rep.$vues['erreur']); + } + } + public function login(){ + global $rep, $vues, $sel, $error; + try { + $Utilisateur = $this->utilisateur_gateway->getUtilisateurByEmail($_REQUEST['email']); + if ($Utilisateur->getEmail() == "null") { + $error = "Utilisateur non trouvé."; + throw new Exception("Utilisateur introuvable"); + } + $mdp = $this->utilisateur_gateway->getMdpByEmail($_REQUEST['email']); + if (password_verify($mdp, $_REQUEST['password'])){ + $error = "Mot de passe incorrect."; + throw new Exception("Mot de passe invalide"); + } + $estAdmin = $this->utilisateur_gateway->getEstAdminByEmail($_REQUEST['email']); + if ($estAdmin == true) { + $_SESSION['role'] = "admin"; + } + else{ + $_SESSION['role'] = "utilisateur"; + } + + $_SESSION['connected'] = 'true'; + require ($rep.$vues['main']); + }catch (Exception $e){ + require($rep.$vues['erreur']); + } + } + + public function goToPresentation() { + global $rep, $vues, $error; + try { + require ($rep.$vues['presentation']); + }catch (Exception $e){ + $error = "Erreur Inconnue"; + require($rep.$vues['erreur']); + } + } + + public function goToHome() { + global $rep, $vues, $error; + try { + require ($rep.$vues['main']); + }catch (Exception $e){ + $error = "404"; + require($rep.$vues['erreur']); + } + } + + public function goToLogin() { + global $rep, $vues, $error; + try { + require ($rep.$vues['login']); + }catch (Exception $e){ + $error = "404"; + require($rep.$vues['erreur']); + } + } + + public function goToSignUp() { + global $rep, $vues, $error; + try { + require ($rep.$vues['signUp']); + }catch (Exception $e){ + $error = "404"; + require($rep.$vues['erreur']); + } + } + + public function goToEnigme() { + global $rep, $vues, $error; + try { + require ($rep.$vues['enigme']); + }catch (Exception $e){ + $error = "404"; + require($rep.$vues['erreur']); + } + } + + public function goToQueue() { + global $rep, $vues, $error; + try { + require ($rep.$vues['Queue']); + }catch (Exception $e){ + $error = "404"; + require($rep.$vues['erreur']); + } + } +} \ No newline at end of file