diff --git a/admin/discord_sync_action.php b/admin/discord_sync_action.php index 7e56cb3..aeed1e9 100644 --- a/admin/discord_sync_action.php +++ b/admin/discord_sync_action.php @@ -7,7 +7,46 @@ if ($_SERVER['REQUEST_METHOD'] !== 'POST') { } verify_csrf(); -// Manueller Abgleich mit Discord - fuer ein Konto (aus dem Benutzerformular) oder fuer alle. +// Abgleich aller Konten mit Fortschrittsfenster (assets/js/discord-sync.js): Das Skript holt die Liste und gleicht dann +// ein Konto nach dem anderen ab - jede Anfrage ein Konto, damit Discord nicht viele auf einmal sieht. Zaehlt im Topf +// "discord_admin" statt "post" (includes/security.php). +$jsonAction = (string) ($_POST['action'] ?? ''); +if (in_array($jsonAction, DISCORD_SYNC_STEPS, true)) { + header('Cache-Control: no-store'); + if ($jsonAction === 'list') { + $rows = db()->query( + "SELECT id, COALESCE(NULLIF(display_name, ''), username) AS name FROM users WHERE discord_id IS NOT NULL ORDER BY id" + )->fetchAll(); + json_response(true, '', ['users' => array_map( + static fn (array $r): array => ['id' => (int) $r['id'], 'name' => (string) $r['name']], + $rows + )]); + } + if ($jsonAction === 'finish') { + $counts = array_map('intval', array_intersect_key((array) ($_POST['counts'] ?? []), array_flip(['changed', 'unchanged', 'skipped', 'error']))); + $fields = ['Ergebnis' => ($counts['changed'] ?? 0) . ' geändert, ' . ($counts['unchanged'] ?? 0) . ' unverändert, ' + . ($counts['skipped'] ?? 0) . ' nicht prüfbar, ' . ($counts['error'] ?? 0) . ' fehlgeschlagen']; + if (!empty($_POST['cancelled'])) { + $fields['Hinweis'] = 'Vorzeitig abgebrochen'; + } + admin_log('🔄 Discord-Abgleich (alle Konten)', $fields); + json_response(true); + } + $checkId = (int) ($_POST['user_id'] ?? 0); + $before = discord_sync_snapshot($checkId); + if ($before === null) { + json_response(false, 'Konto nicht gefunden.', ['result' => 'missing']); + } + $result = discord_live_check($checkId, true); + $after = discord_sync_snapshot($checkId) ?? $before; + json_response($result !== 'error', $result === 'error' ? (discord_last_error() !== '' ? discord_last_error() : 'Discord nicht erreichbar') : '', [ + 'result' => $result, + 'name' => $after['name'], + 'changes' => discord_sync_changes($before, $after), + ]); +} + +// Manueller Abgleich mit Discord - fuer ein Konto (aus dem Benutzerformular) oder fuer alle (ohne JavaScript). $userId = (int) ($_POST['user_id'] ?? 0); if ($userId > 0) { diff --git a/admin/users.php b/admin/users.php index 863aaba..dd31816 100644 --- a/admin/users.php +++ b/admin/users.php @@ -91,7 +91,7 @@ include __DIR__ . '/../includes/header.php'; Neuen Benutzer anlegen -