| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- <?php
- declare(strict_types=1);
- use App\App\Bootstrap;
- use App\Security\Csrf;
- use App\Security\RateLimiter;
- use App\Storage\FileSystem;
- use App\Storage\JsonStore;
- require dirname(__DIR__) . '/src/autoload.php';
- Bootstrap::init();
- if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
- Bootstrap::jsonResponse(['ok' => false, 'message' => 'Method not allowed'], 405);
- }
- $csrf = $_POST['csrf'] ?? '';
- if (!Csrf::validate(is_string($csrf) ? $csrf : null)) {
- Bootstrap::jsonResponse(['ok' => false, 'message' => 'Ungültiges CSRF-Token.'], 419);
- }
- if (trim((string) ($_POST['website'] ?? '')) !== '') {
- Bootstrap::jsonResponse(['ok' => false, 'message' => 'Anfrage blockiert.'], 400);
- }
- $email = strtolower(trim((string) ($_POST['email'] ?? '')));
- if (filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
- Bootstrap::jsonResponse(['ok' => false, 'message' => 'Bitte gültige E-Mail eingeben.'], 422);
- }
- $app = Bootstrap::config('app');
- $limiter = new RateLimiter();
- $ip = $_SERVER['REMOTE_ADDR'] ?? 'unknown';
- $rateKey = sprintf('reset:%s:%s', $ip, $email);
- if (!$limiter->allow($rateKey, (int) $app['rate_limit']['requests'], (int) $app['rate_limit']['window_seconds'])) {
- Bootstrap::jsonResponse(['ok' => false, 'message' => 'Zu viele Löschanfragen. Bitte später erneut versuchen.'], 429);
- }
- $store = new JsonStore();
- try {
- $result = $store->withEmailLock($email, static function () use ($store, $app, $email): array {
- $hadDraft = $store->getDraft($email) !== null;
- $submission = $store->getSubmissionByEmail($email);
- $hadSubmission = $submission !== null;
- if ($hadSubmission) {
- $submissionKey = (string) ($submission['application_key'] ?? $store->emailKey($email));
- $store->deleteSubmissionByKey($submissionKey);
- }
- $store->deleteDraft($email);
- $uploadDir = rtrim((string) $app['storage']['uploads'], '/') . '/' . $store->emailKey($email);
- FileSystem::removeTree($uploadDir);
- return [
- 'had_draft' => $hadDraft,
- 'had_submission' => $hadSubmission,
- ];
- });
- } catch (Throwable $e) {
- Bootstrap::log('app', 'reset error: ' . $e->getMessage());
- Bootstrap::jsonResponse(['ok' => false, 'message' => 'Daten konnten nicht gelöscht werden.'], 500);
- }
- Bootstrap::jsonResponse([
- 'ok' => true,
- 'message' => 'Gespeicherte Daten wurden gelöscht.',
- 'had_draft' => (bool) ($result['had_draft'] ?? false),
- 'had_submission' => (bool) ($result['had_submission'] ?? false),
- ]);
|