submit.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. declare(strict_types=1);
  3. use App\App\Bootstrap;
  4. use App\Form\FormSchema;
  5. use App\Form\Validator;
  6. use App\Mail\Mailer;
  7. use App\Security\Csrf;
  8. use App\Security\FormAccess;
  9. use App\Storage\FileUploadStore;
  10. use App\Storage\JsonStore;
  11. require dirname(__DIR__) . '/src/autoload.php';
  12. Bootstrap::init();
  13. /** @param array<string, mixed> $app */
  14. function resolveSubmitSuccessMessage(array $app): string
  15. {
  16. $configured = Bootstrap::appMessage('submit.success');
  17. $contactEmail = trim((string) ($app['contact_email'] ?? ''));
  18. $message = str_replace(
  19. ['%contact_email%', '{{contact_email}}'],
  20. $contactEmail !== '' ? $contactEmail : 'uns',
  21. $configured
  22. );
  23. return trim($message);
  24. }
  25. function isMinorBirthdate(string $birthdate): bool
  26. {
  27. $birthdate = trim($birthdate);
  28. if ($birthdate === '') {
  29. return false;
  30. }
  31. $date = DateTimeImmutable::createFromFormat('!Y-m-d', $birthdate);
  32. if (!$date || $date->format('Y-m-d') !== $birthdate) {
  33. return false;
  34. }
  35. $today = new DateTimeImmutable('today');
  36. if ($date > $today) {
  37. return false;
  38. }
  39. return $date->diff($today)->y < 18;
  40. }
  41. if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
  42. Bootstrap::jsonResponse([
  43. 'ok' => false,
  44. 'message' => Bootstrap::appMessage('common.method_not_allowed'),
  45. ], 405);
  46. }
  47. $csrf = $_POST['csrf'] ?? '';
  48. if (!Csrf::validate(is_string($csrf) ? $csrf : null)) {
  49. Bootstrap::jsonResponse([
  50. 'ok' => false,
  51. 'message' => Bootstrap::appMessage('common.invalid_csrf'),
  52. ], 419);
  53. }
  54. if (trim((string) ($_POST['website'] ?? '')) !== '') {
  55. Bootstrap::jsonResponse([
  56. 'ok' => false,
  57. 'message' => Bootstrap::appMessage('common.request_blocked'),
  58. ], 400);
  59. }
  60. $email = strtolower(trim((string) ($_POST['email'] ?? '')));
  61. if (filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
  62. Bootstrap::jsonResponse([
  63. 'ok' => false,
  64. 'message' => Bootstrap::appMessage('common.invalid_email'),
  65. ], 422);
  66. }
  67. $activityRaw = $_POST['last_user_activity_at'] ?? null;
  68. $lastUserActivityAt = is_scalar($activityRaw) ? (int) $activityRaw : null;
  69. $formAccess = new FormAccess();
  70. $auth = $formAccess->assertVerifiedForEmail($email, $lastUserActivityAt);
  71. if (($auth['ok'] ?? false) !== true) {
  72. $reason = (string) ($auth['reason'] ?? '');
  73. Bootstrap::jsonResponse([
  74. 'ok' => false,
  75. 'message' => (string) ($auth['message'] ?? 'Bitte E-Mail erneut verifizieren.'),
  76. 'auth_required' => $reason === 'auth_required',
  77. 'auth_expired' => $reason === 'auth_expired',
  78. ], (int) ($auth['status_code'] ?? 401));
  79. }
  80. $app = Bootstrap::config('app');
  81. $formDataRaw = $_POST['form_data'] ?? [];
  82. $formData = [];
  83. if (is_array($formDataRaw)) {
  84. foreach ($formDataRaw as $key => $value) {
  85. if (!is_string($key)) {
  86. continue;
  87. }
  88. $formData[$key] = is_array($value) ? '' : trim((string) $value);
  89. }
  90. }
  91. $store = new JsonStore();
  92. $schema = new FormSchema();
  93. $uploadStore = new FileUploadStore();
  94. $validator = new Validator($schema);
  95. // Fast fail before upload processing to avoid writing files for known duplicates.
  96. if ($store->hasSubmission($email)) {
  97. Bootstrap::jsonResponse([
  98. 'ok' => false,
  99. 'already_submitted' => true,
  100. 'message' => Bootstrap::appMessage('submit.already_submitted'),
  101. ], 409);
  102. }
  103. try {
  104. $submitResult = $store->withEmailLock($email, static function () use ($store, $email, $formData, $validator, $uploadStore, $schema): array {
  105. if ($store->hasSubmission($email)) {
  106. return [
  107. 'ok' => false,
  108. 'already_submitted' => true,
  109. 'message' => Bootstrap::appMessage('submit.already_submitted'),
  110. ];
  111. }
  112. $uploadResult = $uploadStore->processUploads($_FILES, $schema->getUploadFields(), $store->emailKey($email));
  113. if (!empty($uploadResult['errors'])) {
  114. return [
  115. 'ok' => false,
  116. 'already_submitted' => false,
  117. 'message' => Bootstrap::appMessage('submit.upload_error'),
  118. 'errors' => $uploadResult['errors'],
  119. ];
  120. }
  121. $draft = $store->getDraft($email) ?? [];
  122. $mergedFormData = array_merge((array) ($draft['form_data'] ?? []), $formData);
  123. $mergedUploads = (array) ($draft['uploads'] ?? []);
  124. foreach ($uploadResult['uploads'] as $field => $items) {
  125. $mergedUploads[$field] = array_values(array_merge((array) ($mergedUploads[$field] ?? []), $items));
  126. }
  127. $errors = $validator->validateSubmit($mergedFormData, $mergedUploads);
  128. if (!empty($errors)) {
  129. $store->saveDraft($email, [
  130. 'step' => 4,
  131. 'form_data' => $mergedFormData,
  132. 'uploads' => $uploadResult['uploads'],
  133. ]);
  134. return [
  135. 'ok' => false,
  136. 'already_submitted' => false,
  137. 'message' => Bootstrap::appMessage('submit.validation_error'),
  138. 'errors' => $errors,
  139. ];
  140. }
  141. $submission = $store->saveSubmission($email, [
  142. 'step' => 4,
  143. 'form_data' => $mergedFormData,
  144. 'uploads' => $mergedUploads,
  145. 'is_minor_submission' => isMinorBirthdate((string) ($mergedFormData['geburtsdatum'] ?? '')),
  146. ]);
  147. return [
  148. 'ok' => true,
  149. 'submission' => $submission,
  150. ];
  151. });
  152. } catch (Throwable $e) {
  153. Bootstrap::log('app', 'submit lock error: ' . $e->getMessage());
  154. Bootstrap::jsonResponse([
  155. 'ok' => false,
  156. 'message' => Bootstrap::appMessage('submit.lock_error'),
  157. ], 500);
  158. }
  159. if (($submitResult['ok'] ?? false) !== true) {
  160. $status = ($submitResult['already_submitted'] ?? false) ? 409 : 422;
  161. Bootstrap::jsonResponse([
  162. 'ok' => false,
  163. 'already_submitted' => (bool) ($submitResult['already_submitted'] ?? false),
  164. 'message' => (string) ($submitResult['message'] ?? Bootstrap::appMessage('submit.failure')),
  165. 'errors' => $submitResult['errors'] ?? [],
  166. ], $status);
  167. }
  168. $submission = $submitResult['submission'];
  169. $mailer = new Mailer();
  170. $mailer->sendSubmissionMails($submission);
  171. Bootstrap::jsonResponse([
  172. 'ok' => true,
  173. 'message' => resolveSubmitSuccessMessage($app),
  174. 'application_key' => $submission['application_key'] ?? null,
  175. ]);