From 1a4d2c0e3942dbcdbefd57643e17f7edf8c46cf5 Mon Sep 17 00:00:00 2001 From: flashwave Date: Thu, 17 Aug 2023 00:23:55 +0000 Subject: [PATCH] Store server list in the database. --- ...2023_08_17_001507_create_servers_table.php | 26 +++++ public/index.php | 2 +- src/HomeRoutes.php | 2 + src/RemoteV2.php | 4 +- src/ServerInfo.php | 97 +++++++++++++++++++ src/Servers.php | 37 +++++++ templates/index.twig | 28 ++++-- tools/sync | 2 + 8 files changed, 189 insertions(+), 9 deletions(-) create mode 100644 database/2023_08_17_001507_create_servers_table.php create mode 100644 src/ServerInfo.php create mode 100644 src/Servers.php diff --git a/database/2023_08_17_001507_create_servers_table.php b/database/2023_08_17_001507_create_servers_table.php new file mode 100644 index 0000000..feb5663 --- /dev/null +++ b/database/2023_08_17_001507_create_servers_table.php @@ -0,0 +1,26 @@ +execute(' + CREATE TABLE servers ( + server_id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT, + server_name VARCHAR(255) NOT NULL COLLATE "utf8mb4_unicode_520_ci", + server_details TEXT NOT NULL DEFAULT "" COLLATE "utf8mb4_unicode_520_ci", + server_order SMALLINT(6) NOT NULL DEFAULT "0", + server_java_address VARCHAR(255) NULL DEFAULT NULL COLLATE "ascii_general_ci", + server_java_version VARCHAR(255) NULL DEFAULT NULL COLLATE "ascii_general_ci", + server_bedrock_address VARCHAR(255) NULL DEFAULT NULL COLLATE "ascii_general_ci", + server_bedrock_version VARCHAR(255) NULL DEFAULT NULL COLLATE "ascii_general_ci", + server_created TIMESTAMP NOT NULL DEFAULT current_timestamp(), + server_deleted TIMESTAMP NULL DEFAULT NULL, + PRIMARY KEY (server_id), + KEY servers_created_index (server_created), + KEY servers_deleted_index (server_deleted), + KEY servers_order_index (server_order) + ) ENGINE=InnoDB COLLATE=utf8mb4_bin + '); + } +} diff --git a/public/index.php b/public/index.php index 93f1df1..eb44953 100644 --- a/public/index.php +++ b/public/index.php @@ -42,7 +42,7 @@ $router->setDefaultErrorHandler(function($response, $request, $code, $text) use ])); }); -(new HomeRoutes($templating, $userInfo))->register($router); +(new HomeRoutes(new Servers($db), $templating, $userInfo))->register($router); (new WhitelistRoutes(new Whitelist($db), $csrfp, $userInfo))->register($router); $router->dispatch(); diff --git a/src/HomeRoutes.php b/src/HomeRoutes.php index 02613b2..0019997 100644 --- a/src/HomeRoutes.php +++ b/src/HomeRoutes.php @@ -5,6 +5,7 @@ use Index\Routing\IRouter; class HomeRoutes { public function __construct( + private Servers $servers, private Templating $templating, private object $userInfo ) {} @@ -54,6 +55,7 @@ class HomeRoutes { $this->templating->setVar('whitelist_pending', floor($this->userInfo->mc_whitelisted / 300) === floor(time() / 300)); return $this->templating->render('index', [ + 'servers' => $this->servers->getServers(deleted: false), 'wladdform_username' => $name, ]); } diff --git a/src/RemoteV2.php b/src/RemoteV2.php index 8057889..8840bff 100644 --- a/src/RemoteV2.php +++ b/src/RemoteV2.php @@ -2,7 +2,7 @@ namespace Mince; use RuntimeException; -use Index\Serialisation\Serialiser; +use Index\Serialisation\UriBase64; class RemoteV2 { public function __construct( @@ -45,7 +45,7 @@ class RemoteV2 { $input = "{$time}%{$method} {$path}%" . implode('#', $compare); - return Serialiser::uriBase64()->serialise( + return UriBase64::encode( hash_hmac('sha256', $input, $this->secretKey, true) ); } diff --git a/src/ServerInfo.php b/src/ServerInfo.php new file mode 100644 index 0000000..256c96b --- /dev/null +++ b/src/ServerInfo.php @@ -0,0 +1,97 @@ +id = $result->getString(0); + $this->name = $result->getString(1); + $this->details = $result->getString(2); + $this->javaAddress = $result->isNull(3) ? null : $result->getString(3); + $this->javaVersion = $result->isNull(4) ? null : $result->getString(4); + $this->bedrockAddress = $result->isNull(5) ? null : $result->getString(5); + $this->bedrockVersion = $result->isNull(6) ? null : $result->getString(6); + $this->created = $result->getInteger(7); + $this->deleted = $result->isNull(8) ? null : $result->getInteger(8); + } + + public function getId(): string { + return $this->id; + } + + public function getName(): string { + return $this->name; + } + + public function hasDetails(): bool { + return $this->details !== ''; + } + + public function getDetails(): string { + return $this->details; + } + + public function hasJavaAddress(): bool { + return $this->javaAddress !== null; + } + + public function getJavaAddress(): ?string { + return $this->javaAddress; + } + + public function hasJavaVersion(): bool { + return $this->javaVersion !== null; + } + + public function getJavaVersion(): ?string { + return $this->javaVersion; + } + + public function hasBedrockAddress(): bool { + return $this->bedrockAddress !== null; + } + + public function getBedrockAddress(): ?string { + return $this->bedrockAddress; + } + + public function hasBedrockVersion(): bool { + return $this->bedrockVersion !== null; + } + + public function getBedrockVersion(): ?string { + return $this->bedrockVersion; + } + + public function getCreatedTime(): int { + return $this->created; + } + + public function getCreatedAt(): DateTime { + return DateTime::fromUnixTimeSeconds($this->created); + } + + public function isDeleted(): bool { + return $this->deleted !== null; + } + + public function getDeletedTime(): ?int { + return $this->deleted; + } + + public function getDeletedAt(): ?DateTime { + return $this->deleted === null ? null : DateTime::fromUnixTimeSeconds($this->deleted); + } +} diff --git a/src/Servers.php b/src/Servers.php new file mode 100644 index 0000000..45f0d25 --- /dev/null +++ b/src/Servers.php @@ -0,0 +1,37 @@ +dbConn = $dbConn; + $this->cache = new DbStatementCache($dbConn); + } + + public function getServers( + ?bool $deleted = null + ): array { + $hasDeleted = $deleted !== null; + + $query = 'SELECT server_id, server_name, server_details, server_java_address, server_java_version, server_bedrock_address, server_bedrock_version, UNIX_TIMESTAMP(server_created), UNIX_TIMESTAMP(server_deleted) FROM servers'; + if($hasDeleted) + $query .= sprintf(' WHERE server_deleted %s NULL', $deleted ? 'IS NOT ' : 'IS'); + $query .= ' ORDER BY server_order'; + + $stmt = $this->cache->get($query); + $stmt->execute(); + + $result = $stmt->getResult(); + $servers = []; + + while($result->next()) + $servers[] = new ServerInfo($result); + + return $servers; + } +} diff --git a/templates/index.twig b/templates/index.twig index 8ed61c1..1f5a4bf 100644 --- a/templates/index.twig +++ b/templates/index.twig @@ -34,14 +34,30 @@

