reset.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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\Security\RateLimiter;
  7. use App\Storage\FileSystem;
  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. $app = Bootstrap::config('app');
  51. $limiter = new RateLimiter();
  52. $ip = $_SERVER['REMOTE_ADDR'] ?? 'unknown';
  53. $rateKey = sprintf('reset:%s:%s', $ip, $email);
  54. if (!$limiter->allow($rateKey, (int) $app['rate_limit']['requests'], (int) $app['rate_limit']['window_seconds'])) {
  55. Bootstrap::jsonResponse([
  56. 'ok' => false,
  57. 'message' => Bootstrap::appMessage('reset.rate_limited'),
  58. ], 429);
  59. }
  60. $store = new JsonStore();
  61. try {
  62. $result = $store->withEmailLock($email, static function () use ($store, $app, $email): array {
  63. $hadDraft = $store->getDraft($email) !== null;
  64. $submission = $store->getSubmissionByEmail($email);
  65. $hadSubmission = $submission !== null;
  66. if ($hadSubmission) {
  67. return [
  68. 'ok' => false,
  69. 'status' => 409,
  70. 'message' => Bootstrap::appMessage('reset.already_submitted'),
  71. 'had_draft' => $hadDraft,
  72. 'had_submission' => true,
  73. ];
  74. }
  75. $store->deleteDraft($email);
  76. $uploadDir = rtrim((string) $app['storage']['uploads'], '/') . '/' . $store->emailKey($email);
  77. FileSystem::removeTree($uploadDir);
  78. return [
  79. 'ok' => true,
  80. 'status' => 200,
  81. 'had_draft' => $hadDraft,
  82. 'had_submission' => $hadSubmission,
  83. ];
  84. });
  85. } catch (Throwable $e) {
  86. Bootstrap::log('app', 'reset error: ' . $e->getMessage());
  87. Bootstrap::jsonResponse([
  88. 'ok' => false,
  89. 'message' => Bootstrap::appMessage('reset.delete_error'),
  90. ], 500);
  91. }
  92. if (($result['ok'] ?? false) !== true) {
  93. Bootstrap::jsonResponse([
  94. 'ok' => false,
  95. 'message' => (string) ($result['message'] ?? Bootstrap::appMessage('reset.delete_error')),
  96. 'had_draft' => (bool) ($result['had_draft'] ?? false),
  97. 'had_submission' => (bool) ($result['had_submission'] ?? false),
  98. ], (int) ($result['status'] ?? 422));
  99. }
  100. Bootstrap::jsonResponse([
  101. 'ok' => true,
  102. 'message' => Bootstrap::appMessage('reset.success'),
  103. 'had_draft' => (bool) ($result['had_draft'] ?? false),
  104. 'had_submission' => (bool) ($result['had_submission'] ?? false),
  105. ]);