Add Discord account linking and raid signup/management from the website
- Class name on raid signups now shows only as an icon tooltip instead
of trailing text (falls back to text when no icon matches).
- Discord OAuth2 linking (discord_login.php/discord_callback.php,
includes/discord.php): members connect their Discord account from
the dashboard, storing discord_id/discord_username so raid signups
can be attributed to their real Discord identity. Needs a Discord
application's client_id/client_secret in config.php.
- raids.php gets self-service signup/withdraw controls (class + group
picker, upsert semantics) for members with a linked Discord account,
plus archive/restore controls gated behind a new raids.manage
permission. New action scripts raid_signup.php, raid_unsignup.php,
raid_archive.php, raid_restore.php call the corresponding
includes/raids_api.php functions.
- Archive/restore already work today (verified live against the bot's
API - archived and restored the one active raid, signups intact).
Signup/withdraw are fully wired on the website side but need two new
bot-side endpoints (POST .../signup, DELETE .../signup/{user_id})
that don't exist yet - contract documented in the README.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
parent
9a278678cb
commit
8c703f0e1f
17 changed files with 554 additions and 20 deletions
47
README.md
47
README.md
|
|
@ -17,7 +17,12 @@ und wird später ausgebaut. Fokus dieser Version ist der Mitglieder-/Admin-Berei
|
||||||
für zukünftige Funktionen
|
für zukünftige Funktionen
|
||||||
- Jeder Benutzer kann mehreren Berechtigungsgruppen angehören
|
- Jeder Benutzer kann mehreren Berechtigungsgruppen angehören
|
||||||
- Raids-Seite: zeigt anstehende Raid-Termine, Anforderungen, Gruppen und Anmeldungen,
|
- Raids-Seite: zeigt anstehende Raid-Termine, Anforderungen, Gruppen und Anmeldungen,
|
||||||
bezogen von einer externen API (`config.php: raids_api`)
|
bezogen von einer externen API (`config.php: raids_api`), Filter Aktiv/Archiviert/Alle
|
||||||
|
- Discord-Verknüpfung: Mitglieder können ihren Discord-Account per OAuth mit ihrem
|
||||||
|
Website-Account verknüpfen (nötig, um sich über die Website zu Raids anzumelden)
|
||||||
|
- Raid-Anmeldung: verknüpfte Mitglieder können sich direkt über die Website zu einem Raid
|
||||||
|
an-/ummelden bzw. abmelden; Admins mit der Berechtigung `raids.manage` können Raids
|
||||||
|
archivieren/wiederherstellen
|
||||||
|
|
||||||
## Ordnerstruktur
|
## Ordnerstruktur
|
||||||
|
|
||||||
|
|
@ -78,6 +83,46 @@ Berechtigung `raids.view` noch nicht automatisch. Leg sie einmalig unter **Berec
|
||||||
die Raids sehen sollen (z.B. Administrator und Mitglied). Bei einer frischen Installation über
|
die Raids sehen sollen (z.B. Administrator und Mitglied). Bei einer frischen Installation über
|
||||||
`install.php` passiert das automatisch.
|
`install.php` passiert das automatisch.
|
||||||
|
|
||||||
|
### Discord-Verknüpfung einrichten
|
||||||
|
|
||||||
|
1. Discord-Application erstellen: https://discord.com/developers/applications -> **New
|
||||||
|
Application** -> unter **OAuth2 -> General** die **Client ID** und das **Client Secret**
|
||||||
|
kopieren -> unter **Redirects** exakt `https://deine-domain.de/discord_callback.php`
|
||||||
|
eintragen und speichern.
|
||||||
|
2. In `config.php` ergänzen:
|
||||||
|
```php
|
||||||
|
'discord' => [
|
||||||
|
'client_id' => 'deine-discord-client-id',
|
||||||
|
'client_secret' => 'dein-discord-client-secret',
|
||||||
|
'redirect_uri' => 'https://deine-domain.de/discord_callback.php',
|
||||||
|
],
|
||||||
|
```
|
||||||
|
3. Falls die Datenbank schon vor diesem Update existierte, fehlen die Spalten `discord_id` und
|
||||||
|
`discord_username`:
|
||||||
|
```sql
|
||||||
|
ALTER TABLE users ADD COLUMN discord_id BIGINT UNSIGNED NULL UNIQUE AFTER must_change_password;
|
||||||
|
ALTER TABLE users ADD COLUMN discord_username VARCHAR(100) NULL AFTER discord_id;
|
||||||
|
```
|
||||||
|
|
||||||
|
Mitglieder verknüpfen ihren Account danach selbst über einen Button im Dashboard.
|
||||||
|
|
||||||
|
### Raid-Anmeldung über die Website
|
||||||
|
|
||||||
|
Damit sich Mitglieder direkt über die Website zu Raids an-/abmelden können, braucht die
|
||||||
|
Raid-Bot-API zwei zusätzliche Endpunkte (bestehen aktuell noch nicht in der API, die nur
|
||||||
|
Lesen/Archivieren/Wiederherstellen kann):
|
||||||
|
|
||||||
|
- `POST /api/raids/{raid_id}/signup` mit Body `{"user_id": ..., "name": ..., "class": ..., "group": ...}`
|
||||||
|
(Upsert: bereits bestehende Anmeldung der gleichen `user_id` aktualisieren statt zu duplizieren)
|
||||||
|
- `DELETE /api/raids/{raid_id}/signup/{user_id}` (idempotent, auch ohne bestehende Anmeldung
|
||||||
|
`{"status": "success"}` zurückgeben)
|
||||||
|
|
||||||
|
Sobald diese Endpunkte auf Bot-Seite existieren, funktionieren die Anmelden-/Abmelden-Buttons auf
|
||||||
|
der Raids-Seite automatisch. Archivieren/Wiederherstellen funktionieren bereits jetzt, da die API
|
||||||
|
das schon unterstützt. Zusätzlich gibt es die neue Berechtigung `raids.manage`, die genauso wie
|
||||||
|
`raids.view` nachträglich unter Berechtigungsgruppen angelegt/zugewiesen werden muss, falls dein
|
||||||
|
Account vor diesem Update installiert wurde.
|
||||||
|
|
||||||
## Berechtigungskonzept
|
## Berechtigungskonzept
|
||||||
|
|
||||||
- **Berechtigungen** sind einzelne Schlüssel wie `users.view`, `roles.manage`, `dashboard.view`.
|
- **Berechtigungen** sind einzelne Schlüssel wie `users.view`, `roles.manage`, `dashboard.view`.
|
||||||
|
|
|
||||||
|
|
@ -403,6 +403,24 @@ input:focus, select:focus, textarea:focus {
|
||||||
|
|
||||||
.panel-archived { opacity: 0.7; }
|
.panel-archived { opacity: 0.7; }
|
||||||
|
|
||||||
|
.raid-signup-note {
|
||||||
|
margin-top: 1rem;
|
||||||
|
padding-top: 1rem;
|
||||||
|
border-top: 1px solid var(--border);
|
||||||
|
}
|
||||||
|
|
||||||
|
.raid-signup-form {
|
||||||
|
flex-direction: row;
|
||||||
|
flex-wrap: wrap;
|
||||||
|
align-items: center;
|
||||||
|
gap: 0.5rem;
|
||||||
|
margin-top: 1rem;
|
||||||
|
padding-top: 1rem;
|
||||||
|
border-top: 1px solid var(--border);
|
||||||
|
}
|
||||||
|
|
||||||
|
.raid-signup-form select { width: auto; }
|
||||||
|
|
||||||
.filter-tabs {
|
.filter-tabs {
|
||||||
display: flex;
|
display: flex;
|
||||||
flex-wrap: wrap;
|
flex-wrap: wrap;
|
||||||
|
|
|
||||||
|
|
@ -17,4 +17,9 @@ return [
|
||||||
'url' => 'https://api.maaxxs-coding.de/api/raids',
|
'url' => 'https://api.maaxxs-coding.de/api/raids',
|
||||||
'key' => 'dein-api-key',
|
'key' => 'dein-api-key',
|
||||||
],
|
],
|
||||||
|
'discord' => [
|
||||||
|
'client_id' => 'deine-discord-client-id',
|
||||||
|
'client_secret' => 'dein-discord-client-secret',
|
||||||
|
'redirect_uri' => 'https://lumina.maaxxs-coding.de/discord_callback.php',
|
||||||
|
],
|
||||||
];
|
];
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,20 @@ include __DIR__ . '/includes/header.php';
|
||||||
<p class="text-muted">Dies ist dein Mitgliederbereich. Die eigentliche Gilden-Homepage folgt hier später.</p>
|
<p class="text-muted">Dies ist dein Mitgliederbereich. Die eigentliche Gilden-Homepage folgt hier später.</p>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<div class="panel">
|
||||||
|
<h2>Discord-Verknüpfung</h2>
|
||||||
|
<?php if (!empty($user['discord_id'])): ?>
|
||||||
|
<p>Verknüpft als <strong><?= e((string) $user['discord_username']) ?></strong></p>
|
||||||
|
<form method="post" action="/discord_unlink.php" class="inline-form" onsubmit="return confirm('Discord-Verknüpfung wirklich entfernen?');">
|
||||||
|
<?= csrf_field() ?>
|
||||||
|
<button type="submit" class="button-secondary">Verknüpfung entfernen</button>
|
||||||
|
</form>
|
||||||
|
<?php else: ?>
|
||||||
|
<p class="text-muted">Verknüpfe deinen Discord-Account, um dich über die Website zu Raids anmelden zu können.</p>
|
||||||
|
<a class="button" href="/discord_login.php">Mit Discord verknüpfen</a>
|
||||||
|
<?php endif; ?>
|
||||||
|
</div>
|
||||||
|
|
||||||
<?php if ($canViewRaids): ?>
|
<?php if ($canViewRaids): ?>
|
||||||
<div class="panel">
|
<div class="panel">
|
||||||
<div class="card-grid">
|
<div class="card-grid">
|
||||||
|
|
|
||||||
35
discord_callback.php
Normal file
35
discord_callback.php
Normal file
|
|
@ -0,0 +1,35 @@
|
||||||
|
<?php
|
||||||
|
require_once __DIR__ . '/includes/bootstrap.php';
|
||||||
|
require_once __DIR__ . '/includes/discord.php';
|
||||||
|
require_login();
|
||||||
|
|
||||||
|
$state = (string) ($_GET['state'] ?? '');
|
||||||
|
$code = (string) ($_GET['code'] ?? '');
|
||||||
|
$expectedState = (string) ($_SESSION['discord_oauth_state'] ?? '');
|
||||||
|
unset($_SESSION['discord_oauth_state']);
|
||||||
|
|
||||||
|
if (!empty($_GET['error'])) {
|
||||||
|
set_flash('error', 'Discord-Verknüpfung wurde abgebrochen.');
|
||||||
|
redirect('/dashboard.php');
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($code === '' || $state === '' || $expectedState === '' || !hash_equals($expectedState, $state)) {
|
||||||
|
set_flash('error', 'Ungültige Antwort von Discord. Bitte erneut versuchen.');
|
||||||
|
redirect('/dashboard.php');
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
$discordUser = discord_exchange_code($code);
|
||||||
|
$user = current_user();
|
||||||
|
|
||||||
|
$stmt = db()->prepare('UPDATE users SET discord_id = ?, discord_username = ? WHERE id = ?');
|
||||||
|
$stmt->execute([$discordUser['id'], $discordUser['username'], $user['id']]);
|
||||||
|
|
||||||
|
set_flash('success', 'Discord-Account "' . $discordUser['username'] . '" wurde verknüpft.');
|
||||||
|
} catch (PDOException $e) {
|
||||||
|
set_flash('error', 'Dieser Discord-Account ist bereits mit einem anderen Benutzer verknüpft.');
|
||||||
|
} catch (Throwable $e) {
|
||||||
|
set_flash('error', 'Discord-Verknüpfung fehlgeschlagen: ' . $e->getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
redirect('/dashboard.php');
|
||||||
13
discord_login.php
Normal file
13
discord_login.php
Normal file
|
|
@ -0,0 +1,13 @@
|
||||||
|
<?php
|
||||||
|
require_once __DIR__ . '/includes/bootstrap.php';
|
||||||
|
require_once __DIR__ . '/includes/discord.php';
|
||||||
|
require_login();
|
||||||
|
|
||||||
|
try {
|
||||||
|
$state = bin2hex(random_bytes(16));
|
||||||
|
$_SESSION['discord_oauth_state'] = $state;
|
||||||
|
redirect(discord_authorize_url($state));
|
||||||
|
} catch (Throwable $e) {
|
||||||
|
set_flash('error', 'Discord-Login konnte nicht gestartet werden: ' . $e->getMessage());
|
||||||
|
redirect('/dashboard.php');
|
||||||
|
}
|
||||||
14
discord_unlink.php
Normal file
14
discord_unlink.php
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
<?php
|
||||||
|
require_once __DIR__ . '/includes/bootstrap.php';
|
||||||
|
require_login();
|
||||||
|
|
||||||
|
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
||||||
|
redirect('/dashboard.php');
|
||||||
|
}
|
||||||
|
verify_csrf();
|
||||||
|
|
||||||
|
$user = current_user();
|
||||||
|
db()->prepare('UPDATE users SET discord_id = NULL, discord_username = NULL WHERE id = ?')->execute([$user['id']]);
|
||||||
|
|
||||||
|
set_flash('success', 'Discord-Verknüpfung wurde entfernt.');
|
||||||
|
redirect('/dashboard.php');
|
||||||
|
|
@ -15,7 +15,7 @@ function current_user(): ?array
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
$stmt = db()->prepare('SELECT id, username, display_name, status, must_change_password FROM users WHERE id = ?');
|
$stmt = db()->prepare('SELECT id, username, display_name, status, must_change_password, discord_id, discord_username FROM users WHERE id = ?');
|
||||||
$stmt->execute([$_SESSION['user_id']]);
|
$stmt->execute([$_SESSION['user_id']]);
|
||||||
$row = $stmt->fetch();
|
$row = $stmt->fetch();
|
||||||
|
|
||||||
|
|
|
||||||
108
includes/discord.php
Normal file
108
includes/discord.php
Normal file
|
|
@ -0,0 +1,108 @@
|
||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array{client_id: string, client_secret: string, redirect_uri: string}
|
||||||
|
* @throws RuntimeException
|
||||||
|
*/
|
||||||
|
function discord_config(): array
|
||||||
|
{
|
||||||
|
$configPath = __DIR__ . '/../config.php';
|
||||||
|
if (!file_exists($configPath)) {
|
||||||
|
throw new RuntimeException('Konfigurationsdatei fehlt.');
|
||||||
|
}
|
||||||
|
|
||||||
|
$config = require $configPath;
|
||||||
|
$discord = $config['discord'] ?? [];
|
||||||
|
|
||||||
|
if (empty($discord['client_id']) || empty($discord['client_secret']) || empty($discord['redirect_uri'])) {
|
||||||
|
throw new RuntimeException('Discord-Login ist nicht konfiguriert (config.php: discord.client_id / client_secret / redirect_uri fehlt).');
|
||||||
|
}
|
||||||
|
|
||||||
|
return $discord;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @throws RuntimeException */
|
||||||
|
function discord_authorize_url(string $state): string
|
||||||
|
{
|
||||||
|
$config = discord_config();
|
||||||
|
|
||||||
|
$params = [
|
||||||
|
'client_id' => $config['client_id'],
|
||||||
|
'redirect_uri' => $config['redirect_uri'],
|
||||||
|
'response_type' => 'code',
|
||||||
|
'scope' => 'identify',
|
||||||
|
'state' => $state,
|
||||||
|
'prompt' => 'consent',
|
||||||
|
];
|
||||||
|
|
||||||
|
return 'https://discord.com/oauth2/authorize?' . http_build_query($params);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tauscht den OAuth-Code gegen ein Access-Token und liest damit die
|
||||||
|
* Discord-Identitaet (ID + Benutzername) aus.
|
||||||
|
*
|
||||||
|
* @return array{id: string, username: string}
|
||||||
|
* @throws RuntimeException
|
||||||
|
*/
|
||||||
|
function discord_exchange_code(string $code): array
|
||||||
|
{
|
||||||
|
if (!function_exists('curl_init')) {
|
||||||
|
throw new RuntimeException('Die PHP-cURL-Erweiterung ist auf diesem Server nicht aktiviert.');
|
||||||
|
}
|
||||||
|
|
||||||
|
$config = discord_config();
|
||||||
|
|
||||||
|
$ch = curl_init('https://discord.com/api/oauth2/token');
|
||||||
|
curl_setopt_array($ch, [
|
||||||
|
CURLOPT_RETURNTRANSFER => true,
|
||||||
|
CURLOPT_POST => true,
|
||||||
|
CURLOPT_POSTFIELDS => http_build_query([
|
||||||
|
'client_id' => $config['client_id'],
|
||||||
|
'client_secret' => $config['client_secret'],
|
||||||
|
'grant_type' => 'authorization_code',
|
||||||
|
'code' => $code,
|
||||||
|
'redirect_uri' => $config['redirect_uri'],
|
||||||
|
]),
|
||||||
|
CURLOPT_HTTPHEADER => ['Content-Type: application/x-www-form-urlencoded'],
|
||||||
|
CURLOPT_TIMEOUT => 10,
|
||||||
|
]);
|
||||||
|
$response = curl_exec($ch);
|
||||||
|
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||||
|
$curlError = curl_error($ch);
|
||||||
|
curl_close($ch);
|
||||||
|
|
||||||
|
if ($response === false) {
|
||||||
|
throw new RuntimeException('Verbindung zu Discord fehlgeschlagen: ' . $curlError);
|
||||||
|
}
|
||||||
|
|
||||||
|
$tokenData = json_decode($response, true);
|
||||||
|
if ($httpCode !== 200 || !is_array($tokenData) || empty($tokenData['access_token'])) {
|
||||||
|
throw new RuntimeException('Discord hat den Login-Code nicht akzeptiert.');
|
||||||
|
}
|
||||||
|
|
||||||
|
$ch2 = curl_init('https://discord.com/api/users/@me');
|
||||||
|
curl_setopt_array($ch2, [
|
||||||
|
CURLOPT_RETURNTRANSFER => true,
|
||||||
|
CURLOPT_HTTPHEADER => ['Authorization: Bearer ' . $tokenData['access_token']],
|
||||||
|
CURLOPT_TIMEOUT => 10,
|
||||||
|
]);
|
||||||
|
$userResponse = curl_exec($ch2);
|
||||||
|
$userHttpCode = curl_getinfo($ch2, CURLINFO_HTTP_CODE);
|
||||||
|
curl_close($ch2);
|
||||||
|
|
||||||
|
if ($userResponse === false || $userHttpCode !== 200) {
|
||||||
|
throw new RuntimeException('Discord-Benutzerdaten konnten nicht abgerufen werden.');
|
||||||
|
}
|
||||||
|
|
||||||
|
$userData = json_decode($userResponse, true);
|
||||||
|
if (!is_array($userData) || empty($userData['id']) || !isset($userData['username'])) {
|
||||||
|
throw new RuntimeException('Unerwartete Antwort von Discord.');
|
||||||
|
}
|
||||||
|
|
||||||
|
return [
|
||||||
|
'id' => (string) $userData['id'],
|
||||||
|
'username' => (string) $userData['username'],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
@ -13,6 +13,7 @@ const CORE_PERMISSIONS = [
|
||||||
['key' => 'users.delete', 'description' => 'Benutzer löschen'],
|
['key' => 'users.delete', 'description' => 'Benutzer löschen'],
|
||||||
['key' => 'roles.manage', 'description' => 'Berechtigungsgruppen verwalten'],
|
['key' => 'roles.manage', 'description' => 'Berechtigungsgruppen verwalten'],
|
||||||
['key' => 'raids.view', 'description' => 'Raids einsehen'],
|
['key' => 'raids.view', 'description' => 'Raids einsehen'],
|
||||||
|
['key' => 'raids.manage', 'description' => 'Raids archivieren/wiederherstellen'],
|
||||||
];
|
];
|
||||||
|
|
||||||
function user_has_permission(string $key): bool
|
function user_has_permission(string $key): bool
|
||||||
|
|
|
||||||
|
|
@ -4,15 +4,11 @@ declare(strict_types=1);
|
||||||
const RAID_STATUSES = ['active', 'archived', 'all'];
|
const RAID_STATUSES = ['active', 'archived', 'all'];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array<int, array<string, mixed>>
|
* @return array<string, mixed>
|
||||||
* @throws RuntimeException
|
* @throws RuntimeException
|
||||||
*/
|
*/
|
||||||
function fetch_raids(string $status = 'active'): array
|
function raids_api_config(): array
|
||||||
{
|
{
|
||||||
if (!in_array($status, RAID_STATUSES, true)) {
|
|
||||||
$status = 'active';
|
|
||||||
}
|
|
||||||
|
|
||||||
$configPath = __DIR__ . '/../config.php';
|
$configPath = __DIR__ . '/../config.php';
|
||||||
if (!file_exists($configPath)) {
|
if (!file_exists($configPath)) {
|
||||||
throw new RuntimeException('Konfigurationsdatei fehlt.');
|
throw new RuntimeException('Konfigurationsdatei fehlt.');
|
||||||
|
|
@ -26,20 +22,48 @@ function fetch_raids(string $status = 'active'): array
|
||||||
throw new RuntimeException('Raid-API ist nicht konfiguriert (config.php: raids_api.url / raids_api.key fehlt).');
|
throw new RuntimeException('Raid-API ist nicht konfiguriert (config.php: raids_api.url / raids_api.key fehlt).');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return ['url' => rtrim($apiUrl, '/'), 'key' => $apiKey];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fuehrt einen Request gegen die Raid-API aus und liefert die dekodierte
|
||||||
|
* JSON-Antwort zurueck.
|
||||||
|
*
|
||||||
|
* @param string $path Wird an die Basis-URL angehaengt, z.B. "/123/signup".
|
||||||
|
* @param array<string, mixed>|null $jsonBody Wird bei POST/DELETE als JSON-Body mitgeschickt.
|
||||||
|
* @return array<string, mixed>
|
||||||
|
* @throws RuntimeException
|
||||||
|
*/
|
||||||
|
function raids_api_request(string $method, string $path = '', array $queryParams = [], ?array $jsonBody = null): array
|
||||||
|
{
|
||||||
if (!function_exists('curl_init')) {
|
if (!function_exists('curl_init')) {
|
||||||
throw new RuntimeException('Die PHP-cURL-Erweiterung ist auf diesem Server nicht aktiviert.');
|
throw new RuntimeException('Die PHP-cURL-Erweiterung ist auf diesem Server nicht aktiviert.');
|
||||||
}
|
}
|
||||||
|
|
||||||
$separator = str_contains($apiUrl, '?') ? '&' : '?';
|
$config = raids_api_config();
|
||||||
$requestUrl = $apiUrl . $separator . 'status=' . urlencode($status);
|
$url = $config['url'] . $path;
|
||||||
|
if (!empty($queryParams)) {
|
||||||
|
$url .= '?' . http_build_query($queryParams);
|
||||||
|
}
|
||||||
|
|
||||||
$ch = curl_init($requestUrl);
|
$headers = ['X-API-Key: ' . $config['key']];
|
||||||
curl_setopt_array($ch, [
|
|
||||||
|
$ch = curl_init($url);
|
||||||
|
$options = [
|
||||||
CURLOPT_RETURNTRANSFER => true,
|
CURLOPT_RETURNTRANSFER => true,
|
||||||
CURLOPT_HTTPHEADER => ['X-API-Key: ' . $apiKey],
|
CURLOPT_CUSTOMREQUEST => $method,
|
||||||
CURLOPT_TIMEOUT => 10,
|
CURLOPT_TIMEOUT => 10,
|
||||||
CURLOPT_SSL_VERIFYPEER => true,
|
CURLOPT_SSL_VERIFYPEER => true,
|
||||||
]);
|
];
|
||||||
|
|
||||||
|
if ($jsonBody !== null) {
|
||||||
|
$headers[] = 'Content-Type: application/json';
|
||||||
|
$options[CURLOPT_POSTFIELDS] = json_encode($jsonBody, JSON_UNESCAPED_UNICODE);
|
||||||
|
}
|
||||||
|
|
||||||
|
$options[CURLOPT_HTTPHEADER] = $headers;
|
||||||
|
curl_setopt_array($ch, $options);
|
||||||
|
|
||||||
$response = curl_exec($ch);
|
$response = curl_exec($ch);
|
||||||
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||||
$curlError = curl_error($ch);
|
$curlError = curl_error($ch);
|
||||||
|
|
@ -48,14 +72,69 @@ function fetch_raids(string $status = 'active'): array
|
||||||
if ($response === false) {
|
if ($response === false) {
|
||||||
throw new RuntimeException('Verbindung zur Raid-API fehlgeschlagen: ' . $curlError);
|
throw new RuntimeException('Verbindung zur Raid-API fehlgeschlagen: ' . $curlError);
|
||||||
}
|
}
|
||||||
if ($httpCode !== 200) {
|
|
||||||
throw new RuntimeException('Raid-API antwortete mit Statuscode ' . $httpCode . '.');
|
|
||||||
}
|
|
||||||
|
|
||||||
$data = json_decode($response, true);
|
$data = json_decode($response, true);
|
||||||
if (!is_array($data) || ($data['status'] ?? '') !== 'success' || !isset($data['raids']) || !is_array($data['raids'])) {
|
|
||||||
|
if ($httpCode < 200 || $httpCode >= 300) {
|
||||||
|
$detail = is_array($data) ? ($data['detail'] ?? '') : '';
|
||||||
|
$message = 'Raid-API antwortete mit Statuscode ' . $httpCode . '.';
|
||||||
|
if ($detail !== '') {
|
||||||
|
$message .= ' (' . $detail . ')';
|
||||||
|
}
|
||||||
|
throw new RuntimeException($message);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!is_array($data)) {
|
||||||
|
throw new RuntimeException('Unerwartetes Antwortformat der Raid-API.');
|
||||||
|
}
|
||||||
|
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array<int, array<string, mixed>>
|
||||||
|
* @throws RuntimeException
|
||||||
|
*/
|
||||||
|
function fetch_raids(string $status = 'active'): array
|
||||||
|
{
|
||||||
|
if (!in_array($status, RAID_STATUSES, true)) {
|
||||||
|
$status = 'active';
|
||||||
|
}
|
||||||
|
|
||||||
|
$data = raids_api_request('GET', '', ['status' => $status]);
|
||||||
|
|
||||||
|
if (($data['status'] ?? '') !== 'success' || !isset($data['raids']) || !is_array($data['raids'])) {
|
||||||
throw new RuntimeException('Unerwartetes Antwortformat der Raid-API.');
|
throw new RuntimeException('Unerwartetes Antwortformat der Raid-API.');
|
||||||
}
|
}
|
||||||
|
|
||||||
return $data['raids'];
|
return $data['raids'];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @throws RuntimeException */
|
||||||
|
function archive_raid(int $raidId): void
|
||||||
|
{
|
||||||
|
raids_api_request('DELETE', '/' . $raidId);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @throws RuntimeException */
|
||||||
|
function restore_raid(int $raidId): void
|
||||||
|
{
|
||||||
|
raids_api_request('POST', '/' . $raidId . '/restore');
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @throws RuntimeException */
|
||||||
|
function raid_signup(int $raidId, int $discordId, string $discordName, string $className, int $group): void
|
||||||
|
{
|
||||||
|
raids_api_request('POST', '/' . $raidId . '/signup', [], [
|
||||||
|
'user_id' => $discordId,
|
||||||
|
'name' => $discordName,
|
||||||
|
'class' => $className,
|
||||||
|
'group' => $group,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @throws RuntimeException */
|
||||||
|
function raid_unsignup(int $raidId, int $discordId): void
|
||||||
|
{
|
||||||
|
raids_api_request('DELETE', '/' . $raidId . '/signup/' . $discordId);
|
||||||
|
}
|
||||||
|
|
|
||||||
29
raid_archive.php
Normal file
29
raid_archive.php
Normal file
|
|
@ -0,0 +1,29 @@
|
||||||
|
<?php
|
||||||
|
require_once __DIR__ . '/includes/bootstrap.php';
|
||||||
|
require_once __DIR__ . '/includes/raids_api.php';
|
||||||
|
require_permission('raids.manage');
|
||||||
|
|
||||||
|
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
||||||
|
redirect('/raids.php');
|
||||||
|
}
|
||||||
|
verify_csrf();
|
||||||
|
|
||||||
|
$status = (string) ($_POST['status'] ?? 'active');
|
||||||
|
if (!in_array($status, RAID_STATUSES, true)) {
|
||||||
|
$status = 'active';
|
||||||
|
}
|
||||||
|
$redirectTo = '/raids.php?status=' . urlencode($status);
|
||||||
|
|
||||||
|
$raidId = (int) ($_POST['raid_id'] ?? 0);
|
||||||
|
if ($raidId <= 0) {
|
||||||
|
redirect($redirectTo);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
archive_raid($raidId);
|
||||||
|
set_flash('success', 'Raid wurde archiviert.');
|
||||||
|
} catch (Throwable $e) {
|
||||||
|
set_flash('error', 'Archivieren fehlgeschlagen: ' . $e->getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
redirect($redirectTo);
|
||||||
29
raid_restore.php
Normal file
29
raid_restore.php
Normal file
|
|
@ -0,0 +1,29 @@
|
||||||
|
<?php
|
||||||
|
require_once __DIR__ . '/includes/bootstrap.php';
|
||||||
|
require_once __DIR__ . '/includes/raids_api.php';
|
||||||
|
require_permission('raids.manage');
|
||||||
|
|
||||||
|
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
||||||
|
redirect('/raids.php');
|
||||||
|
}
|
||||||
|
verify_csrf();
|
||||||
|
|
||||||
|
$status = (string) ($_POST['status'] ?? 'active');
|
||||||
|
if (!in_array($status, RAID_STATUSES, true)) {
|
||||||
|
$status = 'active';
|
||||||
|
}
|
||||||
|
$redirectTo = '/raids.php?status=' . urlencode($status);
|
||||||
|
|
||||||
|
$raidId = (int) ($_POST['raid_id'] ?? 0);
|
||||||
|
if ($raidId <= 0) {
|
||||||
|
redirect($redirectTo);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
restore_raid($raidId);
|
||||||
|
set_flash('success', 'Raid wurde wiederhergestellt.');
|
||||||
|
} catch (Throwable $e) {
|
||||||
|
set_flash('error', 'Wiederherstellen fehlgeschlagen: ' . $e->getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
redirect($redirectTo);
|
||||||
39
raid_signup.php
Normal file
39
raid_signup.php
Normal file
|
|
@ -0,0 +1,39 @@
|
||||||
|
<?php
|
||||||
|
require_once __DIR__ . '/includes/bootstrap.php';
|
||||||
|
require_once __DIR__ . '/includes/raids_api.php';
|
||||||
|
require_permission('raids.view');
|
||||||
|
|
||||||
|
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
||||||
|
redirect('/raids.php');
|
||||||
|
}
|
||||||
|
verify_csrf();
|
||||||
|
|
||||||
|
$status = (string) ($_POST['status'] ?? 'active');
|
||||||
|
if (!in_array($status, RAID_STATUSES, true)) {
|
||||||
|
$status = 'active';
|
||||||
|
}
|
||||||
|
$redirectTo = '/raids.php?status=' . urlencode($status);
|
||||||
|
|
||||||
|
$user = current_user();
|
||||||
|
if (empty($user['discord_id'])) {
|
||||||
|
set_flash('error', 'Bitte verknüpfe zuerst deinen Discord-Account.');
|
||||||
|
redirect($redirectTo);
|
||||||
|
}
|
||||||
|
|
||||||
|
$raidId = (int) ($_POST['raid_id'] ?? 0);
|
||||||
|
$class = trim((string) ($_POST['class'] ?? ''));
|
||||||
|
$group = (int) ($_POST['group'] ?? 0);
|
||||||
|
|
||||||
|
if ($raidId <= 0 || $class === '' || $group <= 0) {
|
||||||
|
set_flash('error', 'Bitte Klasse und Gruppe auswählen.');
|
||||||
|
redirect($redirectTo);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
raid_signup($raidId, (int) $user['discord_id'], (string) $user['discord_username'], $class, $group);
|
||||||
|
set_flash('success', 'Anmeldung gespeichert.');
|
||||||
|
} catch (Throwable $e) {
|
||||||
|
set_flash('error', 'Anmeldung fehlgeschlagen: ' . $e->getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
redirect($redirectTo);
|
||||||
34
raid_unsignup.php
Normal file
34
raid_unsignup.php
Normal file
|
|
@ -0,0 +1,34 @@
|
||||||
|
<?php
|
||||||
|
require_once __DIR__ . '/includes/bootstrap.php';
|
||||||
|
require_once __DIR__ . '/includes/raids_api.php';
|
||||||
|
require_permission('raids.view');
|
||||||
|
|
||||||
|
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
||||||
|
redirect('/raids.php');
|
||||||
|
}
|
||||||
|
verify_csrf();
|
||||||
|
|
||||||
|
$status = (string) ($_POST['status'] ?? 'active');
|
||||||
|
if (!in_array($status, RAID_STATUSES, true)) {
|
||||||
|
$status = 'active';
|
||||||
|
}
|
||||||
|
$redirectTo = '/raids.php?status=' . urlencode($status);
|
||||||
|
|
||||||
|
$user = current_user();
|
||||||
|
if (empty($user['discord_id'])) {
|
||||||
|
redirect($redirectTo);
|
||||||
|
}
|
||||||
|
|
||||||
|
$raidId = (int) ($_POST['raid_id'] ?? 0);
|
||||||
|
if ($raidId <= 0) {
|
||||||
|
redirect($redirectTo);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
raid_unsignup($raidId, (int) $user['discord_id']);
|
||||||
|
set_flash('success', 'Abmeldung gespeichert.');
|
||||||
|
} catch (Throwable $e) {
|
||||||
|
set_flash('error', 'Abmeldung fehlgeschlagen: ' . $e->getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
redirect($redirectTo);
|
||||||
73
raids.php
73
raids.php
|
|
@ -3,6 +3,12 @@ require_once __DIR__ . '/includes/bootstrap.php';
|
||||||
require_once __DIR__ . '/includes/raids_api.php';
|
require_once __DIR__ . '/includes/raids_api.php';
|
||||||
require_permission('raids.view');
|
require_permission('raids.view');
|
||||||
|
|
||||||
|
$currentUser = current_user();
|
||||||
|
$currentDiscordId = $currentUser['discord_id'] ?? null;
|
||||||
|
$canManageRaids = user_has_permission('raids.manage');
|
||||||
|
|
||||||
|
$classOptions = ['Gladiator', 'Templer', 'Assassine', 'Jäger', 'Magier', 'Geisterbeschwörer', 'Kleriker', 'Barde'];
|
||||||
|
|
||||||
$status = (string) ($_GET['status'] ?? 'active');
|
$status = (string) ($_GET['status'] ?? 'active');
|
||||||
if (!in_array($status, RAID_STATUSES, true)) {
|
if (!in_array($status, RAID_STATUSES, true)) {
|
||||||
$status = 'active';
|
$status = 'active';
|
||||||
|
|
@ -67,9 +73,13 @@ include __DIR__ . '/includes/header.php';
|
||||||
$groupNames = array_values(array_filter(array_map('trim', explode(',', (string) ($raid['groups'] ?? '')))));
|
$groupNames = array_values(array_filter(array_map('trim', explode(',', (string) ($raid['groups'] ?? '')))));
|
||||||
|
|
||||||
$signupsByGroup = [];
|
$signupsByGroup = [];
|
||||||
|
$mySignup = null;
|
||||||
foreach ($signups as $signup) {
|
foreach ($signups as $signup) {
|
||||||
$groupNumber = (int) ($signup['group'] ?? 0);
|
$groupNumber = (int) ($signup['group'] ?? 0);
|
||||||
$signupsByGroup[$groupNumber][] = $signup;
|
$signupsByGroup[$groupNumber][] = $signup;
|
||||||
|
if ($currentDiscordId && (string) ($signup['user_id'] ?? '') === (string) $currentDiscordId) {
|
||||||
|
$mySignup = $signup;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
?>
|
?>
|
||||||
<div class="panel<?= $isArchived ? ' panel-archived' : '' ?>">
|
<div class="panel<?= $isArchived ? ' panel-archived' : '' ?>">
|
||||||
|
|
@ -80,6 +90,23 @@ include __DIR__ . '/includes/header.php';
|
||||||
<span class="badge badge-disabled">Archiviert<?= $archivedAt ? ' (' . e((string) $archivedAt) . ')' : '' ?></span>
|
<span class="badge badge-disabled">Archiviert<?= $archivedAt ? ' (' . e((string) $archivedAt) . ')' : '' ?></span>
|
||||||
<?php endif; ?>
|
<?php endif; ?>
|
||||||
<span class="badge"><?= count($signups) ?> / <?= $maxSlots ?> angemeldet</span>
|
<span class="badge"><?= count($signups) ?> / <?= $maxSlots ?> angemeldet</span>
|
||||||
|
<?php if ($canManageRaids): ?>
|
||||||
|
<?php if ($isArchived): ?>
|
||||||
|
<form method="post" action="/raid_restore.php" class="inline-form">
|
||||||
|
<?= csrf_field() ?>
|
||||||
|
<input type="hidden" name="raid_id" value="<?= (int) ($raid['raid_id'] ?? 0) ?>">
|
||||||
|
<input type="hidden" name="status" value="<?= e($status) ?>">
|
||||||
|
<button type="submit" class="text-button">Wiederherstellen</button>
|
||||||
|
</form>
|
||||||
|
<?php else: ?>
|
||||||
|
<form method="post" action="/raid_archive.php" class="inline-form" onsubmit="return confirm('Diesen Raid wirklich archivieren?');">
|
||||||
|
<?= csrf_field() ?>
|
||||||
|
<input type="hidden" name="raid_id" value="<?= (int) ($raid['raid_id'] ?? 0) ?>">
|
||||||
|
<input type="hidden" name="status" value="<?= e($status) ?>">
|
||||||
|
<button type="submit" class="text-button">Archivieren</button>
|
||||||
|
</form>
|
||||||
|
<?php endif; ?>
|
||||||
|
<?php endif; ?>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<p class="text-muted"><?= e($dateLabel) ?></p>
|
<p class="text-muted"><?= e($dateLabel) ?></p>
|
||||||
|
|
@ -107,10 +134,12 @@ include __DIR__ . '/includes/header.php';
|
||||||
?>
|
?>
|
||||||
<li>
|
<li>
|
||||||
<?php if ($classIconUrl): ?>
|
<?php if ($classIconUrl): ?>
|
||||||
<img src="<?= e($classIconUrl) ?>" alt="" title="<?= e($memberClass) ?>" class="class-icon">
|
<img src="<?= e($classIconUrl) ?>" alt="<?= e($memberClass) ?>" title="<?= e($memberClass) ?>" class="class-icon">
|
||||||
<?php endif; ?>
|
<?php endif; ?>
|
||||||
<?= e((string) ($member['name'] ?? '?')) ?>
|
<?= e((string) ($member['name'] ?? '?')) ?>
|
||||||
<span class="text-muted">(<?= e($memberClass) ?>)</span>
|
<?php if (!$classIconUrl && $memberClass !== ''): ?>
|
||||||
|
<span class="text-muted">(<?= e($memberClass) ?>)</span>
|
||||||
|
<?php endif; ?>
|
||||||
</li>
|
</li>
|
||||||
<?php endforeach; ?>
|
<?php endforeach; ?>
|
||||||
</ul>
|
</ul>
|
||||||
|
|
@ -119,6 +148,46 @@ include __DIR__ . '/includes/header.php';
|
||||||
<?php endforeach; ?>
|
<?php endforeach; ?>
|
||||||
</div>
|
</div>
|
||||||
<?php endif; ?>
|
<?php endif; ?>
|
||||||
|
|
||||||
|
<?php if ($isArchived): ?>
|
||||||
|
<p class="text-muted raid-signup-note">Dieser Raid ist archiviert – Anmeldungen sind geschlossen.</p>
|
||||||
|
<?php elseif (!$currentDiscordId): ?>
|
||||||
|
<p class="text-muted raid-signup-note">Verknüpfe deinen <a href="/dashboard.php">Discord-Account</a>, um dich anzumelden.</p>
|
||||||
|
<?php else: ?>
|
||||||
|
<?php if ($mySignup): ?>
|
||||||
|
<div class="raid-signup-note">
|
||||||
|
<p>
|
||||||
|
Du bist angemeldet als <strong><?= e((string) ($mySignup['class'] ?? '')) ?></strong>
|
||||||
|
in Gruppe <strong><?= e($groupNames[((int) ($mySignup['group'] ?? 1)) - 1] ?? ('#' . ($mySignup['group'] ?? '?'))) ?></strong>.
|
||||||
|
</p>
|
||||||
|
<form method="post" action="/raid_unsignup.php" class="inline-form">
|
||||||
|
<?= csrf_field() ?>
|
||||||
|
<input type="hidden" name="raid_id" value="<?= (int) ($raid['raid_id'] ?? 0) ?>">
|
||||||
|
<input type="hidden" name="status" value="<?= e($status) ?>">
|
||||||
|
<button type="submit" class="button button-secondary">Abmelden</button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
<?php endif; ?>
|
||||||
|
<?php if (!empty($groupNames)): ?>
|
||||||
|
<form method="post" action="/raid_signup.php" class="raid-signup-form">
|
||||||
|
<?= csrf_field() ?>
|
||||||
|
<input type="hidden" name="raid_id" value="<?= (int) ($raid['raid_id'] ?? 0) ?>">
|
||||||
|
<input type="hidden" name="status" value="<?= e($status) ?>">
|
||||||
|
<select name="class" required>
|
||||||
|
<option value="">Klasse wählen…</option>
|
||||||
|
<?php foreach ($classOptions as $classOption): ?>
|
||||||
|
<option value="<?= e($classOption) ?>" <?= ($mySignup && ($mySignup['class'] ?? '') === $classOption) ? 'selected' : '' ?>><?= e($classOption) ?></option>
|
||||||
|
<?php endforeach; ?>
|
||||||
|
</select>
|
||||||
|
<select name="group" required>
|
||||||
|
<?php foreach ($groupNames as $index => $groupName): ?>
|
||||||
|
<option value="<?= $index + 1 ?>" <?= ($mySignup && (int) ($mySignup['group'] ?? 0) === $index + 1) ? 'selected' : '' ?>><?= e($groupName) ?></option>
|
||||||
|
<?php endforeach; ?>
|
||||||
|
</select>
|
||||||
|
<button type="submit"><?= $mySignup ? 'Aktualisieren' : 'Anmelden' ?></button>
|
||||||
|
</form>
|
||||||
|
<?php endif; ?>
|
||||||
|
<?php endif; ?>
|
||||||
</div>
|
</div>
|
||||||
<?php endforeach; ?>
|
<?php endforeach; ?>
|
||||||
<?php endif; ?>
|
<?php endif; ?>
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,8 @@ CREATE TABLE IF NOT EXISTS users (
|
||||||
display_name VARCHAR(100) NOT NULL DEFAULT '',
|
display_name VARCHAR(100) NOT NULL DEFAULT '',
|
||||||
status ENUM('active', 'disabled') NOT NULL DEFAULT 'active',
|
status ENUM('active', 'disabled') NOT NULL DEFAULT 'active',
|
||||||
must_change_password TINYINT(1) NOT NULL DEFAULT 0,
|
must_change_password TINYINT(1) NOT NULL DEFAULT 0,
|
||||||
|
discord_id BIGINT UNSIGNED NULL UNIQUE,
|
||||||
|
discord_username VARCHAR(100) NULL,
|
||||||
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
last_login DATETIME NULL
|
last_login DATETIME NULL
|
||||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue