upload-preview.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. declare(strict_types=1);
  3. use App\App\Bootstrap;
  4. use App\Security\Csrf;
  5. use App\Security\FormAccess;
  6. use App\Storage\JsonStore;
  7. require dirname(__DIR__) . '/src/autoload.php';
  8. Bootstrap::init();
  9. if ($_SERVER['REQUEST_METHOD'] !== 'GET') {
  10. Bootstrap::textResponse(Bootstrap::appMessage('common.method_not_allowed'), 405);
  11. }
  12. $csrf = $_GET['csrf'] ?? '';
  13. if (!Csrf::validate(is_string($csrf) ? $csrf : null)) {
  14. Bootstrap::textResponse(Bootstrap::appMessage('common.invalid_csrf'), 419);
  15. }
  16. $email = strtolower(trim((string) ($_GET['email'] ?? '')));
  17. if (filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
  18. Bootstrap::textResponse(Bootstrap::appMessage('common.invalid_email'), 422);
  19. }
  20. $activityRaw = $_GET['last_user_activity_at'] ?? null;
  21. $lastUserActivityAt = is_scalar($activityRaw) ? (int) $activityRaw : null;
  22. $formAccess = new FormAccess();
  23. $auth = $formAccess->assertVerifiedForEmail($email, $lastUserActivityAt);
  24. if (($auth['ok'] ?? false) !== true) {
  25. $reason = (string) ($auth['reason'] ?? '');
  26. Bootstrap::jsonResponse([
  27. 'ok' => false,
  28. 'message' => (string) ($auth['message'] ?? 'Bitte E-Mail erneut verifizieren.'),
  29. 'auth_required' => $reason === 'auth_required',
  30. 'auth_expired' => $reason === 'auth_expired',
  31. ], (int) ($auth['status_code'] ?? 401));
  32. }
  33. $field = trim((string) ($_GET['field'] ?? ''));
  34. $index = (int) ($_GET['index'] ?? -1);
  35. if ($field === '' || $index < 0) {
  36. Bootstrap::textResponse(Bootstrap::appMessage('upload_preview.invalid_upload_entry'), 422);
  37. }
  38. /** @return string|null */
  39. function resolveStoredPreviewPath(array $entry, array $app): ?string
  40. {
  41. $baseDir = rtrim((string) ($app['storage']['uploads'] ?? ''), '/');
  42. if ($baseDir === '') {
  43. return null;
  44. }
  45. $storedDir = trim((string) ($entry['stored_dir'] ?? ''), '/');
  46. $storedFilename = trim((string) ($entry['stored_filename'] ?? ''));
  47. if ($storedDir === '' || $storedFilename === '') {
  48. return null;
  49. }
  50. if (str_contains($storedDir, '..') || str_contains($storedFilename, '..')) {
  51. return null;
  52. }
  53. if (!preg_match('/^[A-Za-z0-9._\/-]+$/', $storedDir)) {
  54. return null;
  55. }
  56. if (!preg_match('/^[A-Za-z0-9._ -]+$/', $storedFilename)) {
  57. return null;
  58. }
  59. $path = $baseDir . '/' . $storedDir . '/' . $storedFilename;
  60. $realBase = realpath($baseDir);
  61. $realPath = realpath($path);
  62. if ($realBase !== false && $realPath !== false && !str_starts_with($realPath, $realBase)) {
  63. return null;
  64. }
  65. return $path;
  66. }
  67. $app = Bootstrap::config('app');
  68. $store = new JsonStore();
  69. $draft = $store->getDraft($email);
  70. if (!is_array($draft)) {
  71. Bootstrap::textResponse(Bootstrap::appMessage('upload_preview.draft_not_found'), 404);
  72. }
  73. $uploads = (array) ($draft['uploads'] ?? []);
  74. $files = $uploads[$field] ?? null;
  75. $entry = (is_array($files) && isset($files[$index]) && is_array($files[$index])) ? $files[$index] : null;
  76. if (!is_array($entry)) {
  77. Bootstrap::textResponse(Bootstrap::appMessage('upload_preview.upload_not_found'), 404);
  78. }
  79. $path = resolveStoredPreviewPath($entry, $app);
  80. if ($path === null || !is_file($path)) {
  81. Bootstrap::textResponse(Bootstrap::appMessage('upload_preview.file_not_found'), 404);
  82. }
  83. $mime = (string) ($entry['mime'] ?? '');
  84. if ($mime === '') {
  85. $detected = @mime_content_type($path);
  86. $mime = is_string($detected) ? $detected : 'application/octet-stream';
  87. }
  88. $downloadName = (string) ($entry['original_filename'] ?? basename($path));
  89. $fallbackName = preg_replace('/[^A-Za-z0-9._-]/', '_', $downloadName) ?: 'upload.bin';
  90. $encodedName = rawurlencode($downloadName);
  91. header('Content-Type: ' . $mime);
  92. header('X-Content-Type-Options: nosniff');
  93. header('Content-Length: ' . (string) filesize($path));
  94. header('Content-Disposition: inline; filename="' . $fallbackName . '"; filename*=UTF-8\'\'' . $encodedName);
  95. readfile($path);
  96. exit;