LuminaWebsite/includes/functions.php
Maaxxs 70f050dc19 Turn Archivieren/Wiederherstellen/Endgültig löschen into icon buttons
Replaces the three raid action text-buttons with icon-only buttons
(archive box, restore arrow, trash can) using the existing
icon-button style; the label now only shows as a native tooltip on
hover via the title attribute. The delete button gets a new
icon-button-danger modifier so it still reads as destructive (red on
hover) instead of the default gold.

Also fixes a pre-existing bug this surfaced: .icon-button:hover only
set color/border-color, so the global button:hover rule's gold
background bled through on hover for every icon-button on the site
(edit toggle, modal close, etc.), not just the new ones.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-19 07:51:56 +02:00

211 lines
9.8 KiB
PHP

<?php
declare(strict_types=1);
function e(?string $value): string
{
return htmlspecialchars($value ?? '', ENT_QUOTES, 'UTF-8');
}
function redirect(string $path): void
{
header('Location: ' . $path);
exit;
}
function csrf_token(): string
{
if (empty($_SESSION['csrf_token'])) {
$_SESSION['csrf_token'] = bin2hex(random_bytes(32));
}
return $_SESSION['csrf_token'];
}
function csrf_field(): string
{
return '<input type="hidden" name="csrf_token" value="' . e(csrf_token()) . '">';
}
function verify_csrf(): void
{
verify_csrf_token($_POST['csrf_token'] ?? '');
}
// Fuer JSON-Endpunkte (fetch()), die kein normales $_POST haben.
function verify_csrf_token($token): void
{
if (!is_string($token) || $token === '' || !hash_equals($_SESSION['csrf_token'] ?? '', $token)) {
http_response_code(400);
exit('Ungültige Anfrage (CSRF-Token fehlt oder ist abgelaufen). Bitte Seite neu laden und erneut versuchen.');
}
}
/**
* Liest einen JSON-Request-Body (fuer fetch()-Endpunkte). Bricht mit 400 ab,
* wenn der Body kein gueltiges JSON-Objekt ist.
*
* @return array<string, mixed>
*/
function read_json_body(): array
{
$raw = file_get_contents('php://input');
$data = json_decode((string) $raw, true);
if (!is_array($data)) {
http_response_code(400);
header('Content-Type: application/json');
echo json_encode(['status' => 'error', 'message' => 'Ungültiger Request-Body.']);
exit;
}
return $data;
}
/**
* Beendet einen JSON-Endpunkt mit einer Erfolgs- oder Fehlerantwort.
*
* @param array<string, mixed> $extra Zusaetzliche Felder fuer die JSON-Antwort.
*/
function json_response(bool $success, string $message = '', array $extra = []): void
{
header('Content-Type: application/json');
echo json_encode(array_merge([
'status' => $success ? 'success' : 'error',
'message' => $message,
], $extra), JSON_UNESCAPED_UNICODE);
exit;
}
function set_flash(string $type, string $message): void
{
$_SESSION['flash'][] = ['type' => $type, 'message' => $message];
}
function get_flashes(): array
{
$flashes = $_SESSION['flash'] ?? [];
unset($_SESSION['flash']);
return $flashes;
}
function generate_temp_password(int $length = 12): string
{
// Ohne visuell mehrdeutige Zeichen (0/O, 1/l/I, ...), damit sich das
// Einmalpasswort leicht von Hand abtippen lässt.
$chars = 'ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz23456789';
$max = strlen($chars) - 1;
$password = '';
for ($i = 0; $i < $length; $i++) {
$password .= $chars[random_int(0, $max)];
}
return $password;
}
// Kleine, abhängigkeitsfreie Icon-Sammlung (Strichgrafiken, angelehnt an
// Feather Icons) für die Sidebar-Navigation.
function icon(string $name): string
{
$paths = [
'home' => '<path d="M3 9.5 12 3l9 6.5V20a1 1 0 0 1-1 1h-5v-7H9v7H4a1 1 0 0 1-1-1z"/>',
'users' => '<path d="M16 19v-1.5a3.5 3.5 0 0 0-3.5-3.5h-5A3.5 3.5 0 0 0 4 17.5V19"/><circle cx="9" cy="8" r="3"/><path d="M20 19v-1.5a3.5 3.5 0 0 0-2.5-3.36"/><path d="M14.5 4.6a3 3 0 0 1 0 5.8"/>',
'shield' => '<path d="M12 21c4.5-1.8 7-5.2 7-9.5V6l-7-3-7 3v5.5c0 4.3 2.5 7.7 7 9.5z"/>',
'log-out' => '<path d="M9 20H6a2 2 0 0 1-2-2V6a2 2 0 0 1 2-2h3"/><path d="m14 16 4-4-4-4"/><path d="M18 12H9"/>',
'lock' => '<rect x="4" y="11" width="16" height="9" rx="2"/><path d="M7.5 11V7.5a4.5 4.5 0 0 1 9 0V11"/>',
'calendar' => '<rect x="3" y="5" width="18" height="16" rx="2"/><path d="M16 3v4"/><path d="M8 3v4"/><path d="M3 10h18"/>',
'profile' => '<circle cx="12" cy="8" r="4"/><path d="M4.5 20c0-4 3.5-6.5 7.5-6.5s7.5 2.5 7.5 6.5"/>',
'link' => '<path d="M9 17H7a5 5 0 0 1 0-10h2"/><path d="M15 7h2a5 5 0 0 1 0 10h-2"/><line x1="8" y1="12" x2="16" y2="12"/>',
'list' => '<line x1="9" y1="6" x2="20" y2="6"/><line x1="9" y1="12" x2="20" y2="12"/><line x1="9" y1="18" x2="20" y2="18"/><circle cx="4.5" cy="6" r="1"/><circle cx="4.5" cy="12" r="1"/><circle cx="4.5" cy="18" r="1"/>',
'edit' => '<path d="M12 20h9"/><path d="M16.5 3.5a2.121 2.121 0 0 1 3 3L7 19l-4 1 1-4 12.5-12.5z"/>',
'x' => '<line x1="18" y1="6" x2="6" y2="18"/><line x1="6" y1="6" x2="18" y2="18"/>',
'move' => '<polyline points="5 9 2 12 5 15"/><polyline points="9 5 12 2 15 5"/><polyline points="15 19 12 22 9 19"/><polyline points="19 9 22 12 19 15"/><line x1="2" y1="12" x2="22" y2="12"/><line x1="12" y1="2" x2="12" y2="22"/>',
'archive' => '<polyline points="21 8 21 21 3 21 3 8"/><rect x="1" y="3" width="22" height="5"/><line x1="10" y1="12" x2="14" y2="12"/>',
'restore' => '<polyline points="1 4 1 10 7 10"/><path d="M3.51 15a9 9 0 1 0 2.13-9.36L1 10"/>',
'trash' => '<polyline points="3 6 5 6 21 6"/><path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"/><line x1="10" y1="11" x2="10" y2="17"/><line x1="14" y1="11" x2="14" y2="17"/>',
];
$path = $paths[$name] ?? '';
return '<svg class="icon" width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" '
. 'stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">' . $path . '</svg>';
}
// Offizielles Discord-Markenzeichen (gefuellte Flaeche statt Strichgrafik).
function discord_logo(int $size = 24): string
{
$path = 'M20.317 4.3698a19.7913 19.7913 0 0 0-4.8851-1.5152.0741.0741 0 0 0-.0785.0371c-.211.3753-.4447.8648-.6083 1.2495-1.8447-.2762-3.68-.2762-5.4868 0-.1636-.3933-.4058-.8742-.6177-1.2495a.077.077 0 0 0-.0785-.037 19.7363 19.7363 0 0 0-4.8852 1.515.0699.0699 0 0 0-.0321.0277C.5334 9.0458-.319 13.5799.0992 18.0578a.0824.0824 0 0 0 .0312.0561c2.0528 1.5076 4.0413 2.4228 5.9929 3.0294a.0777.0777 0 0 0 .0842-.0276c.4616-.6304.8731-1.2952 1.226-1.9942a.076.076 0 0 0-.0416-.1057c-.6528-.2476-1.2743-.5495-1.8722-.8923a.077.077 0 0 1-.0076-.1277c.1258-.0943.2517-.1923.3718-.2914a.0743.0743 0 0 1 .0776-.0105c3.9278 1.7933 8.18 1.7933 12.0614 0a.0739.0739 0 0 1 .0785.0095c.1202.099.246.1981.3728.2924a.077.077 0 0 1-.0066.1276 12.2986 12.2986 0 0 1-1.873.8914.0766.0766 0 0 0-.0407.1067c.3604.698.7719 1.3628 1.225 1.9932a.076.076 0 0 0 .0842.0286c1.961-.6067 3.9495-1.522 6.0023-3.0294a.077.077 0 0 0 .0313-.0552c.5004-5.177-.8382-9.6739-3.5485-13.6604a.061.061 0 0 0-.0312-.0286zM8.02 15.3312c-1.1825 0-2.1569-1.0857-2.1569-2.419 0-1.3332.9555-2.4189 2.157-2.4189 1.2108 0 2.1757 1.0952 2.1568 2.419 0 1.3332-.9555 2.4189-2.1569 2.4189zm7.9748 0c-1.1825 0-2.1569-1.0857-2.1569-2.419 0-1.3332.9554-2.4189 2.1569-2.4189 1.2108 0 2.1757 1.0952 2.1568 2.419 0 1.3332-.946 2.4189-2.1568 2.4189Z';
return '<svg class="discord-logo" width="' . $size . '" height="' . $size . '" viewBox="0 0 24 24" '
. 'fill="currentColor" aria-hidden="true"><path d="' . $path . '"/></svg>';
}
// Profilbild: selbst hochgeladenes Bild, falls vorhanden, sonst ein Platzhalter
// mit dem Anfangsbuchstaben des Anzeigenamens auf einem nutzerfest zugeordneten
// Hintergrund aus einer zur Lumina-Optik passenden Palette.
// Der Platzhalter wird zusaetzlich immer mitgerendert (nur unsichtbar), damit
// bei einem fehlenden/kaputten Bild per onerror automatisch darauf
// zurueckgefallen wird, statt ein kaputtes Bild-Icon anzuzeigen.
function avatar_html(array $user, int $size = 40): string
{
static $palette = ['#4f6f52', '#6a4c93', '#1b6ca8', '#b5533c', '#8a6d3b', '#3f6c86', '#4a5568', '#a4443a'];
$name = trim((string) ($user['display_name'] ?? '')) !== '' ? $user['display_name'] : (string) ($user['username'] ?? '');
$letter = mb_strtoupper(mb_substr(trim($name), 0, 1, 'UTF-8'), 'UTF-8');
if ($letter === '') {
$letter = '?';
}
$seed = (string) ($user['id'] ?? $name);
$color = $palette[crc32($seed) % count($palette)];
$fontSize = round($size * 0.42, 1);
$hasCustomAvatar = !empty($user['avatar_filename']);
$placeholderStyle = 'width:' . $size . 'px;height:' . $size . 'px;background:' . e($color) . ';font-size:' . $fontSize . 'px;'
. ($hasCustomAvatar ? 'display:none;' : '');
$placeholder = '<span class="avatar avatar-placeholder" style="' . $placeholderStyle . '">' . e($letter) . '</span>';
if (!$hasCustomAvatar) {
return $placeholder;
}
$url = '/assets/uploads/avatars/' . rawurlencode((string) $user['avatar_filename']);
return '<span class="avatar-wrap">'
. '<img class="avatar" src="' . e($url) . '" alt="" width="' . $size . '" height="' . $size . '" '
. 'style="width:' . $size . 'px;height:' . $size . 'px;" '
. "onerror=\"this.style.display='none';this.nextElementSibling.style.display='inline-flex';\">"
. $placeholder
. '</span>';
}
// Ordnet die (teils deutschen) Klassennamen aus dem Raid-Bot den Icon-Dateien
// unter assets/images/Classes zu. Gibt null zurück, wenn keine passende
// Datei existiert (z.B. bei "Ausstehend").
function class_icon_url(string $className): ?string
{
static $map = [
'assassin' => 'Assassin_ICO.png',
'assassine' => 'Assassin_ICO.png',
'chanter' => 'Chanter_ICO.png',
'barde' => 'Chanter_ICO.png',
'gesangsmeister' => 'Chanter_ICO.png',
'cleric' => 'Cleric_ICO.png',
'kleriker' => 'Cleric_ICO.png',
'gladiator' => 'Gladiator_ICO.png',
'ranger' => 'Ranger_ICO.png',
'waldläufer' => 'Ranger_ICO.png',
'jäger' => 'Ranger_ICO.png',
'sorcerer' => 'Sorcerer_ICO.png',
'magier' => 'Sorcerer_ICO.png',
'spiritmaster' => 'Spiritmaster_ICO.png',
'geisterbeschwörer' => 'Spiritmaster_ICO.png',
'beschwörer' => 'Spiritmaster_ICO.png',
'kantor' => 'Chanter_ICO.png',
'templar' => 'Templar_ICO.png',
'templer' => 'Templar_ICO.png',
];
$key = mb_strtolower(trim($className), 'UTF-8');
if (!isset($map[$key])) {
return null;
}
return '/assets/images/Classes/normalized/' . $map[$key];
}