LuminaWebsite/includes/discord.php
Maaxxs 8c703f0e1f 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>
2026-09-18 13:25:41 +02:00

108 lines
3.5 KiB
PHP

<?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'],
];
}