rinkplanner/hub/install.php
torsten 055a2919c3 feat: Kür-Hub mit Einladungen, Uploads und API
- PHP-App unter hub/ für rinkplanner-hub.programmschmiede.de
- MariaDB-Schema, Argon2id, Sessions, CSRF und Ratenbegrenzung
- Einladungscodes, Admin-Bereich und Kür-Pakete mit Audiodatei
- JSON-API v1 mit Bearer-Tokens für die Godot-App
- Deployment und Erstinstallation über tools/deploy_hub.sh
- Doku und Credentials-Vorlage ergänzt
2026-11-11 13:14:26 +01:00

135 lines
4.2 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
declare(strict_types=1);
/**
* Einmal-Installer für den Rinkplanner-Hub.
*
* Wird beim Deployment mit hochgeladen, per HTTPS mit dem Installations-Token
* aufgerufen und löscht sich nach Erfolg selbst. Vorher legt er das Schema an
* und erzeugt den ersten Benutzer als Administrator.
*
* Aufruf nur per POST:
* token, admin_user, admin_password, admin_display, admin_email (optional)
*/
require __DIR__ . '/app/bootstrap.php';
function install_fail(int $status, string $message): never
{
http_response_code($status);
header('Content-Type: text/plain; charset=utf-8');
echo $message . "\n";
exit;
}
if (request_method() !== 'POST') {
install_fail(405, 'Nur POST.');
}
$expected = (string) config('app.install_token', '');
$given = (string) ($_POST['token'] ?? '');
if ($expected === '' || $given === '' || !hash_equals($expected, $given)) {
install_fail(403, 'Installations-Token ungültig.');
}
if (is_file(storage_path('.installed'))) {
install_fail(409, 'Bereits installiert. Zum Neuaufsetzen storage/.installed entfernen.');
}
$adminUser = strtolower(trim((string) ($_POST['admin_user'] ?? '')));
$adminPassword = (string) ($_POST['admin_password'] ?? '');
$adminDisplay = trim((string) ($_POST['admin_display'] ?? ''));
$adminEmail = trim((string) ($_POST['admin_email'] ?? ''));
if (preg_match('/^[a-z0-9](?:[a-z0-9._-]{1,30}[a-z0-9])$/', $adminUser) !== 1) {
install_fail(422, 'Admin-Benutzername ungültig (3–32 Zeichen, Kleinbuchstaben/Ziffern/._-).');
}
if (mb_strlen($adminPassword, 'UTF-8') < 10) {
install_fail(422, 'Admin-Passwort muss mindestens 10 Zeichen haben.');
}
if ($adminDisplay === '') {
$adminDisplay = $adminUser;
}
if ($adminEmail !== '' && filter_var($adminEmail, FILTER_VALIDATE_EMAIL) === false) {
install_fail(422, 'Admin-E-Mail ungültig.');
}
$pdo = db();
$hasUsers = false;
try {
$hasUsers = (int) $pdo->query('SELECT COUNT(*) FROM users')->fetchColumn() > 0;
} catch (PDOException) {
$hasUsers = false;
}
if ($hasUsers) {
install_fail(409, 'Es gibt bereits Benutzer. Installation abgebrochen.');
}
$schemaFile = __DIR__ . '/sql/schema.sql';
if (!is_file($schemaFile)) {
install_fail(500, 'schema.sql fehlt.');
}
try {
$sql = (string) file_get_contents($schemaFile);
$sql = preg_replace('/^\s*--.*$/m', '', $sql) ?? $sql;
$statements = preg_split('/;\s*(?:\r?\n|$)/', $sql) ?: [];
foreach ($statements as $statement) {
$statement = trim($statement);
if ($statement === '') {
continue;
}
try {
$pdo->exec($statement);
} catch (PDOException $error) {
// Ein erneuter Lauf darf an einer bereits vorhandenen
// Fremdschlüssel-Bedingung nicht scheitern.
$code = (int) ($error->errorInfo[1] ?? 0);
if (str_contains($statement, 'ADD CONSTRAINT')
&& in_array($code, [121, 1005, 1061, 1826], true)
) {
continue;
}
throw $error;
}
}
$stmt = $pdo->prepare(
'INSERT INTO users
(username, display_name, email, password_hash, role, status,
must_change_password, auth_epoch, created_at, password_changed_at)
VALUES (?, ?, ?, ?, ?, ?, 0, 1, ?, ?)'
);
$now = utc_now();
$stmt->execute([
$adminUser,
mb_substr($adminDisplay, 0, 64, 'UTF-8'),
$adminEmail !== '' ? $adminEmail : null,
hash_password($adminPassword),
'admin',
'active',
$now,
$now,
]);
$adminId = (int) $pdo->lastInsertId();
audit('install.admin', 'user', $adminId, $adminUser, $adminId);
foreach (['', 'logs', 'uploads'] as $sub) {
$dir = storage_path($sub);
if (!is_dir($dir)) {
@mkdir($dir, 0770, true);
}
}
$guard = storage_path('.htaccess');
if (!is_file($guard)) {
@file_put_contents($guard, "Require all denied\n");
}
@file_put_contents(storage_path('.installed'), utc_now() . "\n");
} catch (Throwable $error) {
install_fail(500, 'Installation fehlgeschlagen: ' . $error->getMessage());
}
@unlink(__FILE__);
header('Content-Type: text/plain; charset=utf-8');
echo "installiert\n";
echo "admin=" . $adminUser . "\n";