✨ Implement file download function
Signed-off-by: Luke Tainton <luke@tainton.uk>
This commit is contained in:
46
app/public/actions/download.php
Normal file
46
app/public/actions/download.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
$PAGE_NAME = "Download file";
|
||||
require_once __DIR__ . "/../../includes/prereqs.php";
|
||||
use Ramsey\Uuid\Uuid;
|
||||
|
||||
$file = get_single_file($db, $_GET['file']);
|
||||
$request = get_request($db, $file['ticket']);
|
||||
$authorised_users = get_subscribers($db, $request);
|
||||
$is_authorised = isAuthorised($_SESSION['uuid'], $authorised_users, $request);
|
||||
|
||||
$local_filename = $_ENV['ATTACHMENTS_PATH']."/".$file['id'];
|
||||
$remote_filename = $file['filename'];
|
||||
|
||||
if ($is_authorised == true) {
|
||||
if (file_exists($local_filename)) {
|
||||
//Get file type and set it as Content Type
|
||||
$finfo = finfo_open(FILEINFO_MIME_TYPE);
|
||||
header('Content-Type: ' . finfo_file($finfo, $local_filename));
|
||||
finfo_close($finfo);
|
||||
|
||||
//Use Content-Disposition: attachment to specify the filename
|
||||
header('Content-Disposition: attachment; filename='.$remote_filename);
|
||||
|
||||
//No cache
|
||||
header('Expires: 0');
|
||||
header('Cache-Control: must-revalidate');
|
||||
header('Pragma: public');
|
||||
|
||||
//Define file size
|
||||
header('Content-Length: ' . filesize($local_filename));
|
||||
|
||||
ob_clean();
|
||||
flush();
|
||||
readfile($local_filename);
|
||||
$alert = array("success", "File download started.");
|
||||
} else {
|
||||
$alert = array("danger", "The requested file does not exist.");
|
||||
}
|
||||
} else {
|
||||
$alert = array("danger", "You are not authorised to download that file.");
|
||||
}
|
||||
$newURL = "/view?rid=" . $request['uuid'];
|
||||
echo("<script>window.location = '$newURL'</script>");
|
||||
|
||||
?>
|
||||
@@ -17,7 +17,7 @@
|
||||
$file_size = $_FILES['file']['size'];
|
||||
$file_type = $_FILES['file']['type'];
|
||||
$file_tmp = $_FILES['file']['tmp_name'];
|
||||
move_uploaded_file($file_tmp,"/srv/attachments/".$file_name);
|
||||
move_uploaded_file($file_tmp,$_ENV['ATTACHMENTS_PATH']."/".$file_uuid);
|
||||
$stmt = "INSERT INTO ticket_uploads (id, ticket, user, filename) VALUES (:fileuuid, :ticket, :user, :name)";
|
||||
$sql = $db->prepare($stmt);
|
||||
$sql->bindParam(':fileuuid', $file_uuid);
|
||||
|
||||
@@ -58,17 +58,17 @@
|
||||
</div>
|
||||
</section>
|
||||
<section>
|
||||
<div class="container">
|
||||
<div class="container-fluid">
|
||||
<div class="row">
|
||||
<div class="col-4">
|
||||
<div class="card mx-auto" style="margin-bottom:50px;">
|
||||
<div class="col-3">
|
||||
<div class="card mx-auto">
|
||||
<div class="card-header"><span class="mdi mdi-information-outline"></span> Information</div>
|
||||
<ul class="list-group list-group-flush">
|
||||
<li class="list-group-item">
|
||||
<div class="container">
|
||||
<div class="row">
|
||||
<span style="display: inline;"><b>Status:</b></span>
|
||||
<span style="display: inline; margin-left: 1%;">New</span>
|
||||
<span style="display: inline; margin-left: 1%;"><?php echo($request['status']); ?></span>
|
||||
</div>
|
||||
</div>
|
||||
</li>
|
||||
@@ -76,7 +76,7 @@
|
||||
<div class="container">
|
||||
<div class="row">
|
||||
<span style="display: inline;"><b>Created by:</b></span>
|
||||
<span style="display: inline; margin-left: 1%;">Luke Tainton</span>
|
||||
<span style="display: inline; margin-left: 1%;"><?php echo(get_user_name($db, $request['created_by'])); ?></span>
|
||||
</div>
|
||||
</div>
|
||||
</li>
|
||||
@@ -84,14 +84,15 @@
|
||||
<div class="container">
|
||||
<div class="row">
|
||||
<span style="display: inline;"><b>Assigned to:</b></span>
|
||||
<span class="text-muted" style="display: inline; margin-left: 1%;">None</span> </div>
|
||||
<span class="text-muted" style="display: inline; margin-left: 1%;"><?php echo(get_user_name($db, $request['status']) || "None"); ?></span>
|
||||
</div>
|
||||
</div>
|
||||
</li>
|
||||
<li class="list-group-item">
|
||||
<div class="container">
|
||||
<div class="row">
|
||||
<span style="display: inline;"><b>Created:</b></span>
|
||||
<span style="display: inline; margin-left: 1%;">2020-08-04 13:26:22</span>
|
||||
<span style="display: inline; margin-left: 1%;"><?php echo($request['created_on']); ?></span>
|
||||
</div>
|
||||
</div>
|
||||
</li>
|
||||
@@ -99,36 +100,40 @@
|
||||
<div class="container">
|
||||
<div class="row">
|
||||
<span style="display: inline;"><b>Last updated:</b></span>
|
||||
<span style="display: inline; margin-left: 1%;">2020-08-04 13:26:22</span>
|
||||
<span style="display: inline; margin-left: 1%;"><?php echo($request['last_updated']); ?></span>
|
||||
</div>
|
||||
</div>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
<div class="card mx-auto" style="margin-bottom:25px;">
|
||||
<div class="card-header"><span class="mdi mdi-file-document-outline"></span> Uploaded files</div>
|
||||
<ul class="list-group list-group-flush">
|
||||
<?php
|
||||
if (count($files) == 0) {
|
||||
echo("<center><b>No files uploaded</b></center>");
|
||||
} else {
|
||||
foreach($files as $file) {
|
||||
?>
|
||||
<li class="list-group-item">
|
||||
<div class="container">
|
||||
<div class="row">
|
||||
<span style="display: inline;"><b><?php echo(get_user_name($db, $file['user'])); ?></b></span><span class="text-muted"><i> <?php echo(" " . $file['created']); ?></i></span>
|
||||
</div>
|
||||
<div class="row">
|
||||
<span><?php echo($file['path']); ?></span>
|
||||
</div>
|
||||
</div>
|
||||
</li>
|
||||
<?php } } ?>
|
||||
</ul>
|
||||
</div></div>
|
||||
</div>
|
||||
|
||||
<div class="col-8">
|
||||
<div class="col-3">
|
||||
<div class="card mx-auto">
|
||||
<div class="card-header"><span class="mdi mdi-file-document-outline"></span> Files</div>
|
||||
<ul class="list-group list-group-flush">
|
||||
<?php
|
||||
if (count($files) == 0) {
|
||||
echo("<center><b>No files uploaded</b></center>");
|
||||
} else {
|
||||
foreach($files as $file) {
|
||||
?>
|
||||
<li class="list-group-item">
|
||||
<div class="container">
|
||||
<div class="row">
|
||||
<span style="display: inline;"><b><?php echo(get_user_name($db, $file['user'])); ?></b></span><span class="text-muted"><i> <?php echo(" " . $file['created']); ?></i></span>
|
||||
</div>
|
||||
<div class="row">
|
||||
<a target="_blank" href="<?php echo('/actions/download?file=' . $file['id']); ?>"><span><?php echo($file['path']); ?></span></a>
|
||||
</div>
|
||||
</div>
|
||||
</li>
|
||||
<?php } } ?>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="col-6">
|
||||
<div class="card mx-auto">
|
||||
<div class="card-header"><span class="mdi mdi-update"></span> Updates</div>
|
||||
<ul class="list-group list-group-flush">
|
||||
|
||||
Reference in New Issue
Block a user