Nettoyage et validation des données version 1
continuous-integration/drone/push Build is passing Details

interestingProfiles
Johan LACHENAL 2 years ago
parent f14c6b07df
commit 2872f25859

@ -15,11 +15,30 @@ class Clean
public static function simpleString(string $string): string public static function simpleString(string $string): string
{ {
$string = trim($string); $string = trim($string);
$string = strip_tags($string); $string = strip_tags($string);
return htmlspecialchars($string); return htmlspecialchars($string);
} }
/**
* Cette fonction prend un tableau de chaînes de caractères en entrée et retourne un tableau de chaînes
* nettoyées.
* Elle supprime les espaces de début et de fin, ainsi que toutes les balises HTML, et encode les
* caractères spéciaux.
*
* @param array $array Le tableau de chaînes à nettoyer
* @return array Le tableau de chaînes nettoyées
*/
public static function simpleStringArray(array $array): array
{
$array = array_map('trim', $array);
$array = array_map('strip_tags', $array);
$array = array_map('htmlspecialchars', $array);
return $array;
}
/** /**
* Cette fonction prend une chaîne de caractères en entrée et retourne une version nettoyée de cette chaîne. * Cette fonction prend une chaîne de caractères en entrée et retourne une version nettoyée de cette chaîne.
* Elle supprime les espaces de début et de fin, ainsi que toutes les balises HTML, et encode les * Elle supprime les espaces de début et de fin, ainsi que toutes les balises HTML, et encode les
@ -45,4 +64,6 @@ class Clean
{ {
return filter_var($int, FILTER_SANITIZE_NUMBER_INT); return filter_var($int, FILTER_SANITIZE_NUMBER_INT);
} }
} }

@ -98,10 +98,35 @@ class Validate
global $responseMaxLength; global $responseMaxLength;
return (strlen($response) <= $responseMaxLength); return (strlen($response) <= $responseMaxLength);
} }
/**
* Vérifie si le nom est valide.
*
* @param string $name Le nom a vérifié.
* @return bool Vrai si le nom est valide, faux sinon.
*/
public static function username(string $username): bool public static function username(string $username): bool
{ {
global $usernameMaxLength; global $usernameMaxLength;
return (strlen($username) >= 3 && preg_match("#[a-zA-Z0-9]+#", $username) && strlen($username) <= $usernameMaxLength); return (strlen($username) >= 3 && preg_match("#[a-zA-Z0-9]+#", $username) && strlen($username) <= $usernameMaxLength);
} }
/**
* Vérifie si la description est valide.
*
* @param string $description La description a vérifié.
* @return bool Vrai si la description est valide, faux sinon.
*/
public static function categories(array $categories): bool
{
global $categoryMaxLength;
foreach ($categories as $category) {
if (strlen($category) > $categoryMaxLength) {
return false;
}
}
return true;
}
} }

@ -49,4 +49,5 @@ $keyWordMaxLength=50;
$titleMaxLength=50; $titleMaxLength=50;
$typeMaxLength=50; $typeMaxLength=50;
$responseMaxLength=200; $responseMaxLength=200;
$categoryMaxLenght=150;

