plateforme-tfjm2/server_files/solutions.php

125 lines
3.8 KiB
PHP
Raw Normal View History

2019-08-21 20:56:46 +00:00
<?php
include 'config.php';
if (!isset($_SESSION["team_id"]))
include "403.php";
2019-08-21 20:56:46 +00:00
if (isset($_POST["send_solution"])) {
$error_message = saveSolution();
}
$solutions_req = $DB->prepare("SELECT `file_id`, `problem`, COUNT(`problem`) AS `version` FROM `solutions` WHERE `team` = ? GROUP BY `problem`, `uploaded_at` ORDER BY `problem`, `uploaded_at` DESC;");
2019-08-21 20:56:46 +00:00
$solutions_req->execute([$_SESSION["team_id"]]);
$tournament_req = $DB->prepare("SELECT `date_solutions` FROM `tournaments` WHERE `id` = ?;");
$tournament_req->execute([$_SESSION["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.";
$req = $DB->prepare("INSERT INTO `solutions`(`file_id`, `team`, `tournament`, `problem`)
VALUES (?, ?, ?, ?);");
$req->execute([$id, $_SESSION["team_id"], $_SESSION["tournament_id"], $problem]);
return false;
}
?>
<?php include 'header.php' ?>
<?php if (isset($error_message)) {
if ($error_message !== false) {
echo "<h2>Erreur : " . $error_message . "</h2>";
} else {
echo "<h2>Le fichier a été correctement envoyé !</h2>";
}
}?>
<?php if (date("yyyy-mm-dd") < $tournament_data["date_solutions"]) { ?>
<form method="post" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="5000000" />
<table>
<tbody>
<tr>
<td>
<label for="problem">Problème :</label>
</td>
<td>
<select id="problem" name="problem">
<?php
for ($i = 1; $i <= 9; ++$i) {
echo "<option value=\"$i\">$i</option>\n";
}
?>
</select>
</td>
</tr>
<tr>
<td>
<label for="file">Fichier :</label>
</td>
<td>
<input type="file" id="file" name="solution" />
</td>
</tr>
<tr>
<td colspan="2">
<input style="width: 100%;" type="submit" name="send_solution" value="Envoyer" />
</td>
</tr>
</tbody>
</table>
</form>
<?php } ?>
<hr />
2019-08-21 20:56:46 +00:00
<h2>Solutions soumises :</h2>
<?php
while (($data = $solutions_req->fetch()) !== false) {
$file_id = $data["file_id"];
$problem = $data["problem"];
$version = $data["version"];
echo "Problème $problem (Version $version) : <a href=\"$URL_BASE/file/$file_id\">Télécharger</a><br />";
}
?>
<?php include 'footer.php' ?>