diff --git a/public/css/dk.css b/public/css/dk.css index fc47fec..9d3457f 100644 --- a/public/css/dk.css +++ b/public/css/dk.css @@ -8,7 +8,11 @@ html { } body { - background-image: url('/img/background.jpg'); + background-image: url('/img/backgrounds/classic.jpg'); + + &.skin-1 { + background-image: url('/img/backgrounds/snowstorm.jpg'); + } } div#game-container { diff --git a/public/img/background.jpg b/public/img/backgrounds/classic.jpg similarity index 100% rename from public/img/background.jpg rename to public/img/backgrounds/classic.jpg diff --git a/public/img/backgrounds/snowstorm.jpg b/public/img/backgrounds/snowstorm.jpg new file mode 100644 index 0000000..01e17df Binary files /dev/null and b/public/img/backgrounds/snowstorm.jpg differ diff --git a/src/actions/install.php b/src/actions/install.php index 9cbfdb5..d9bbe92 100644 --- a/src/actions/install.php +++ b/src/actions/install.php @@ -41,6 +41,8 @@ function first() */ function second() { + if (file_exists('../database.db')) unlink('../database.db'); + echo "Dragon Knight InstallationDragon Knight Installation: Page Two

"; $query = db()->exec(<<' : 'Error creating Babble Box table.'; + echo table_status_msg($query === true, 'Babble', 'create'); $query = db()->exec(<<' : 'Error creating Control table.'; + echo table_status_msg($query === true, 'Control', 'create'); - $query = db()->exec("INSERT INTO control VALUES (1, 'Dragon Knight', 250, 1, {$_SERVER['SERVER_NAME']}, 'noreply@'.{$_SERVER['SERVER_NAME']}, 'Mage', 'Warrior', 'Paladin', 1, 1, 1, 1);"); + $query = db()->query("INSERT INTO control VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", [ + 1, 'Dragon Knight', 250, 1, $_SERVER['SERVER_NAME'], 'noreply@'.$_SERVER['SERVER_NAME'], + 'Mage', 'Warrior', 'Paladin', 1, 1, 1, 1 + ]); - echo $query === true ? 'Control table populated.
' : 'Error populating Control table.'; + echo table_status_msg($query !== false, 'Control', 'populate'); $query = db()->exec(<<' : 'Error creating Drops table.'; + echo table_status_msg($query === true, 'Drops', 'create'); $query = db()->exec(<<' : 'Error populating Drops table.'; + echo table_status_msg($query === true, 'Drops', 'populate'); $query = db()->exec(<<' : 'Error creating Forum table.'; + echo table_status_msg($query === true, 'Forum', 'create'); $query = db()->exec(<<' : 'Error creating Items table.'; + echo table_status_msg($query === true, 'Items', 'create'); $query = db()->exec(<<' : 'Error populating Items table.'; + echo table_status_msg($query === true, 'Drops', 'populate'); $query = db()->exec(<<' : 'Error creating Levels table.'; + echo table_status_msg($query === true, 'Levels', 'create'); $query = db()->exec(<<' : 'Error populating Levels table.'; + echo table_status_msg($query === true, 'Levels', 'populate'); $query = db()->exec(<<' : 'Error creating Monsters table.'; + echo table_status_msg($query === true, 'Monsters', 'create'); $query = db()->exec(<<' : 'Error populating Monsters table.'; + echo table_status_msg($query === true, 'Monsters', 'populate'); $query = db()->exec(<<' : 'Error creating News table.'; + echo table_status_msg($query === true, 'News', 'create'); $query = db()->exec("INSERT INTO news (content) VALUES ('This is the first news post. Please use the admin control panel to add another one and make this one go away.');"); - echo $query === true ? 'News table populated.
' : 'Error populating News table.'; + echo table_status_msg($query === true, 'News', 'populate'); $query = db()->exec(<<' : 'Error creating Spells table.'; + echo table_status_msg($query === true, 'Spells', 'create'); $query = db()->exec(<<' : 'Error populating Spells table.'; + echo table_status_msg($query === true, 'Spells', 'populate'); $query = db()->exec(<<' : 'Error creating Towns table.'; + echo table_status_msg($query === true, 'Towns', 'create'); $query = db()->exec(<<' : 'Error populating Towns table.'; + echo table_status_msg($query === true, 'Towns', 'populate'); $query = db()->exec(<<' : 'Error creating Users table.'; + echo table_status_msg($query === true, 'Users', 'create'); $time = round((microtime(true) - START), 4); echo "
Database setup complete in $time seconds.

Click here to continue with installation."; @@ -691,7 +697,7 @@ function fourth() $form = $form['data']; if (db()->query( - "INSERT INTO users (username, password, email, verify, charclass, authlevel) VALUES (?, ?, ?, 1, ?, 1)", + "INSERT INTO users (username, password, email, verify, charclass, authlevel) VALUES (?, ?, ?, 'g2g', ?, 1)", [$form['username'], password_hash($form['password'], PASSWORD_ARGON2ID), $form['email'], $form['charclass']] ) === false) { exit("Failed to create user."); @@ -742,3 +748,17 @@ function fifth() HTML; } + +function table_status_msg(bool $condition, string $table_name, string $verb): string +{ + $verb = match($verb) { + 'create' => ['created', 'creating'], + 'populate' => ['populated', 'populating'] + }; + + if ($condition === false) { + return "Error {$verb[1]} $table_name table. (".db()->lastErrorMsg().")
"; + } + + return "$table_name table {$verb[0]}.
"; +} diff --git a/src/actions/users.php b/src/actions/users.php index a386cb7..07c267b 100644 --- a/src/actions/users.php +++ b/src/actions/users.php @@ -12,6 +12,7 @@ function register_routes(Router $r): Router $r->form('/lostpassword', 'Users\lostpassword'); $r->form('/changepassword', 'Users\changepassword'); $r->form('/verify', 'Users\verify'); + $r->form('/settings', 'Users\settings'); return $r; } @@ -183,6 +184,26 @@ function changepassword() display(render('changepassword'), "Change Password", true, false, false); } +function settings() +{ + global $userrow; + + if ($_SERVER['REQUEST_METHOD'] === 'POST') { + $form = validate($_POST, [ + 'game_skin' => ['in:0,1'] + ]); + if (!$form['valid']) exit(ul_from_validate_errors($form['errors'])); + $form = $form['data']; + + db()->query('UPDATE users SET game_skin=? WHERE id=?;', [$form['game_skin'], $userrow['id']]); + + $alert = '
Settings updated
'; + display($alert . render('settings'), "Account Settings"); + } + + display(render('settings'), "Account Settings"); +} + function sendpassemail($emailaddress, $password) { global $controlrow; diff --git a/src/bootstrap.php b/src/bootstrap.php index bd171cd..1536b08 100644 --- a/src/bootstrap.php +++ b/src/bootstrap.php @@ -41,7 +41,7 @@ if (!file_exists('../.installed') && $uri[0] !== 'install') { } // Force verify if the user isn't verified yet. - if ($controlrow['verifyemail'] && $userrow['verify'] !== 'g2g' && $uri[0] !== 'verify') { + if ($controlrow['verifyemail'] && $userrow['verify'] !== 'g2g' && !in_array($uri[0], ['verify', 'logout'])) { redirect('/verify'); } diff --git a/src/database.php b/src/database.php index bcc7bd7..d00e110 100644 --- a/src/database.php +++ b/src/database.php @@ -28,12 +28,18 @@ class Database extends SQLite3 public function query(string $query, array $params = []): SQLite3Result|false { $p = strpos($query, '?') !== false; - $stmt = $this->prepare($query); - foreach ($params ?? [] as $k => $v) $stmt->bindValue($p ? $k + 1 : $k, $v, $this->getSQLiteType($v)); + try { + $stmt = $this->prepare($query); + foreach ($params ?? [] as $k => $v) $stmt->bindValue($p ? $k + 1 : $k, $v, $this->getSQLiteType($v)); + } catch (Exception $e) { + exit("Failed to prepare query ($query): ".$this->lastErrorMsg()); + } $start = microtime(true); - $r = $stmt->execute(); - $this->log($query, microtime(true) - $start); + $error = ''; + $r = $stmt->execute(); + + $this->log($query, microtime(true) - $start, $error); return $r; } @@ -68,11 +74,11 @@ class Database extends SQLite3 /** * Log the query, including the time it took. Increment the query counter. */ - private function log(string $query, float $time_taken): void + private function log(string $query, float $time_taken, string $error = ''): void { $this->count++; $this->query_time += $time_taken; - $this->log[] = [$query, $time_taken]; + $this->log[] = [$query, $time_taken, $error]; } /** diff --git a/src/lib.php b/src/lib.php index 6f68c41..466d99a 100644 --- a/src/lib.php +++ b/src/lib.php @@ -184,6 +184,7 @@ function display($content, $title, bool $topnav = true, bool $leftnav = true, bo "dkgamename" => $controlrow["gamename"], "title" => $title, "content" => $content, + "game_skin" => $userrow['game_skin'] ??= '0', 'rightnav' => $rightnav ? render('rightnav', ['user' => $userrow]) : '', "leftnav" => $leftnav ? render('leftnav', ['user' => $userrow]) : '', "topnav" => $topnav, diff --git a/templates/leftnav.php b/templates/leftnav.php index e89237e..cba34f6 100644 --- a/templates/leftnav.php +++ b/templates/leftnav.php @@ -26,6 +26,7 @@ Home
+ Settings
Change Password
Log Out
Help diff --git a/templates/minimal.php b/templates/minimal.php index dd6c7e7..237f937 100644 --- a/templates/minimal.php +++ b/templates/minimal.php @@ -1,63 +1,9 @@ + + <?= $title ?> - + diff --git a/templates/primary.php b/templates/primary.php index e6b42cc..cab075b 100644 --- a/templates/primary.php +++ b/templates/primary.php @@ -19,7 +19,7 @@ } - +
@@ -42,7 +42,11 @@ '; - foreach ($querylog as $record) echo '
['.round($record[1], 2)."s] {$record[0]}
"; + foreach ($querylog as $record) { + $query_string = str_replace(["\r\n", "\n", "\r"], ' ', $record[0]); + $error_string = !empty($record[2]) ? '// '.$record[2] : ''; + echo '
['.round($record[1], 2)."s] {$query_string}{$error_string}
"; + } echo ''; } ?> diff --git a/templates/settings.php b/templates/settings.php new file mode 100644 index 0000000..0db0f66 --- /dev/null +++ b/templates/settings.php @@ -0,0 +1,12 @@ +

Account Settings

+

Here you can change some basic settings for your account.

+ +
+ + + + +