211 lines
8.9 KiB
PHP
211 lines
8.9 KiB
PHP
<?php
|
||
declare(strict_types=1);
|
||
|
||
/**
|
||
* Westreiche – einmaliger Installer.
|
||
*
|
||
* Legt Schema, Zauber-/Fertigkeitengrundlage und das erste Admin-Konto an.
|
||
* Aufruf über den Link aus dem Deployment: install.php?token=…
|
||
*/
|
||
|
||
define('WS_ROOT', __DIR__);
|
||
define('WS_APP', WS_ROOT . '/app');
|
||
|
||
$configFile = WS_APP . '/config.local.php';
|
||
if (!is_file($configFile)) {
|
||
http_response_code(500);
|
||
header('Content-Type: text/plain; charset=utf-8');
|
||
echo "app/config.local.php fehlt. Bitte zuerst deployen.\n";
|
||
exit;
|
||
}
|
||
|
||
$config = require $configFile;
|
||
$GLOBALS['ws_config'] = $config;
|
||
date_default_timezone_set('Europe/Berlin');
|
||
require WS_APP . '/util.php';
|
||
require WS_APP . '/rules.php';
|
||
require WS_APP . '/db.php';
|
||
require WS_APP . '/auth.php';
|
||
require WS_APP . '/character.php';
|
||
|
||
$expectedToken = (string) ($config['app']['install_token'] ?? '');
|
||
$token = (string) ($_POST['token'] ?? $_GET['token'] ?? '');
|
||
$tokenOk = $expectedToken !== '' && $token !== '' && hash_equals($expectedToken, $token);
|
||
|
||
$errors = [];
|
||
$messages = [];
|
||
$done = false;
|
||
$alreadyInstalled = false;
|
||
$installedUser = '';
|
||
|
||
if (!$tokenOk) {
|
||
http_response_code(403);
|
||
$errors[] = 'Der Installations-Token ist ungültig. Bitte den Link aus dem Deployment verwenden.';
|
||
} else {
|
||
try {
|
||
$pdo = db();
|
||
$hasUsers = (bool) $pdo->query("SHOW TABLES LIKE 'users'")->fetchColumn();
|
||
if ($hasUsers) {
|
||
$count = (int) $pdo->query('SELECT COUNT(*) FROM users')->fetchColumn();
|
||
if ($count > 0) {
|
||
$alreadyInstalled = true;
|
||
$installedUser = (string) $pdo->query('SELECT username FROM users ORDER BY id LIMIT 1')->fetchColumn();
|
||
}
|
||
}
|
||
} catch (Throwable $error) {
|
||
$errors[] = 'Datenbankverbindung fehlgeschlagen: ' . $error->getMessage();
|
||
}
|
||
|
||
if ($errors === [] && !$alreadyInstalled && $_SERVER['REQUEST_METHOD'] === 'POST') {
|
||
$username = trim((string) ($_POST['username'] ?? ''));
|
||
$email = trim((string) ($_POST['email'] ?? ''));
|
||
$password = (string) ($_POST['password'] ?? '');
|
||
$repeat = (string) ($_POST['password_repeat'] ?? '');
|
||
|
||
if ($username === '') {
|
||
$errors[] = 'Bitte einen Benutzernamen für das Admin-Konto angeben.';
|
||
}
|
||
if (mb_strlen($password, 'UTF-8') < 8) {
|
||
$errors[] = 'Das Passwort braucht mindestens 8 Zeichen.';
|
||
}
|
||
if ($password !== $repeat) {
|
||
$errors[] = 'Die beiden Passwörter stimmen nicht überein.';
|
||
}
|
||
if ($email !== '' && !filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
||
$errors[] = 'Diese E-Mail-Adresse sieht nicht gültig aus.';
|
||
}
|
||
|
||
if ($errors === []) {
|
||
try {
|
||
$pdo = db();
|
||
$schema = (string) file_get_contents(WS_ROOT . '/sql/schema.sql');
|
||
foreach (explode(';', $schema) as $statement) {
|
||
// Kommentarzeilen entfernen; der Kommentarkopf der Datei darf
|
||
// das erste Statement nicht verschlucken.
|
||
$clean = implode("\n", array_filter(
|
||
explode("\n", $statement),
|
||
static fn ($line) => !str_starts_with(ltrim($line), '--')
|
||
));
|
||
if (trim($clean) === '') {
|
||
continue;
|
||
}
|
||
$pdo->exec($clean);
|
||
}
|
||
$messages[] = 'Tabellen angelegt.';
|
||
|
||
$seedFile = WS_ROOT . '/sql/seed_spells.sql';
|
||
if (is_file($seedFile)) {
|
||
$seed = (string) file_get_contents($seedFile);
|
||
foreach (array_filter(array_map('trim', explode(";\n", $seed))) as $statement) {
|
||
$clean = implode("\n", array_filter(
|
||
explode("\n", $statement),
|
||
static fn ($line) => !str_starts_with(ltrim($line), '--')
|
||
));
|
||
if (trim($clean) === '') {
|
||
continue;
|
||
}
|
||
$pdo->exec($clean);
|
||
}
|
||
$messages[] = 'Zauber und Liturgien aus dem Vault übernommen (' . (int) $pdo->query('SELECT COUNT(*) FROM spells')->fetchColumn() . ').';
|
||
}
|
||
|
||
$equipmentSeed = WS_ROOT . '/sql/seed_equipment.sql';
|
||
if (is_file($equipmentSeed)) {
|
||
$seed = (string) file_get_contents($equipmentSeed);
|
||
foreach (array_filter(array_map('trim', explode(";\n", $seed))) as $statement) {
|
||
$clean = implode("\n", array_filter(
|
||
explode("\n", $statement),
|
||
static fn ($line) => !str_starts_with(ltrim($line), '--')
|
||
));
|
||
if (trim($clean) === '') {
|
||
continue;
|
||
}
|
||
$pdo->exec($clean);
|
||
}
|
||
$messages[] = 'Waffen und Rüstungen angelegt.';
|
||
}
|
||
|
||
$sync = skills_sync_catalog();
|
||
$messages[] = 'Fertigkeiten-Katalog angelegt (' . (int) $pdo->query('SELECT COUNT(*) FROM skills')->fetchColumn()
|
||
. ' Fertigkeiten in ' . count(WS_SKILL_GROUPS) . ' Gruppen).';
|
||
$messages[] = 'Konstitution zählt nur einmal auf die Trefferpunkte (Hausregel V1.1) – Trag das im Bogen selbst ein.';
|
||
|
||
$stmt = $pdo->prepare(
|
||
'INSERT INTO users (username, email, display_name, password_hash, role, status,
|
||
must_change_password, created_at, updated_at, password_changed_at)
|
||
VALUES (?, ?, ?, ?, \'admin\', \'active\', 0, ?, ?, ?)'
|
||
);
|
||
$stmt->execute([
|
||
$username,
|
||
$email === '' ? null : $email,
|
||
$username,
|
||
hash_password($password),
|
||
utc_now(),
|
||
utc_now(),
|
||
utc_now(),
|
||
]);
|
||
$messages[] = 'Admin-Konto „' . $username . '“ angelegt.';
|
||
$done = true;
|
||
} catch (Throwable $error) {
|
||
$errors[] = 'Installation fehlgeschlagen: ' . $error->getMessage();
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
$base = rtrim((string) ($config['app']['base_path'] ?? ''), '/');
|
||
$loginUrl = $base . '/login';
|
||
?>
|
||
<!doctype html>
|
||
<html lang="de">
|
||
<head>
|
||
<meta charset="utf-8">
|
||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||
<meta name="robots" content="noindex, nofollow">
|
||
<title>Westreiche · Installation</title>
|
||
<link rel="stylesheet" href="<?= htmlspecialchars($base . '/assets/app.css', ENT_QUOTES) ?>">
|
||
</head>
|
||
<body class="auth-shell">
|
||
<div class="auth-stars" aria-hidden="true"></div>
|
||
<main class="auth-card">
|
||
<p class="brand-kicker">Westreiche</p>
|
||
<h1>Installation</h1>
|
||
|
||
<?php foreach ($errors as $message): ?>
|
||
<p class="alert alert-error"><?= htmlspecialchars($message, ENT_QUOTES) ?></p>
|
||
<?php endforeach; ?>
|
||
<?php foreach ($messages as $message): ?>
|
||
<p class="alert alert-success"><?= htmlspecialchars($message, ENT_QUOTES) ?></p>
|
||
<?php endforeach; ?>
|
||
|
||
<?php if ($alreadyInstalled): ?>
|
||
<p>Die Installation ist bereits erfolgt (erstes Konto: <strong><?= htmlspecialchars($installedUser, ENT_QUOTES) ?></strong>).</p>
|
||
<p><a class="btn btn-primary" href="<?= htmlspecialchars($loginUrl, ENT_QUOTES) ?>">Zur Anmeldung</a></p>
|
||
<?php elseif ($done): ?>
|
||
<p>Fertig. Du kannst dich jetzt anmelden.</p>
|
||
<p><a class="btn btn-primary" href="<?= htmlspecialchars($loginUrl, ENT_QUOTES) ?>">Zur Anmeldung</a></p>
|
||
<?php elseif ($tokenOk): ?>
|
||
<form method="post" class="form-grid">
|
||
<input type="hidden" name="token" value="<?= htmlspecialchars($token, ENT_QUOTES) ?>">
|
||
<label class="field">
|
||
<span>Benutzername (Spielleiter)</span>
|
||
<input type="text" name="username" value="<?= htmlspecialchars((string) ($_POST['username'] ?? 'Torsten'), ENT_QUOTES) ?>" required maxlength="60" autocomplete="username">
|
||
</label>
|
||
<label class="field">
|
||
<span>E-Mail (optional)</span>
|
||
<input type="email" name="email" value="<?= htmlspecialchars((string) ($_POST['email'] ?? ''), ENT_QUOTES) ?>" maxlength="190" autocomplete="email">
|
||
</label>
|
||
<label class="field">
|
||
<span>Passwort</span>
|
||
<input type="password" name="password" required minlength="8" autocomplete="new-password">
|
||
</label>
|
||
<label class="field">
|
||
<span>Passwort wiederholen</span>
|
||
<input type="password" name="password_repeat" required minlength="8" autocomplete="new-password">
|
||
</label>
|
||
<button class="btn btn-primary" type="submit">Westreiche errichten</button>
|
||
</form>
|
||
<?php endif; ?>
|
||
</main>
|
||
</body>
|
||
</html>
|