diff --git a/discord_callback.php b/discord_callback.php
new file mode 100644
index 0000000..8a9fd11
--- /dev/null
+++ b/discord_callback.php
@@ -0,0 +1,35 @@
+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');
diff --git a/discord_login.php b/discord_login.php
new file mode 100644
index 0000000..2d6071d
--- /dev/null
+++ b/discord_login.php
@@ -0,0 +1,13 @@
+getMessage());
+ redirect('/dashboard.php');
+}
diff --git a/discord_unlink.php b/discord_unlink.php
new file mode 100644
index 0000000..29c09cc
--- /dev/null
+++ b/discord_unlink.php
@@ -0,0 +1,14 @@
+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');
diff --git a/includes/auth.php b/includes/auth.php
index ef21ecd..e9bb02f 100644
--- a/includes/auth.php
+++ b/includes/auth.php
@@ -15,7 +15,7 @@ function current_user(): ?array
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']]);
$row = $stmt->fetch();
diff --git a/includes/discord.php b/includes/discord.php
new file mode 100644
index 0000000..fcb6e68
--- /dev/null
+++ b/includes/discord.php
@@ -0,0 +1,108 @@
+ $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'],
+ ];
+}
diff --git a/includes/permissions.php b/includes/permissions.php
index fe5986a..050eaca 100644
--- a/includes/permissions.php
+++ b/includes/permissions.php
@@ -13,6 +13,7 @@ const CORE_PERMISSIONS = [
['key' => 'users.delete', 'description' => 'Benutzer löschen'],
['key' => 'roles.manage', 'description' => 'Berechtigungsgruppen verwalten'],
['key' => 'raids.view', 'description' => 'Raids einsehen'],
+ ['key' => 'raids.manage', 'description' => 'Raids archivieren/wiederherstellen'],
];
function user_has_permission(string $key): bool
diff --git a/includes/raids_api.php b/includes/raids_api.php
index 51141e8..769b4d0 100644
--- a/includes/raids_api.php
+++ b/includes/raids_api.php
@@ -4,15 +4,11 @@ declare(strict_types=1);
const RAID_STATUSES = ['active', 'archived', 'all'];
/**
- * @return array
>
+ * @return array
* @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';
if (!file_exists($configPath)) {
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).');
}
+ 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|null $jsonBody Wird bei POST/DELETE als JSON-Body mitgeschickt.
+ * @return array
+ * @throws RuntimeException
+ */
+function raids_api_request(string $method, string $path = '', array $queryParams = [], ?array $jsonBody = null): array
+{
if (!function_exists('curl_init')) {
throw new RuntimeException('Die PHP-cURL-Erweiterung ist auf diesem Server nicht aktiviert.');
}
- $separator = str_contains($apiUrl, '?') ? '&' : '?';
- $requestUrl = $apiUrl . $separator . 'status=' . urlencode($status);
+ $config = raids_api_config();
+ $url = $config['url'] . $path;
+ if (!empty($queryParams)) {
+ $url .= '?' . http_build_query($queryParams);
+ }
- $ch = curl_init($requestUrl);
- curl_setopt_array($ch, [
+ $headers = ['X-API-Key: ' . $config['key']];
+
+ $ch = curl_init($url);
+ $options = [
CURLOPT_RETURNTRANSFER => true,
- CURLOPT_HTTPHEADER => ['X-API-Key: ' . $apiKey],
+ CURLOPT_CUSTOMREQUEST => $method,
CURLOPT_TIMEOUT => 10,
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);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$curlError = curl_error($ch);
@@ -48,14 +72,69 @@ function fetch_raids(string $status = 'active'): array
if ($response === false) {
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);
- 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>
+ * @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.');
}
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);
+}
diff --git a/raid_archive.php b/raid_archive.php
new file mode 100644
index 0000000..6589172
--- /dev/null
+++ b/raid_archive.php
@@ -0,0 +1,29 @@
+getMessage());
+}
+
+redirect($redirectTo);
diff --git a/raid_restore.php b/raid_restore.php
new file mode 100644
index 0000000..58596e0
--- /dev/null
+++ b/raid_restore.php
@@ -0,0 +1,29 @@
+getMessage());
+}
+
+redirect($redirectTo);
diff --git a/raid_signup.php b/raid_signup.php
new file mode 100644
index 0000000..ab6fd11
--- /dev/null
+++ b/raid_signup.php
@@ -0,0 +1,39 @@
+getMessage());
+}
+
+redirect($redirectTo);
diff --git a/raid_unsignup.php b/raid_unsignup.php
new file mode 100644
index 0000000..285516c
--- /dev/null
+++ b/raid_unsignup.php
@@ -0,0 +1,34 @@
+getMessage());
+}
+
+redirect($redirectTo);
diff --git a/raids.php b/raids.php
index 0da7a66..a296ae6 100644
--- a/raids.php
+++ b/raids.php
@@ -3,6 +3,12 @@ require_once __DIR__ . '/includes/bootstrap.php';
require_once __DIR__ . '/includes/raids_api.php';
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');
if (!in_array($status, RAID_STATUSES, true)) {
$status = 'active';
@@ -67,9 +73,13 @@ include __DIR__ . '/includes/header.php';
$groupNames = array_values(array_filter(array_map('trim', explode(',', (string) ($raid['groups'] ?? '')))));
$signupsByGroup = [];
+ $mySignup = null;
foreach ($signups as $signup) {
$groupNumber = (int) ($signup['group'] ?? 0);
$signupsByGroup[$groupNumber][] = $signup;
+ if ($currentDiscordId && (string) ($signup['user_id'] ?? '') === (string) $currentDiscordId) {
+ $mySignup = $signup;
+ }
}
?>
@@ -80,6 +90,23 @@ include __DIR__ . '/includes/header.php';
Archiviert= $archivedAt ? ' (' . e((string) $archivedAt) . ')' : '' ?>
= count($signups) ?> / = $maxSlots ?> angemeldet
+
+
+
+
+
+
+
@@ -107,10 +134,12 @@ include __DIR__ . '/includes/header.php';
?>
@@ -119,6 +148,46 @@ include __DIR__ . '/includes/header.php';