load-draft.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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'] !== 'POST') {
  11. Bootstrap::jsonResponse([
  12. 'ok' => false,
  13. 'message' => Bootstrap::appMessage('common.method_not_allowed'),
  14. ], 405);
  15. }
  16. $csrf = $_POST['csrf'] ?? '';
  17. if (!Csrf::validate(is_string($csrf) ? $csrf : null)) {
  18. Bootstrap::jsonResponse([
  19. 'ok' => false,
  20. 'message' => Bootstrap::appMessage('common.invalid_csrf'),
  21. ], 419);
  22. }
  23. $honeypot = trim((string) ($_POST['website'] ?? ''));
  24. if ($honeypot !== '') {
  25. Bootstrap::jsonResponse([
  26. 'ok' => false,
  27. 'message' => Bootstrap::appMessage('common.request_blocked'),
  28. ], 400);
  29. }
  30. $email = strtolower(trim((string) ($_POST['email'] ?? '')));
  31. if (filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
  32. Bootstrap::jsonResponse([
  33. 'ok' => false,
  34. 'message' => Bootstrap::appMessage('common.invalid_email'),
  35. ], 422);
  36. }
  37. $activityRaw = $_POST['last_user_activity_at'] ?? null;
  38. $lastUserActivityAt = is_scalar($activityRaw) ? (int) $activityRaw : null;
  39. $formAccess = new FormAccess();
  40. $auth = $formAccess->assertVerifiedForEmail($email, $lastUserActivityAt);
  41. if (($auth['ok'] ?? false) !== true) {
  42. $reason = (string) ($auth['reason'] ?? '');
  43. Bootstrap::jsonResponse([
  44. 'ok' => false,
  45. 'message' => (string) ($auth['message'] ?? 'Bitte E-Mail erneut verifizieren.'),
  46. 'auth_required' => $reason === 'auth_required',
  47. 'auth_expired' => $reason === 'auth_expired',
  48. ], (int) ($auth['status_code'] ?? 401));
  49. }
  50. $app = Bootstrap::config('app');
  51. $limiter = new RateLimiter();
  52. $ip = $_SERVER['REMOTE_ADDR'] ?? 'unknown';
  53. $rateKey = sprintf('load:%s:%s', $ip, $email);
  54. if (!$limiter->allow($rateKey, (int) $app['rate_limit']['requests'], (int) $app['rate_limit']['window_seconds'])) {
  55. Bootstrap::jsonResponse([
  56. 'ok' => false,
  57. 'message' => Bootstrap::appMessage('load_draft.rate_limited'),
  58. ], 429);
  59. }
  60. $store = new JsonStore();
  61. $submission = $store->getSubmissionByEmail($email);
  62. if ($submission !== null) {
  63. Bootstrap::jsonResponse([
  64. 'ok' => true,
  65. 'already_submitted' => true,
  66. 'message' => Bootstrap::appMessage('load_draft.already_submitted'),
  67. ]);
  68. }
  69. $draft = $store->getDraft($email);
  70. Bootstrap::jsonResponse([
  71. 'ok' => true,
  72. 'already_submitted' => false,
  73. 'data' => $draft['form_data'] ?? [],
  74. 'uploads' => $draft['uploads'] ?? [],
  75. 'step' => $draft['step'] ?? 1,
  76. 'updated_at' => $draft['updated_at'] ?? null,
  77. ]);