upload-preview.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. declare(strict_types=1);
  3. use App\App\Bootstrap;
  4. use App\Security\Csrf;
  5. use App\Security\RateLimiter;
  6. use App\Storage\JsonStore;
  7. require dirname(__DIR__) . '/src/autoload.php';
  8. Bootstrap::init();
  9. if ($_SERVER['REQUEST_METHOD'] !== 'GET') {
  10. http_response_code(405);
  11. header('Content-Type: text/plain; charset=utf-8');
  12. echo 'Method not allowed';
  13. exit;
  14. }
  15. $csrf = $_GET['csrf'] ?? '';
  16. if (!Csrf::validate(is_string($csrf) ? $csrf : null)) {
  17. http_response_code(419);
  18. header('Content-Type: text/plain; charset=utf-8');
  19. echo 'Ungültiges CSRF-Token.';
  20. exit;
  21. }
  22. $email = strtolower(trim((string) ($_GET['email'] ?? '')));
  23. if (filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
  24. http_response_code(422);
  25. header('Content-Type: text/plain; charset=utf-8');
  26. echo 'Bitte gültige E-Mail eingeben.';
  27. exit;
  28. }
  29. $field = trim((string) ($_GET['field'] ?? ''));
  30. $index = (int) ($_GET['index'] ?? -1);
  31. if ($field === '' || $index < 0) {
  32. http_response_code(422);
  33. header('Content-Type: text/plain; charset=utf-8');
  34. echo 'Ungültiger Upload-Eintrag.';
  35. exit;
  36. }
  37. /** @return string|null */
  38. function resolveStoredPreviewPath(array $entry, array $app): ?string
  39. {
  40. $baseDir = rtrim((string) ($app['storage']['uploads'] ?? ''), '/');
  41. if ($baseDir === '') {
  42. return null;
  43. }
  44. $storedDir = trim((string) ($entry['stored_dir'] ?? ''), '/');
  45. $storedFilename = trim((string) ($entry['stored_filename'] ?? ''));
  46. if ($storedDir === '' || $storedFilename === '') {
  47. return null;
  48. }
  49. if (str_contains($storedDir, '..') || str_contains($storedFilename, '..')) {
  50. return null;
  51. }
  52. if (!preg_match('/^[A-Za-z0-9._\/-]+$/', $storedDir)) {
  53. return null;
  54. }
  55. if (!preg_match('/^[A-Za-z0-9._ -]+$/', $storedFilename)) {
  56. return null;
  57. }
  58. $path = $baseDir . '/' . $storedDir . '/' . $storedFilename;
  59. $realBase = realpath($baseDir);
  60. $realPath = realpath($path);
  61. if ($realBase !== false && $realPath !== false && !str_starts_with($realPath, $realBase)) {
  62. return null;
  63. }
  64. return $path;
  65. }
  66. $app = Bootstrap::config('app');
  67. $limiter = new RateLimiter();
  68. $ip = $_SERVER['REMOTE_ADDR'] ?? 'unknown';
  69. $rateKey = sprintf('preview-upload:%s:%s', $ip, $email);
  70. if (!$limiter->allow($rateKey, (int) $app['rate_limit']['requests'], (int) $app['rate_limit']['window_seconds'])) {
  71. http_response_code(429);
  72. header('Content-Type: text/plain; charset=utf-8');
  73. echo 'Zu viele Anfragen. Bitte später erneut versuchen.';
  74. exit;
  75. }
  76. $store = new JsonStore();
  77. $draft = $store->getDraft($email);
  78. if (!is_array($draft)) {
  79. http_response_code(404);
  80. header('Content-Type: text/plain; charset=utf-8');
  81. echo 'Entwurf nicht gefunden.';
  82. exit;
  83. }
  84. $uploads = (array) ($draft['uploads'] ?? []);
  85. $files = $uploads[$field] ?? null;
  86. $entry = (is_array($files) && isset($files[$index]) && is_array($files[$index])) ? $files[$index] : null;
  87. if (!is_array($entry)) {
  88. http_response_code(404);
  89. header('Content-Type: text/plain; charset=utf-8');
  90. echo 'Upload nicht gefunden.';
  91. exit;
  92. }
  93. $path = resolveStoredPreviewPath($entry, $app);
  94. if ($path === null || !is_file($path)) {
  95. http_response_code(404);
  96. header('Content-Type: text/plain; charset=utf-8');
  97. echo 'Datei nicht gefunden.';
  98. exit;
  99. }
  100. $mime = (string) ($entry['mime'] ?? '');
  101. if ($mime === '') {
  102. $detected = @mime_content_type($path);
  103. $mime = is_string($detected) ? $detected : 'application/octet-stream';
  104. }
  105. $downloadName = (string) ($entry['original_filename'] ?? basename($path));
  106. $fallbackName = preg_replace('/[^A-Za-z0-9._-]/', '_', $downloadName) ?: 'upload.bin';
  107. $encodedName = rawurlencode($downloadName);
  108. header('Content-Type: ' . $mime);
  109. header('X-Content-Type-Options: nosniff');
  110. header('Content-Length: ' . (string) filesize($path));
  111. header('Content-Disposition: inline; filename="' . $fallbackName . '"; filename*=UTF-8\'\'' . $encodedName);
  112. readfile($path);
  113. exit;