diff --git a/admin/reset.php b/admin/reset.php new file mode 100644 index 0000000..c9f1511 --- /dev/null +++ b/admin/reset.php @@ -0,0 +1,100 @@ + +
+

Daten zurücksetzen

+

+ Wähle aus, welche Bereiche geleert werden sollen - z.B. um Testdaten vor dem Start loszuwerden. + Konten, Berechtigungsgruppen und Einstellungen bleiben immer erhalten, ebenso Raids, Events und + Premades (die liegen beim Bot). Gelöschtes lässt sich nicht wiederherstellen. +

+ +
+ +
+ + + 0 ausgewählt +
+ +
+ $area): ?> + + + +
+ +
+ +
+ + +
+
+
+
+ + + diff --git a/admin/reset_action.php b/admin/reset_action.php new file mode 100644 index 0000000..0198142 --- /dev/null +++ b/admin/reset_action.php @@ -0,0 +1,41 @@ +getMessage()); + redirect('/admin/reset.php'); +} + +$summary = []; +foreach ($deleted as $key => $count) { + $summary[DATA_RESET_AREAS[$key]['label']] = data_reset_amount($key, $count); +} +// Bereich "security": landet hervorgehoben im Log und zaehlt nicht fuer die Loesch-Notbremse +admin_log('🧹 Daten zurückgesetzt', $summary, 'danger', 'security'); + +set_flash('success', count($keys) === 1 + ? DATA_RESET_AREAS[$keys[0]]['label'] . ' zurückgesetzt.' + : count($keys) . ' Bereiche zurückgesetzt.'); +redirect('/admin/reset.php'); diff --git a/assets/css/style.css b/assets/css/style.css index f2411b7..089e092 100644 --- a/assets/css/style.css +++ b/assets/css/style.css @@ -5177,6 +5177,41 @@ input[type="file"]::file-selector-button { .not-found-stars, .not-found-code { animation: none; } } +/* ---- Daten zuruecksetzen (nur Entwickler) ---- */ +.reset-toolbar { display: flex; flex-wrap: wrap; align-items: center; gap: 0.5rem; margin: 1.25rem 0 0.9rem; } +.reset-toolbar .text-muted { margin-left: auto; font-size: 0.9rem; } +.reset-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 0.75rem; } +.reset-option { + display: flex; + gap: 0.75rem; + align-items: flex-start; + padding: 0.85rem 1rem; + border: 1px solid var(--border); + border-radius: 8px; + background: rgba(255, 255, 255, 0.03); + cursor: pointer; + transition: border-color 0.15s, background 0.15s; +} +.reset-option:hover { border-color: rgba(217, 83, 79, 0.55); } +.reset-option.is-checked { border-color: var(--danger); background: rgba(217, 83, 79, 0.1); } +.reset-option input { width: auto; margin: 0.2rem 0 0; accent-color: var(--danger); flex-shrink: 0; } +.reset-option-body { display: flex; flex-direction: column; gap: 0.25rem; min-width: 0; } +.reset-option-head { display: flex; flex-wrap: wrap; align-items: center; gap: 0.4rem 0.6rem; } +.reset-option-text { color: var(--text-muted); font-size: 0.85rem; line-height: 1.4; } +.reset-option.is-empty .reset-count { opacity: 0.55; } +.reset-confirm { + margin-top: 1.5rem; + padding: 1rem 1.1rem; + border: 1px solid rgba(217, 83, 79, 0.45); + border-radius: 8px; + background: rgba(217, 83, 79, 0.07); +} +.reset-confirm label { display: block; margin-bottom: 0.5rem; } +.reset-confirm-row { display: flex; flex-wrap: wrap; gap: 0.6rem; } +.reset-confirm-row input { flex: 1 1 200px; width: auto; } +.reset-confirm-row .button { display: inline-flex; align-items: center; gap: 0.4rem; } +.reset-confirm-row .button:disabled { opacity: 0.45; cursor: not-allowed; } + /* ---- Moderation: Freigabe, Melden ---- */ .pending-badge { position: absolute; diff --git a/includes/data_reset.php b/includes/data_reset.php new file mode 100644 index 0000000..c5188ab --- /dev/null +++ b/includes/data_reset.php @@ -0,0 +1,228 @@ + Beschriftung, Beschreibung, Zaehl-Abfrage und die Loeschbefehle + * (in dieser Reihenfolge, wegen der Fremdschluessel). + */ +const DATA_RESET_AREAS = [ + 'contributions' => [ + 'label' => 'Lumipunkte', + 'description' => 'Alle Vergaben und Abzüge - jeder Punktestand steht danach auf 0.', + 'count' => 'SELECT COUNT(*) FROM contribution_awards', + 'unit' => ['Vergabe', 'Vergaben'], + 'delete' => ['DELETE FROM contribution_entries', 'DELETE FROM contribution_awards'], + 'reset_ids' => ['contribution_entries', 'contribution_awards'], + ], + 'shop_orders' => [ + 'label' => 'Lumishop: Anträge', + 'description' => 'Alle Anträge der Mitglieder (offen, bewilligt, ausgegeben, abgelehnt). Punkte und Bestand werden dabei nicht zurückgebucht.', + 'count' => 'SELECT COUNT(*) FROM shop_orders', + 'unit' => ['Antrag', 'Anträge'], + 'delete' => ['DELETE FROM shop_orders'], + 'reset_ids' => ['shop_orders'], + ], + 'shop_items' => [ + 'label' => 'Lumishop: Angebot', + 'description' => 'Alle Gegenstände und Kategorien im Shop. Alte Anträge behalten Name und Preis.', + 'count' => 'SELECT COUNT(*) FROM shop_items', + 'unit' => ['Gegenstand', 'Gegenstände'], + 'delete' => ['DELETE FROM shop_items', 'DELETE FROM shop_categories'], + 'reset_ids' => ['shop_items', 'shop_categories'], + ], + 'warehouse' => [ + 'label' => 'Lagerhaus', + 'description' => 'Alle Einlagerungen und Entnahmen (Bestand danach überall 0) und selbst angelegte Gegenstände. Die Gegenstände aus dem Aion-Wiki bleiben.', + 'count' => 'SELECT COUNT(*) FROM warehouse_movements', + 'unit' => ['Buchung', 'Buchungen'], + 'delete' => ['DELETE FROM warehouse_movements', "DELETE FROM warehouse_items WHERE source = 'custom'"], + 'reset_ids' => ['warehouse_movements'], + ], + 'raid_attendance' => [ + 'label' => 'Raid-Anwesenheit', + 'description' => 'Anwesenheitslisten und Punkte-Einstellungen der Raids. Die Raids selbst liegen beim Bot und bleiben.', + 'count' => 'SELECT (SELECT COUNT(*) FROM raid_attendance) + (SELECT COUNT(*) FROM raid_settings)', + 'unit' => ['Eintrag', 'Einträge'], + 'delete' => ['DELETE FROM raid_attendance', 'DELETE FROM raid_settings'], + 'reset_ids' => [], + ], + 'tactics' => [ + 'label' => 'Taktiken (Karte)', + 'description' => 'Alle Taktiken mit ihren Schritten und Zeichnungen.', + 'count' => 'SELECT COUNT(*) FROM tactics', + 'unit' => ['Taktik', 'Taktiken'], + 'delete' => ['DELETE FROM tactics'], + 'reset_ids' => ['tactic_shapes', 'tactic_steps', 'tactics'], + ], + 'calendar' => [ + 'label' => 'Kalender-Termine', + 'description' => 'Von Mitgliedern eingetragene Termine samt Teilnehmern und Meldungen. Raids und Events aus dem Bot bleiben.', + 'count' => 'SELECT COUNT(*) FROM calendar_entries', + 'unit' => ['Termin', 'Termine'], + 'delete' => ["DELETE FROM content_reports WHERE content_type = 'calendar'", 'DELETE FROM calendar_entries'], + 'reset_ids' => ['calendar_entries'], + ], + 'gallery' => [ + 'label' => 'Profil-Galerien', + 'description' => 'Alle Galeriebilder (auch die Dateien), Kommentare, Reaktionen und Meldungen dazu.', + 'count' => 'SELECT COUNT(*) FROM member_gallery', + 'unit' => ['Bild', 'Bilder'], + 'delete' => [ + "DELETE FROM content_reports WHERE content_type IN ('picture', 'comment')", + "DELETE FROM reactions WHERE content_type IN ('picture', 'comment')", + 'DELETE FROM gallery_comments', + 'DELETE FROM member_gallery', + // Profil-Hintergruende, die auf ein Galeriebild zeigen, gibt es danach nicht mehr + "UPDATE users SET profile_background = NULL WHERE profile_background LIKE 'gallery:%'", + ], + 'reset_ids' => ['gallery_comments', 'member_gallery', 'reactions'], + ], + 'characters' => [ + 'label' => 'Charaktere', + 'description' => 'Alle Charaktere auf den Profilseiten mit Portraits und Bildern.', + 'count' => 'SELECT COUNT(*) FROM member_characters', + 'unit' => ['Charakter', 'Charaktere'], + 'delete' => ['DELETE FROM member_characters'], + 'reset_ids' => ['member_characters'], + ], + 'profiles' => [ + 'label' => 'Profiltexte & Hintergründe', + 'description' => 'Beschreibungstexte und gewählte Hintergründe aller Profilseiten. Konten und Profilbilder bleiben.', + 'count' => "SELECT COUNT(*) FROM users WHERE (profile_bio IS NOT NULL AND profile_bio <> '') OR profile_background IS NOT NULL", + 'unit' => ['Profil', 'Profile'], + 'delete' => ['UPDATE users SET profile_bio = NULL, profile_background = NULL'], + 'reset_ids' => [], + ], + 'wiki' => [ + 'label' => 'Wiki', + 'description' => 'Alle Seiten, Versionen, Bilder und Mitwirkenden. Der Einsteiger-Guide lässt sich danach im Wiki neu übernehmen.', + 'count' => 'SELECT COUNT(*) FROM wiki_pages', + 'unit' => ['Seite', 'Seiten'], + 'delete' => ['DELETE FROM wiki_revisions', 'DELETE FROM wiki_contributors', 'UPDATE wiki_pages SET parent_id = NULL', 'DELETE FROM wiki_pages'], + 'reset_ids' => ['wiki_revisions', 'wiki_pages'], + ], + 'bugs' => [ + 'label' => 'Fehlermeldungen', + 'description' => 'Alle eingegangenen Fehlermeldungen.', + 'count' => 'SELECT COUNT(*) FROM bug_reports', + 'unit' => ['Meldung', 'Meldungen'], + 'delete' => ['DELETE FROM bug_reports'], + 'reset_ids' => ['bug_reports'], + ], + 'tour' => [ + 'label' => 'Einführungstour', + 'description' => 'Allen Mitgliedern die Dashboard-Einführung beim nächsten Besuch erneut zeigen.', + 'count' => 'SELECT COUNT(*) FROM users WHERE tour_completed_at IS NOT NULL', + 'unit' => ['Konto', 'Konten'], + 'delete' => ['UPDATE users SET tour_completed_at = NULL'], + 'reset_ids' => [], + ], + 'audit' => [ + 'label' => 'Audit-Log & Tempolimits', + 'description' => 'Alle bisherigen Einträge im Audit-Log und die Zähler des Spam-Schutzes. Das Zurücksetzen selbst wird danach als erster Eintrag protokolliert.', + 'count' => 'SELECT COUNT(*) FROM audit_log', + 'unit' => ['Eintrag', 'Einträge'], + 'delete' => ['DELETE FROM audit_log', 'DELETE FROM rate_limits'], + 'reset_ids' => ['audit_log', 'rate_limits'], + ], +]; + +/** Wort, das zur Bestaetigung eingetippt werden muss. */ +const DATA_RESET_CONFIRM_WORD = 'ZURÜCKSETZEN'; + +/** @return array Anzahl der Eintraege je Bereich */ +function data_reset_counts(): array +{ + $counts = []; + foreach (DATA_RESET_AREAS as $key => $area) { + try { + $counts[$key] = (int) db()->query($area['count'])->fetchColumn(); + } catch (PDOException $e) { + $counts[$key] = 0; + } + } + return $counts; +} + +/** "1 Bild" / "12 Bilder" */ +function data_reset_amount(string $key, int $count): string +{ + [$one, $many] = DATA_RESET_AREAS[$key]['unit']; + return number_format($count, 0, ',', '.') . ' ' . ($count === 1 ? $one : $many); +} + +/** + * Setzt die gewaehlten Bereiche zurueck - alles in einer Transaktion, danach werden die + * zugehoerigen Dateien entfernt und die IDs wieder bei 1 begonnen. + * + * @param string[] $keys + * @return array geloeschte Eintraege je Bereich (Stand vor dem Loeschen) + */ +function data_reset_run(array $keys): array +{ + $keys = array_values(array_filter($keys, static fn ($key): bool => isset(DATA_RESET_AREAS[$key]))); + $pdo = db(); + $counts = data_reset_counts(); + + // Dateien merken, bevor die Zeilen weg sind + $files = []; + if (in_array('gallery', $keys, true)) { + $files = array_merge($files, $pdo->query('SELECT filename FROM member_gallery')->fetchAll(PDO::FETCH_COLUMN)); + } + if (in_array('characters', $keys, true)) { + foreach ($pdo->query('SELECT portrait_filename, image_filename FROM member_characters')->fetchAll() as $row) { + $files[] = $row['portrait_filename']; + $files[] = $row['image_filename']; + } + } + + $pdo->beginTransaction(); + try { + foreach ($keys as $key) { + foreach (DATA_RESET_AREAS[$key]['delete'] as $sql) { + $pdo->exec($sql); + } + } + $pdo->commit(); + } catch (Throwable $e) { + if ($pdo->inTransaction()) { + $pdo->rollBack(); + } + throw $e; + } + + foreach ($files as $filename) { + profile_delete_file($filename); + } + if (in_array('wiki', $keys, true) && is_dir(WIKI_UPLOAD_DIR)) { + foreach (scandir(WIKI_UPLOAD_DIR) ?: [] as $name) { + // .htaccess/index-Dateien des Ordners bleiben + if ($name[0] !== '.' && !str_starts_with($name, 'index.') && is_file(WIKI_UPLOAD_DIR . $name)) { + @unlink(WIKI_UPLOAD_DIR . $name); + } + } + } + + // Neue Eintraege beginnen wieder bei #1 (ALTER TABLE beendet Transaktionen, deshalb danach; + // MySQL nimmt automatisch die naechste freie Nummer, falls noch Zeilen uebrig sind). + foreach ($keys as $key) { + foreach (DATA_RESET_AREAS[$key]['reset_ids'] as $table) { + try { + $pdo->exec("ALTER TABLE `$table` AUTO_INCREMENT = 1"); + } catch (PDOException $e) { + // nicht wichtig - dann laufen die Nummern eben weiter + } + } + } + + return array_intersect_key($counts, array_flip($keys)); +} diff --git a/includes/header.php b/includes/header.php index afad3ba..72ed467 100644 --- a/includes/header.php +++ b/includes/header.php @@ -52,7 +52,9 @@ if ($canManageBugs) { require_once __DIR__ . '/bugs.php'; $bugOpenCount = bug_open_count(); } -$showAdminMenu = $canViewUsers || $canManageRoles || $canViewReports || $canManageBugs || $canViewAudit || $canModerate; +// Daten zuruecksetzen (Testdaten loeschen) nur fuer Entwickler +$canResetData = $currentUser && is_developer($currentUser); +$showAdminMenu = $canViewUsers || $canManageRoles || $canViewReports || $canManageBugs || $canViewAudit || $canModerate || $canResetData; // Neue Discord-Anmeldungen, die auf Freischaltung warten - als Zahl am Menuepunkt. $pendingUserCount = $canViewUsers ? (int) db()->query("SELECT COUNT(*) FROM users WHERE status = 'pending'")->fetchColumn() @@ -75,6 +77,7 @@ $isWikiActive = in_array($currentScript, ['wiki.php', 'wiki_edit.php', 'wiki_his $isReportsActive = $currentScript === 'reports.php'; $isBugsActive = $currentScript === 'bugs.php'; $isAuditActive = $currentScript === 'audit.php'; +$isResetActive = $currentScript === 'reset.php'; $isModerationActive = $currentScript === 'moderation.php'; $isUsersActive = in_array($currentScript, ['users.php', 'user_form.php', 'user_delete.php', 'user_approve.php'], true); $isRolesActive = in_array($currentScript, ['roles.php', 'role_form.php', 'role_delete.php'], true); @@ -110,6 +113,7 @@ $pageBackgrounds = [ 'reports.php' => 'bg-reports', 'bugs.php' => 'bg-reports', 'audit.php' => 'bg-admin', + 'reset.php' => 'bg-admin', 'moderation.php' => 'bg-reports', 'member_picture.php' => 'bg-members', ]; @@ -284,6 +288,11 @@ $otherFlashes = array_values(array_filter($allFlashes, fn (array $f): bool => ($ Audit-Log + + + Daten zurücksetzen + +