Recreate setup.php with auto-delete after successful admin creation

This commit is contained in:
danny 2026-06-14 14:41:36 +02:00
parent e1968d3e04
commit 09d601e303

158
setup.php Normal file
View file

@ -0,0 +1,158 @@
<?php
/**
* One-time admin setup.
* Visit: https://yourdomain.com/setup.php
* This file deletes itself automatically after a successful setup.
*/
declare(strict_types=1);
$cfg = require __DIR__ . '/config.php';
function db(array $cfg): PDO
{
$dsn = sprintf('mysql:host=%s;dbname=%s;charset=%s', $cfg['host'], $cfg['dbname'], $cfg['charset']);
return new PDO($dsn, $cfg['user'], $cfg['pass'], [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
]);
}
$error = '';
$success = false;
$deleted = false;
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$adminUser = trim($_POST['username'] ?? '');
$adminPass = $_POST['password'] ?? '';
$adminPass2 = $_POST['password2'] ?? '';
if (!preg_match('/^[a-zA-Z0-9_\-]{2,40}$/', $adminUser)) {
$error = 'Username: 2–40 characters, only letters / digits / _ / -';
} elseif (strlen($adminPass) < 8) {
$error = 'Password must be at least 8 characters.';
} elseif ($adminPass !== $adminPass2) {
$error = 'Passwords do not match.';
} else {
try {
$pdo = db($cfg);
// Run migrations so all tables exist before we insert
require __DIR__ . '/migrate.php';
runMigrations($pdo, $cfg['dbname']);
$check = $pdo->prepare("SELECT id FROM gtl_users WHERE role = 'admin' LIMIT 1");
$check->execute();
if ($check->fetch()) {
$error = 'An admin account already exists. Delete this file manually.';
} else {
$hash = password_hash($adminPass, PASSWORD_DEFAULT);
$now = time();
$ins = $pdo->prepare(
"INSERT INTO gtl_users (username, credential_hash, role, created_at)
VALUES (?, ?, 'admin', ?)"
);
$ins->execute([$adminUser, $hash, $now]);
$uid = (int) $pdo->lastInsertId();
$pdo->prepare("INSERT INTO gtl_user_stats (user_id) VALUES (?)")->execute([$uid]);
// Ensure upload directory exists
$dir = __DIR__ . '/uploads/avatars';
if (!is_dir($dir)) {
mkdir($dir, 0755, true);
}
$success = htmlspecialchars($adminUser);
// Self-destruct — delete this file now that setup is complete
$deleted = @unlink(__FILE__);
}
} catch (Throwable $e) {
$error = 'Database error: ' . $e->getMessage();
}
}
}
?><!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="robots" content="noindex, nofollow">
<title>Setup – Black Sheep Bluff</title>
<style>
*{box-sizing:border-box;margin:0;padding:0}
body{font-family:system-ui,sans-serif;background:#0e1117;color:#e8edf8;
min-height:100vh;display:flex;align-items:center;justify-content:center;padding:20px}
.box{background:#1a2030;border:1px solid #2a3550;border-radius:18px;
padding:32px;width:100%;max-width:400px}
h1{font-size:22px;font-weight:800;margin-bottom:6px;color:#3ECC5C}
p.sub{color:#7a8aaa;font-size:13px;margin-bottom:24px;line-height:1.5}
label{display:block;font-size:12px;color:#7a8aaa;font-weight:600;
text-transform:uppercase;letter-spacing:.6px;margin-bottom:6px}
input{width:100%;background:#0e1117;border:1.5px solid #2a3550;color:#e8edf8;
border-radius:12px;padding:12px 14px;margin-bottom:16px;font-size:15px;
font-family:inherit;outline:none;transition:border-color .15s}
input:focus{border-color:#3ECC5C}
button{width:100%;background:linear-gradient(180deg,#65DC7A,#3ECC5C);
color:#0a2010;border:none;border-radius:12px;padding:14px;
font-weight:700;font-size:15px;cursor:pointer;font-family:inherit;
transition:filter .15s}
button:hover{filter:brightness(1.07)}
.err{background:rgba(232,93,92,.13);border:1px solid rgba(232,93,92,.35);
color:#f0a0a0;padding:11px 14px;border-radius:12px;font-size:13px;margin-bottom:16px}
.ok{background:rgba(43,181,133,.13);border:1px solid rgba(43,181,133,.35);
color:#5fd3a8;padding:14px;border-radius:12px;font-size:14px;line-height:1.6}
.ok strong{color:#e8edf8}
.warn{margin-top:16px;font-size:12px;color:#ffcf6e;
background:rgba(255,207,110,.08);border:1px solid rgba(255,207,110,.25);
border-radius:8px;padding:10px 12px;line-height:1.5}
.gone{margin-top:12px;font-size:12px;color:#7a8aaa;font-style:italic}
</style>
</head>
<body>
<div class="box">
<h1>🐑 Admin Setup</h1>
<?php if ($success): ?>
<div class="ok">
✅ Admin account <strong><?= $success ?></strong> created successfully.<br><br>
You can now <a href="login.php" style="color:#3ECC5C">log in</a> and start playing.
</div>
<?php if ($deleted): ?>
<p class="gone">🗑 setup.php has been deleted automatically.</p>
<?php else: ?>
<div class="warn">⚠️ Could not delete setup.php automatically — please remove it manually from your server.</div>
<?php endif; ?>
<?php else: ?>
<p class="sub">Creates the first admin account. This file will delete itself after a successful setup.</p>
<?php if ($error): ?>
<div class="err"><?= htmlspecialchars($error) ?></div>
<?php endif; ?>
<form method="POST">
<label for="username">Admin username</label>
<input id="username" type="text" name="username"
value="<?= htmlspecialchars($_POST['username'] ?? 'admin') ?>"
maxlength="40" autocomplete="off" autofocus>
<label for="password">Password (min. 8 characters)</label>
<input id="password" type="password" name="password" autocomplete="new-password">
<label for="password2">Confirm password</label>
<input id="password2" type="password" name="password2" autocomplete="new-password">
<button type="submit">Create admin account</button>
</form>
<div class="warn">⚠️ Run setup only once. The file deletes itself on success — if it doesn't, remove it manually.</div>
<?php endif; ?>
</div>
</body>
</html>