From 6835d6832c2869f365c2260776f7c966b889e556 Mon Sep 17 00:00:00 2001
From: Sky Johnson
Date: Sat, 13 Jul 2024 23:15:25 -0500
Subject: [PATCH] Finish install module rewrite
---
public/css/dragon.css | 16 ++++++
public/install/doodie.php | 38 -------------
server/app/app.php | 10 ++++
server/app/database.php | 5 ++
server/bootstrap.php | 4 ++
server/library.php | 19 -------
server/models/Classes.php | 10 ++++
server/models/Player.php | 13 +++++
server/modules/InstallModule.php | 53 +++++++++++++++++++
server/templates/install/done.php | 2 +-
server/templates/install/first.php | 7 ++-
server/templates/install/four.php | 7 +++
.../templates/install/partials/adminForm.php | 10 ++--
13 files changed, 130 insertions(+), 64 deletions(-)
delete mode 100644 public/install/doodie.php
create mode 100644 server/models/Classes.php
create mode 100644 server/models/Player.php
create mode 100644 server/templates/install/four.php
diff --git a/public/css/dragon.css b/public/css/dragon.css
index 01c72c1..a6e81a8 100644
--- a/public/css/dragon.css
+++ b/public/css/dragon.css
@@ -38,7 +38,23 @@ body {
margin-bottom: 2rem;
}
+.my-1 {
+ margin-top: 1rem;
+ margin-bottom: 1rem;
+}
+
.my-2 {
margin-top: 2rem;
margin-bottom: 2rem;
+}
+
+div.alert {
+ font-size: 0.9rem;
+ padding: 0.5rem;
+ background-color: gray;
+
+ &.is-danger {
+ color: hsl(359deg, 68%, 11%);
+ background-color: hsl(359deg, 68%, 71%);
+ }
}
\ No newline at end of file
diff --git a/public/install/doodie.php b/public/install/doodie.php
deleted file mode 100644
index 5d76312..0000000
--- a/public/install/doodie.php
+++ /dev/null
@@ -1,38 +0,0 @@
- 'Admin Account', 'step' => 'third', 'errors' => $errors, 'complete' => $_POST['complete'] ?? false]);
- exit;
- }
-
- // Create the .installed file in the server folder
- file_put_contents(SERVER.'/.installed', 'Installed on '.date('Y-m-d H:i:s'));
-
- // Create the admin account
- createUser($_POST['username'], $_POST['password'], $_POST['email'], $class, ['role' => 5, 'verified' => 1]);
-
- // Render the finished page!
- echo render('install/layout', ['title' => 'Finished!', 'step' => 'done', 'name' => $_POST['username'], 'complete' => $_POST['complete'] ?? false]);
- exit;
-}
diff --git a/server/app/app.php b/server/app/app.php
index 74f73eb..1963222 100644
--- a/server/app/app.php
+++ b/server/app/app.php
@@ -7,11 +7,21 @@
class App
{
public static Database $db;
+ private static string $dbPath;
public static Request $req;
public function __construct(string $dbPath)
{
self::$req = new Request(); // the current request
self::$db = new Database($dbPath); // the database
+ self::$dbPath = $dbPath; // the database path
+ }
+
+ public static function performDatabaseReset(): void
+ {
+ if (file_exists(self::$dbPath)) {
+ unlink(self::$dbPath);
+ self::$db = new Database(self::$dbPath);
+ }
}
}
diff --git a/server/app/database.php b/server/app/database.php
index 893f922..536b092 100644
--- a/server/app/database.php
+++ b/server/app/database.php
@@ -82,6 +82,11 @@ class Database
return $this->time;
}
+ public function lastInsertID(): int
+ {
+ return $this->c->lastInsertId();
+ }
+
public function insertFromCSV(string $table, string $path): PDOStatement|false
{
// open the file
diff --git a/server/bootstrap.php b/server/bootstrap.php
index 5217a25..bf5933c 100644
--- a/server/bootstrap.php
+++ b/server/bootstrap.php
@@ -30,6 +30,10 @@ const MAP = [
// modules
'HomeModule' => SERVER.'/modules/HomeModule.php',
'InstallModule' => SERVER.'/modules/InstallModule.php',
+
+ // models
+ 'Classes' => SERVER.'/models/Classes.php',
+ 'Player' => SERVER.'/models/Player.php',
];
// autoloader
diff --git a/server/library.php b/server/library.php
index 7066cb6..e2bfa12 100644
--- a/server/library.php
+++ b/server/library.php
@@ -57,25 +57,6 @@ function dd(mixed $var, bool $r = false)
exit;
}
-/**
- * Creates a new user. Optionally pass an array of additional fields to add. Returns the user ID, or 0 if failed.
- */
-function createUser(string $username, string $password, string $email, int $class = 1, array $addtl = []): int
-{
- // @BAD Yes, this is bad, but it works.
- global $db;
-
- $data = [
- 'username' => trim($username),
- 'password' => password_hash($password, PASSWORD_ARGON2ID),
- 'email' => trim($email),
- 'class_id' => $class
- ];
-
- $db->table('players')->insert(array_merge($data, $addtl));
- return $db->lastInsertId();
-}
-
function getmicrotime() { // Used for timing script operations.
list($usec, $sec) = explode(" ",microtime());
diff --git a/server/models/Classes.php b/server/models/Classes.php
new file mode 100644
index 0000000..042672e
--- /dev/null
+++ b/server/models/Classes.php
@@ -0,0 +1,10 @@
+q("SELECT * FROM classes");
+ return $res->fetchAll() ?: false;
+ }
+}
\ No newline at end of file
diff --git a/server/models/Player.php b/server/models/Player.php
new file mode 100644
index 0000000..e81914b
--- /dev/null
+++ b/server/models/Player.php
@@ -0,0 +1,13 @@
+do("INSERT INTO 'players' ($keys) VALUES ($placeholders);", array_values($data));
+ return App::$db->lastInsertID();
+ }
+}
\ No newline at end of file
diff --git a/server/modules/InstallModule.php b/server/modules/InstallModule.php
index 8a25d20..04b0290 100644
--- a/server/modules/InstallModule.php
+++ b/server/modules/InstallModule.php
@@ -13,6 +13,8 @@ class InstallModule
if ($s == '' || $s == 'intro') return self::intro();
if ($s == 'database' && $m == 'POST') return self::database();
+ if ($s == 'finish' && $m == 'POST') return self::finish();
+ return self::fourOhFour();
}
private static function intro()
@@ -27,6 +29,9 @@ class InstallModule
$complete = $_POST['mode'] == 'complete'; // complete or partial setup
$defaults = SERVER.'/database/packs/Default/';
+ // if the database already exists, have the app remake it
+ App::performDatabaseReset();
+
// @Settings
App::$db->q("CREATE TABLE IF NOT EXISTS 'settings' (
'id' INTEGER PRIMARY KEY,
@@ -232,4 +237,52 @@ class InstallModule
echo render('install/layout', ['title' => 'Database Setup', 'step' => 'second', 'complete' => $complete, 'start' => $istart]);
}
+
+ private static function finish()
+ {
+ $errors = [];
+
+ // Make sure our info is at least mostly valid
+ if (!required(['username', 'password', 'email'])) {
+ $errors[] = 'All fields are required.';
+ } else {
+ if (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
+ $errors[] = 'Invalid email address format.';
+ }
+
+ if (strlen($_POST['password']) < 6) {
+ $errors[] = 'Password must be at least 6 characters long.';
+ }
+ }
+
+ // Make sure the class selection is valid
+ $class = isset($_POST['class']) && in_array($_POST['class'], [1, 2, 3]) ? $_POST['class'] : 1;
+
+ // If we have any errors, bail to the form and let the user know
+ if (!empty($errors)) {
+ echo render('install/layout', ['title' => 'Admin Account', 'step' => 'third', 'errors' => $errors, 'complete' => $_POST['complete'] ?? false]);
+ exit;
+ }
+
+ // Create the .installed file in the server folder
+ file_put_contents(SERVER.'/.installed', 'Installed on '.date('Y-m-d H:i:s'));
+
+ // Create the admin account
+ Player::create([
+ 'username' => trim($_POST['username']),
+ 'password' => password_hash($_POST['password'], PASSWORD_ARGON2ID),
+ 'email' => trim($_POST['email']),
+ 'class_id' => $class,
+ 'verified' => 1,
+ 'role' => 5
+ ]);
+
+ // Render the finished page!
+ echo render('install/layout', ['title' => 'Finished!', 'step' => 'done', 'name' => $_POST['username'], 'complete' => $_POST['complete'] ?? false]);
+ }
+
+ private static function fourOhFour()
+ {
+ echo render('install/layout', ['title' => 'Four Oh Four', 'step' => 'four']);
+ }
}
\ No newline at end of file
diff --git a/server/templates/install/done.php b/server/templates/install/done.php
index 00184ab..94bf1e1 100644
--- a/server/templates/install/done.php
+++ b/server/templates/install/done.php
@@ -6,7 +6,7 @@
- Click here to log in.
+ Click here to log in.
diff --git a/server/templates/install/first.php b/server/templates/install/first.php
index 0f0116c..237bad7 100644
--- a/server/templates/install/first.php
+++ b/server/templates/install/first.php
@@ -23,7 +23,7 @@
-
+
Click the appropriate button below for your preferred installation method.
+
+
+ WARNING: if the database already exists, clicking either
+ option will delete all existing data. This is not reversible.
+
diff --git a/server/templates/install/four.php b/server/templates/install/four.php
new file mode 100644
index 0000000..07d5f16
--- /dev/null
+++ b/server/templates/install/four.php
@@ -0,0 +1,7 @@
+
+ That's weird... you're not supposed to be here.
+
+
+
+ Go back.
+
\ No newline at end of file
diff --git a/server/templates/install/partials/adminForm.php b/server/templates/install/partials/adminForm.php
index 4ef4aad..101af37 100644
--- a/server/templates/install/partials/adminForm.php
+++ b/server/templates/install/partials/adminForm.php
@@ -1,9 +1,9 @@
-