From 09d601e3037bb67839c0314e86d4803f1b360373 Mon Sep 17 00:00:00 2001 From: danny Date: Sun, 14 Jun 2026 14:41:36 +0200 Subject: [PATCH] Recreate setup.php with auto-delete after successful admin creation --- setup.php | 158 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 158 insertions(+) create mode 100644 setup.php diff --git a/setup.php b/setup.php new file mode 100644 index 0000000..b6b9ee0 --- /dev/null +++ b/setup.php @@ -0,0 +1,158 @@ + 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(); + } + } +} +?> + + + + + +Setup – Black Sheep Bluff + + + +
+

🐑 Admin Setup

+ + + +
+ ✅ Admin account created successfully.

+ You can now log in and start playing. +
+ +

🗑 setup.php has been deleted automatically.

+ +
⚠️ Could not delete setup.php automatically — please remove it manually from your server.
+ + + + +

Creates the first admin account. This file will delete itself after a successful setup.

+ + +
+ + +
+ + + + + + + + + + +
+ +
⚠️ Run setup only once. The file deletes itself on success — if it doesn't, remove it manually.
+ + +
+ +