upload-preview.php 4.2 KB

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