submit.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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\RateLimiter;
  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. if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
  26. Bootstrap::jsonResponse([
  27. 'ok' => false,
  28. 'message' => Bootstrap::appMessage('common.method_not_allowed'),
  29. ], 405);
  30. }
  31. $csrf = $_POST['csrf'] ?? '';
  32. if (!Csrf::validate(is_string($csrf) ? $csrf : null)) {
  33. Bootstrap::jsonResponse([
  34. 'ok' => false,
  35. 'message' => Bootstrap::appMessage('common.invalid_csrf'),
  36. ], 419);
  37. }
  38. if (trim((string) ($_POST['website'] ?? '')) !== '') {
  39. Bootstrap::jsonResponse([
  40. 'ok' => false,
  41. 'message' => Bootstrap::appMessage('common.request_blocked'),
  42. ], 400);
  43. }
  44. $email = strtolower(trim((string) ($_POST['email'] ?? '')));
  45. if (filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
  46. Bootstrap::jsonResponse([
  47. 'ok' => false,
  48. 'message' => Bootstrap::appMessage('common.invalid_email'),
  49. ], 422);
  50. }
  51. $app = Bootstrap::config('app');
  52. $limiter = new RateLimiter();
  53. $ip = $_SERVER['REMOTE_ADDR'] ?? 'unknown';
  54. $rateKey = sprintf('submit:%s:%s', $ip, $email);
  55. if (!$limiter->allow($rateKey, (int) $app['rate_limit']['requests'], (int) $app['rate_limit']['window_seconds'])) {
  56. Bootstrap::jsonResponse([
  57. 'ok' => false,
  58. 'message' => Bootstrap::appMessage('submit.rate_limited'),
  59. ], 429);
  60. }
  61. $formDataRaw = $_POST['form_data'] ?? [];
  62. $formData = [];
  63. if (is_array($formDataRaw)) {
  64. foreach ($formDataRaw as $key => $value) {
  65. if (!is_string($key)) {
  66. continue;
  67. }
  68. $formData[$key] = is_array($value) ? '' : trim((string) $value);
  69. }
  70. }
  71. $store = new JsonStore();
  72. $schema = new FormSchema();
  73. $uploadStore = new FileUploadStore();
  74. $validator = new Validator($schema);
  75. // Fast fail before upload processing to avoid writing files for known duplicates.
  76. if ($store->hasSubmission($email)) {
  77. Bootstrap::jsonResponse([
  78. 'ok' => false,
  79. 'already_submitted' => true,
  80. 'message' => Bootstrap::appMessage('submit.already_submitted'),
  81. ], 409);
  82. }
  83. try {
  84. $submitResult = $store->withEmailLock($email, static function () use ($store, $email, $formData, $validator, $uploadStore, $schema): array {
  85. if ($store->hasSubmission($email)) {
  86. return [
  87. 'ok' => false,
  88. 'already_submitted' => true,
  89. 'message' => Bootstrap::appMessage('submit.already_submitted'),
  90. ];
  91. }
  92. $uploadResult = $uploadStore->processUploads($_FILES, $schema->getUploadFields(), $store->emailKey($email));
  93. if (!empty($uploadResult['errors'])) {
  94. return [
  95. 'ok' => false,
  96. 'already_submitted' => false,
  97. 'message' => Bootstrap::appMessage('submit.upload_error'),
  98. 'errors' => $uploadResult['errors'],
  99. ];
  100. }
  101. $draft = $store->getDraft($email) ?? [];
  102. $mergedFormData = array_merge((array) ($draft['form_data'] ?? []), $formData);
  103. $mergedUploads = (array) ($draft['uploads'] ?? []);
  104. foreach ($uploadResult['uploads'] as $field => $items) {
  105. $mergedUploads[$field] = array_values(array_merge((array) ($mergedUploads[$field] ?? []), $items));
  106. }
  107. $errors = $validator->validateSubmit($mergedFormData, $mergedUploads);
  108. if (!empty($errors)) {
  109. $store->saveDraft($email, [
  110. 'step' => 4,
  111. 'form_data' => $mergedFormData,
  112. 'uploads' => $uploadResult['uploads'],
  113. ]);
  114. return [
  115. 'ok' => false,
  116. 'already_submitted' => false,
  117. 'message' => Bootstrap::appMessage('submit.validation_error'),
  118. 'errors' => $errors,
  119. ];
  120. }
  121. $submission = $store->saveSubmission($email, [
  122. 'step' => 4,
  123. 'form_data' => $mergedFormData,
  124. 'uploads' => $mergedUploads,
  125. ]);
  126. return [
  127. 'ok' => true,
  128. 'submission' => $submission,
  129. ];
  130. });
  131. } catch (Throwable $e) {
  132. Bootstrap::log('app', 'submit lock error: ' . $e->getMessage());
  133. Bootstrap::jsonResponse([
  134. 'ok' => false,
  135. 'message' => Bootstrap::appMessage('submit.lock_error'),
  136. ], 500);
  137. }
  138. if (($submitResult['ok'] ?? false) !== true) {
  139. $status = ($submitResult['already_submitted'] ?? false) ? 409 : 422;
  140. Bootstrap::jsonResponse([
  141. 'ok' => false,
  142. 'already_submitted' => (bool) ($submitResult['already_submitted'] ?? false),
  143. 'message' => (string) ($submitResult['message'] ?? Bootstrap::appMessage('submit.failure')),
  144. 'errors' => $submitResult['errors'] ?? [],
  145. ], $status);
  146. }
  147. $submission = $submitResult['submission'];
  148. $mailer = new Mailer();
  149. $mailer->sendSubmissionMails($submission);
  150. Bootstrap::jsonResponse([
  151. 'ok' => true,
  152. 'message' => resolveSubmitSuccessMessage($app),
  153. 'application_key' => $submission['application_key'] ?? null,
  154. ]);