mirror of
https://gitlab.com/animath/si/plateforme-corres2math.git
synced 2025-07-04 02:12:08 +02:00
Copie du site du TFJM² à adapter aux Correspondances
This commit is contained in:
72
server_files/controllers/solutions.php
Normal file
72
server_files/controllers/solutions.php
Normal file
@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
if (!isset($_SESSION["team"]))
|
||||
require_once "server_files/403.php";
|
||||
|
||||
/**
|
||||
* @var Team $team
|
||||
* @var Tournament $tournament
|
||||
*/
|
||||
$team = $_SESSION["team"];
|
||||
$tournament = Tournament::fromId($team->getTournamentId());
|
||||
|
||||
$has_error = false;
|
||||
$error_message = null;
|
||||
|
||||
if (isset($_POST["send_solution"])) {
|
||||
$save_solution = new SaveSolution();
|
||||
try {
|
||||
$save_solution->makeVerifications();
|
||||
$save_solution->saveSolution();
|
||||
} catch (AssertionError $e) {
|
||||
$has_error = true;
|
||||
$error_message = $e->getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
$solutions = $tournament->getAllSolutions($team->getId());
|
||||
$solutions_final = null;
|
||||
if ($team->isSelectedForFinal())
|
||||
$solutions_final = $FINAL->getAllSolutions($team->getId());
|
||||
|
||||
class SaveSolution
|
||||
{
|
||||
private $problem;
|
||||
private $file;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->file = $_FILES["document"];
|
||||
$this->problem = htmlspecialchars($_POST["problem"]);
|
||||
}
|
||||
|
||||
public function makeVerifications()
|
||||
{
|
||||
global $LOCAL_PATH;
|
||||
|
||||
ensure(preg_match("#[1-9]#", $this->problem), "Le numéro du problème est invalide.");
|
||||
ensure($this->file["size"] <= 2e6, "Le fichier doit peser moins que 2 Mo.");
|
||||
ensure(!$this->file["error"], "Une erreur est survenue.");
|
||||
ensure(finfo_file(finfo_open(FILEINFO_MIME_TYPE), $this->file["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 saveSolution()
|
||||
{
|
||||
global $LOCAL_PATH, $DB, $team, $tournament, $FINAL;
|
||||
|
||||
do
|
||||
$id = genRandomPhrase(64);
|
||||
while (file_exists("$LOCAL_PATH/files/$id"));
|
||||
|
||||
if (!rename($this->file["tmp_name"], "$LOCAL_PATH/files/$id"))
|
||||
throw new AssertionError("Une erreur est survenue lors de l'envoi du fichier.");
|
||||
|
||||
$req = $DB->prepare("INSERT INTO `solutions`(`file_id`, `team`, `tournament`, `problem`) VALUES (?, ?, ?, ?);");
|
||||
$req->execute([$id, $team->getId(), $team->isSelectedForFinal() ? $FINAL->getId() : $tournament->getId(), $this->problem]);
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
require_once "server_files/views/solutions.php";
|
Reference in New Issue
Block a user