LuminaWebsite/discord_callback.php
Maaxxs 525bd0d4b5 Gear planner: sets can be shared by link or build code and imported as your own
Asked for: share a build with a link or build code, and an import tool that takes such a code.

- "Teilen" on an own set creates a link (gear.php?build=CODE) and the same code as build code: 10
  characters without 0/O and 1/I/L, shown as K7QX2-M9FRT, stored in gear_sets.share_code (schema
  version 59, unique; also in sql/schema.sql). Whoever opens it sees the set even if it is not on the
  profile, always in its current state; the session remembers the code so tabs, slots and compare
  work, and a renewed or withdrawn code ends that access. "Neuen Code erzeugen" and "Nicht mehr
  teilen" invalidate the old one. The dialog reopens after creating (#teilen) with copy buttons.
- "Build importieren" on "Meine Sets" takes the code (with or without dash, any case) or the whole
  link; viewing someone else's set (shared or on the profile) offers "In meine Sets übernehmen".
  Both copy the set with gear, skills, macros and all planner pages via gear_set_copy() (also used
  by "Kopieren" now); the copy is neither shared nor on the profile. Errors reopen the dialog (#import).
- Members only (gear.view). Opening any page without being logged in now brings you back to it after
  the login (auth_login_return_target) - only for page views in the browser, not for background
  requests like live updates or autosave.
- Privacy policy (shared sets), patch notes and README.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-26 01:44:52 +02:00

75 lines
2.8 KiB
PHP

<?php
require_once __DIR__ . '/includes/bootstrap.php';
require_once __DIR__ . '/includes/discord_auth.php';
$state = (string) ($_GET['state'] ?? '');
$code = (string) ($_GET['code'] ?? '');
$expectedState = (string) ($_SESSION['discord_oauth_state'] ?? '');
$mode = (string) ($_SESSION['discord_oauth_mode'] ?? 'login');
unset($_SESSION['discord_oauth_state'], $_SESSION['discord_oauth_mode']);
$currentUser = current_user();
if ($mode === 'link' && !$currentUser) {
$mode = 'login';
}
$backTo = $mode === 'link' ? '/profile.php' : '/login.php';
if (!empty($_GET['error'])) {
set_flash('error', $mode === 'link' ? 'Discord-Verknüpfung wurde abgebrochen.' : 'Discord-Anmeldung wurde abgebrochen.');
redirect($backTo);
}
if ($code === '' || $state === '' || $expectedState === '' || !hash_equals($expectedState, $state)) {
set_flash('error', 'Ungültige Antwort von Discord. Bitte erneut versuchen.');
redirect($backTo);
}
try {
$tokens = discord_exchange_code($code);
$discordUser = discord_fetch_user($tokens['access_token']);
$member = discord_fetch_member($tokens['access_token']);
} catch (Throwable $e) {
set_flash('error', 'Discord-Anmeldung fehlgeschlagen: ' . $e->getMessage());
redirect($backTo);
}
// Nur wer auf dem Discord-Server der Legion ist, kommt hinein - auch beim Verknuepfen.
if ($member === null) {
set_flash('error', 'Der Discord-Account "' . $discordUser['username'] . '" ist nicht auf dem Discord-Server der Legion.');
redirect($backTo);
}
// ---- Verknuepfen: angemeldetes Konto bekommt den Discord-Account ----
if ($mode === 'link') {
try {
db()->prepare('UPDATE users SET discord_id = ?, discord_username = ? WHERE id = ?')
->execute([$discordUser['id'], $discordUser['username'], $currentUser['id']]);
} catch (PDOException $e) {
set_flash('error', 'Dieser Discord-Account ist bereits mit einem anderen Benutzer verknüpft.');
redirect('/profile.php');
}
discord_store_tokens((int) $currentUser['id'], $tokens);
discord_apply_member((int) $currentUser['id'], $member);
set_flash('success', 'Discord-Account "' . $discordUser['username'] . '" wurde verknüpft.');
redirect('/profile.php');
}
// ---- Anmelden: bestehendes Konto oder neues (noch nicht freigeschaltetes) ----
try {
$account = discord_find_or_create_user($discordUser, $member);
} catch (Throwable $e) {
set_flash('error', 'Das Konto konnte nicht angelegt werden: ' . $e->getMessage());
redirect('/login.php');
}
if ($account['status'] === 'disabled') {
set_flash('error', 'Dieses Konto ist gesperrt.');
redirect('/login.php');
}
discord_store_tokens($account['id'], $tokens);
discord_apply_member($account['id'], $member);
auth_login_session($account['id'], 'discord');
redirect($account['status'] === 'pending' ? '/pending.php' : auth_login_return_target());