<?php
// pdf.php — Apache-only Gatekeeper: liefert PDFs aus /srv/module/Psychotherapie
// ACHTUNG: Das ist Zugriffskontrolle nur via Webpfad. Optional: BasicAuth zusätzlich (siehe unten).

$BASE = "/srv/module/Psychotherapie/";

// Optional: harte Whitelist (empfohlen). Leer lassen, wenn du alle PDFs erlaubst.
$ALLOW = [
  // "doc1.pdf",
  // "doc2.pdf",
];

$f = $_GET["f"] ?? "";
$f = basename($f); // verhindert ../ traversal

// Nur simple Dateinamen erlauben (keine Pfade)
if (!preg_match('/^[A-Za-z0-9._-]+\.pdf$/', $f)) {
  http_response_code(400);
  exit("Bad Request");
}

if (!empty($ALLOW) && !in_array($f, $ALLOW, true)) {
  http_response_code(404);
  exit("Not Found");
}

$path = $BASE . $f;

if (!is_file($path)) {
  http_response_code(404);
  exit("Not Found");
}

header("Content-Type: application/pdf");
header('Content-Disposition: inline; filename="' . $f . '"');
header("X-Content-Type-Options: nosniff");
header("Cache-Control: no-store");

// Ohne Range-Support ist PDF.js okay, wenn du im JS disableRange/disableStream setzt.
readfile($path);