<?php
declare(strict_types=1);
require_once __DIR__ . '/app/Helpers/bootstrap.php';
ob_start(static function (string $html): string {
    if (language() === 'de') { return $html; }
    return strtr($html, ['<html lang="de">' => '<html lang="en">', 'Anmeldung · TripTale' => t('magic.title'), '>Anmeldung nicht möglich<' => '>' . t('magic.unavailable') . '<', '>Neuen Anmeldelink anfordern<' => '>' . t('magic.request') . '<']);
});
$id=filter_input(INPUT_GET,'id',FILTER_VALIDATE_INT,['options'=>['min_range'=>1]]);$token=(string)($_GET['token']??'');$error=null;
if (!$id || !preg_match('/^[a-f0-9]{64}$/',$token)) { $error=t('magic.error_invalid'); }
else { try { $pdo=db(); $pdo->beginTransaction(); $query=$pdo->prepare('SELECT mt.id,mt.token_hash,u.id AS user_id FROM '.table('magic_login_tokens').' mt JOIN '.table('users').' u ON u.id=mt.user_id WHERE mt.id=:id AND mt.used_at IS NULL AND mt.expires_at>NOW() AND u.status=\'active\' AND u.deleted_at IS NULL FOR UPDATE'); $query->execute([':id'=>$id]); $record=$query->fetch(); if (!$record || !hash_equals($record['token_hash'],hash('sha256',$token))) { $pdo->rollBack(); $error=t('magic.error_invalid'); } else { $pdo->prepare('UPDATE '.table('magic_login_tokens').' SET used_at=NOW() WHERE id=:id')->execute([':id'=>$id]); $pdo->commit(); start_session(); session_regenerate_id(true); $_SESSION['user_id']=(int)$record['user_id']; $_SESSION['login_expires_at']=time()+(14*24*60*60); header('Location: /dashboard.php'); exit; } } catch (Throwable $exception) { if (isset($pdo) && $pdo->inTransaction()) { $pdo->rollBack(); } $error=t('magic.error_check'); } }
?><!doctype html><html lang="de"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Anmeldung · TripTale</title><style>body{font-family:system-ui,sans-serif;background:#f4f6f8;margin:0;padding:32px;color:#17202a}main{max-width:520px;margin:auto;background:#fff;padding:28px;border-radius:14px;box-shadow:0 4px 18px #0001}.error{background:#ffe6e3;padding:12px;border-radius:7px}.button{display:inline-block;background:#126e82;color:#fff;padding:10px 14px;border-radius:7px;text-decoration:none;font-weight:700}@media(max-width:600px){body{padding:12px}main{padding:16px}}</style></head><body><main><h1>Anmeldung nicht möglich</h1><p class="error"><?= htmlspecialchars((string)$error,ENT_QUOTES,'UTF-8') ?></p><a class="button" href="/login.php">Neuen Anmeldelink anfordern</a></main></body></html>
