delete-upload.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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\FileSystem;
  7. use App\Storage\JsonStore;
  8. require dirname(__DIR__) . '/src/autoload.php';
  9. Bootstrap::init();
  10. if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
  11. Bootstrap::jsonResponse(['ok' => false, 'message' => 'Method not allowed'], 405);
  12. }
  13. $csrf = $_POST['csrf'] ?? '';
  14. if (!Csrf::validate(is_string($csrf) ? $csrf : null)) {
  15. Bootstrap::jsonResponse(['ok' => false, 'message' => 'Ungültiges CSRF-Token.'], 419);
  16. }
  17. if (trim((string) ($_POST['website'] ?? '')) !== '') {
  18. Bootstrap::jsonResponse(['ok' => false, 'message' => 'Anfrage blockiert.'], 400);
  19. }
  20. $email = strtolower(trim((string) ($_POST['email'] ?? '')));
  21. if (filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
  22. Bootstrap::jsonResponse(['ok' => false, 'message' => 'Bitte gültige E-Mail eingeben.'], 422);
  23. }
  24. $field = trim((string) ($_POST['field'] ?? ''));
  25. $index = (int) ($_POST['index'] ?? -1);
  26. if ($field === '' || $index < 0) {
  27. Bootstrap::jsonResponse(['ok' => false, 'message' => 'Ungültiger Upload-Eintrag.'], 422);
  28. }
  29. /** @return array{path: string, dir: string}|null */
  30. function resolveStoredUploadPath(array $entry, array $app): ?array
  31. {
  32. $baseDir = rtrim((string) ($app['storage']['uploads'] ?? ''), '/');
  33. if ($baseDir === '') {
  34. return null;
  35. }
  36. $storedDir = trim((string) ($entry['stored_dir'] ?? ''), '/');
  37. $storedFilename = trim((string) ($entry['stored_filename'] ?? ''));
  38. if ($storedDir === '' || $storedFilename === '') {
  39. return null;
  40. }
  41. if (str_contains($storedDir, '..') || str_contains($storedFilename, '..')) {
  42. return null;
  43. }
  44. if (!preg_match('/^[A-Za-z0-9._\/-]+$/', $storedDir)) {
  45. return null;
  46. }
  47. if (!preg_match('/^[A-Za-z0-9._ -]+$/', $storedFilename)) {
  48. return null;
  49. }
  50. $path = $baseDir . '/' . $storedDir . '/' . $storedFilename;
  51. $dir = dirname($path);
  52. $realBase = realpath($baseDir);
  53. if ($realBase !== false) {
  54. $realDir = realpath($dir);
  55. $realPath = realpath($path);
  56. if ($realDir !== false && !str_starts_with($realDir, $realBase)) {
  57. return null;
  58. }
  59. if ($realPath !== false && !str_starts_with($realPath, $realBase)) {
  60. return null;
  61. }
  62. }
  63. return ['path' => $path, 'dir' => $dir];
  64. }
  65. $app = Bootstrap::config('app');
  66. $limiter = new RateLimiter();
  67. $ip = $_SERVER['REMOTE_ADDR'] ?? 'unknown';
  68. $rateKey = sprintf('delete-upload:%s:%s', $ip, $email);
  69. if (!$limiter->allow($rateKey, (int) $app['rate_limit']['requests'], (int) $app['rate_limit']['window_seconds'])) {
  70. Bootstrap::jsonResponse(['ok' => false, 'message' => 'Zu viele Löschanfragen. Bitte später erneut versuchen.'], 429);
  71. }
  72. $store = new JsonStore();
  73. try {
  74. $result = $store->withEmailLock($email, static function () use ($store, $app, $email, $field, $index): array {
  75. if ($store->hasSubmission($email)) {
  76. return [
  77. 'ok' => false,
  78. 'status' => 409,
  79. 'message' => 'Für diese E-Mail liegt bereits ein abgeschlossener Antrag vor.',
  80. ];
  81. }
  82. $draft = $store->getDraft($email);
  83. if (!is_array($draft)) {
  84. return [
  85. 'ok' => false,
  86. 'status' => 404,
  87. 'message' => 'Kein Entwurf gefunden.',
  88. ];
  89. }
  90. $uploads = (array) ($draft['uploads'] ?? []);
  91. $files = $uploads[$field] ?? null;
  92. if (!is_array($files) || !isset($files[$index]) || !is_array($files[$index])) {
  93. return [
  94. 'ok' => false,
  95. 'status' => 404,
  96. 'message' => 'Upload nicht gefunden.',
  97. ];
  98. }
  99. $entry = $files[$index];
  100. unset($files[$index]);
  101. $files = array_values($files);
  102. if ($files === []) {
  103. unset($uploads[$field]);
  104. } else {
  105. $uploads[$field] = $files;
  106. }
  107. $updatedDraft = $store->replaceDraft($email, [
  108. 'step' => $draft['step'] ?? 1,
  109. 'form_data' => (array) ($draft['form_data'] ?? []),
  110. 'uploads' => $uploads,
  111. ]);
  112. $resolved = resolveStoredUploadPath($entry, $app);
  113. if ($resolved !== null) {
  114. $fullPath = $resolved['path'];
  115. if (is_file($fullPath)) {
  116. @unlink($fullPath);
  117. }
  118. $entryDir = $resolved['dir'];
  119. if (is_dir($entryDir)) {
  120. $remaining = scandir($entryDir);
  121. if (is_array($remaining) && count($remaining) <= 2) {
  122. FileSystem::removeTree($entryDir);
  123. }
  124. }
  125. }
  126. return [
  127. 'ok' => true,
  128. 'status' => 200,
  129. 'uploads' => $updatedDraft['uploads'] ?? [],
  130. 'updated_at' => $updatedDraft['updated_at'] ?? null,
  131. ];
  132. });
  133. } catch (Throwable $e) {
  134. Bootstrap::log('app', 'delete-upload error: ' . $e->getMessage());
  135. Bootstrap::jsonResponse(['ok' => false, 'message' => 'Upload konnte nicht gelöscht werden.'], 500);
  136. }
  137. if (($result['ok'] ?? false) !== true) {
  138. Bootstrap::jsonResponse([
  139. 'ok' => false,
  140. 'message' => (string) ($result['message'] ?? 'Upload konnte nicht gelöscht werden.'),
  141. ], (int) ($result['status'] ?? 422));
  142. }
  143. Bootstrap::jsonResponse([
  144. 'ok' => true,
  145. 'message' => 'Upload gelöscht.',
  146. 'uploads' => $result['uploads'] ?? [],
  147. 'updated_at' => $result['updated_at'] ?? null,
  148. ]);