confirm.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. declare(strict_types=1);
  3. use App\App\Bootstrap;
  4. use App\Security\EntraAuth;
  5. use App\Security\Csrf;
  6. use App\Storage\JsonStore;
  7. use App\Mail\Mailer;
  8. require dirname(__DIR__) . '/src/autoload.php';
  9. Bootstrap::init();
  10. $auth = new EntraAuth();
  11. $auth->requireLogin();
  12. if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
  13. header('Location: ' . Bootstrap::url('portal/index.php'));
  14. exit;
  15. }
  16. if (!Csrf::validate((string) ($_POST['csrf'] ?? ''))) {
  17. header('Location: ' . Bootstrap::url('portal/index.php'));
  18. exit;
  19. }
  20. $applicationKey = (string) ($_POST['id'] ?? '');
  21. $store = new JsonStore();
  22. $submission = $store->getSubmissionByKey($applicationKey);
  23. // Only minor submissions are actionable here.
  24. if ($submission === null || !(bool) ($submission['is_minor_submission'] ?? false)) {
  25. header('Location: ' . Bootstrap::url('portal/index.php'));
  26. exit;
  27. }
  28. // Already confirmed → no duplicate email.
  29. if (isset($submission['signed_form_received']) && is_array($submission['signed_form_received'])) {
  30. header('Location: ' . Bootstrap::url('portal/index.php?done=already'));
  31. exit;
  32. }
  33. $user = $auth->user() ?? ['email' => '', 'name' => ''];
  34. $result = $store->markSignedFormReceived($applicationKey, [
  35. 'received_by' => (string) ($user['name'] ?? ''),
  36. 'received_by_email' => (string) ($user['email'] ?? ''),
  37. ]);
  38. if ($result === null) {
  39. header('Location: ' . Bootstrap::url('portal/index.php'));
  40. exit;
  41. }
  42. // Only the call that actually set the marker sends the notification (no duplicate mail).
  43. if (!$result['newly_marked']) {
  44. header('Location: ' . Bootstrap::url('portal/index.php?done=already'));
  45. exit;
  46. }
  47. $updated = $result['submission'];
  48. Bootstrap::log('portal', 'Eingang unterschriebenes Formular bestätigt für ' . (string) ($updated['email'] ?? '')
  49. . ' durch ' . (string) ($user['email'] ?? ''));
  50. $mailer = new Mailer();
  51. $mailer->sendSignedFormReceivedMail($updated);
  52. header('Location: ' . Bootstrap::url('portal/index.php?done=1'));
  53. exit;