reset.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. declare(strict_types=1);
  3. use App\App\Bootstrap;
  4. use App\Security\Csrf;
  5. use App\Security\FormAccess;
  6. use App\Storage\FileSystem;
  7. use App\Storage\JsonStore;
  8. require dirname(__DIR__) . '/src/autoload.php';
  9. Bootstrap::init();
  10. if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
  11. Bootstrap::jsonResponse([
  12. 'ok' => false,
  13. 'message' => Bootstrap::appMessage('common.method_not_allowed'),
  14. ], 405);
  15. }
  16. $csrf = $_POST['csrf'] ?? '';
  17. if (!Csrf::validate(is_string($csrf) ? $csrf : null)) {
  18. Bootstrap::jsonResponse([
  19. 'ok' => false,
  20. 'message' => Bootstrap::appMessage('common.invalid_csrf'),
  21. ], 419);
  22. }
  23. if (trim((string) ($_POST['website'] ?? '')) !== '') {
  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. $activityRaw = $_POST['last_user_activity_at'] ?? null;
  37. $lastUserActivityAt = is_scalar($activityRaw) ? (int) $activityRaw : null;
  38. $formAccess = new FormAccess();
  39. $auth = $formAccess->assertVerifiedForEmail($email, $lastUserActivityAt);
  40. if (($auth['ok'] ?? false) !== true) {
  41. $reason = (string) ($auth['reason'] ?? '');
  42. Bootstrap::jsonResponse([
  43. 'ok' => false,
  44. 'message' => (string) ($auth['message'] ?? 'Bitte E-Mail erneut verifizieren.'),
  45. 'auth_required' => $reason === 'auth_required',
  46. 'auth_expired' => $reason === 'auth_expired',
  47. ], (int) ($auth['status_code'] ?? 401));
  48. }
  49. $app = Bootstrap::config('app');
  50. $store = new JsonStore();
  51. try {
  52. $result = $store->withEmailLock($email, static function () use ($store, $app, $email): array {
  53. $hadDraft = $store->getDraft($email) !== null;
  54. $submission = $store->getSubmissionByEmail($email);
  55. $hadSubmission = $submission !== null;
  56. if ($hadSubmission) {
  57. return [
  58. 'ok' => false,
  59. 'status' => 409,
  60. 'message' => Bootstrap::appMessage('reset.already_submitted'),
  61. 'had_draft' => $hadDraft,
  62. 'had_submission' => true,
  63. ];
  64. }
  65. $store->deleteDraft($email);
  66. $uploadDir = rtrim((string) $app['storage']['uploads'], '/') . '/' . $store->emailKey($email);
  67. FileSystem::removeTree($uploadDir);
  68. return [
  69. 'ok' => true,
  70. 'status' => 200,
  71. 'had_draft' => $hadDraft,
  72. 'had_submission' => $hadSubmission,
  73. ];
  74. });
  75. } catch (Throwable $e) {
  76. Bootstrap::log('app', 'reset error: ' . $e->getMessage());
  77. Bootstrap::jsonResponse([
  78. 'ok' => false,
  79. 'message' => Bootstrap::appMessage('reset.delete_error'),
  80. ], 500);
  81. }
  82. if (($result['ok'] ?? false) !== true) {
  83. Bootstrap::jsonResponse([
  84. 'ok' => false,
  85. 'message' => (string) ($result['message'] ?? Bootstrap::appMessage('reset.delete_error')),
  86. 'had_draft' => (bool) ($result['had_draft'] ?? false),
  87. 'had_submission' => (bool) ($result['had_submission'] ?? false),
  88. ], (int) ($result['status'] ?? 422));
  89. }
  90. Bootstrap::jsonResponse([
  91. 'ok' => true,
  92. 'message' => Bootstrap::appMessage('reset.success'),
  93. 'had_draft' => (bool) ($result['had_draft'] ?? false),
  94. 'had_submission' => (bool) ($result['had_submission'] ?? false),
  95. ]);