From 41c4734624ab9bc8a57562d87bce1d45b652fad9 Mon Sep 17 00:00:00 2001 From: galaxyoyo Date: Sat, 21 Sep 2019 13:37:48 +0200 Subject: [PATCH] =?UTF-8?q?Phase=202=20:=20pi=C3=A8ces=20jointes=20dans=20?= =?UTF-8?q?les=20questions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server_files/classes/Question.php | 22 ++++++++ server_files/controllers/poser_questions.php | 59 +++++++++++++++----- server_files/controllers/view_file.php | 34 ++++++++++- server_files/views/poser_questions.php | 33 +++++++++-- 4 files changed, 128 insertions(+), 20 deletions(-) diff --git a/server_files/classes/Question.php b/server_files/classes/Question.php index 4407d7f..251b821 100644 --- a/server_files/classes/Question.php +++ b/server_files/classes/Question.php @@ -8,6 +8,7 @@ class Question private $to; private $problem; private $question; + private $attached_file; private function __construct() { @@ -29,6 +30,22 @@ class Question return $question; } + public static function fromAttachedFile($attached_file) + { + global $DB; + + $req = $DB->prepare("SELECT * FROM `questions` WHERE `attached_file` = ?;"); + $req->execute([htmlspecialchars($attached_file)]); + $data = $req->fetch(); + + if ($data === false) + return null; + + $question = new Question(); + $question->fill($data); + return $question; + } + public function fill($data) { foreach ($data as $key => $value) @@ -88,4 +105,9 @@ class Question { return $this->question; } + + public function getAttachedFile() + { + return $this->attached_file; + } } \ No newline at end of file diff --git a/server_files/controllers/poser_questions.php b/server_files/controllers/poser_questions.php index e2ecc04..daf3613 100644 --- a/server_files/controllers/poser_questions.php +++ b/server_files/controllers/poser_questions.php @@ -13,7 +13,7 @@ $has_error = false; $error_message = null; if (isset($_POST["give_questions"])) { - $give_questions = new GiveQuestions($_POST); + $give_questions = new GiveQuestions($_POST, $_FILES); try { $give_questions->makeVerifications(); $give_questions->giveQuestions(); @@ -36,38 +36,71 @@ class GiveQuestions private $question_4; private $question_5; private $question_6; + private $no_drawing; + private $files; - public function __construct($data) + public function __construct($data, $files) { foreach ($data as $key => $value) { $this->$key = $value; } + + $this->files = []; + + for ($i = 1; $i <= 6; ++$i) + $this->files[] = strlen($files["file_$i"]["name"]) > 0 ? $files["file_$i"] : null; } public function makeVerifications() { - global $team; + global $LOCAL_PATH, $team; $this->to_team = Team::fromTrigram($this->to); ensure($this->to_team, "L'équipe indiquée n'existe pas."); ensure($team->getProblem() == $this->to_team->getProblem(), "Les équipes ne travaillent pas sur le même problème."); - ensure($this->question_1 == null || $this->question_1 == "" || $this->question_2 == null || $this->question_2 == "" || $this->question_3 == null || $this->question_3 == "", + ensure($this->question_1 != null && $this->question_1 != "" && $this->question_2 != null && $this->question_2 != "" && $this->question_3 != null && $this->question_3 != "", "Vous devez poser au moins 3 questions."); + ensure(sizeof($_FILES) == 0 || $this->no_drawing, "Vous devez confirmer ne pas avoir inclus de texte dans vos pièces jointes."); + + for ($i = 0; $i < 6; ++$i) { + ensure($this->files[$i]["size"] <= 2e6, "Le fichier doit peser moins que 2 Mo."); + ensure(!$this->files[$i]["error"], "Une erreur est survenue."); + //ensure(finfo_file(finfo_open(FILEINFO_MIME_TYPE), $this->files[$i]["tmp_name"]) == "application/pdf", "Le fichier doit être au format PDF."); + } + + ensure(is_dir("$LOCAL_PATH/files") || mkdir("$LOCAL_PATH/files"), "Un problème est survenue dans l'envoi du fichier. Veuillez contacter l'administrateur du serveur."); } public function giveQuestions() { - global $DB, $team; + global $DB, $LOCAL_PATH, $team; + + $attached_file = []; + for ($i = 0; $i < 5; ++$i) + { + if ($this->files[$i] == null) + $attached_file[] = null; + else { + do + $id = genRandomPhrase(64); + while (file_exists("$LOCAL_PATH/files/$id")); + + if (!rename($this->files[$i]["tmp_name"], "$LOCAL_PATH/files/$id")) + throw new AssertionError("Une erreur est survenue lors de l'envoi du fichier."); + + $attached_file[] = $id; + } + } $DB->exec("DELETE FROM `questions` WHERE `from` = " . $team->getId() . " AND `to` = " . $this->to_team->getId() . ";"); - $req = $DB->prepare("INSERT INTO `questions`(`from`, `to`, `problem`, `question`) VALUES " - . "(?, ?, ?, ?), (?, ?, ?, ?), (?, ?, ?, ?), (?, ?, ?, ?), (?, ?, ?, ?), (?, ?, ?, ?);"); - $req->execute([$team->getId(), $this->to_team->getId(), $team->getProblem(), $this->question_1, - $team->getId(), $this->to_team->getId(), $team->getProblem(), $this->question_2, - $team->getId(), $this->to_team->getId(), $team->getProblem(), $this->question_3, - $team->getId(), $this->to_team->getId(), $team->getProblem(), $this->question_4, - $team->getId(), $this->to_team->getId(), $team->getProblem(), $this->question_5, - $team->getId(), $this->to_team->getId(), $team->getProblem(), $this->question_6]); + $req = $DB->prepare("INSERT INTO `questions`(`from`, `to`, `problem`, `question`, `attached_file`) VALUES " + . "(?, ?, ?, ?, ?), (?, ?, ?, ?, ?), (?, ?, ?, ?, ?), (?, ?, ?, ?, ?), (?, ?, ?, ?, ?), (?, ?, ?, ?, ?);"); + $req->execute([$team->getId(), $this->to_team->getId(), $team->getProblem(), $this->question_1, $attached_file[0], + $team->getId(), $this->to_team->getId(), $team->getProblem(), $this->question_2, $attached_file[1], + $team->getId(), $this->to_team->getId(), $team->getProblem(), $this->question_3, $attached_file[2], + $team->getId(), $this->to_team->getId(), $team->getProblem(), $this->question_4, $attached_file[3], + $team->getId(), $this->to_team->getId(), $team->getProblem(), $this->question_5, $attached_file[4], + $team->getId(), $this->to_team->getId(), $team->getProblem(), $this->question_6, $attached_file[5]]); } } diff --git a/server_files/controllers/view_file.php b/server_files/controllers/view_file.php index d7ac9d9..e6c180d 100644 --- a/server_files/controllers/view_file.php +++ b/server_files/controllers/view_file.php @@ -25,10 +25,38 @@ if ($file !== null) { $surname = $user->getSurname(); $first_name = $user->getFirstName(); $name = "Autorisation de droit à l'image de $first_name $surname.pdf"; -} else - require_once "server_files/404.php"; -header("Content-Type: application/pdf"); + header("Content-Type: application/pdf"); +} +else { + $question = Question::fromAttachedFile($id); + if ($question != null) + { + $from = Team::fromId($question->getFrom()); + $to = Team::fromId($question->getTo()); + $mime_type = finfo_file(finfo_open(FILEINFO_MIME_TYPE), "$LOCAL_PATH/files/$id"); + $name = "Pièce jointe de l'équipe " . $from->getTrigram() . " pour l'équipe " . $from->getTrigram(); + switch ($mime_type) { + case "application/pdf": + $name .= "pdf"; + break; + case "image/png": + $name .= ".png"; + break; + case "image/jpg": + case "image/jpeg": + $name .= ".jpg"; + break; + case "application/zip": + $name .= ".zip"; + break; + } + header("Content-Type: " . $mime_type); + } + else + require_once "server_files/404.php"; +} + header("Content-Disposition: inline; filename=\"$name\""); readfile("$LOCAL_PATH/files/$id"); diff --git a/server_files/views/poser_questions.php b/server_files/views/poser_questions.php index 309bc3c..c7d1e7a 100644 --- a/server_files/views/poser_questions.php +++ b/server_files/views/poser_questions.php @@ -8,21 +8,46 @@ for ($i = 0; $i < 2; ++$i) { Lien de la vidéo : getLink() ?>
getLink()) ?>
-
+ + for ($j = 0; $j < 6; ++$j) { + $question = $questions[$i][$j]; ?> + getAttachedFile() != null) { ?> + + + + + + + + + + + +
- + - +
+ Pièce jointe : + + getAttachedFile() ?>">Télécharger +
+ + + +
+ +