LuminaWebsite/includes/auth.php
Maaxxs 08db95f3a3 Replace Discord avatar sync with self-service profile picture upload
Drops the Discord-derived avatar entirely (discord_avatar,
discord_refresh_token, discord_avatar_synced_at and the background
refresh via OAuth refresh token) in favor of members uploading their
own picture. "Mein Profil" now shows a larger (120px) avatar with a
pencil button pinned to its top-right corner; picking a file uploads
it immediately. Uploads are validated by actual image content
(getimagesize, not the original filename/extension) and stored under
a randomly generated name in assets/uploads/avatars/, which ships
with its own .htaccess disabling script execution so a disguised
upload can never run as PHP. Falls back to the existing letter
placeholder when no picture is set.

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

69 lines
1.7 KiB
PHP

<?php
declare(strict_types=1);
function current_user(): ?array
{
static $user = null;
static $loaded = false;
if ($loaded) {
return $user;
}
$loaded = true;
if (empty($_SESSION['user_id'])) {
return null;
}
$stmt = db()->prepare('SELECT id, username, display_name, status, must_change_password, discord_id, discord_username, avatar_filename FROM users WHERE id = ?');
$stmt->execute([$_SESSION['user_id']]);
$row = $stmt->fetch();
if (!$row || $row['status'] !== 'active') {
// Benutzer wurde gelöscht oder deaktiviert -> Session beenden.
session_destroy();
return null;
}
$user = $row;
return $user;
}
function require_login(): void
{
$user = current_user();
if (!$user) {
redirect('/login.php');
}
$exemptScripts = ['change_password.php', 'logout.php'];
$currentScript = basename((string) ($_SERVER['SCRIPT_NAME'] ?? ''));
if (!empty($user['must_change_password']) && !in_array($currentScript, $exemptScripts, true)) {
redirect('/change_password.php');
}
}
function attempt_login(string $username, string $password): bool
{
$stmt = db()->prepare('SELECT id, password_hash, status FROM users WHERE username = ?');
$stmt->execute([$username]);
$row = $stmt->fetch();
if (!$row || $row['status'] !== 'active' || !password_verify($password, $row['password_hash'])) {
return false;
}
session_regenerate_id(true);
$_SESSION['user_id'] = $row['id'];
$update = db()->prepare('UPDATE users SET last_login = NOW() WHERE id = ?');
$update->execute([$row['id']]);
return true;
}
function logout(): void
{
$_SESSION = [];
session_destroy();
}