load-draft.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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\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. $app = Bootstrap::config('app');
  37. $limiter = new RateLimiter();
  38. $ip = $_SERVER['REMOTE_ADDR'] ?? 'unknown';
  39. $rateKey = sprintf('load:%s:%s', $ip, $email);
  40. if (!$limiter->allow($rateKey, (int) $app['rate_limit']['requests'], (int) $app['rate_limit']['window_seconds'])) {
  41. Bootstrap::jsonResponse([
  42. 'ok' => false,
  43. 'message' => Bootstrap::appMessage('load_draft.rate_limited'),
  44. ], 429);
  45. }
  46. $store = new JsonStore();
  47. $submission = $store->getSubmissionByEmail($email);
  48. if ($submission !== null) {
  49. Bootstrap::jsonResponse([
  50. 'ok' => true,
  51. 'already_submitted' => true,
  52. 'message' => Bootstrap::appMessage('load_draft.already_submitted'),
  53. ]);
  54. }
  55. $draft = $store->getDraft($email);
  56. Bootstrap::jsonResponse([
  57. 'ok' => true,
  58. 'already_submitted' => false,
  59. 'data' => $draft['form_data'] ?? [],
  60. 'uploads' => $draft['uploads'] ?? [],
  61. 'step' => $draft['step'] ?? 1,
  62. 'updated_at' => $draft['updated_at'] ?? null,
  63. ]);