reset.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. declare(strict_types=1);
  3. use App\App\Bootstrap;
  4. use App\Security\Csrf;
  5. use App\Security\RateLimiter;
  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(['ok' => false, 'message' => 'Method not allowed'], 405);
  12. }
  13. $csrf = $_POST['csrf'] ?? '';
  14. if (!Csrf::validate(is_string($csrf) ? $csrf : null)) {
  15. Bootstrap::jsonResponse(['ok' => false, 'message' => 'Ungültiges CSRF-Token.'], 419);
  16. }
  17. if (trim((string) ($_POST['website'] ?? '')) !== '') {
  18. Bootstrap::jsonResponse(['ok' => false, 'message' => 'Anfrage blockiert.'], 400);
  19. }
  20. $email = strtolower(trim((string) ($_POST['email'] ?? '')));
  21. if (filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
  22. Bootstrap::jsonResponse(['ok' => false, 'message' => 'Bitte gültige E-Mail eingeben.'], 422);
  23. }
  24. $app = Bootstrap::config('app');
  25. $limiter = new RateLimiter();
  26. $ip = $_SERVER['REMOTE_ADDR'] ?? 'unknown';
  27. $rateKey = sprintf('reset:%s:%s', $ip, $email);
  28. if (!$limiter->allow($rateKey, (int) $app['rate_limit']['requests'], (int) $app['rate_limit']['window_seconds'])) {
  29. Bootstrap::jsonResponse(['ok' => false, 'message' => 'Zu viele Löschanfragen. Bitte später erneut versuchen.'], 429);
  30. }
  31. $store = new JsonStore();
  32. try {
  33. $result = $store->withEmailLock($email, static function () use ($store, $app, $email): array {
  34. $hadDraft = $store->getDraft($email) !== null;
  35. $submission = $store->getSubmissionByEmail($email);
  36. $hadSubmission = $submission !== null;
  37. if ($hadSubmission) {
  38. $submissionKey = (string) ($submission['application_key'] ?? $store->emailKey($email));
  39. $store->deleteSubmissionByKey($submissionKey);
  40. }
  41. $store->deleteDraft($email);
  42. $uploadDir = rtrim((string) $app['storage']['uploads'], '/') . '/' . $store->emailKey($email);
  43. FileSystem::removeTree($uploadDir);
  44. return [
  45. 'had_draft' => $hadDraft,
  46. 'had_submission' => $hadSubmission,
  47. ];
  48. });
  49. } catch (Throwable $e) {
  50. Bootstrap::log('app', 'reset error: ' . $e->getMessage());
  51. Bootstrap::jsonResponse(['ok' => false, 'message' => 'Daten konnten nicht gelöscht werden.'], 500);
  52. }
  53. Bootstrap::jsonResponse([
  54. 'ok' => true,
  55. 'message' => 'Gespeicherte Daten wurden gelöscht.',
  56. 'had_draft' => (bool) ($result['had_draft'] ?? false),
  57. 'had_submission' => (bool) ($result['had_submission'] ?? false),
  58. ]);