124 lines
4.4 KiB
PHP
124 lines
4.4 KiB
PHP
<?php
|
|
/******************************************************************************
|
|
* YMS - Yacht Management Software
|
|
* Copyright (C) 2024 Thomas Hooge
|
|
*
|
|
* SPDX-License-Identifier: WTFPL
|
|
******************************************************************************/
|
|
|
|
/* Document Downloader
|
|
Documents 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';
|
|
require 'lib/fileutils.inc';
|
|
|
|
$docid = gpc_get_int($_GET, 'id');
|
|
if (! isset($docid)) {
|
|
exit(1);
|
|
}
|
|
|
|
$page = gpc_get_int($_REQUEST, 'p', 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, pages FROM document WHERE docid=?";
|
|
$sth = $pdo->prepare($sql);
|
|
$sth->execute([$docid]);
|
|
$doc = $sth->fetch(PDO::FETCH_OBJ);
|
|
|
|
// alter Dateiname
|
|
// $ofn = sprintf("%s-%s.%s", $dtmap[$row['doctype']], $row['hash'], $row['extension'])
|
|
// $file = sprintf("%s/%s-%s.%s", $g_doc_basepath, $g_doc_typemap[$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, $g_doc_typemap[$row['doctype']], $docid, $row['extension']);
|
|
rename($file, $fnew);
|
|
$file = $fnew;
|
|
|
|
} else { */
|
|
$file = sprintf('%s/%s-%06d.%s', $g_doc_basepath, $g_doc_typemap[$doc->doctype], $docid, $doc->extension);
|
|
// }
|
|
|
|
if (isset($thumb)) {
|
|
if ($page < 1) {
|
|
$page = 1;
|
|
} elseif ($page > $doc->pages) {
|
|
$page = $doc->pages;
|
|
}
|
|
if ($doc->mimetype == 'application/pdf') {
|
|
// PDF creates PNG-thumbnails
|
|
$thumbname = sprintf('%s-%06d-%03d-%s.png', $g_doc_typemap[$doc->doctype], $docid, $page, $thumb);
|
|
$thumbpath = $g_doc_basepath . '/thumb/' . $thumbname;
|
|
if (!file_exists($thumbpath)) {
|
|
exec("/usr/bin/mutool draw -o {$thumbpath} -w {$size[$thumb]} $file $page", $output, $retval);
|
|
}
|
|
} else {
|
|
$thumbname = sprintf('%s-%06d-%03d-%s.%s', $g_doc_typemap[$doc->doctype], $docid, $page, $thumb, $doc->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 ($doc->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 ($doc->mimetype == 'image/png') {
|
|
$orig = imagecreatefrompng($file);
|
|
} elseif ($doc_>mimetype == 'image/xml+svg') {
|
|
// TODO anything here?
|
|
}
|
|
}
|
|
}
|
|
$file = $thumbpath;
|
|
$filename = $thumbname;
|
|
$fsize = 0; // let's calc later
|
|
} else {
|
|
// normal
|
|
$filename = $doc->filename;
|
|
$fsize = $doc->docsize;
|
|
}
|
|
|
|
if (file_exists($file)) {
|
|
// send file to caller
|
|
header('Content-Description: File Transfer');
|
|
header('Content-Type: ' . $doc->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);
|
|
}
|