Customer feedback round (4 items):
- Packages: one-click "Sichtbar/Ausgeblendet" toggle per package (saved
immediately). Hidden packages disappear from the model page; the tier
look of the remaining cards is ranked across all packages so it stays
stable.
- Packages: badge per card (Empfohlen, Am beliebtesten, Tipp, Coming soon
or custom text) plus an optional video link shown as "Zum Video". Legacy
is_featured maps to "Empfohlen".
- New download area (/downloads + Admin -> Downloads): upload with
progress, inline audio player, allow-listed file types, optional access
password, new permission downloads.manage. Files live in
api/uploads/downloads/ (gitignored, created at runtime).
- Performance:
- SetupGate no longer refetches and remounts the whole app on every
navigation.
- Page loader waits only for pending API calls and above-the-fold images
(fixes an 8 s hang on /leistungen caused by lazy images inside the
hidden container).
- Admin pages lazy-loaded; icon library moved out of the main bundle
(initial JS 2.2 MB -> 0.48 MB).
- Background video loads only near the viewport and was re-encoded to
720p (32 MB -> 8.5 MB).
- Visitors without a session cookie get no PHP session, so their parallel
API requests no longer queue on the session lock.
- index.html is served with Cache-Control: no-cache so browsers don't
keep a stale build after deploys.
- Fonts (Barlow, Barlow Condensed, Michroma) are self-hosted instead of
loaded from Google Fonts; the default privacy policy mentions this, lists
all local storage uses and uses the new TDDDG name.
- A code update that adds a table no longer sends the site into the setup
wizard while an admin exists.
Run "Datenbankstruktur aktualisieren" after deploying (new package
columns, downloads table, app_settings.downloads_password).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
199 lines
7.2 KiB
PHP
199 lines
7.2 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers;
|
|
|
|
use App\Config\Database;
|
|
use App\Middleware\AuthMiddleware;
|
|
use App\Support\Http;
|
|
use App\Support\RecoveryCodes;
|
|
use App\Support\Totp;
|
|
|
|
class AuthController
|
|
{
|
|
public static function login(): void
|
|
{
|
|
$body = Http::jsonBody();
|
|
$username = trim($body['username'] ?? '');
|
|
$password = (string) ($body['password'] ?? '');
|
|
|
|
if ($username === '' || $password === '') {
|
|
Http::error('Benutzername und Passwort erforderlich', 422);
|
|
}
|
|
|
|
$stmt = Database::connection()->prepare(
|
|
'SELECT id, username, password_hash, two_factor_enabled FROM admin_users WHERE username = ?'
|
|
);
|
|
$stmt->execute([$username]);
|
|
$user = $stmt->fetch();
|
|
|
|
if (!$user || !password_verify($password, $user['password_hash'])) {
|
|
Http::error('Benutzername oder Passwort falsch', 401);
|
|
}
|
|
|
|
if ((bool) $user['two_factor_enabled']) {
|
|
session_regenerate_id(true);
|
|
$_SESSION['pending_2fa_user_id'] = (int) $user['id'];
|
|
unset($_SESSION['admin_id'], $_SESSION['admin_username']);
|
|
Http::send(['requires_2fa' => true]);
|
|
return;
|
|
}
|
|
|
|
self::completeLogin((int) $user['id'], $user['username']);
|
|
}
|
|
|
|
public static function verifyTwoFactor(): void
|
|
{
|
|
if (empty($_SESSION['pending_2fa_user_id'])) {
|
|
Http::error('Keine ausstehende Anmeldung. Bitte erneut einloggen.', 400);
|
|
}
|
|
$userId = (int) $_SESSION['pending_2fa_user_id'];
|
|
$body = Http::jsonBody();
|
|
$code = trim($body['code'] ?? '');
|
|
|
|
if ($code === '') {
|
|
Http::error('Code erforderlich', 422);
|
|
}
|
|
|
|
$stmt = Database::connection()->prepare(
|
|
'SELECT username, two_factor_secret, two_factor_recovery_codes FROM admin_users WHERE id = ?'
|
|
);
|
|
$stmt->execute([$userId]);
|
|
$user = $stmt->fetch();
|
|
|
|
if (!$user) {
|
|
Http::error('Benutzer nicht gefunden', 404);
|
|
}
|
|
|
|
$valid = Totp::verifyCode($user['two_factor_secret'] ?? '', $code);
|
|
|
|
if (!$valid) {
|
|
$hashes = $user['two_factor_recovery_codes'] ? json_decode($user['two_factor_recovery_codes'], true) : [];
|
|
$remaining = RecoveryCodes::consume($hashes, $code);
|
|
if ($remaining !== null) {
|
|
$valid = true;
|
|
$update = Database::connection()->prepare(
|
|
'UPDATE admin_users SET two_factor_recovery_codes = ? WHERE id = ?'
|
|
);
|
|
$update->execute([json_encode($remaining), $userId]);
|
|
}
|
|
}
|
|
|
|
if (!$valid) {
|
|
Http::error('Code ungültig oder abgelaufen', 401);
|
|
}
|
|
|
|
self::completeLogin($userId, $user['username']);
|
|
}
|
|
|
|
public static function changePassword(): void
|
|
{
|
|
$userId = AuthMiddleware::requireAdmin();
|
|
$body = Http::jsonBody();
|
|
$currentPassword = (string) ($body['current_password'] ?? '');
|
|
$newPassword = (string) ($body['new_password'] ?? '');
|
|
$newPasswordConfirmation = (string) ($body['new_password_confirmation'] ?? '');
|
|
$twoFactorCode = trim((string) ($body['two_factor_code'] ?? ''));
|
|
|
|
if ($currentPassword === '' || $newPassword === '' || $newPasswordConfirmation === '') {
|
|
Http::error('Bitte alle Felder ausfüllen', 422);
|
|
}
|
|
if (strlen($newPassword) < 8) {
|
|
Http::error('Das neue Passwort muss mindestens 8 Zeichen lang sein', 422);
|
|
}
|
|
if ($newPassword !== $newPasswordConfirmation) {
|
|
Http::error('Die neuen Passwörter stimmen nicht überein', 422);
|
|
}
|
|
|
|
$stmt = Database::connection()->prepare(
|
|
'SELECT password_hash, two_factor_enabled, two_factor_secret, two_factor_recovery_codes FROM admin_users WHERE id = ?'
|
|
);
|
|
$stmt->execute([$userId]);
|
|
$user = $stmt->fetch();
|
|
|
|
if (!$user || !password_verify($currentPassword, $user['password_hash'])) {
|
|
Http::error('Aktuelles Passwort ist falsch', 401);
|
|
}
|
|
|
|
if ((bool) $user['two_factor_enabled']) {
|
|
if ($twoFactorCode === '') {
|
|
Http::error('Bitte deinen 2FA-Code eingeben', 422);
|
|
}
|
|
|
|
$valid = Totp::verifyCode($user['two_factor_secret'] ?? '', $twoFactorCode);
|
|
|
|
if (!$valid) {
|
|
$hashes = $user['two_factor_recovery_codes'] ? json_decode($user['two_factor_recovery_codes'], true) : [];
|
|
$remaining = RecoveryCodes::consume($hashes, $twoFactorCode);
|
|
if ($remaining !== null) {
|
|
$valid = true;
|
|
$update = Database::connection()->prepare(
|
|
'UPDATE admin_users SET two_factor_recovery_codes = ? WHERE id = ?'
|
|
);
|
|
$update->execute([json_encode($remaining), $userId]);
|
|
}
|
|
}
|
|
|
|
if (!$valid) {
|
|
Http::error('2FA-Code ungültig oder abgelaufen', 401);
|
|
}
|
|
}
|
|
|
|
$newHash = password_hash($newPassword, PASSWORD_DEFAULT);
|
|
$update = Database::connection()->prepare('UPDATE admin_users SET password_hash = ? WHERE id = ?');
|
|
$update->execute([$newHash, $userId]);
|
|
|
|
Http::send(['ok' => true]);
|
|
}
|
|
|
|
public static function logout(): void
|
|
{
|
|
$_SESSION = [];
|
|
// Ohne Login-Zustand startet index.php keine (bzw. keine offene) Session.
|
|
if (session_status() === PHP_SESSION_ACTIVE) {
|
|
session_destroy();
|
|
}
|
|
Http::send(['ok' => true]);
|
|
}
|
|
|
|
public static function me(): void
|
|
{
|
|
// Bewusst kein 401 hier: /auth/me wird auf JEDER oeffentlichen Seite aufgerufen,
|
|
// um den Login-Status zu pruefen - "nicht angemeldet" ist der Normalfall fuer
|
|
// Besucher, kein Fehler. Ein 401 erzeugt in der Browser-Konsole/DevTools trotz
|
|
// JS-seitigem catch() einen sichtbaren Netzwerk-Fehler (bemaengelt u.a. Lighthouse).
|
|
if (empty($_SESSION['admin_id'])) {
|
|
Http::send(['authenticated' => false]);
|
|
}
|
|
Http::send(self::userPayload((int) $_SESSION['admin_id'], $_SESSION['admin_username']));
|
|
}
|
|
|
|
private static function completeLogin(int $id, string $username): void
|
|
{
|
|
session_regenerate_id(true);
|
|
$_SESSION['admin_id'] = $id;
|
|
$_SESSION['admin_username'] = $username;
|
|
$_SESSION['last_active_touched_at'] = time();
|
|
unset($_SESSION['pending_2fa_user_id']);
|
|
|
|
Database::connection()
|
|
->prepare('UPDATE admin_users SET last_active_at = NOW() WHERE id = ?')
|
|
->execute([$id]);
|
|
|
|
Http::send(self::userPayload($id, $username));
|
|
}
|
|
|
|
private static function userPayload(int $id, string $username): array
|
|
{
|
|
$stmt = Database::connection()->prepare('SELECT two_factor_enabled FROM admin_users WHERE id = ?');
|
|
$stmt->execute([$id]);
|
|
$row = $stmt->fetch();
|
|
|
|
return [
|
|
'id' => $id,
|
|
'username' => $username,
|
|
'is_super_admin' => AuthMiddleware::isSuperAdmin($id),
|
|
'two_factor_enabled' => (bool) ($row['two_factor_enabled'] ?? false),
|
|
'permissions' => AuthMiddleware::effectivePermissions($id),
|
|
];
|
|
}
|
|
}
|