<?php
require_once __DIR__.'/../app/bootstrap.php';
if (current_user($pdo)) { header('Location: dashboard.php'); exit; }
if (empty($config['registration_enabled'])) {
    http_response_code(403);
    ?><!doctype html><html lang="de"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Registrierung deaktiviert</title><link rel="stylesheet" href="../public/assets/style.css"><script src="../public/assets/admin.js" defer></script></head><body><main class="login"><section class="card formgrid"><h1>Registrierung deaktiviert</h1><p class="notice">Die öffentliche Benutzerregistrierung ist für diese Installation deaktiviert.</p><p><a href="login.php">Zurück zum Login</a></p></section></main></body></html><?php
    exit;
}
header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0');
header('Pragma: no-cache');
csrf_check();
$message='';
$ok=false;
if($_SERVER['REQUEST_METHOD']==='POST'){
    $email = strtolower(trim($_POST['email'] ?? ''));
    [$allowed,$rateMessage]=rate_limit_check($pdo,$config,'register',4,60*60,60*60,$email);
    if(!$allowed){
        $ok=false; $message=$rateMessage; $newUserId=null;
    } else {
        [$ok,$message,$newUserId]=register_user($pdo, $email, $_POST['password']??'', 'user', false);
    }
    if($ok && $newUserId){
        $token = create_email_verification_token($pdo, (int)$newUserId);
        $link = base_url($config).'/admin/verify-email.php?token='.$token;
        $sent = mailer_send_email_verification($config, strtolower(trim($_POST['email']??'')), $link);
        $message = $sent
            ? 'Konto erstellt. Bitte bestätige deine E-Mail-Adresse über den Link, den wir dir geschickt haben.'
            : 'Konto erstellt, aber die Bestätigungsmail konnte nicht versendet werden. Bitte kontaktiere den Administrator.';
    }
}
?>
<!doctype html><html lang="de"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Registrieren · KuhRator</title><link rel="stylesheet" href="../public/assets/style.css"><script src="../public/assets/admin.js" defer></script></head><body><main class="login auth-screen"><form method="post" action="register.php" class="card formgrid auth-card"><div class="auth-logo"><img src="../public/assets/qrator-logo.png" alt="KuhRator Logo"></div><h1>KuhRator Konto erstellen</h1><?php if($message): ?><p class="notice <?= $ok?'ok':'' ?>"><?= e($message) ?></p><?php endif; ?><input type="hidden" name="csrf" value="<?= e(csrf_token()) ?>"><label>E-Mail<input name="email" type="email" required autofocus autocomplete="email" value="<?= e($_POST['email'] ?? '') ?>"></label><label>Passwort<span class="password-field"><input name="password" type="password" minlength="12" required autocomplete="new-password" data-password-toggle><button type="button" class="password-toggle" data-password-toggle-button aria-label="Passwort anzeigen" aria-pressed="false"><span class="eye-open" aria-hidden="true">&#128065;</span><span class="eye-closed" aria-hidden="true">&#128584;</span></button></span></label><button class="btn" type="submit">Registrieren</button><p><a href="login.php">Zurück zum Login</a></p></form></main></body></html>
