save-draft.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. declare(strict_types=1);
  3. use App\App\Bootstrap;
  4. use App\Form\FormSchema;
  5. use App\Security\Csrf;
  6. use App\Security\FormAccess;
  7. use App\Storage\FileUploadStore;
  8. use App\Storage\JsonStore;
  9. require dirname(__DIR__) . '/src/autoload.php';
  10. Bootstrap::init();
  11. if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
  12. Bootstrap::jsonResponse([
  13. 'ok' => false,
  14. 'message' => Bootstrap::appMessage('common.method_not_allowed'),
  15. ], 405);
  16. }
  17. $csrf = $_POST['csrf'] ?? '';
  18. if (!Csrf::validate(is_string($csrf) ? $csrf : null)) {
  19. Bootstrap::jsonResponse([
  20. 'ok' => false,
  21. 'message' => Bootstrap::appMessage('common.invalid_csrf'),
  22. ], 419);
  23. }
  24. if (trim((string) ($_POST['website'] ?? '')) !== '') {
  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. $step = (int) ($_POST['step'] ?? 1);
  51. $formDataRaw = $_POST['form_data'] ?? [];
  52. $formData = [];
  53. if (is_array($formDataRaw)) {
  54. foreach ($formDataRaw as $key => $value) {
  55. if (!is_string($key)) {
  56. continue;
  57. }
  58. $formData[$key] = is_array($value) ? '' : trim((string) $value);
  59. }
  60. }
  61. $store = new JsonStore();
  62. try {
  63. $result = $store->withEmailLock($email, static function () use ($store, $email, $step, $formData): array {
  64. if ($store->hasSubmission($email)) {
  65. return [
  66. 'blocked' => true,
  67. 'message' => Bootstrap::appMessage('save_draft.already_submitted'),
  68. ];
  69. }
  70. return [
  71. 'blocked' => false,
  72. 'draft' => $store->saveDraft($email, [
  73. 'step' => max(1, $step),
  74. 'form_data' => $formData,
  75. 'uploads' => [],
  76. ]),
  77. ];
  78. });
  79. } catch (Throwable $e) {
  80. Bootstrap::log('app', 'save-draft lock error: ' . $e->getMessage());
  81. Bootstrap::jsonResponse([
  82. 'ok' => false,
  83. 'message' => Bootstrap::appMessage('save_draft.lock_error'),
  84. ], 500);
  85. }
  86. if (($result['blocked'] ?? false) === true) {
  87. Bootstrap::jsonResponse([
  88. 'ok' => false,
  89. 'already_submitted' => true,
  90. 'message' => (string) ($result['message'] ?? Bootstrap::appMessage('save_draft.blocked_fallback')),
  91. ], 409);
  92. }
  93. $schema = new FormSchema();
  94. $uploadStore = new FileUploadStore();
  95. $uploadResult = $uploadStore->processUploads($_FILES, $schema->getUploadFields(), $store->emailKey($email));
  96. if (!empty($uploadResult['uploads'])) {
  97. try {
  98. $store->withEmailLock($email, static function () use ($store, $email, $step, $formData, $uploadResult): void {
  99. if ($store->hasSubmission($email)) {
  100. return;
  101. }
  102. $store->saveDraft($email, [
  103. 'step' => max(1, $step),
  104. 'form_data' => $formData,
  105. 'uploads' => $uploadResult['uploads'],
  106. ]);
  107. });
  108. } catch (Throwable $e) {
  109. Bootstrap::log('app', 'save-draft upload merge error: ' . $e->getMessage());
  110. }
  111. }
  112. $draft = $store->getDraft($email);
  113. Bootstrap::jsonResponse([
  114. 'ok' => true,
  115. 'message' => Bootstrap::appMessage('save_draft.success'),
  116. 'updated_at' => $draft['updated_at'] ?? null,
  117. 'upload_errors' => $uploadResult['errors'],
  118. 'uploads' => $draft['uploads'] ?? [],
  119. ]);