LuminaWebsite/profile.php
Maaxxs 9032118b6a Classes come from Discord: all class roles are taken over automatically, members pick their main class, the main class is listed first everywhere
- Discord sync (login and live check) stores every class role of a member in users.classes (role names match the classes, German and English names are recognised); users.class_name is the main class: the one chosen in the profile (users.main_class) while the member still has it, otherwise the highest class role; no class role means no class
- The guild role list is fetched once per request and kept when the bot is briefly unreachable (it used to be emptied); nothing is changed while the role list is unknown
- Profile: the manual class select is gone; new "Klassen" card with the detected classes, main class choice for members with several classes and a "Jetzt mit Discord abgleichen" button (at most every 30 s and 10 per hour, new security bucket discord_sync with a log entry)
- Main class first on the profile page, in the member list (the class filter also finds secondary classes), in the raid signup and premade class pickers (own classes first, main preselected for new raid signups)
- Berechtigungsgruppen shows which Discord roles count as classes
- Schema version 36 (users.classes filled from class_name until the first sync, users.main_class), patch notes, README

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-23 17:34:37 +02:00

343 lines
19 KiB
PHP

<?php
require_once __DIR__ . '/includes/bootstrap.php';
require_login();
$user = current_user();
$profileError = null;
$passwordError = null;
$avatarError = null;
$avatarUploadDir = __DIR__ . '/assets/uploads/avatars/';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
verify_csrf();
$action = (string) ($_POST['action'] ?? '');
if ($action === 'update_profile') {
$displayName = trim((string) ($_POST['display_name'] ?? ''));
// Mit Discord-Verknuepfung folgt der Anzeigename dem Namen auf dem Discord-Server.
if (!empty($user['discord_id'])) {
$displayName = (string) $user['display_name'];
}
if ($displayName === '') {
$profileError = 'Anzeigename darf nicht leer sein.';
} elseif (mb_strlen($displayName) > 100) {
$profileError = 'Der Anzeigename darf höchstens 100 Zeichen lang sein.';
} else {
db()->prepare('UPDATE users SET display_name = ? WHERE id = ?')->execute([$displayName, $user['id']]);
set_flash('success', 'Profil wurde aktualisiert.');
redirect('/profile.php');
}
$user['display_name'] = $displayName;
} elseif ($action === 'change_password' && !empty($user['password_login'])) {
$newPassword = (string) ($_POST['new_password'] ?? '');
$newPasswordConfirm = (string) ($_POST['new_password_confirm'] ?? '');
if (strlen($newPassword) < 8) {
$passwordError = 'Das neue Passwort muss mindestens 8 Zeichen lang sein.';
} elseif ($newPassword !== $newPasswordConfirm) {
$passwordError = 'Die Passwörter stimmen nicht überein.';
} else {
db()->prepare('UPDATE users SET password_hash = ?, must_change_password = 0 WHERE id = ?')
->execute([password_hash($newPassword, PASSWORD_DEFAULT), $user['id']]);
set_flash('success', 'Passwort wurde geändert.');
redirect('/profile.php');
}
} elseif ($action === 'update_avatar') {
$file = $_FILES['avatar'] ?? null;
$allowedTypes = [IMAGETYPE_JPEG, IMAGETYPE_PNG, IMAGETYPE_GIF, IMAGETYPE_WEBP];
if (!$file || ($file['error'] ?? UPLOAD_ERR_NO_FILE) === UPLOAD_ERR_NO_FILE) {
$avatarError = 'Bitte eine Bilddatei auswählen.';
} elseif ($file['error'] !== UPLOAD_ERR_OK) {
$avatarError = 'Upload fehlgeschlagen.';
} elseif ($file['size'] > 3 * 1024 * 1024) {
$avatarError = 'Die Datei ist zu groß (max. 3 MB).';
} else {
// Dateityp wird ausschließlich am tatsaechlichen Bildinhalt erkannt (nicht am
// Originalnamen), die Endung selbst erzeugt - so kann keine ausfuehrbare Datei
// unter falschem Namen/falscher Endung eingeschleust werden.
$imageInfo = @getimagesize($file['tmp_name']);
if ($imageInfo === false || !in_array($imageInfo[2], $allowedTypes, true)) {
$avatarError = 'Nur JPG, PNG, GIF oder WEBP sind als Profilbild erlaubt.';
} else {
$filename = 'user' . $user['id'] . '_' . bin2hex(random_bytes(8)) . '.' . image_type_to_extension($imageInfo[2], false);
// Neu rechnen (entfernt angehaengten Code/EXIF) und auf 512 px begrenzen
$storeError = store_uploaded_image($file['tmp_name'], $avatarUploadDir . $filename, $imageInfo[2], 512);
if ($storeError === null) {
$oldFilename = $user['avatar_filename'] ?? null;
// Eigenes Bild: nicht mehr vom Discord-Profilbild ueberschreiben lassen.
db()->prepare('UPDATE users SET avatar_filename = ?, avatar_sync = 0, discord_avatar = NULL WHERE id = ?')
->execute([$filename, $user['id']]);
if ($oldFilename && is_file($avatarUploadDir . $oldFilename)) {
@unlink($avatarUploadDir . $oldFilename);
}
set_flash('success', 'Profilbild wurde aktualisiert.');
redirect('/profile.php');
} else {
$avatarError = $storeError;
}
}
}
} elseif ($action === 'set_main_class') {
// Main-Klasse: nur eine der Klassen, die man auf Discord gewaehlt hat
$choice = (string) ($_POST['main_class'] ?? '');
if (!in_array($choice, classes_from_list($user['classes'] ?? null), true)) {
set_flash('error', 'Diese Klasse hast du auf unserem Discord nicht ausgewählt.');
} else {
db()->prepare('UPDATE users SET main_class = ?, class_name = ? WHERE id = ?')->execute([$choice, $choice, $user['id']]);
set_flash('success', $choice . ' ist jetzt deine Main-Klasse.');
}
redirect('/profile.php#klassen');
} elseif ($action === 'sync_discord' && !empty($user['discord_id'])) {
// Klassen (und Gruppen) gleich abgleichen, statt auf den naechsten automatischen Abgleich zu
// warten - hoechstens alle 30 Sekunden.
$stmt = db()->prepare('SELECT discord_synced_at FROM users WHERE id = ?');
$stmt->execute([$user['id']]);
$lastSync = strtotime((string) $stmt->fetchColumn()) ?: 0;
if ($lastSync > time() - 30) {
set_flash('info', 'Gerade eben erst abgeglichen - versuch es in ein paar Sekunden noch einmal.');
} elseif (!security_throttle('discord_sync')) {
set_flash('error', 'Du hast in der letzten Stunde schon oft abgeglichen. Automatisch passiert das ohnehin alle paar Minuten.');
} else {
$result = discord_live_check((int) $user['id'], true);
$messages = [
'ok' => ['success', 'Mit Discord abgeglichen - deine Klassen sind aktuell.'],
'no_token' => ['info', 'Melde dich einmal über „Mit Discord einloggen“ an, dann werden deine Klassen erkannt.'],
'revoked' => ['info', 'Deine Discord-Anmeldung ist abgelaufen - melde dich einmal über „Mit Discord einloggen“ an.'],
'left' => ['error', 'Du bist nicht mehr auf unserem Discord-Server.'],
'error' => ['error', 'Discord war gerade nicht erreichbar - versuch es später noch einmal.'],
];
[$type, $message] = $messages[$result] ?? ['error', 'Abgleich fehlgeschlagen.'];
set_flash($type, $message);
}
redirect('/profile.php#klassen');
} elseif ($action === 'restart_tour') {
// Einfuehrung zuruecksetzen - sie startet beim naechsten Aufruf des Dashboards.
db()->prepare('UPDATE users SET tour_completed_at = NULL WHERE id = ?')->execute([$user['id']]);
redirect('/dashboard.php');
} elseif ($action === 'use_discord_avatar' && !empty($user['discord_id'])) {
// Wieder das Discord-Profilbild verwenden: eigenes Bild weg, Übernahme an, sofort abgleichen.
$oldFilename = $user['avatar_filename'] ?? null;
db()->prepare('UPDATE users SET avatar_filename = NULL, avatar_sync = 1, discord_avatar = NULL WHERE id = ?')
->execute([$user['id']]);
if ($oldFilename && is_file($avatarUploadDir . $oldFilename)) {
@unlink($avatarUploadDir . $oldFilename);
}
$result = discord_live_check((int) $user['id'], true);
set_flash($result === 'ok' ? 'success' : 'info', $result === 'ok'
? 'Dein Discord-Profilbild wird jetzt verwendet und bleibt automatisch aktuell.'
: 'Dein Discord-Profilbild wird ab der nächsten Discord-Anmeldung übernommen.');
redirect('/profile.php');
} elseif ($action === 'remove_avatar') {
$oldFilename = $user['avatar_filename'] ?? null;
// Bewusst kein Bild: auch nicht wieder von Discord holen.
db()->prepare('UPDATE users SET avatar_filename = NULL, avatar_sync = 0, discord_avatar = NULL WHERE id = ?')->execute([$user['id']]);
if ($oldFilename && is_file($avatarUploadDir . $oldFilename)) {
@unlink($avatarUploadDir . $oldFilename);
}
set_flash('success', 'Profilbild wurde entfernt.');
redirect('/profile.php');
}
}
$pageTitle = 'Mein Profil';
include __DIR__ . '/includes/header.php';
?>
<div class="panel">
<div class="profile-header">
<div class="avatar-edit-wrap">
<?= avatar_html($user, 120) ?>
<label class="avatar-edit-btn" for="avatar-upload-input" title="Profilbild ändern">
<?= icon('edit') ?>
</label>
</div>
<div>
<h1>Mein Profil</h1>
<p class="text-muted">Benutzername: <strong><?= e($user['username']) ?></strong></p>
<?php if ($avatarError): ?>
<p class="flash flash-error" style="margin-top:0.5rem;"><?= e($avatarError) ?></p>
<?php endif; ?>
<?php if (!empty($user['discord_id']) && !empty($user['avatar_sync'])): ?>
<p class="avatar-sync-hint"><?= discord_logo(14) ?> Dein Profilbild wird automatisch von Discord übernommen. Lädst du ein eigenes hoch, bleibt dieses bestehen.</p>
<?php elseif (!empty($user['discord_id'])): ?>
<form method="post" action="/profile.php" class="inline-form">
<?= csrf_field() ?>
<input type="hidden" name="action" value="use_discord_avatar">
<button type="submit" class="button-secondary button-small avatar-sync-button"><?= discord_logo(14) ?> Discord-Profilbild verwenden</button>
</form>
<?php endif; ?>
<?php if (!empty($user['avatar_filename'])): ?>
<form method="post" action="/profile.php" class="inline-form" data-confirm="Profilbild wirklich entfernen?" data-confirm-title="Profilbild entfernen" data-confirm-danger data-confirm-ok="Entfernen">
<?= csrf_field() ?>
<input type="hidden" name="action" value="remove_avatar">
<button type="submit" class="text-button" style="font-size:0.85rem;margin-top:0.4rem;">Profilbild entfernen</button>
</form>
<?php endif; ?>
</div>
</div>
<form id="avatar-upload-form" method="post" action="/profile.php" enctype="multipart/form-data" style="display:none;">
<?= csrf_field() ?>
<input type="hidden" name="action" value="update_avatar">
<input type="file" id="avatar-upload-input" name="avatar" accept="image/png,image/jpeg,image/gif,image/webp">
</form>
</div>
<script>
document.getElementById('avatar-upload-input').addEventListener('change', function () {
if (this.files && this.files.length) {
document.getElementById('avatar-upload-form').submit();
}
});
</script>
<div class="profile-grid">
<div class="panel profile-card">
<h2>Anzeigename</h2>
<?php if ($profileError): ?>
<div class="flash flash-error"><?= e($profileError) ?></div>
<?php endif; ?>
<form method="post" action="/profile.php" class="profile-card-form">
<?= csrf_field() ?>
<input type="hidden" name="action" value="update_profile">
<div>
<label for="display_name">Anzeigename</label>
<input type="text" id="display_name" name="display_name" value="<?= e($user['display_name']) ?>" maxlength="100" required<?= !empty($user['discord_id']) ? ' readonly' : '' ?>>
<?php if (!empty($user['discord_id'])): ?>
<div class="field-hint"><?= discord_logo(12) ?> Wird automatisch von deinem Namen auf unserem Discord-Server übernommen. Ändern kannst du ihn dort über „Server-Profil bearbeiten“.</div>
<?php endif; ?>
</div>
<?php if (empty($user['discord_id'])): ?>
<button type="submit" class="profile-card-button">Speichern</button>
<?php endif; ?>
</form>
</div>
<?php $myClasses = user_classes($user); ?>
<div class="panel profile-card" id="klassen">
<h2>Klassen</h2>
<?php if ($myClasses): ?>
<form method="post" action="/profile.php" class="profile-classes-form">
<?= csrf_field() ?>
<input type="hidden" name="action" value="set_main_class">
<div class="profile-classes" role="radiogroup" aria-label="Main-Klasse">
<?php foreach ($myClasses as $class): ?>
<?php $isMain = $class === ($user['class_name'] ?? ''); ?>
<label class="profile-class<?= $isMain ? ' is-main' : '' ?>">
<?php if (count($myClasses) > 1): ?>
<input type="radio" name="main_class" value="<?= e($class) ?>"<?= $isMain ? ' checked' : '' ?> onchange="this.form.submit()">
<?php endif; ?>
<?= class_badge_html($class, 30) ?>
<span class="profile-class-name"><?= e($class) ?></span>
<?php if ($isMain && count($myClasses) > 1): ?><span class="profile-class-main">Main</span><?php endif; ?>
</label>
<?php endforeach; ?>
</div>
<?php if (count($myClasses) > 1): ?>
<noscript><button type="submit" class="button-secondary button-small">Main-Klasse speichern</button></noscript>
<p class="field-hint">Du spielst mehrere Klassen: Wähle deine Main-Klasse - sie steht überall als erste und wird z.B. bei der Raid-Anmeldung vorausgewählt.</p>
<?php endif; ?>
</form>
<?php else: ?>
<p class="text-muted">Noch keine Klasse erkannt.</p>
<?php endif; ?>
<?php if (!empty($user['discord_id'])): ?>
<p class="field-hint"><?= discord_logo(12) ?> Deine Klassen kommen automatisch von deinen Klassenrollen auf unserem Discord-Server - ändern kannst du sie nur dort.</p>
<form method="post" action="/profile.php" class="profile-classes-sync">
<?= csrf_field() ?>
<input type="hidden" name="action" value="sync_discord">
<button type="submit" class="button-secondary button-small"><?= discord_logo(14) ?> Jetzt mit Discord abgleichen</button>
</form>
<?php else: ?>
<p class="field-hint">Die Klassen kommen von deinen Klassenrollen auf unserem Discord-Server. Verknüpfe unten dein Discord-Konto, dann werden sie automatisch erkannt.</p>
<?php endif; ?>
</div>
<?php if (!empty($user['password_login'])): ?>
<div class="panel profile-card">
<h2>Passwort ändern</h2>
<?php if ($passwordError): ?>
<div class="flash flash-error"><?= e($passwordError) ?></div>
<?php endif; ?>
<form method="post" action="/profile.php" class="profile-card-form">
<?= csrf_field() ?>
<input type="hidden" name="action" value="change_password">
<div>
<label for="new_password">Neues Passwort</label>
<input type="password" id="new_password" name="new_password" required>
<div class="field-hint">Mindestens 8 Zeichen.</div>
</div>
<div>
<label for="new_password_confirm">Neues Passwort wiederholen</label>
<input type="password" id="new_password_confirm" name="new_password_confirm" required>
</div>
<button type="submit" class="profile-card-button">Passwort speichern</button>
</form>
</div>
<?php else: ?>
<div class="panel profile-card">
<h2>Anmeldung</h2>
<p class="text-muted">Du meldest dich über Discord an. Die Anmeldung mit Benutzername und Passwort ist für dein Konto nicht freigeschaltet - das kann die Legionsleitung bei Bedarf ändern.</p>
</div>
<?php endif; ?>
</div>
<div class="panel discord-card">
<div class="discord-card-header">
<?= discord_logo(40) ?>
<div>
<h2>Discord</h2>
<?php if (!empty($user['discord_id'])): ?>
<span class="badge badge-active">Verbunden</span>
<?php else: ?>
<span class="badge badge-disabled">Nicht verbunden</span>
<?php endif; ?>
</div>
</div>
<?php if (!empty($user['discord_id'])): ?>
<p>Verknüpft als <strong><?= e((string) $user['discord_username']) ?></strong></p>
<p class="text-muted">Deine Berechtigungsgruppen werden automatisch mit deinen Rollen auf dem Discord-Server abgeglichen.</p>
<?php if (!discord_unlink_allowed()): ?>
<p class="text-muted">Das Lösen der Verknüpfung ist von der Legionsleitung deaktiviert.</p>
<?php elseif (!empty($user['password_login'])): ?>
<form method="post" action="/discord_unlink.php" class="inline-form" data-confirm="Gruppen, die über Discord-Rollen vergeben werden, gehen dabei verloren." data-confirm-title="Discord-Verknüpfung entfernen?" data-confirm-danger data-confirm-ok="Entfernen">
<?= csrf_field() ?>
<button type="submit" class="button-secondary">Verknüpfung entfernen</button>
</form>
<?php endif; ?>
<?php else: ?>
<p class="text-muted">Verknüpfe deinen Discord-Account, um dich über Discord anzumelden, deine Gruppen automatisch aus deinen Discord-Rollen zu bekommen und dich über die Website zu Raids anmelden zu können.</p>
<a class="button discord-connect-button" href="/discord_login.php">
<?= discord_logo(18) ?> Mit Discord verknüpfen
</a>
<?php endif; ?>
</div>
<?php if (user_has_permission('members.view')): ?>
<div class="panel tour-restart-panel">
<div class="tour-restart">
<span class="icon-badge"><?= icon('users') ?></span>
<div>
<h2>Meine Profilseite</h2>
<p class="text-muted">Deine Seite in der Mitgliederliste: ein Text über dich, deine Charaktere mit Portrait und Bild und eine Bildergalerie.</p>
</div>
<a class="button button-secondary tour-restart-button" href="/member_edit.php">Profilseite gestalten</a>
</div>
</div>
<?php endif; ?>
<div class="panel tour-restart-panel">
<div class="tour-restart">
<span class="icon-badge"><?= icon('route') ?></span>
<div>
<h2>Einführung</h2>
<p class="text-muted">Lass dir das Dashboard noch einmal Schritt für Schritt zeigen - wo du was findest und wofür es gut ist.</p>
</div>
<form method="post" action="/profile.php" class="inline-form">
<?= csrf_field() ?>
<input type="hidden" name="action" value="restart_tour">
<button type="submit" class="button button-secondary tour-restart-button">Einführung neu starten</button>
</form>
</div>
</div>
<?php include __DIR__ . '/includes/footer.php'; ?>