drink-it-cold/install.php

178 lines
8.5 KiB
PHP

<?php
declare(strict_types=1);
/**
* Einmaliger Installer: legt das Schema an, setzt die Grunddaten und erzeugt
* den ersten Administrator. Wird nach erfolgreicher Installation vom
* Deployment entfernt.
*/
define('DIC_ROOT', is_dir(__DIR__ . '/app') ? __DIR__ : dirname(__DIR__));
define('DIC_APP', DIC_ROOT . '/app');
$configFile = DIC_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['dic_config'] = $config;
date_default_timezone_set('Europe/Berlin');
require DIC_APP . '/util.php';
require DIC_APP . '/db.php';
require DIC_APP . '/auth.php';
$expectedToken = (string) ($config['app']['install_token'] ?? '');
$token = (string) ($_POST['token'] ?? $_GET['token'] ?? '');
$tokenOk = $expectedToken !== '' && $token !== '' && hash_equals($expectedToken, $token);
$errors = [];
$done = false;
$alreadyInstalled = false;
$installedName = '';
if (!$tokenOk) {
http_response_code(403);
$errors[] = 'Der Installations-Token ist ungültig. Bitte den Link aus dem Deployment verwenden.';
} else {
try {
$pdo = db();
$usersTable = $pdo->query("SHOW TABLES LIKE 'users'")->fetchColumn();
if ($usersTable) {
$count = (int) $pdo->query('SELECT COUNT(*) FROM users')->fetchColumn();
if ($count > 0) {
$alreadyInstalled = true;
$row = $pdo->query('SELECT first_name, last_name FROM users ORDER BY id LIMIT 1')->fetch();
$installedName = $row ? $row['first_name'] . ' ' . $row['last_name'] : '';
}
}
} catch (Throwable $error) {
$errors[] = 'Datenbankverbindung fehlgeschlagen. Bitte die Konfiguration prüfen.';
}
}
$values = [
'first_name' => trim((string) ($_POST['first_name'] ?? '')),
'last_name' => trim((string) ($_POST['last_name'] ?? '')),
];
if ($tokenOk && !$alreadyInstalled && $_SERVER['REQUEST_METHOD'] === 'POST' && !$errors) {
$password = (string) ($_POST['password'] ?? '');
$repeat = (string) ($_POST['password_repeat'] ?? '');
if ($values['first_name'] === '' || $values['last_name'] === '') {
$errors[] = 'Bitte Vor- und Nachnamen angeben.';
}
if (mb_strlen($password) < 8) {
$errors[] = 'Das Passwort muss mindestens 8 Zeichen haben.';
}
if ($password !== $repeat) {
$errors[] = 'Die Passwörter stimmen nicht überein.';
}
if (!$errors) {
try {
$pdo = db();
$schema = (string) file_get_contents(DIC_APP . '/../sql/schema.sql');
$schema = preg_replace('/^\s*--.*$/m', '', $schema) ?? $schema;
foreach (array_filter(array_map('trim', explode(';', $schema))) as $statement) {
if ($statement !== '') {
$pdo->exec($statement);
}
}
$stmt = $pdo->prepare(
'INSERT INTO users (first_name, last_name, email, password_hash, role, status, employee_id,
must_change_password, auth_epoch, created_at, updated_at, last_login_at, password_changed_at)
VALUES (?, ?, NULL, ?, "admin", "active", NULL, 0, 1, ?, ?, NULL, ?)'
);
$stmt->execute([$values['first_name'], $values['last_name'], hash_password($password), utc_now(), utc_now(), utc_now()]);
$done = true;
} catch (Throwable $error) {
$errors[] = 'Installation fehlgeschlagen: ' . $error->getMessage();
}
}
}
$base = rtrim((string) ($config['app']['base_path'] ?? ''), '/');
$css = ($base !== '' ? $base : '') . '/assets/app.css';
?>
<!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>Installation · Drink it cold</title>
<link rel="icon" type="image/svg+xml" href="<?= htmlspecialchars(($base !== '' ? $base : '') . '/assets/favicon.svg') ?>">
<link rel="stylesheet" href="<?= htmlspecialchars($css) ?>">
</head>
<body class="auth-page">
<div class="auth-shell">
<section class="auth-visual" aria-hidden="true">
<img class="auth-visual__bg" src="<?= htmlspecialchars(($base !== '' ? $base : '') . '/assets/img/hero-1280.webp') ?>" alt="">
<div class="auth-visual__shade"></div>
<div class="auth-visual__content">
<div class="auth-brand">
<img src="<?= htmlspecialchars(($base !== '' ? $base : '') . '/assets/logo-mark.svg') ?>" alt="" width="56" height="40">
<span><strong>Drink it cold</strong><small>Kühlanhänger-Vermietung</small></span>
</div>
<div class="auth-visual__claim">
<p class="eyebrow">Einrichtung</p>
<h2>Einmal einrichten.<br>Dann läuft es.</h2>
<p>Der Installer legt die in der Anforderung genannten Start-Anhänger, das Mitarbeiter-Team und den ersten Administrator an.</p>
</div>
<p class="auth-visual__foot">Drink it cold · Neuss</p>
</div>
</section>
<section class="auth-panel">
<div class="auth-card">
<p class="eyebrow">Installation</p>
<h1>Drink it cold</h1>
<?php if ($alreadyInstalled): ?>
<p class="auth-card__lede">Die Anwendung ist bereits eingerichtet<?= $installedName !== '' ? ' (erster Benutzer: ' . htmlspecialchars($installedName) . ')' : '' ?>.</p>
<div class="alert alert--error"><strong>Der Installer ist gesperrt.</strong><p>Bitte die Datei install.php auf dem Server löschen, falls das noch nicht geschehen ist.</p></div>
<a class="btn primary btn--block" href="<?= htmlspecialchars(($base !== '' ? $base : '') . '/login') ?>">Zur Anmeldung</a>
<?php elseif ($done): ?>
<p class="auth-card__lede">Die Anwendung ist eingerichtet.</p>
<div class="alert" style="background:var(--green-soft);border-color:#bfe3bf;color:#1f5a23">
<strong>Fertig.</strong><p>Der Administrator wurde angelegt. Bitte install.php jetzt vom Server entfernen.</p>
</div>
<a class="btn primary btn--block" href="<?= htmlspecialchars(($base !== '' ? $base : '') . '/login') ?>">Jetzt anmelden</a>
<?php else: ?>
<p class="auth-card__lede">Erster Administrator und Grunddaten. Der Token steht in der Server-Konfiguration.</p>
<?php if ($errors): ?>
<div class="alert alert--error" role="alert"><div><strong>Bitte prüfen.</strong>
<?php foreach ($errors as $error): ?><p><?= htmlspecialchars($error) ?></p><?php endforeach; ?>
</div></div>
<?php endif; ?>
<form method="post" class="auth-form">
<?php if ($tokenOk): ?>
<input type="hidden" name="token" value="<?= htmlspecialchars($token) ?>">
<?php else: ?>
<label class="field"><span>Installations-Token</span><input type="text" name="token" required autocomplete="off"></label>
<?php endif; ?>
<div class="field-row">
<label class="field"><span>Vorname</span><input type="text" name="first_name" value="<?= htmlspecialchars($values['first_name']) ?>" required autofocus></label>
<label class="field"><span>Nachname</span><input type="text" name="last_name" value="<?= htmlspecialchars($values['last_name']) ?>" required></label>
</div>
<label class="field"><span>Passwort</span><input type="password" name="password" minlength="8" required autocomplete="new-password"></label>
<label class="field"><span>Passwort wiederholen</span><input type="password" name="password_repeat" minlength="8" required autocomplete="new-password"></label>
<button class="btn primary btn--block" type="submit">Installation starten</button>
</form>
<?php endif; ?>
<p class="auth-card__legal">
<a href="<?= htmlspecialchars(($base !== '' ? $base : '') . '/impressum') ?>">Impressum</a>
<span aria-hidden="true">·</span>
<a href="<?= htmlspecialchars(($base !== '' ? $base : '') . '/datenschutz') ?>">Datenschutz</a>
</p>
</div>
</section>
</div>
</body>
</html>