diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..28d54e6
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+.installed
+database.db
diff --git a/config.php b/config.php
deleted file mode 100644
index b1d2606..0000000
--- a/config.php
+++ /dev/null
@@ -1,11 +0,0 @@
- "localhost", // MySQL server name. (Default: localhost)
- "user" => "", // MySQL username.
- "pass" => "", // MySQL password.
- "name" => "", // MySQL database name.
- "prefix" => "dk", // Prefix for table names. (Default: dk)
- "secretword" => ""); // Secret word used when hashing information for cookies.
-
-?>
\ No newline at end of file
diff --git a/cookies.php b/cookies.php
deleted file mode 100644
index 1ffc5df..0000000
--- a/cookies.php
+++ /dev/null
@@ -1,32 +0,0 @@
-
\ No newline at end of file
diff --git a/database.php b/database.php
new file mode 100644
index 0000000..1f988fc
--- /dev/null
+++ b/database.php
@@ -0,0 +1,71 @@
+prepare($query);
+
+ foreach ($params ?? [] as $k => $v) $stmt->bindValue($p ? $k + 1 : $k, $v, $this->getSQLiteType($v));
+
+ $start = microtime(true);
+ $r = $stmt->execute();
+ $this->log($query, microtime(true) - $start);
+
+ return $r;
+ }
+
+ public function exec(string $query): bool
+ {
+ $start = microtime(true);
+ $r = parent::exec($query);
+ $this->log($query, microtime(true) - $start);
+ return $r;
+ }
+
+ public function exists(string $table, string $column, mixed $value, bool $case_insensitive = true): bool
+ {
+ if ($case_insensitive) {
+ $query = "SELECT 1 FROM $table WHERE $column = :v COLLATE NOCASE LIMIT 1";
+ } else {
+ $query = "SELECT 1 FROM $table WHERE $column = :v LIMIT 1";
+ }
+
+ $result = $this->query($query, [':v' => $value]);
+ return $result->fetchArray(SQLITE3_NUM) !== false;
+ }
+
+ private function log(string $query, float $time_taken): void
+ {
+ $this->count++;
+ $this->query_time += $time_taken;
+ $this->log[] = [$query, $time_taken];
+ }
+
+ private function getSQLiteType(mixed $value): int
+ {
+ return match (true) {
+ is_int($value) => SQLITE3_INTEGER,
+ is_float($value) => SQLITE3_FLOAT,
+ is_null($value) => SQLITE3_NULL,
+ default => SQLITE3_TEXT
+ };
+ }
+}
diff --git a/lib.php b/lib.php
index 4e7b47e..d8bb741 100644
--- a/lib.php
+++ b/lib.php
@@ -1,28 +1,18 @@
-query('SELECT * FROM control WHERE id=1 LIMIT 1;');
+ $controlrow = $query->fetchArray(SQLITE3_ASSOC);
}
- $template = gettemplate("admin");
-
- // Make page tags for XHTML validation.
- $xml = "\n"
- . "\n"
- . "\n";
-
- $finalarray = array(
- "title"=>$title,
+ $page = parsetemplate(gettemplate("admin"), [
+ "title"=>$title,
"content"=>$content,
"totaltime"=>round(getmicrotime() - $starttime, 4),
"numqueries"=>$numqueries,
"version"=>$version,
- "build"=>$build);
- $page = parsetemplate($template, $finalarray);
- $page = $xml . $page;
+ "build"=>$build
+ ]);
- if ($controlrow["compression"] == 1) { ob_start("ob_gzhandler"); }
- echo $page;
- die();
+ echo "\n" . $page;
+ exit;
}
function display($content, $title, $topnav=true, $leftnav=true, $rightnav=true, $badstart=false) { // Finalize page and output to browser.
global $numqueries, $userrow, $controlrow, $version, $build;
if (!isset($controlrow)) {
- $controlquery = doquery("SELECT * FROM {{table}} WHERE id='1' LIMIT 1", "control");
- $controlrow = mysql_fetch_array($controlquery);
+ $query = db()->query('SELECT * FROM control WHERE id=1 LIMIT 1;');
+ $controlrow = $query->fetchArray(SQLITE3_ASSOC);
}
if ($badstart == false) { global $starttime; } else { $starttime = $badstart; }
- // Make page tags for XHTML validation.
- $xml = "\n"
- . "\n"
- . "\n";
-
- $template = gettemplate("primary");
-
if ($rightnav == true) { $rightnav = gettemplate("rightnav"); } else { $rightnav = ""; }
if ($leftnav == true) { $leftnav = gettemplate("leftnav"); } else { $leftnav = ""; }
if ($topnav == true) {
@@ -137,14 +111,14 @@ function display($content, $title, $topnav=true, $leftnav=true, $rightnav=true,
if (isset($userrow)) {
// Get userrow again, in case something has been updated.
- $userquery = doquery("SELECT * FROM {{table}} WHERE id='".$userrow["id"]."' LIMIT 1", "users");
+ $userquery = db()->query('SELECT * FROM users WHERE id = ? LIMIT 1;', [$userrow['id']]);
unset($userrow);
- $userrow = mysql_fetch_array($userquery);
+ $userrow = $userquery->fetchArray(SQLITE3_ASSOC);
// Current town name.
if ($userrow["currentaction"] == "In Town") {
- $townquery = doquery("SELECT * FROM {{table}} WHERE latitude='".$userrow["latitude"]."' AND longitude='".$userrow["longitude"]."' LIMIT 1", "towns");
- $townrow = mysql_fetch_array($townquery);
+ $townquery = db()->query('SELECT * FROM towns WHERE latitude = ? AND longitude = ? LIMIT 1;', [$userrow["latitude"], $userrow["longitude"]]);
+ $townrow = $townquery->fetchArray(SQLITE3_ASSOC);
$userrow["currenttown"] = "Welcome to ".$townrow["name"].".
";
} else {
$userrow["currenttown"] = "";
@@ -188,10 +162,10 @@ function display($content, $title, $topnav=true, $leftnav=true, $rightnav=true,
if ($userrow["currenthp"] <= ($userrow["maxhp"]/5)) { $userrow["currenthp"] = ""; }
if ($userrow["currentmp"] <= ($userrow["maxmp"]/5)) { $userrow["currentmp"] = ""; }
- $spellquery = doquery("SELECT id,name,type FROM {{table}}","spells");
+ $spellquery = db()->query('SELECT id, name, type FROM spells;');
$userspells = explode(",",$userrow["spells"]);
$userrow["magiclist"] = "";
- while ($spellrow = mysql_fetch_array($spellquery)) {
+ foreach ($spellquery->fetchArray(SQLITE3_ASSOC) as $spellrow) {
$spell = false;
foreach($userspells as $a => $b) {
if ($b == $spellrow["id"] && $spellrow["type"] == 1) { $spell = true; }
@@ -204,9 +178,9 @@ function display($content, $title, $topnav=true, $leftnav=true, $rightnav=true,
// Travel To list.
$townslist = explode(",",$userrow["towns"]);
- $townquery2 = doquery("SELECT * FROM {{table}} ORDER BY id", "towns");
+ $townquery2 = db()->query('SELECT * FROM towns ORDER BY id;');
$userrow["townslist"] = "";
- while ($townrow2 = mysql_fetch_array($townquery2)) {
+ foreach ($townquery2->fetchArray(SQLITE3_ASSOC) as $townrow2) {
$town = false;
foreach($townslist as $a => $b) {
if ($b == $townrow2["id"]) { $town = true; }
@@ -215,29 +189,59 @@ function display($content, $title, $topnav=true, $leftnav=true, $rightnav=true,
$userrow["townslist"] .= "".$townrow2["name"]."
\n";
}
}
-
} else {
- $userrow = array();
+ $userrow = [];
}
- $finalarray = array(
- "dkgamename"=>$controlrow["gamename"],
+ $page = parsetemplate(gettemplate("primary"), [
+ "dkgamename"=>$controlrow["gamename"],
"title"=>$title,
"content"=>$content,
"rightnav"=>parsetemplate($rightnav,$userrow),
"leftnav"=>parsetemplate($leftnav,$userrow),
"topnav"=>$topnav,
- "totaltime"=>round(getmicrotime() - $starttime, 4),
+ "totaltime"=>round(microtime(true) - $starttime, 4),
"numqueries"=>$numqueries,
"version"=>$version,
- "build"=>$build);
- $page = parsetemplate($template, $finalarray);
- $page = $xml . $page;
-
- if ($controlrow["compression"] == 1) { ob_start("ob_gzhandler"); }
- echo $page;
- die();
+ "build"=>$build
+ ]);
+ echo "\n" . $page;
+ exit;
}
-?>
+function checkcookies()
+{
+ $row = false;
+
+ if (isset($_COOKIE["dkgame"])) {
+ // COOKIE FORMAT:
+ // {ID} {USERNAME} {PASSWORDHASH} {REMEMBERME}
+ $theuser = explode(" ",$_COOKIE["dkgame"]);
+ $query = db()->query('SELECT * FROM users WHERE id = ?, username = ?, password = ? LIMIT 1;', [$theuser[0], $theuser[1], $theuser[2]]);
+ if ($query === false) {
+ set_cookie('dkgame', '', -3600);
+ die("Invalid cookie data. Please log in again.");
+ }
+ $row = $query->fetchArray(SQLITE3_ASSOC);
+ set_cookie('dkgame', implode(" ", $theuser), (int) $theuser[3] === 1 ? time() + 31536000 : 0);
+ db()->exec('UPDATE users SET onlinetime = CURRENT_TIMESTAMP WHERE id = ? LIMIT 1;', [$theuser[0]]);
+ }
+
+ return $row;
+}
+
+/**
+ * Set a cookie with secure and HTTP-only flags.
+ */
+function set_cookie($name, $value, $expires)
+{
+ setcookie($name, $value, [
+ 'expires' => $expires,
+ 'path' => '/',
+ 'domain' => '', // Defaults to the current domain
+ 'secure' => true, // Ensure the cookie is only sent over HTTPS
+ 'httponly' => true, // Prevent access to cookie via JavaScript
+ 'samesite' => 'Strict' // Enforce SameSite=Strict
+ ]);
+}
diff --git a/public/install.php b/public/install.php
index d956bfc..c07334a 100644
--- a/public/install.php
+++ b/public/install.php
@@ -1,812 +1,813 @@
-
-