1
0
mirror of https://gitlab.com/animath/si/plateforme.git synced 2024-11-27 10:13:02 +00:00
plateforme-tfjm2/server_files/controllers/solutions.php

64 lines
2.3 KiB
PHP
Raw Normal View History

2019-08-21 20:56:46 +00:00
<?php
2019-09-06 11:48:50 +00:00
require_once "../config.php";
2019-08-21 20:56:46 +00:00
if (!isset($_SESSION["team_id"]))
2019-09-06 11:48:50 +00:00
require_once "../403.php";
2019-08-21 20:56:46 +00:00
if (isset($_POST["send_solution"])) {
$error_message = saveSolution();
}
2019-09-02 22:01:54 +00:00
$solutions_req = $DB->prepare("SELECT `file_id`, `problem`, COUNT(`problem`) AS `version` FROM `solutions` WHERE `team` = ? AND `tournament` = ? GROUP BY `problem`, `uploaded_at` ORDER BY `problem`, `uploaded_at` DESC;");
$solutions_req->execute([$_SESSION["team_id"], $_SESSION[isset($_SESSION["final_id"]) ? "final_id" : "tournament_id"]]);
2019-08-21 20:56:46 +00:00
$tournament_req = $DB->prepare("SELECT `date_solutions` FROM `tournaments` WHERE `id` = ?;");
2019-09-02 22:01:54 +00:00
$tournament_req->execute([$_SESSION[isset($_SESSION["final_id"]) ? "final_id" : "tournament_id"]]);
$tournament_data = $tournament_req->fetch();
2019-08-21 20:56:46 +00:00
function saveSolution() {
global $LOCAL_PATH, $DB;
try {
$problem = $_POST["problem"];
if ($problem < 1 || $problem > 9)
return "Le numéro de problème est invalide.";
}
catch (Throwable $t) {
return "Le numéro de problème n'est pas valide. Merci de ne pas créer vos propres requêtes.";
}
$file = $_FILES["solution"];
if ($file["size"] > 5000000 || $file["error"])
return "Une erreur est survenue. Merci de vérifier que le fichier pèse moins que 5 Mo.";
if (finfo_file(finfo_open(FILEINFO_MIME_TYPE), $file["tmp_name"]) != 'application/pdf')
return "Le fichier doit être au format PDF.";
if (!is_dir("$LOCAL_PATH/files") && !mkdir("$LOCAL_PATH/files"))
return "Les droits sont insuffisants. Veuillez contacter l'administrateur du serveur.";
$alphabet = "abcdefghijklmnopqrstuvwxyz0123456789";
do {
$id = "";
for ($i = 0; $i < 64; ++$i) {
$id .= $alphabet[rand(0, strlen($alphabet) - 1)];
}
}
while (file_exists("$LOCAL_PATH/files/$id"));
if (!rename($file["tmp_name"], "$LOCAL_PATH/files/$id"))
return "Une erreur est survenue lors de l'envoi du fichier.";
2019-09-02 22:01:54 +00:00
$req = $DB->prepare("INSERT INTO `solutions`(`file_id`, `team`, `tournament`, `problem`) VALUES (?, ?, ?, ?);");
2019-08-21 20:56:46 +00:00
$req->execute([$id, $_SESSION["team_id"], $_SESSION["tournament_id"], $problem]);
return false;
}
2019-09-06 11:48:50 +00:00
require_once "../views/header.php";
require_once "../views/solutions.php";
require_once "../views/footer.php";