Files
YMS/webgui/dl.php
T
2024-04-19 13:25:56 +02:00

121 lines
4.3 KiB
PHP

<?php
/******************************************************************************
* YMS - Yacht Management Software
* Copyright (C) 2024 Thomas Hooge
*
* SPDX-License-Identifier: WTFPL
******************************************************************************/
/* Document Downloader
Doucuments are not directly accessible via filesystem
dl.php must be used with "docid" as parameter: e.g, dl.php?id=23
In additional parameter "t" (thumb) is set a thumbnail will be returned
Possible values are: s (small), m (medium), l (large)
Thumbnails are generated for pictures (png, jpg) and documents (pdf)
TODO thumbs for svg? needed? perhaps small png thumbs?
*/
require 'globals.inc';
$docid = gpc_get_int($_GET, 'id');
if (! isset($docid)) {
exit(1);
}
$thumb = gpc_get_string($_GET, 't', 1);
$size = array ('s' => 120, 'm' => 240, 'l' => '320');
if (!array_key_exists($thumb, $size)) {
unset($thumb);
}
$sql = "SELECT doctype, docsize, mimetype, hash, extension, filename FROM document WHERE docid=?";
$sth = $pdo->prepare($sql);
$sth->execute([$docid]);
$row = $sth->fetch();
$dtmap = array(
'generic' => 'doc',
'manual' => 'man',
'invoice' => 'inv',
'picture' => 'pic',
'drawing' => 'drw');
// alter Dateiname
// $ofn = sprintf("%s-%s.%s", $dtmap[$row['doctype']], $row['hash'], $row['extension'])
$file = sprintf("%s/%s-%s.%s", $g_doc_basepath, $dtmap[$row['doctype']], $row['hash'], $row['extension']);
if (file_exists($file)) {
// Hash-Dateinamen: Umbenennen in neues Format
// document-format: ttt-nnnnnn.eee
// thumbnail-format: ttt-nnnnnn-ppp-s.eee
$fnew = sprintf('%s/%s-%06d.%s', $g_doc_basepath, $dtmap[$row['doctype']], $docid, $row['extension']);
rename($file, $fnew);
$file = $fnew;
} else {
$file = sprintf('%s/%s-%06d.%s', $g_doc_basepath, $dtmap[$row['doctype']], $docid, $row['extension']);
}
if (isset($thumb)) {
$page = 1; // could be variable in future
if ($row['mimetype'] == 'application/pdf') {
// PDF creates PNG-thumbnails
$thumbname = sprintf('%s-%06d-%03d-%s.png', $dtmap[$row['doctype']], $docid, $page, $thumb);
} else {
$thumbname = sprintf('%s-%06d-%03d-%s.%s', $dtmap[$row['doctype']], $docid, $page, $thumb, $row['extension']);
}
$thumbpath = $g_doc_basepath . '/thumb/' . $thumbname;
if (!file_exists($thumbpath)) {
list($width, $height) = getimagesize($file);
$factor = $size[$thumb] / max($width, $height);
// scale down
if ($row['mimetype'] == 'image/jpeg') {
$orig = imagecreatefromjpeg($file);
$scaled = imagescale($orig, $width * $factor, $height * $factor, IMG_BICUBIC);
imagejpeg($scaled, $thumbpath, 85);
$file = $thumbpath;
$filename = $thumbname;
$fsize = 0; // let's calc later
} elseif ($row['mimetype'] == 'image/png') {
$orig = imagecreatefrompng($file);
} elseif ($row['mimetype'] == 'application/pdf') {
// create preview image from first page of png
// mutool draw -o <thumb>.png -w <width> <filename>.pdf <page>
exec("/usr/bin/mutool draw -o {$thumbpath} -w {$size[$thumb]} $file $page", $output, $retval);
}
}
$file = $thumbpath;
$filename = $thumbname;
$fsize = 0; // let's calc later
} else {
// normal
$filename = $row['filename'];
$fsize = $row['docsize'];
}
if (file_exists($file)) {
// send file to caller
header('Content-Description: File Transfer');
header('Content-Type: ' . $row['mimetype']);
header('Content-Disposition: attachment; filename="' . $filename . '"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
if ($fsize == 0) {
header('Content-Length: ' . filesize($file));
} else {
header('Content-Length: ' . $fsize);
}
readfile($file);
exit;
} else {
// dynamically create error image
header("Content-Type: image/png");
$im = @imagecreate(110, 20) or die(_("Cannot initialize new GD image stream"));
$bgcolor = imagecolorallocate($im, 0, 0, 0);
$txcolor = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5, "Resource not found!", $txcolor);
imagepng($im);
imagedestroy($im);
}