load-draft.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. declare(strict_types=1);
  3. use App\App\Bootstrap;
  4. use App\Storage\JsonStore;
  5. use App\Security\Csrf;
  6. use App\Security\RateLimiter;
  7. require dirname(__DIR__) . '/src/autoload.php';
  8. Bootstrap::init();
  9. if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
  10. Bootstrap::jsonResponse(['ok' => false, 'message' => 'Method not allowed'], 405);
  11. }
  12. $csrf = $_POST['csrf'] ?? '';
  13. if (!Csrf::validate(is_string($csrf) ? $csrf : null)) {
  14. Bootstrap::jsonResponse(['ok' => false, 'message' => 'Ungültiges CSRF-Token.'], 419);
  15. }
  16. $honeypot = trim((string) ($_POST['website'] ?? ''));
  17. if ($honeypot !== '') {
  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. $app = Bootstrap::config('app');
  25. $limiter = new RateLimiter();
  26. $ip = $_SERVER['REMOTE_ADDR'] ?? 'unknown';
  27. $rateKey = sprintf('load:%s:%s', $ip, $email);
  28. if (!$limiter->allow($rateKey, (int) $app['rate_limit']['requests'], (int) $app['rate_limit']['window_seconds'])) {
  29. Bootstrap::jsonResponse(['ok' => false, 'message' => 'Zu viele Anfragen. Bitte später erneut versuchen.'], 429);
  30. }
  31. $store = new JsonStore();
  32. $submission = $store->getSubmissionByEmail($email);
  33. if ($submission !== null) {
  34. Bootstrap::jsonResponse([
  35. 'ok' => true,
  36. 'already_submitted' => true,
  37. 'message' => 'Für diese E-Mail liegt bereits ein abgeschlossener Antrag vor.',
  38. ]);
  39. }
  40. $draft = $store->getDraft($email);
  41. Bootstrap::jsonResponse([
  42. 'ok' => true,
  43. 'already_submitted' => false,
  44. 'data' => $draft['form_data'] ?? [],
  45. 'uploads' => $draft['uploads'] ?? [],
  46. 'step' => $draft['step'] ?? 1,
  47. 'updated_at' => $draft['updated_at'] ?? null,
  48. ]);