Servers

- + + + + + {# #} + {# #} + + - {# #} - {# #} - {# #} - {# #} - + {% if servers is empty %} + + {% else %} + {% for server in servers %} + + + + + {# #} + {# #} + + + {% endfor %} + {% endif %}
Name Address Java version Bedrock version Details
NameAddressVersionBedrock addressBedrock versionDetails
Vanilla Survival mc-survival.flashii.net 1.19.2 N/A Regular Minecraft Survival with some server-side extensions.
Beta Survival mc-beta.flashii.net Beta 1.7.3 N/A Classic Minecraft Survival!
Tekkit Classic mc-tekkit.flashii.net 1.2.5 N/A Page for this modpack on the Technic Platform
All of Fabric 6 mc-aof6.flashii.net 1.19.2 N/A Page for this modpack on CurseForge
There are currently no active servers, check back later!
There are currently no active servers, check back later!
{{ server.name }}{% if server.hasJavaAddress %}{{ server.javaAddress }}{% else %}N/A{% endif %}{% if server.hasJavaVersion %}{{ server.javaVersion }}{% else %}N/A{% endif %}{% if server.hasBedrockAddress %}{{ server.bedrockAddress }}{% else %}N/A{% endif %}{% if server.hasBedrockVersion %}{{ server.bedrockVersion }}{% else %}N/A{% endif %}{{ server.details|raw }}
diff --git a/tools/sync b/tools/sync index 4a4fd95..a3964e9 100755 --- a/tools/sync +++ b/tools/sync @@ -4,6 +4,8 @@ use Mince\Whitelist; require_once __DIR__ . '/../mince.php'; +// rewrite this to use the database shit + echo 'Syncing server whitelists...' . PHP_EOL; $rInfo = $remote->getInfo();