load-draft.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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\FormAccess;
  7. use App\Security\RateLimiter;
  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. $honeypot = trim((string) ($_POST['website'] ?? ''));
  18. if ($honeypot !== '') {
  19. Bootstrap::jsonResponse(['ok' => false, 'message' => 'Anfrage blockiert.'], 400);
  20. }
  21. $email = strtolower(trim((string) ($_POST['email'] ?? '')));
  22. if (filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
  23. Bootstrap::jsonResponse(['ok' => false, 'message' => 'Bitte gültige E-Mail eingeben.'], 422);
  24. }
  25. $activityRaw = $_POST['last_user_activity_at'] ?? null;
  26. $lastUserActivityAt = is_scalar($activityRaw) ? (int) $activityRaw : null;
  27. $formAccess = new FormAccess();
  28. $auth = $formAccess->assertVerifiedForEmail($email, $lastUserActivityAt);
  29. if (($auth['ok'] ?? false) !== true) {
  30. $reason = (string) ($auth['reason'] ?? '');
  31. Bootstrap::jsonResponse([
  32. 'ok' => false,
  33. 'message' => (string) ($auth['message'] ?? 'Bitte E-Mail erneut verifizieren.'),
  34. 'auth_required' => $reason === 'auth_required',
  35. 'auth_expired' => $reason === 'auth_expired',
  36. ], (int) ($auth['status_code'] ?? 401));
  37. }
  38. $app = Bootstrap::config('app');
  39. $limiter = new RateLimiter();
  40. $ip = $_SERVER['REMOTE_ADDR'] ?? 'unknown';
  41. $rateKey = sprintf('load:%s:%s', $ip, $email);
  42. if (!$limiter->allow($rateKey, (int) $app['rate_limit']['requests'], (int) $app['rate_limit']['window_seconds'])) {
  43. Bootstrap::jsonResponse(['ok' => false, 'message' => 'Zu viele Anfragen. Bitte später erneut versuchen.'], 429);
  44. }
  45. $store = new JsonStore();
  46. $submission = $store->getSubmissionByEmail($email);
  47. if ($submission !== null) {
  48. Bootstrap::jsonResponse([
  49. 'ok' => true,
  50. 'already_submitted' => true,
  51. 'message' => 'Für diese E-Mail liegt bereits ein abgeschlossener Antrag vor.',
  52. ]);
  53. }
  54. $draft = $store->getDraft($email);
  55. Bootstrap::jsonResponse([
  56. 'ok' => true,
  57. 'already_submitted' => false,
  58. 'data' => $draft['form_data'] ?? [],
  59. 'uploads' => $draft['uploads'] ?? [],
  60. 'step' => $draft['step'] ?? 1,
  61. 'updated_at' => $draft['updated_at'] ?? null,
  62. ]);