From 1cc1128b00fd9ba8007f295828c40b038ee16636 Mon Sep 17 00:00:00 2001 From: "anthony.richard" Date: Wed, 15 Nov 2023 21:08:38 +0100 Subject: [PATCH] altorouter + fix des liens dans les pages --- Project/php/.htaccess | 3 + Project/php/composer.json | 3 +- Project/php/config/config.php | 3 + Project/php/controller/AdminController.php | 73 +----------- Project/php/controller/FrontController.php | 60 ++++------ Project/php/controller/StudentController.php | 42 ------- Project/php/controller/TeacherController.php | 81 ++++---------- Project/php/gateway/TranslationGateway.php | 11 +- Project/php/gateway/VocabularyListGateway.php | 11 +- Project/php/index.php | 2 +- Project/php/model/MdlTeacher.php | 29 ++--- Project/php/modeles/Student.php | 52 --------- Project/php/modeles/Teacher.php | 18 --- Project/php/templates/addGroupForm.twig | 3 +- Project/php/templates/addVocalList.html | 46 ++++++++ Project/php/templates/groupContainer.twig | 24 ++-- Project/php/templates/modifyPasswordForm.twig | 3 +- Project/php/templates/myAccountView.html | 3 +- Project/php/templates/userContainer.twig | 10 +- Project/php/templates/usersView.html | 30 +---- Project/php/templates/vuephp1.html | 105 ------------------ 21 files changed, 152 insertions(+), 460 deletions(-) create mode 100644 Project/php/.htaccess delete mode 100644 Project/php/modeles/Student.php delete mode 100755 Project/php/modeles/Teacher.php create mode 100644 Project/php/templates/addVocalList.html delete mode 100755 Project/php/templates/vuephp1.html diff --git a/Project/php/.htaccess b/Project/php/.htaccess new file mode 100644 index 0000000..7ded04e --- /dev/null +++ b/Project/php/.htaccess @@ -0,0 +1,3 @@ +RewriteEngine on +RewriteCond %{REQUEST_FILENAME} !-f +RewriteRule . index.php [L] \ No newline at end of file diff --git a/Project/php/composer.json b/Project/php/composer.json index 125a32b..fac9558 100755 --- a/Project/php/composer.json +++ b/Project/php/composer.json @@ -1,6 +1,7 @@ { "require": { - "twig/twig": "^3.0" + "twig/twig": "^3.0", + "altorouter/altorouter": "^2.0" }, "autoload": { "psr-4": { diff --git a/Project/php/config/config.php b/Project/php/config/config.php index d4364fc..0fbb5bc 100755 --- a/Project/php/config/config.php +++ b/Project/php/config/config.php @@ -16,3 +16,6 @@ $login = 'anrichard7'; global $password; $password = 'achanger'; + +global $altorouterPath; +$altorouterPath = "/SAE_2A_Anglais/Project/php"; diff --git a/Project/php/controller/AdminController.php b/Project/php/controller/AdminController.php index f281a0e..43d7b39 100755 --- a/Project/php/controller/AdminController.php +++ b/Project/php/controller/AdminController.php @@ -8,75 +8,6 @@ use Exception; class AdminController { - public function __construct() - { - global $twig; - - try { - $action = Validation::val_action($_REQUEST['action'] ?? null); - - switch($action) { - case 'showAllUsers': - $this->showAllUsers(); - break; - - case 'showAllAdmins': - $this->showAllAdmins(); - break; - - case 'showAllTeachers': - $this->showAllTeachers(); - break; - - case 'showAllStudents': - $this->showAllStudents(); - break; - - case 'removeUser': - $this->removeUser(); - break; - - case 'showAllGroups': - $this->showAllGroups(); - break; - - case 'showGroupDetails': - $this->showGroupDetails(); - break; - - case 'removeUserFromGroup': - $this->removeUserFromGroup(); - break; - - case 'removeGroup': - $this->removeGroup(); - break; - - case 'addGroup': - $this->addGroup(); - break; - - case 'addUserToGroup': - $this->addUserToGroup(); - break; - - case null: - echo $twig->render('home.html'); - break; - - default: - $dVueEreur[] = "Erreur d'appel php"; - echo $twig->render('vuephp1.html', ['dVueEreur' => $dVueEreur]); - break; - } - } - catch (Exception $e) { - $dVueEreur[] = $e->getMessage()." ".$e->getFile()." ".$e->getLine().'Erreur inattendue!!! '; - echo $twig->render('erreur.html', ['dVueEreur' => $dVueEreur]); - } - exit(0); - } - public function showAllUsers(): void { global $twig; $model = new MdlAdmin(); @@ -107,7 +38,7 @@ class AdminController public function removeUser(): void { try { - $id = Validation::filter_int($_GET['id'] ?? null); + $id = Validation::filter_int($_GET['userID'] ?? null); $model = new MdlAdmin(); $model->removeUser($id); $this->showAllUsers(); @@ -136,7 +67,7 @@ class AdminController echo $twig->render('manageGroupView.html', ['groups' => $groups, 'selectedGroup' => $selectedGroup, 'users' => $users, 'unassignedUsers' => $unassignedUsers]); } catch (Exception $e) { - throw new Exception("invalid group ID"); + throw new Exception("invalid group ID"); } } diff --git a/Project/php/controller/FrontController.php b/Project/php/controller/FrontController.php index 1804d12..5eeefb6 100755 --- a/Project/php/controller/FrontController.php +++ b/Project/php/controller/FrontController.php @@ -1,45 +1,31 @@ setBasePath($altorouterPath); + + $router->map('GET', '/', 'AppController'); + $router->map( 'GET|POST', '/admin/[i:id]/[a:action]?', 'AdminController'); + $router->map( 'GET|POST', '/teacher/[i:id]/[a:action]?', 'TeacherController'); + $router->map( 'GET|POST', '/student/[i:id]/[a:action]?', 'StudentController'); + + $match = $router->match(); + + if (!$match) { throw new Exception("Erreur 404");} + + $controller = $match['target'] ?? null; + $action = Validation::val_action($match['params']['action'] ?? null); switch ($action) { case null: @@ -47,15 +33,17 @@ class FrontController break; default : - if (in_array($action, $this->adminActions)) new AdminController(); - else if (in_array($action, $this->teacherActions)) new TeacherController(); - else if (in_array($action, $this->studentActions)) new StudentController(); - else throw new Exception("invalid Action"); + $controller = '\\controller\\' . $controller; + $controller = new $controller; + + if (is_callable(array($controller, $action))) + call_user_func_array(array($controller, $action), array($match['params'])); + break; } } catch (Exception $e) { - $dVueEreur[] = $e->getMessage()." ".$e->getFile()." ".$e->getLine().'Erreur inattendue!!! '; + $dVueEreur[] = $e->getMessage(); echo $twig->render('erreur.html', ['dVueEreur' => $dVueEreur]); } } diff --git a/Project/php/controller/StudentController.php b/Project/php/controller/StudentController.php index a018836..1ed57d2 100755 --- a/Project/php/controller/StudentController.php +++ b/Project/php/controller/StudentController.php @@ -7,48 +7,6 @@ use Exception; class StudentController { - public function __construct() - { - global $twig; - - try { - $action = Validation::val_action($_REQUEST['action'] ?? null); - switch ($action) { - case 'allVocab': - $this->affAllVocab(); - break; - - case 'getByName': - $this->getByName($_REQUEST['nom']); - break; - - case 'showAccountInfos': - $this->showAccountInfos(); - break; - - case 'modifyNickname': - $this->modifyNickname(); - break; - - case 'modifyPassword': - $this->modifyPassword(); - break; - - case null: - echo $twig->render('home.html'); - break; - - default: - $dVueEreur[] = "Erreur d'appel php"; - echo $twig->render('vuephp1.html', ['dVueEreur' => $dVueEreur]); - break; - } - } - catch (Exception $e) { - $dVueEreur[] = $e->getMessage()." ".$e->getFile()." ".$e->getLine().'Erreur inattendue!!! '; - echo $twig->render('erreur.html', ['dVueEreur' => $dVueEreur]); - } - } public function affAllVocab(): void { diff --git a/Project/php/controller/TeacherController.php b/Project/php/controller/TeacherController.php index 0e17a9c..5900a5c 100755 --- a/Project/php/controller/TeacherController.php +++ b/Project/php/controller/TeacherController.php @@ -7,50 +7,6 @@ use Exception; class TeacherController { - public function __construct() - { - global $twig; - - try { - $action = Validation::val_action($_REQUEST['action'] ?? null); - switch ($action) { - - case 'getAllStudent': - $this->affAllStudent(); - break; - - case 'showAllVocab': - $this->affAllVocab(); - break; - case 'getVocabByName': - $this->getByName(); - break; - case 'addVocab': - break; - case 'showAllGroup': - $this->findAllGroup(); - break; - - - /* case 'delVoc': - $this->delById($_REQUEST['id']); - break;*/ - - case null: - echo $twig->render('home.html'); - break; - - default: - $dVueEreur[] = "Erreur d'appel php"; - echo $twig->render('vuephp1.html', ['dVueEreur' => $dVueEreur]); - break; - } - } - catch (Exception $e) { - $dVueEreur[] = $e->getMessage()." ".$e->getFile()." ".$e->getLine().'Erreur inattendue!!! '; - echo $twig->render('erreur.html', ['dVueEreur' => $dVueEreur]); - } - } public function affAllStudent(): void { global $twig; @@ -60,26 +16,20 @@ class TeacherController } - public function affAllVocab(): void { global $twig; $mdl = new MdlTeacher(); $student = $mdl->getAll(); echo $twig->render('usersView.html', ['users' => $student]); - } - public function getByName(): void + public function getByName($name): void { global $twig; $mdl = new MdlTeacher(); - if (isset($_GET['name'])) { - // Get the 'name' parameter from the $_GET array - $name = $_GET['name']; - $vocab = $mdl->getVocabByName($name); - echo $twig->render('usersView.html', ['users' => $vocab,]); - } + $vocab = $mdl->getVocabByName($name); + echo $twig->render('usersView.html', ['users' => $vocab]); } @@ -87,17 +37,28 @@ class TeacherController global $twig; $mdl = new MdlTeacher(); $vocab = $mdl->removeVocById($id); - echo $twig->render('usersView.html', ['vocab' => $vocab]); + echo $twig->render('usersView.html', ['users' => $vocab]); + } + public function showVocabListForm(): void { + global $twig; + $userID = Validation::filter_int($_GET['userID'] ?? null); + echo $twig->render('addVocabList.html', ['user' => $userID]); } - public function findAllGroup(){ + public function addVocabList():void { global $twig; $mdl = new MdlTeacher(); - $group = $mdl->getGroup(); - $user = $mdl->getUnassignedUsers(); - echo $twig->render('manageVocabListView.html', ['groups' => $group,'unassignedUsers' => $user]); + $userID = Validation::filter_int($_GET['userID'] ?? null); + $name = Validation::filter_str_simple($_GET['listName'] ?? null); + $words = array(); + for ($i = 0; $i <= 1; $i++) { + $frenchWord = Validation::filter_str_simple($_GET['frenchWord'.$i] ?? null); + $englishWord = Validation::filter_str_simple($_GET['englishWord'.$i] ?? null); + $words[] = array($frenchWord, $englishWord); + } + var_dump($words); + $mdl->addVocabList($userID, $name, "", $words); + echo $twig->render('addVocabList.html'); } - - } \ No newline at end of file diff --git a/Project/php/gateway/TranslationGateway.php b/Project/php/gateway/TranslationGateway.php index 32509c2..2ed54e3 100755 --- a/Project/php/gateway/TranslationGateway.php +++ b/Project/php/gateway/TranslationGateway.php @@ -23,13 +23,12 @@ class TranslationGateway extends AbsGateway public function add(array $parameters): int // require 4 elements { try { + $this->addWord($parameters[0]);var_dump($parameters[0]);var_dump($parameters[1]); $this->addWord($parameters[1]); - $this->addWord($parameters[2]); - $query = "INSERT INTO Translate VALUES(:id, :word1, :word2, :idVoc)"; - $args = array(':id' => array($parameters[0], PDO::PARAM_INT), - ':word1' => array($parameters[1], PDO::PARAM_STR), - ':word2' => array($parameters[2], PDO::PARAM_STR), - ':idVoc' => array($parameters[3], PDO::PARAM_INT)); + $query = "INSERT INTO Translate VALUES(null, :word1, :word2, :idVoc)"; + $args = array(':word1' => array($parameters[0], PDO::PARAM_STR), + ':word2' => array($parameters[1], PDO::PARAM_STR), + ':idVoc' => array($parameters[2], PDO::PARAM_INT)); $this->con->executeQuery($query, $args); return $this->con->lastInsertId(); } diff --git a/Project/php/gateway/VocabularyListGateway.php b/Project/php/gateway/VocabularyListGateway.php index ff0c037..4cc07b2 100755 --- a/Project/php/gateway/VocabularyListGateway.php +++ b/Project/php/gateway/VocabularyListGateway.php @@ -12,14 +12,13 @@ class VocabularyListGateway extends AbsGateway parent::__construct(); } - public function add(array $parameters): int // require 4 elements + public function add(array $parameters): int // require 3 elements { try{ - $query = "INSERT INTO VocabularyList VALUES(:id,:name,:img,:aut)"; - $args = array(':id'=>array($parameters[0],PDO::PARAM_INT), - ':name'=>array($parameters[1],PDO::PARAM_STR), - ':img'=>array($parameters[2],PDO::PARAM_STR), - ':aut'=>array($parameters[3],PDO::PARAM_INT)); + $query = "INSERT INTO VocabularyList VALUES(NULL, :name,:img,:aut)"; + $args = array(':name'=>array($parameters[0],PDO::PARAM_STR), + ':img'=>array($parameters[1],PDO::PARAM_STR), + ':aut'=>array($parameters[2],PDO::PARAM_INT)); $this->con->ExecuteQuery($query,$args); return $this->con->lastInsertId(); } diff --git a/Project/php/index.php b/Project/php/index.php index 6a5810d..06ae047 100755 --- a/Project/php/index.php +++ b/Project/php/index.php @@ -13,4 +13,4 @@ $twig = new \Twig\Environment($loader, [ 'cache' => false, ]); -$ctrl = new FrontController(); \ No newline at end of file +new FrontController(); \ No newline at end of file diff --git a/Project/php/model/MdlTeacher.php b/Project/php/model/MdlTeacher.php index ae7231e..d28f15e 100755 --- a/Project/php/model/MdlTeacher.php +++ b/Project/php/model/MdlTeacher.php @@ -2,9 +2,11 @@ namespace model; -use gateway\GroupGateway; +use gateway\TranslationGateway; use gateway\UserGateway; +use gateway\VocabularyGateway; use gateway\VocabularyListGateway; + class MdlTeacher extends AbsModel { @@ -14,7 +16,7 @@ class MdlTeacher extends AbsModel } public function getAll():array{ - $gtw = new VocabularyListGateway(); + $gtw = new VocabularyGateway(); return $gtw->findAll(); } @@ -24,27 +26,26 @@ class MdlTeacher extends AbsModel } public function getVocabByName(string $name):array{ - $gtw = new VocabularyListGateway(); + $gtw = new VocabularyGateway(); $res = $gtw->findByName($name); return $res; } public function RemoveVocById(int $id):void{ - $gtw = new VocabularyListGateway(); - $gtw->remove($id); - } - - public function getGroup():array{ - $gtw = new GroupGateway(); - return $gtw->findAll(); + $gtw = new VocabularyGateway(); + $res = $gtw->remove($id); } - public function getUnassignedUsers(): array { - $gtw = new UserGateway(); - return $gtw->findUnassignedUsers(); + public function addVocabList(int $userID, string $name, string $image, array $words): void { + $vocabGtw = new VocabularyListGateway(); + $vocabID = $vocabGtw->add(array($name, $image, $userID)); + $transGtw = new TranslationGateway(); + foreach ($words as $word) { + var_dump($word[0]." ".$word[1]); + $transGtw->add(array($word[0], $word[1], $vocabID)); + } } - public function is() { // TODO: Implement is() method. diff --git a/Project/php/modeles/Student.php b/Project/php/modeles/Student.php deleted file mode 100644 index dc118d5..0000000 --- a/Project/php/modeles/Student.php +++ /dev/null @@ -1,52 +0,0 @@ -nickname = $nickname; - $this->extraTime = $extraTime; - } - - /** - * @return bool - */ - public function isExtraTime(): bool - { - return $this->extraTime; - } - - /** - * @param bool $extraTime - */ - public function setExtraTime(bool $extraTime): void - { - $this->extraTime = $extraTime; - } - - /** - * @param String $nickname - */ - public function setNickname(string $nickname): void - { - $this->nickname = $nickname; - } - - - - - - -} \ No newline at end of file diff --git a/Project/php/modeles/Teacher.php b/Project/php/modeles/Teacher.php deleted file mode 100755 index a4507a4..0000000 --- a/Project/php/modeles/Teacher.php +++ /dev/null @@ -1,18 +0,0 @@ - - +
diff --git a/Project/php/templates/addVocalList.html b/Project/php/templates/addVocalList.html new file mode 100644 index 0000000..7bd0c2e --- /dev/null +++ b/Project/php/templates/addVocalList.html @@ -0,0 +1,46 @@ + + + + + My account + + + + + + + + + + + + + +
+

Add words

+ {% if user is defined %} + + + + + + {% for i in 0..1 %} + + + + + {% endfor %} + +
+ + + +
+ + {% endif %} +
+ + + \ No newline at end of file diff --git a/Project/php/templates/groupContainer.twig b/Project/php/templates/groupContainer.twig index a18b675..7bdb423 100644 --- a/Project/php/templates/groupContainer.twig +++ b/Project/php/templates/groupContainer.twig @@ -12,23 +12,27 @@ Sector - {{row.id}} - {{row.num}} - {{row.year}} - {{row.sector}} + {{ row.id }} + {{ row.num }} + {{ row.year }} + {{ row.sector }} {% if actions is defined %} {% if 'showGroupDetails' in actions %} - - - + + + + + {% endif %} {% if 'removeGroup' in actions %} - - - + + + + + {% endif %} {% endif %} diff --git a/Project/php/templates/modifyPasswordForm.twig b/Project/php/templates/modifyPasswordForm.twig index 3a06d25..0b3e503 100644 --- a/Project/php/templates/modifyPasswordForm.twig +++ b/Project/php/templates/modifyPasswordForm.twig @@ -1,5 +1,4 @@ -
- + diff --git a/Project/php/templates/myAccountView.html b/Project/php/templates/myAccountView.html index 8cd6fab..a9dfded 100644 --- a/Project/php/templates/myAccountView.html +++ b/Project/php/templates/myAccountView.html @@ -32,8 +32,7 @@ ID : {{user.id}} Nickname : {{user.nickname}} - - + diff --git a/Project/php/templates/userContainer.twig b/Project/php/templates/userContainer.twig index 0e20492..fb83c49 100644 --- a/Project/php/templates/userContainer.twig +++ b/Project/php/templates/userContainer.twig @@ -24,18 +24,22 @@ {% if action is defined %} {% if action == 'removeUserFromGroup' %} - + {% elseif action == 'addUserToGroup' %} - + + + {% elseif action == 'removeUser' %} + + + {% endif %} {% endif %} - {% endfor %} {% endif %} diff --git a/Project/php/templates/usersView.html b/Project/php/templates/usersView.html index 3a90416..303c428 100755 --- a/Project/php/templates/usersView.html +++ b/Project/php/templates/usersView.html @@ -7,35 +7,7 @@
- - - - - - - - - - - - {% if users is defined %} - {% for row in users %} - - - - - - - - - - - - {% endfor %} - {% endif %} -
IdNicknameNameSurnameMailGroupRoleExtra Time
{{row.id}}{{row.nickname}}{{row.name}}{{row.surname}}{{row.email}}{{row.group}}{{row.roles|join(', ')}}{{row.extraTime? 'yes' : 'no' }} - -
+ {% include 'userContainer.twig' with {'users' : users, 'action' : 'removeUser'} %}
\ No newline at end of file diff --git a/Project/php/templates/vuephp1.html b/Project/php/templates/vuephp1.html deleted file mode 100755 index 29de786..0000000 --- a/Project/php/templates/vuephp1.html +++ /dev/null @@ -1,105 +0,0 @@ - - - - - - Personne - formulaire - - - - - - {% if dVue is defined %} -
- {% if dVueEreur is defined and dVueEreur|length >0 %} -

ERREUR !!!!!

- {% for value in dVueEreur %} -

{{value}}

- {% endfor %} - {% endif %} - -

Personne - formulaire

-
- - {{dVue.data}} - - - - - - - - - - - - -
Nom - -
Age - -
- - - - - - -
- -
- - - -
- {% else %} -

Erreur !!
utilisation anormale de la vuephp

- {% endif %} -

- Essayez de mettre du code html dans nom -> Correspond à une attaque de type injection -

- -