altorouter + fix des liens dans les pages

php
Anthony RICHARD 1 year ago
parent ef6b71e19b
commit 1cc1128b00

@ -0,0 +1,3 @@
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]

@ -1,6 +1,7 @@
{ {
"require": { "require": {
"twig/twig": "^3.0" "twig/twig": "^3.0",
"altorouter/altorouter": "^2.0"
}, },
"autoload": { "autoload": {
"psr-4": { "psr-4": {

@ -16,3 +16,6 @@ $login = 'anrichard7';
global $password; global $password;
$password = 'achanger'; $password = 'achanger';
global $altorouterPath;
$altorouterPath = "/SAE_2A_Anglais/Project/php";

@ -8,75 +8,6 @@ use Exception;
class AdminController 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 { public function showAllUsers(): void {
global $twig; global $twig;
$model = new MdlAdmin(); $model = new MdlAdmin();
@ -107,7 +38,7 @@ class AdminController
public function removeUser(): void { public function removeUser(): void {
try { try {
$id = Validation::filter_int($_GET['id'] ?? null); $id = Validation::filter_int($_GET['userID'] ?? null);
$model = new MdlAdmin(); $model = new MdlAdmin();
$model->removeUser($id); $model->removeUser($id);
$this->showAllUsers(); $this->showAllUsers();
@ -136,7 +67,7 @@ class AdminController
echo $twig->render('manageGroupView.html', ['groups' => $groups, 'selectedGroup' => $selectedGroup, 'users' => $users, 'unassignedUsers' => $unassignedUsers]); echo $twig->render('manageGroupView.html', ['groups' => $groups, 'selectedGroup' => $selectedGroup, 'users' => $users, 'unassignedUsers' => $unassignedUsers]);
} }
catch (Exception $e) { catch (Exception $e) {
throw new Exception("invalid group ID"); throw new Exception("invalid group ID");
} }
} }

@ -1,45 +1,31 @@
<?php <?php
namespace controller; namespace controller;
use config\Validation; use config\Validation;
use Exception; use Exception;
class FrontController class FrontController
{ {
private array $adminActions = array( public function __construct() {
'showAllUsers',
'showAllAdmins',
'showAllTeachers',
'showAllStudents',
'removeUser',
'showAllGroups',
'showGroupDetails',
'removeUserFromGroup',
'removeGroup',
'addGroup',
'addUserToGroup'
);
private array $teacherActions = array(
'showAllGroup',
'showAllVocab',
'getVocabByName'
);
private array $studentActions = array(
'showAccountInfos',
'modifyNickname',
'modifyPassword'
);
public function __construct()
{
global $twig; global $twig;
session_start(); global $altorouterPath;
$dVueEreur = array();
try { try {
$action = Validation::val_action($_REQUEST['action'] ?? null); $router = new \AltoRouter();
$router->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) { switch ($action) {
case null: case null:
@ -47,15 +33,17 @@ class FrontController
break; break;
default : default :
if (in_array($action, $this->adminActions)) new AdminController(); $controller = '\\controller\\' . $controller;
else if (in_array($action, $this->teacherActions)) new TeacherController(); $controller = new $controller;
else if (in_array($action, $this->studentActions)) new StudentController();
else throw new Exception("invalid Action"); if (is_callable(array($controller, $action)))
call_user_func_array(array($controller, $action), array($match['params']));
break; break;
} }
} }
catch (Exception $e) { catch (Exception $e) {
$dVueEreur[] = $e->getMessage()." ".$e->getFile()." ".$e->getLine().'Erreur inattendue!!! '; $dVueEreur[] = $e->getMessage();
echo $twig->render('erreur.html', ['dVueEreur' => $dVueEreur]); echo $twig->render('erreur.html', ['dVueEreur' => $dVueEreur]);
} }
} }

@ -7,48 +7,6 @@ use Exception;
class StudentController 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 public function affAllVocab(): void
{ {

@ -7,50 +7,6 @@ use Exception;
class TeacherController 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 public function affAllStudent(): void
{ {
global $twig; global $twig;
@ -60,26 +16,20 @@ class TeacherController
} }
public function affAllVocab(): void public function affAllVocab(): void
{ {
global $twig; global $twig;
$mdl = new MdlTeacher(); $mdl = new MdlTeacher();
$student = $mdl->getAll(); $student = $mdl->getAll();
echo $twig->render('usersView.html', ['users' => $student]); echo $twig->render('usersView.html', ['users' => $student]);
} }
public function getByName(): void public function getByName($name): void
{ {
global $twig; global $twig;
$mdl = new MdlTeacher(); $mdl = new MdlTeacher();
if (isset($_GET['name'])) { $vocab = $mdl->getVocabByName($name);
// Get the 'name' parameter from the $_GET array echo $twig->render('usersView.html', ['users' => $vocab]);
$name = $_GET['name'];
$vocab = $mdl->getVocabByName($name);
echo $twig->render('usersView.html', ['users' => $vocab,]);
}
} }
@ -87,17 +37,28 @@ class TeacherController
global $twig; global $twig;
$mdl = new MdlTeacher(); $mdl = new MdlTeacher();
$vocab = $mdl->removeVocById($id); $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; global $twig;
$mdl = new MdlTeacher(); $mdl = new MdlTeacher();
$group = $mdl->getGroup(); $userID = Validation::filter_int($_GET['userID'] ?? null);
$user = $mdl->getUnassignedUsers(); $name = Validation::filter_str_simple($_GET['listName'] ?? null);
echo $twig->render('manageVocabListView.html', ['groups' => $group,'unassignedUsers' => $user]); $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');
} }
} }

@ -23,13 +23,12 @@ class TranslationGateway extends AbsGateway
public function add(array $parameters): int // require 4 elements public function add(array $parameters): int // require 4 elements
{ {
try { try {
$this->addWord($parameters[0]);var_dump($parameters[0]);var_dump($parameters[1]);
$this->addWord($parameters[1]); $this->addWord($parameters[1]);
$this->addWord($parameters[2]); $query = "INSERT INTO Translate VALUES(null, :word1, :word2, :idVoc)";
$query = "INSERT INTO Translate VALUES(:id, :word1, :word2, :idVoc)"; $args = array(':word1' => array($parameters[0], PDO::PARAM_STR),
$args = array(':id' => array($parameters[0], PDO::PARAM_INT), ':word2' => array($parameters[1], PDO::PARAM_STR),
':word1' => array($parameters[1], PDO::PARAM_STR), ':idVoc' => array($parameters[2], PDO::PARAM_INT));
':word2' => array($parameters[2], PDO::PARAM_STR),
':idVoc' => array($parameters[3], PDO::PARAM_INT));
$this->con->executeQuery($query, $args); $this->con->executeQuery($query, $args);
return $this->con->lastInsertId(); return $this->con->lastInsertId();
} }

@ -12,14 +12,13 @@ class VocabularyListGateway extends AbsGateway
parent::__construct(); parent::__construct();
} }
public function add(array $parameters): int // require 4 elements public function add(array $parameters): int // require 3 elements
{ {
try{ try{
$query = "INSERT INTO VocabularyList VALUES(:id,:name,:img,:aut)"; $query = "INSERT INTO VocabularyList VALUES(NULL, :name,:img,:aut)";
$args = array(':id'=>array($parameters[0],PDO::PARAM_INT), $args = array(':name'=>array($parameters[0],PDO::PARAM_STR),
':name'=>array($parameters[1],PDO::PARAM_STR), ':img'=>array($parameters[1],PDO::PARAM_STR),
':img'=>array($parameters[2],PDO::PARAM_STR), ':aut'=>array($parameters[2],PDO::PARAM_INT));
':aut'=>array($parameters[3],PDO::PARAM_INT));
$this->con->ExecuteQuery($query,$args); $this->con->ExecuteQuery($query,$args);
return $this->con->lastInsertId(); return $this->con->lastInsertId();
} }

@ -13,4 +13,4 @@ $twig = new \Twig\Environment($loader, [
'cache' => false, 'cache' => false,
]); ]);
$ctrl = new FrontController(); new FrontController();

@ -2,9 +2,11 @@
namespace model; namespace model;
use gateway\GroupGateway; use gateway\TranslationGateway;
use gateway\UserGateway; use gateway\UserGateway;
use gateway\VocabularyGateway;
use gateway\VocabularyListGateway; use gateway\VocabularyListGateway;
class MdlTeacher extends AbsModel class MdlTeacher extends AbsModel
{ {
@ -14,7 +16,7 @@ class MdlTeacher extends AbsModel
} }
public function getAll():array{ public function getAll():array{
$gtw = new VocabularyListGateway(); $gtw = new VocabularyGateway();
return $gtw->findAll(); return $gtw->findAll();
} }
@ -24,27 +26,26 @@ class MdlTeacher extends AbsModel
} }
public function getVocabByName(string $name):array{ public function getVocabByName(string $name):array{
$gtw = new VocabularyListGateway(); $gtw = new VocabularyGateway();
$res = $gtw->findByName($name); $res = $gtw->findByName($name);
return $res; return $res;
} }
public function RemoveVocById(int $id):void{ public function RemoveVocById(int $id):void{
$gtw = new VocabularyListGateway(); $gtw = new VocabularyGateway();
$gtw->remove($id); $res = $gtw->remove($id);
}
public function getGroup():array{
$gtw = new GroupGateway();
return $gtw->findAll();
} }
public function getUnassignedUsers(): array { public function addVocabList(int $userID, string $name, string $image, array $words): void {
$gtw = new UserGateway(); $vocabGtw = new VocabularyListGateway();
return $gtw->findUnassignedUsers(); $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() public function is()
{ {
// TODO: Implement is() method. // TODO: Implement is() method.

@ -1,52 +0,0 @@
<?php
namespace model;
use JetBrains\PhpStorm\Pure;
class Student extends User
{
public string $nickname;
private bool $extraTime;
/**
* @param String $nickname
* @param bool $extraTime
*/
#[Pure] public function __construct(string $id, string $mail, string $nom, string $prenom, string $nickname, bool $extraTime)
{
parent::__construct($id,$mail, $nom, $prenom);
$this->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;
}
}

@ -1,18 +0,0 @@
<?php
namespace modeles;
class Teacher extends User
{
protected function createVocabulary(){
}
protected function modifyVocabulary(){
}
}

@ -1,5 +1,4 @@
<form action="index.php" method="GET"> <form action="addGroup" method="GET">
<input type="hidden" name="action" value="addGroup">
<input name="num" type="text" placeholder="number" required> <input name="num" type="text" placeholder="number" required>
<input name="year" type="text" placeholder="year" required> <input name="year" type="text" placeholder="year" required>
<input name="sector" type="text" placeholder="sector" required> <input name="sector" type="text" placeholder="sector" required>

@ -0,0 +1,46 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>My account</title>
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
<meta name="description" content="" />
<meta name="author" content="" />
<link rel="icon" type="image/x-icon" href="assets/favicon.ico" />
<!-- Font Awesome icons (free version)-->
<script src="https://use.fontawesome.com/releases/v6.3.0/js/all.js" crossorigin="anonymous"></script>
<!-- Google fonts-->
<link href="https://fonts.googleapis.com/css?family=Varela+Round" rel="stylesheet" />
<link
href="https://fonts.googleapis.com/css?family=Nunito:200,200i,300,300i,400,400i,600,600i,700,700i,800,800i,900,900i"
rel="stylesheet" />
<!-- Core theme CSS (includes Bootstrap)-->
<link href="css/styles.css" rel="stylesheet" />
</head>
<section>
<h1>Add words</h1>
{% if user is defined %}
<form action="index.php" method="GET">
<table>
<tr>
<td colspan="2">
<input type="hidden" name="action" value="addVocabList">
<input type="hidden" name="userID" value="{{user}}">
<input type="text" name="listName" placeholder="list name" required>
</td>
</tr>
{% for i in 0..1 %}
<tr>
<td><input type="text" name="frenchWord{{i}}" placeholder="french word"></td>
<td><input type="text" name="englishWord{{i}}" placeholder="english word"></td>
</tr>
{% endfor %}
<tr><td colspan="2"><input type="submit" value="Add"></td></tr>
</table>
</form>
{% endif %}
</section>
</body>
</html>

@ -12,23 +12,27 @@
<td>Sector</td> <td>Sector</td>
</tr> </tr>
<tr> <tr>
<td>{{row.id}}</td> <td>{{ row.id }}</td>
<td>{{row.num}}</td> <td>{{ row.num }}</td>
<td>{{row.year}}</td> <td>{{ row.year }}</td>
<td>{{row.sector}}</td> <td>{{ row.sector }}</td>
{% if actions is defined %} {% if actions is defined %}
{% if 'showGroupDetails' in actions %} {% if 'showGroupDetails' in actions %}
<td><a href="index.php?action=showGroupDetails&selectedGroup={{row.id}}"> <td>
<input class="btn-black" type="button" value="Show"/> <a href="showGroupDetails?selectedGroup={{ row.id }}">
</a></td> <input class="btn-black" type="button" value="Show"/>
</a>
</td>
{% endif %} {% endif %}
{% if 'removeGroup' in actions %} {% if 'removeGroup' in actions %}
<td><a href="index.php?action=removeGroup&selectedGroup={{row.id}}"> <td>
<input class="btn-black" type="button" value="Remove"/> <a href="removeGroup?selectedGroup={{ row.id }}">
</a></td> <input class="btn-black" type="button" value="Remove"/>
</a>
</td>
{% endif %} {% endif %}
{% endif %} {% endif %}

@ -1,5 +1,4 @@
<form action="index.php" method="GET"> <form action="modifyPassword" method="GET">
<input type="hidden" name="action" value="modifyPassword">
<input type="hidden" name="user" value={{user.id}}> <input type="hidden" name="user" value={{user.id}}>
<input type="text" name="currentPassword" placeholder="current password" required> <input type="text" name="currentPassword" placeholder="current password" required>
<input type="text" name="newPassword" placeholder="new password" required> <input type="text" name="newPassword" placeholder="new password" required>

@ -32,8 +32,7 @@
<tr><td>ID : </td><td>{{user.id}}</td></tr> <tr><td>ID : </td><td>{{user.id}}</td></tr>
<tr><td>Nickname : </td><td>{{user.nickname}}</td> <tr><td>Nickname : </td><td>{{user.nickname}}</td>
<td> <td>
<form action="index.php" method="GET"> <form action="modifyNickname" method="GET">
<input type="hidden" name="action" value="modifyNickname">
<input type="hidden" name="user" value={{user.id}}> <input type="hidden" name="user" value={{user.id}}>
<input name="newNickname" type="text" placeholder="new nickname" required> <input name="newNickname" type="text" placeholder="new nickname" required>
<input type="submit" value="Modify your nickname"> <input type="submit" value="Modify your nickname">

@ -24,18 +24,22 @@
{% if action is defined %} {% if action is defined %}
{% if action == 'removeUserFromGroup' %} {% if action == 'removeUserFromGroup' %}
<td><a href="index.php?action=removeUserFromGroup&id={{row.id}}&selectedGroup={{selectedGroup}}"> <td><a href="removeUserFromGroup?id={{row.id}}&selectedGroup={{selectedGroup}}">
<input class="btn-black" type="button" value="Delete from group"/> <input class="btn-black" type="button" value="Delete from group"/>
</a></td> </a></td>
{% elseif action == 'addUserToGroup' %} {% elseif action == 'addUserToGroup' %}
<td><a href="index.php?action=addUserToGroup&userID={{row.id}}&groupID={{selectedGroup}}"> <td><a href="addUserToGroup?userID={{row.id}}&groupID={{selectedGroup}}">
<input class="btn-black" type="button" value="Add to group"/> <input class="btn-black" type="button" value="Add to group"/>
</a></td> </a></td>
{% elseif action == 'removeUser' %}
<td><a href="removeUser?userID={{row.id}}">
<input class="btn-black" type="button" value="Delete"/>
</a></td>
{% endif %} {% endif %}
{% endif %} {% endif %}
</tr> </tr>
{% endfor %} {% endfor %}
{% endif %} {% endif %}

@ -7,35 +7,7 @@
<body> <body>
<section> <section>
<table> {% include 'userContainer.twig' with {'users' : users, 'action' : 'removeUser'} %}
<tr>
<td>Id</td>
<td>Nickname</td>
<td>Name</td>
<td>Surname</td>
<td>Mail</td>
<td>Group</td>
<td>Role</td>
<td>Extra Time</td>
</tr>
{% if users is defined %}
{% for row in users %}
<tr>
<td>{{row.id}}</td>
<td>{{row.nickname}}</td>
<td>{{row.name}}</td>
<td>{{row.surname}}</td>
<td>{{row.email}}</td>
<td>{{row.group}}</td>
<td>{{row.roles|join(', ')}}</td>
<td>{{row.extraTime? 'yes' : 'no' }}</td>
<td><a href="index.php?action=removeUser&id={{row.id}}">
<input class="btn-black" type="button" value="Delete"/>
</a></td>
</tr>
{% endfor %}
{% endif %}
</table>
</section> </section>
</body> </body>
</html> </html>

@ -1,105 +0,0 @@
<!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Personne - formulaire</title>
<script type="text/javascript">
function clearForm(oForm) {
const elements = oForm.elements;
oForm.reset();
for (i = 0; i < elements.length; i++) {
field_type = elements[i].type.toLowerCase();
switch (field_type) {
case "text":
case "password":
case "textarea":
case "hidden":
elements[i].value = "";
break;
case "radio":
case "checkbox":
if (elements[i].checked) {
elements[i].checked = false;
}
break;
case "select-one":
case "select-multi":
elements[i].selectedIndex = -1;
break;
default:
break;
}
}
}
</script>
</head>
<body>
<!-- on vérifie les données provenant du modèle -->
{% if dVue is defined %}
<div align="center">
{% if dVueEreur is defined and dVueEreur|length >0 %}
<h2>ERREUR !!!!!</h2>
{% for value in dVueEreur %}
<p>{{value}}</p>
{% endfor %}
{% endif %}
<h2>Personne - formulaire</h2>
<hr />
<!-- affichage de données provenant du modèle -->
{{dVue.data}}
<form method="post" name="myform" id="myform">
<table>
<tr>
<td>Nom</td>
<td>
<input name="txtNom" value="{{dVue.nom}}" type="text" size="20" />
</td>
</tr>
<tr>
<td>Age</td>
<td>
<input
name="txtAge"
value="{{dVue.age}}"
type="text"
size="3"
required
/>
</td>
</tr>
<tr></tr>
</table>
<table>
<tr>
<td><input type="submit" value="Envoyer" /></td>
<td><input type="reset" value="Rétablir" /></td>
<td>
<input
type="button"
value="Effacer"
onclick="clearForm(this.form);"
/>
</td>
</tr>
</table>
<!-- action !!!!!!!!!! -->
<input type="hidden" name="action" value="validationFormulaire" />
</form>
</div>
{% else %}
<p>Erreur !!<br />utilisation anormale de la vuephp</p>
{% endif %}
<p>
Essayez de mettre du code html dans nom -> Correspond à une attaque de type injection
</p>
</body>
</html>
Loading…
Cancel
Save