Jetzt über pdf.js
This commit is contained in:
BIN
com_eis/site/.DS_Store
vendored
BIN
com_eis/site/.DS_Store
vendored
Binary file not shown.
BIN
com_eis/site/src/.DS_Store
vendored
BIN
com_eis/site/src/.DS_Store
vendored
Binary file not shown.
@@ -1,4 +1,5 @@
|
||||
<?php
|
||||
|
||||
namespace EIS\Component\EIS\Site\Controller;
|
||||
|
||||
\defined('_JEXEC') or die;
|
||||
@@ -12,51 +13,65 @@ class DownloadController extends BaseController
|
||||
{
|
||||
public function download()
|
||||
{
|
||||
$app = Factory::getApplication();
|
||||
$user = Factory::getUser();
|
||||
$app = \Joomla\CMS\Factory::getApplication();
|
||||
$input = $app->input;
|
||||
|
||||
// Nur für eingeloggte Benutzer
|
||||
if ($user->guest) {
|
||||
$app->enqueueMessage('Bitte zuerst einloggen.', 'warning');
|
||||
$app->redirect(Route::_('index.php?option=com_users&view=login', false));
|
||||
return;
|
||||
$id = (int) $input->get('id');
|
||||
if ($id <= 0) {
|
||||
throw new \RuntimeException('Ungültige ID.');
|
||||
}
|
||||
|
||||
// ID aus URL lesen
|
||||
$id = $input->getInt('id');
|
||||
if (!$id) {
|
||||
throw new \RuntimeException("Keine Dokument-ID übergeben.");
|
||||
}
|
||||
|
||||
// Datenbankabfrage
|
||||
/** @var DatabaseDriver $db */
|
||||
$db = Factory::getDbo();
|
||||
|
||||
$query = $db->getQuery(true)
|
||||
->select($db->quoteName(['path', 'name']))
|
||||
->from($db->quoteName('#__eis_documents'))
|
||||
->where($db->quoteName('id') . ' = ' . (int) $id)
|
||||
->where($db->quoteName('is_folder') . ' = 0');
|
||||
|
||||
$row = $db->setQuery($query)->loadAssoc();
|
||||
/** @var \Joomla\Database\DatabaseDriver $db */
|
||||
$db = \Joomla\CMS\Factory::getDbo();
|
||||
$row = $db->setQuery(
|
||||
$db->getQuery(true)
|
||||
->select($db->quoteName(['path', 'name']))
|
||||
->from($db->quoteName('#__eis_documents'))
|
||||
->where($db->quoteName('id') . ' = ' . (int) $id)
|
||||
->where($db->quoteName('is_folder') . ' = 0')
|
||||
)->loadAssoc();
|
||||
|
||||
if (!$row) {
|
||||
throw new \RuntimeException("PDF nicht gefunden oder kein gültiges Dokument.");
|
||||
throw new \RuntimeException('Datei nicht gefunden.');
|
||||
}
|
||||
|
||||
$filePath = $row['path'];
|
||||
$fileName = $row['name'];
|
||||
$filePath = (string) $row['path'];
|
||||
$fileName = (string) $row['name'];
|
||||
|
||||
if (!file_exists($filePath)) {
|
||||
throw new \RuntimeException("Datei existiert nicht auf dem Server.");
|
||||
// ------------- Sicherheit: Pfad muss unterhalb von /var/www/pdf liegen
|
||||
$baseDir = '/var/www/pdf'; // <-- DEIN Basisordner (ohne trailing slash!)
|
||||
$realFile = realpath($filePath);
|
||||
$realBase = realpath($baseDir);
|
||||
|
||||
if (!$realFile || !$realBase || strpos($realFile, $realBase) !== 0 || !is_file($realFile)) {
|
||||
throw new \RuntimeException('Ungültiger Pfad.');
|
||||
}
|
||||
|
||||
// PDF-Datei ausgeben
|
||||
// ------------- Relativpfad (Dateisystem) -> interner Nginx-URI
|
||||
// Beispiel: /var/www/pdf/foo/bar.pdf -> /protected-eis/foo/bar.pdf
|
||||
$relFs = ltrim(substr($realFile, strlen($realBase)), DIRECTORY_SEPARATOR);
|
||||
// URI-sicher kodieren (jedes Segment rawurlencode, Slashes erhalten)
|
||||
$segments = $relFs === '' ? [] : explode('/', str_replace('\\', '/', $relFs));
|
||||
$encodedRel = implode('/', array_map('rawurlencode', $segments));
|
||||
$internalPrefix = '/protected-eis/'; // <-- muss zu Nginx 'location' passen
|
||||
$internalUri = $internalPrefix . $encodedRel;
|
||||
|
||||
// ------------- Saubere Header, keine PHP-Ausgabe
|
||||
while (ob_get_level()) {
|
||||
@ob_end_clean();
|
||||
}
|
||||
@ini_set('zlib.output_compression', 'Off');
|
||||
|
||||
// Content-Disposition inkl. UTF-8 kompatibel (RFC 5987)
|
||||
$dispFilename = basename($fileName);
|
||||
$cd = 'inline; filename="' . \Joomla\CMS\Filter\OutputFilter::stringURLSafe($dispFilename) . '"';
|
||||
$cd .= "; filename*=UTF-8''" . rawurlencode($dispFilename);
|
||||
|
||||
header('Content-Type: application/pdf');
|
||||
header('Content-Disposition: inline; filename="' . basename($fileName) . '"');
|
||||
header('Content-Length: ' . filesize($filePath));
|
||||
readfile($filePath);
|
||||
header('Content-Disposition: ' . $cd);
|
||||
header('X-Accel-Redirect: ' . $internalUri);
|
||||
header('X-Content-Type-Options: nosniff');
|
||||
// Range/Content-Length/Caching erledigt Nginx automatisch perfekt
|
||||
exit;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user