load-draft.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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\Storage\JsonStore;
  7. require dirname(__DIR__) . '/src/autoload.php';
  8. Bootstrap::init();
  9. if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
  10. Bootstrap::jsonResponse([
  11. 'ok' => false,
  12. 'message' => Bootstrap::appMessage('common.method_not_allowed'),
  13. ], 405);
  14. }
  15. $csrf = $_POST['csrf'] ?? '';
  16. if (!Csrf::validate(is_string($csrf) ? $csrf : null)) {
  17. Bootstrap::jsonResponse([
  18. 'ok' => false,
  19. 'message' => Bootstrap::appMessage('common.invalid_csrf'),
  20. ], 419);
  21. }
  22. $honeypot = trim((string) ($_POST['website'] ?? ''));
  23. if ($honeypot !== '') {
  24. Bootstrap::jsonResponse([
  25. 'ok' => false,
  26. 'message' => Bootstrap::appMessage('common.request_blocked'),
  27. ], 400);
  28. }
  29. $email = strtolower(trim((string) ($_POST['email'] ?? '')));
  30. if (filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
  31. Bootstrap::jsonResponse([
  32. 'ok' => false,
  33. 'message' => Bootstrap::appMessage('common.invalid_email'),
  34. ], 422);
  35. }
  36. $activityRaw = $_POST['last_user_activity_at'] ?? null;
  37. $lastUserActivityAt = is_scalar($activityRaw) ? (int) $activityRaw : null;
  38. $formAccess = new FormAccess();
  39. $auth = $formAccess->assertVerifiedForEmail($email, $lastUserActivityAt);
  40. if (($auth['ok'] ?? false) !== true) {
  41. $reason = (string) ($auth['reason'] ?? '');
  42. Bootstrap::jsonResponse([
  43. 'ok' => false,
  44. 'message' => (string) ($auth['message'] ?? 'Bitte E-Mail erneut verifizieren.'),
  45. 'auth_required' => $reason === 'auth_required',
  46. 'auth_expired' => $reason === 'auth_expired',
  47. ], (int) ($auth['status_code'] ?? 401));
  48. }
  49. $store = new JsonStore();
  50. $submission = $store->getSubmissionByEmail($email);
  51. if ($submission !== null) {
  52. Bootstrap::jsonResponse([
  53. 'ok' => true,
  54. 'already_submitted' => true,
  55. 'message' => Bootstrap::appMessage('load_draft.already_submitted'),
  56. ]);
  57. }
  58. $draft = $store->getDraft($email);
  59. Bootstrap::jsonResponse([
  60. 'ok' => true,
  61. 'already_submitted' => false,
  62. 'data' => $draft['form_data'] ?? [],
  63. 'uploads' => $draft['uploads'] ?? [],
  64. 'step' => $draft['step'] ?? 1,
  65. 'updated_at' => $draft['updated_at'] ?? null,
  66. ]);