@ -3,6 +3,8 @@
namespace Controller; namespace Controller;
use Model\ModelAdmin; use Model\ModelAdmin;
use Config\Clean;
use Config\Validate;
/** /**
* Permet de controller les réponses à fournir en fonction des actions passer dans l'URL * Permet de controller les réponses à fournir en fonction des actions passer dans l'URL
@ -19,7 +21,7 @@ class ControllerAdmin
*/ */
public function addQuestion(): void public function addQuestion(): void
{ {
$type = $_POST['type']; $type = Clean::simpleString($_POST['type']);
$idQuestion = (new ModelAdmin())->addQuestion(); $idQuestion = (new ModelAdmin())->addQuestion();
if (strcmp($type, "BusinessClass\TextQuestion") == 0) { if (strcmp($type, "BusinessClass\TextQuestion") == 0) {
$this->goToQuestions(); $this->goToQuestions();
@ -52,9 +54,9 @@ class ControllerAdmin
{ {
(new ModelAdmin())->addResponse(); (new ModelAdmin())->addResponse();
$categories = (new ModelAdmin())->getCategories(); $categories = (new ModelAdmin())->getCategories();
$idQuestion = $_POST['idQuestion']; $idQuestion = Clean::int($_POST['idQuestion']);
$questionContent = $_POST['question']; $questionContent = Clean::simpleString($_POST['question']);
$type = $_POST['type']; $type = Clean::simpleString($_POST['type']);
global $rep, $views; global $rep, $views;
require_once($rep.$views['continue']); require_once($rep.$views['continue']);
} }
@ -79,12 +81,12 @@ class ControllerAdmin
*/ */
public function continueResponse(): void public function continueResponse(): void
{ {
$choose = $_POST['choose']; $choose = Clean::simpleString($_POST['choose']);
if ($choose == "Oui") { if ($choose == "Oui") {
$idQuestion = $_POST['idQuestion']; $idQuestion = Clean::int($_POST['idQuestion']);
$categories = (new ModelAdmin())->getCategories(); $categories = (new ModelAdmin())->getCategories();
$questionContent = $_POST['question']; $questionContent = Clean::simpleString($_POST['question']);
$type = $_POST['type']; $type = Clean::simpleString($_POST['type']);
global $rep, $views; global $rep, $views;
require_once($rep.$views['possibleResponsesForm']); require_once($rep.$views['possibleResponsesForm']);
} else { } else {

@ -7,6 +7,8 @@ use Exception;
use GuzzleHttp\Client; use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException; use GuzzleHttp\Exception\GuzzleException;
use PDOException; use PDOException;
use Config\Validate;
use Config\Clean;
/** /**
* Permet de développer les fonctions appelées par le controllerAdmin pour gérer * Permet de développer les fonctions appelées par le controllerAdmin pour gérer
@ -39,26 +41,29 @@ class ModelAdmin
*/ */
public function addQuestion(): int public function addQuestion(): int
{ {
$questionContent = $_POST['question']; $questionContent = Clean::simpleString($_POST['question']);
$type = $_POST['type']; $type = Clean::simpleString($_POST['type']);
try { try {
if (validate::type($type)) {
$question = new $type(0, $questionContent); $question = new $type(0, $questionContent);
$res = $this->client->request('GET', 'https://codefirst.iut.uca.fr/containers/Temoignages-deploy_api_form/getForm'); $res = $this->client->request('GET', 'https://codefirst.iut.uca.fr/containers/Temoignages-deploy_api_form/getForm');
$form = json_decode($res->getBody()); $form = json_decode($res->getBody());
if (!empty($form)) { if (!empty($form)) {
$res = $this->client->request('POST', 'https://codefirst.iut.uca.fr/containers/Temoignages-deploy_api_form/addQuestion? $res = $this->client->request(
content='.$questionContent.'& 'POST',
classQuestion='.get_class($question).'& 'https://codefirst.iut.uca.fr/containers/Temoignages-deploy_api_form/addQuestion?
idForm='.$form[0]['id'] content='.$questionContent.'&
classQuestion='.get_class($question).'&
idForm='.$form[0]['id']
); );
return json_decode($res->getBody()); return json_decode($res->getBody());
} }
} else {
throw new Exception('Type de question invalide');
}
}catch (GuzzleException $g){ }catch (GuzzleException $g){
throw new Exception($g->getMessage(),$g->getCode(),$g); throw new Exception($g->getMessage(),$g->getCode(),$g);
} }
return -1; return -1;
} }
@ -70,9 +75,12 @@ class ModelAdmin
*/ */
public function deleteQuestion():void public function deleteQuestion():void
{ {
$idQuestion = $_POST["idQuestion"]; $idQuestion = Clean::int($_POST["idQuestion"]);
$type = $_POST["type"]; $type = Clean::simpleString($_POST["type"]);
try { try {
if (!validate::type($type)) {
throw new Exception('Type de question invalide');
}
$res = $this->client->request('DELETE', 'https://codefirst.iut.uca.fr/containers/Temoignages-deploy_api_form/deleteQuestion? $res = $this->client->request('DELETE', 'https://codefirst.iut.uca.fr/containers/Temoignages-deploy_api_form/deleteQuestion?
classQuestion='.$type.'& classQuestion='.$type.'&
id='.$idQuestion id='.$idQuestion
@ -93,13 +101,16 @@ class ModelAdmin
*/ */
public function addResponse(): void public function addResponse(): void
{ {
$idQuestion = $_POST['idQuestion']; $idQuestion = Clean::int($_POST['idQuestion']);
$response = $_POST['response']; $response = Clean::simpleString($_POST['response']);
$categories = $_POST['categories']; $categories = Clean::simpleStringArray($_POST['categories']);
if ($categories == null) { if ($categories == null) {
$categories = []; $categories = [];
} }
try { try {
if(!validate::categories($categories)){
throw new Exception('Categories invalides');
}
$this->client->request('POST', 'https://codefirst.iut.uca.fr/containers/Temoignages-deploy_api_form/insertResponseInQuestion? $this->client->request('POST', 'https://codefirst.iut.uca.fr/containers/Temoignages-deploy_api_form/insertResponseInQuestion?
response='.$response.'& response='.$response.'&
categories='.$categories.'& categories='.$categories.'&
@ -162,8 +173,11 @@ class ModelAdmin
*/ */
public function addKeyword(): void public function addKeyword(): void
{ {
$keyword = $_POST['keyword']; $keyword = Clean::simpleString($_POST['keyword']);
try { try {
if(!validate::keyword($keyword)){
throw new Exception('Mot-clef invalide');
}
$this->client->request('POST', 'https://codefirst.iut.uca.fr/containers/Temoignages-deploy_api_form/insertKeyword? $this->client->request('POST', 'https://codefirst.iut.uca.fr/containers/Temoignages-deploy_api_form/insertKeyword?
keyword='.$keyword keyword='.$keyword
); );
@ -276,7 +290,7 @@ class ModelAdmin
{ {
try { try {
$res = $this->client->request('DELETE', 'https://codefirst.iut.uca.fr/containers/Temoignages-deploy_api_form/deleteListResponseOfCandidate? $res = $this->client->request('DELETE', 'https://codefirst.iut.uca.fr/containers/Temoignages-deploy_api_form/deleteListResponseOfCandidate?
id='.$_POST["idResponseCandidate"] id='.Clean::int($_POST["idResponseCandidate"])
); );
if ($res->getStatusCode()!=200){ if ($res->getStatusCode()!=200){
throw new Exception('DeleteListResponseOfCandidate failed'); throw new Exception('DeleteListResponseOfCandidate failed');

@ -32,9 +32,8 @@ class ModelCandidate
public function submitForm(): void public function submitForm(): void
{ {
$answersAndCategories = $_POST['answers']; $answersAndCategories = $_POST['answers'];
$dataIds = null; $dataIds = null;
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'submitForm') { if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && Clean::simpleString($_POST['action']) === 'submitForm') {
$dataIdsJson = $_POST['data_ids']; $dataIdsJson = $_POST['data_ids'];
$dataIds = json_decode($dataIdsJson); $dataIds = json_decode($dataIdsJson);
} }

Loading…
Cancel
Save