1
0
mirror of https://gitlab.com/animath/si/plateforme.git synced 2024-11-27 03:33:01 +00:00
plateforme-tfjm2/server_files/controllers/view_file.php
2020-05-05 01:06:57 +02:00

102 lines
3.2 KiB
PHP

<?php
if (!isset($_GET["file_id"])) {
header("Location: $URL_BASE");
exit();
}
if (!isset($_SESSION["user_id"]))
require_once "server_files/403.php";
$id = htmlspecialchars($_GET["file_id"]);
$type = DocumentType::SOLUTION;
$file = Solution::fromId($id);
if ($file === null) {
$type = DocumentType::SYNTHESIS;
$file = Synthesis::fromId($id);
if ($file === null) {
$file = Document::fromId($id);
$type = DocumentType::PARENTAL_CONSENT;
}
}
if ($file !== null) {
$team = Team::fromId($file->getTeamId());
$tournament = Tournament::fromId($file->getTournamentId());
$trigram = $team->getTrigram();
if ($_SESSION["role"] == Role::ORGANIZER && !$tournament->organize($_SESSION["user_id"]))
require_once "server_files/403.php";
if ($type == DocumentType::SOLUTION) {
$problem = $file->getProblem();
$name = "Problème $problem $trigram";
if (($_SESSION["role"] == Role::PARTICIPANT || $_SESSION["role"] == Role::ENCADRANT) && (!isset($_SESSION["team"]) || $_SESSION["team"]->getId() != $team->getId())) {
$req = $DB->prepare("SELECT opposed_problem, rapported_problem, opposed_problem_2, rapported_problem_2 FROM teams WHERE id = ?;");
$req->execute([$_SESSION["team"]->getId()]);
$data = $req->fetch();
if ($id != $data["opposed_problem"] && $id != $data["rapported_problem"]) {
if (date("Y-m-d H:i") < $tournament->getSolutionsDate2() || ($id != $data["opposed_problem_2"] && $id != $data["rapported_problem_2"]))
require_once "server_files/403.php";
}
}
}
else if ($type == DocumentType::SYNTHESIS) {
$dest = $file->getDest();
$name = "Note de synthèse $trigram " . ($dest == DestType::OPPOSANT ? "de l'opposant" : "du rapporteur");
if (($_SESSION["role"] == Role::PARTICIPANT || $_SESSION["role"] == Role::ENCADRANT) && (!isset($_SESSION["team"]) || $_SESSION["team"]->getId() != $team->getId()))
require_once "server_files/403.php";
}
else {
$user = User::fromId($file->getUserId());
$type = $file->getType();
if (($_SESSION["role"] == Role::PARTICIPANT || $_SESSION["role"] == Role::ENCADRANT)) {
if ($type != DocumentType::MOTIVATION_LETTER && $user->getId() != $_SESSION["user_id"] || $file->getTeamId() != $team->getId())
require_once "server_files/403.php";
}
switch ($type) {
case DocumentType::PARENTAL_CONSENT:
$name = "Autorisation parentale";
break;
case DocumentType::PHOTO_CONSENT:
$name = "Autorisation de droit à l'image";
break;
case DocumentType::SANITARY_PLUG:
$name = "Fiche sanitaire";
break;
case DocumentType::SCHOLARSHIP:
$name = "Notification de bourse";
break;
}
if ($type == DocumentType::MOTIVATION_LETTER)
$name = "Lettre de motivation de l'équipe $trigram";
else {
$surname = $user->getSurname();
$first_name = $user->getFirstName();
$name .= " de $first_name $surname";
}
}
}
else
require_once "server_files/404.php";
$mime = finfo_file(finfo_open(FILEINFO_MIME_TYPE), "$LOCAL_PATH/files/$id");
if ($mime == "application/pdf")
$name .= ".pdf";
elseif ($mime == "image/png")
$name .= ".png";
else
$name = ".jpg";
header("Content-Type: $mime");
header("Content-Disposition: inline; filename=\"$name\"");
readfile("$LOCAL_PATH/files/$id");
exit();