From 1250659013df7409dbcac5e1b742660ff7048337 Mon Sep 17 00:00:00 2001 From: flashwave Date: Wed, 16 Aug 2023 22:39:35 +0000 Subject: [PATCH] Use Twig for HTML templates. --- composer.json | 3 +- composer.lock | 238 +++++++++++++++++++++++++- mince.php | 17 +- public/index.php | 155 +++++++---------- templates/http-error.twig | 10 ++ templates/index.twig | 80 +++++++++ src/HTML.php => templates/master.twig | 33 ++-- 7 files changed, 403 insertions(+), 133 deletions(-) create mode 100644 templates/http-error.twig create mode 100644 templates/index.twig rename src/HTML.php => templates/master.twig (53%) diff --git a/composer.json b/composer.json index 5d3b9b5..1281136 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,8 @@ "minimum-stability": "dev", "prefer-stable": true, "require": { - "flashwave/index": "*" + "flashwave/index": "*", + "twig/twig": "^3.7" }, "autoload": { "classmap": [ diff --git a/composer.lock b/composer.lock index f430525..d5f4802 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "11f8c90aff3c23a448844cee09bcd23c", + "content-hash": "90e206da96cc83682b957dc89eb26f1d", "packages": [ { "name": "flashwave/index", @@ -51,6 +51,242 @@ "description": "Composer package for the common library for my projects.", "homepage": "https://railgun.sh/index", "time": "2023-08-03T01:29:57+00:00" + }, + { + "name": "symfony/polyfill-ctype", + "version": "v1.27.0", + "source": { + "type": "git", + "url": "https://github.com/symfony/polyfill-ctype.git", + "reference": "5bbc823adecdae860bb64756d639ecfec17b050a" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/polyfill-ctype/zipball/5bbc823adecdae860bb64756d639ecfec17b050a", + "reference": "5bbc823adecdae860bb64756d639ecfec17b050a", + "shasum": "" + }, + "require": { + "php": ">=7.1" + }, + "provide": { + "ext-ctype": "*" + }, + "suggest": { + "ext-ctype": "For best performance" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-main": "1.27-dev" + }, + "thanks": { + "name": "symfony/polyfill", + "url": "https://github.com/symfony/polyfill" + } + }, + "autoload": { + "files": [ + "bootstrap.php" + ], + "psr-4": { + "Symfony\\Polyfill\\Ctype\\": "" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Gert de Pagter", + "email": "BackEndTea@gmail.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony polyfill for ctype functions", + "homepage": "https://symfony.com", + "keywords": [ + "compatibility", + "ctype", + "polyfill", + "portable" + ], + "support": { + "source": "https://github.com/symfony/polyfill-ctype/tree/v1.27.0" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2022-11-03T14:55:06+00:00" + }, + { + "name": "symfony/polyfill-mbstring", + "version": "v1.27.0", + "source": { + "type": "git", + "url": "https://github.com/symfony/polyfill-mbstring.git", + "reference": "8ad114f6b39e2c98a8b0e3bd907732c207c2b534" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/8ad114f6b39e2c98a8b0e3bd907732c207c2b534", + "reference": "8ad114f6b39e2c98a8b0e3bd907732c207c2b534", + "shasum": "" + }, + "require": { + "php": ">=7.1" + }, + "provide": { + "ext-mbstring": "*" + }, + "suggest": { + "ext-mbstring": "For best performance" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-main": "1.27-dev" + }, + "thanks": { + "name": "symfony/polyfill", + "url": "https://github.com/symfony/polyfill" + } + }, + "autoload": { + "files": [ + "bootstrap.php" + ], + "psr-4": { + "Symfony\\Polyfill\\Mbstring\\": "" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Nicolas Grekas", + "email": "p@tchwork.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony polyfill for the Mbstring extension", + "homepage": "https://symfony.com", + "keywords": [ + "compatibility", + "mbstring", + "polyfill", + "portable", + "shim" + ], + "support": { + "source": "https://github.com/symfony/polyfill-mbstring/tree/v1.27.0" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2022-11-03T14:55:06+00:00" + }, + { + "name": "twig/twig", + "version": "v3.7.0", + "source": { + "type": "git", + "url": "https://github.com/twigphp/Twig.git", + "reference": "5cf942bbab3df42afa918caeba947f1b690af64b" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/twigphp/Twig/zipball/5cf942bbab3df42afa918caeba947f1b690af64b", + "reference": "5cf942bbab3df42afa918caeba947f1b690af64b", + "shasum": "" + }, + "require": { + "php": ">=7.2.5", + "symfony/polyfill-ctype": "^1.8", + "symfony/polyfill-mbstring": "^1.3" + }, + "require-dev": { + "psr/container": "^1.0|^2.0", + "symfony/phpunit-bridge": "^4.4.9|^5.0.9|^6.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "Twig\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com", + "homepage": "http://fabien.potencier.org", + "role": "Lead Developer" + }, + { + "name": "Twig Team", + "role": "Contributors" + }, + { + "name": "Armin Ronacher", + "email": "armin.ronacher@active-4.com", + "role": "Project Founder" + } + ], + "description": "Twig, the flexible, fast, and secure template language for PHP", + "homepage": "https://twig.symfony.com", + "keywords": [ + "templating" + ], + "support": { + "issues": "https://github.com/twigphp/Twig/issues", + "source": "https://github.com/twigphp/Twig/tree/v3.7.0" + }, + "funding": [ + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/twig/twig", + "type": "tidelift" + } + ], + "time": "2023-07-26T07:16:09+00:00" } ], "packages-dev": [], diff --git a/mince.php b/mince.php index 6c1e9a7..72f7233 100644 --- a/mince.php +++ b/mince.php @@ -2,7 +2,6 @@ namespace Mince; use Index\Environment; -use Index\XString; use Index\Data\ConnectionFailedException; use Index\Data\DbTools; @@ -14,6 +13,7 @@ define('MCR_DIR_PUB', MCR_ROOT . '/public'); define('MCR_DIR_PRV', MCR_ROOT . '/private'); define('MCR_DIR_CFG', MCR_ROOT . '/config'); define('MCR_DIR_MIG', MCR_ROOT . '/database'); +define('MCR_DIR_TPL', MCR_ROOT . '/templates'); require_once MCR_ROOT . '/vendor/autoload.php'; @@ -30,17 +30,6 @@ try { die($ex->getMessage()); } +$db->execute('SET SESSION time_zone = "+00:00", sql_mode = "STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION"'); + $remote = new RemoteV2($config['remotev2_url'], $config['remotev2_secret']); - -if(PHP_SAPI !== 'cli') { - if(empty($_COOKIE['mc_random'])) { - $sVerification = XString::random(32); - setcookie('mc_random', $sVerification, strtotime('1 day'), '/', $_SERVER['HTTP_HOST']); - } else - $sVerification = (string)filter_input(INPUT_COOKIE, 'mc_random'); - - $sVerification = hash('sha256', $sVerification); - - // replace this with id.flashii.net shit - $userInfo = ChatAuth::attempt($db, $config['chat_endpoint'], $config['chat_secret'], (string)filter_input(INPUT_COOKIE, 'msz_auth')); -} diff --git a/public/index.php b/public/index.php index 300379e..da7f3cf 100644 --- a/public/index.php +++ b/public/index.php @@ -1,25 +1,60 @@ $cache ?? false, + 'debug' => MCR_DEBUG, + 'strict_variables' => true, +]); +$twigArgs = [ + 'global' => [ + 'title' => 'Flashii Minecraft Servers', + 'loginUrl' => $config['login_url'], + ], + 'auth' => $userInfo, + 'verification' => $sVerification, +]; + +function tpl_vars(array $args): void { + global $twigArgs; + $twigArgs = array_merge($twigArgs, $args); +} + +function tpl_render(string $name, array $args, string $suffix = '.twig'): string { + global $twigEnv, $twigArgs; + return $twigEnv->render($name . $suffix, array_merge($twigArgs, $args)); +} + $router = new HttpFx; -$loginUrl = $config['login_url']; - -$router->setDefaultErrorHandler(function($response, $request, $code, $text) use ($loginUrl, $userInfo) { - $body = HTML::getHeader($userInfo, $loginUrl, sprintf('%03d %s', $code, $text)); - $body .= '
'; - $body .= sprintf('

HTTP %03d

', $code); - $body .= sprintf('

%s

', $text); - $body .= '
'; - $body .= HTML::getFooter(); - $response->setContent($body); +$router->setDefaultErrorHandler(function($response, $request, $code, $text) use ($userInfo) { + $response->setContent(tpl_render('http-error', [ + 'error' => [ + 'code' => sprintf('%03d', $code), + 'text' => $text, + ], + ])); }); $router->use('/', function($response) { @@ -30,7 +65,7 @@ $router->get('/index.php', function($response) { $response->redirect('/', true); }); -$router->get('/', function($response, $request) use ($db, $remote, $loginUrl, $userInfo, $sVerification) { +$router->get('/', function($response, $request) use ($userInfo) { $name = (string)$request->getParam('name'); $error = (string)$request->getParam('error'); @@ -54,89 +89,21 @@ $router->get('/', function($response, $request) use ($db, $remote, $loginUrl, $u $mErrorTitle = 'Unexpected response from server'; $mErrorComment = $error; } + + tpl_vars([ + 'error' => [ + 'title' => $mErrorTitle, + 'body' => $mErrorComment, + ], + ]); } - $body = HTML::getHeader($userInfo, $loginUrl); + if($userInfo->mc_whitelisted > 0) + tpl_vars(['whitelist_pending' => floor($userInfo->mc_whitelisted / 300) === floor(time() / 300)]); - if(!empty($mErrorTitle)) { - $body .= '
'; - $body .= sprintf('

%s

', $mErrorTitle); - $body .= sprintf('

%s

', $mErrorComment ?? 'No further details provided.'); - $body .= '
'; - } - - if(!$userInfo->success) { - $body .= '
'; - $body .= '

You must be logged in to use this website!

'; - $body .= '

This website allows you to whitelist yourself on our Minecraft servers, for which you need to be logged in.

'; - $body .= '

So it doesn\'t make sense to display the details either.

'; - $body .= '
'; - } else { - if($userInfo->mc_whitelisted < 1) { - $body .= '
'; - $body .= '

Add to Whitelist

'; - $body .= '

This will give you access to the server.

'; - $body .= '
'; - $body .= sprintf(' ', $sVerification); - $body .= ' '; - $body .= ' '; - $body .= '
'; - $body .= '
'; - } - - $body .= '
'; - $body .= '

Servers

'; - $body .= ' '; - $body .= ' '; - $body .= ' '; - $body .= ' '; - $body .= ' '; - // $body .= ' '; - // $body .= ' '; - // $body .= ' '; - $body .= ' '; - $body .= ' '; - $body .= '
Name Address Java version Bedrock version Details
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
'; - $body .= '
'; - - if($userInfo->mc_whitelisted > 0) { - $body .= '
'; - $body .= '

Remove from Whitelist

'; - $body .= '

This will revoke your access to the server.

'; - $body .= sprintf('

You are currently whitelisted as %s on %s.

', $userInfo->mc_username, date('Y-m-d H:i:s T', $userInfo->mc_whitelisted)); - if(floor($userInfo->mc_whitelisted / 300) === floor(time() / 300)) { - $body .= '

The whitelists are synchronised once every 5 minutes, you\'ll be able to play soon!

'; - $body .= '

If you\'re playing a modpack, take that time to start the game up; it\'ll take a while.

'; - } - $body .= '
'; - $body .= sprintf(' ', $sVerification); - $body .= ' '; - $body .= '
'; - $body .= '
'; - } - - $body .= '
'; - $body .= '

Bedrock versions

'; - $body .= '

Through the black magic bestowed upon us by GeyserMC it\'s possible to play on the server through any of the updated Bedrock versions of Minecraft.

'; - $body .= '

This should allow you to play on the server from a phone, a tablet or a console, provided you also have an account for the original version of the game.

'; - $body .= '

You will need to link your Minecraft and Bedrock accounts, you can do this by connecting to link.geysermc.org in both versions of the game and following the on-screen instructions.

'; - $body .= '

Do note that this only works for servers where both a Java and Bedrock version number is listed!

'; - $body .= '
'; - } - - $body .= '
'; - $body .= '

Rules

'; - $body .= '

1. Observe Global Rules.

'; - $body .= '

2. Don\'t be an asshole.

'; - $body .= '

3. Don\'t flood.

'; - $body .= '
'; - - $body .= HTML::getFooter(); - - return $body; + return tpl_render('index', [ + 'wladdform_username' => $name, + ]); }); $router->use('/whitelist', function($response, $request) use ($sVerification) { @@ -182,7 +149,7 @@ $router->post('/whitelist/remove', function($response) use ($db, $userInfo) { }); $router->get('/errors/:code', function($res, $req, $code) { - $code = intval($code); + $code = (int)$code; if($code < 100 || $code >= 600) $code = 400; return $code; diff --git a/templates/http-error.twig b/templates/http-error.twig new file mode 100644 index 0000000..d6ce656 --- /dev/null +++ b/templates/http-error.twig @@ -0,0 +1,10 @@ +{% extends 'master.twig' %} + +{% set title = error.code ~ ' ' ~ error.text %} + +{% block content %} +
+

HTTP {{ error.code }}

+

{{ error.text }}

+
+{% endblock %} diff --git a/templates/index.twig b/templates/index.twig new file mode 100644 index 0000000..cfaa908 --- /dev/null +++ b/templates/index.twig @@ -0,0 +1,80 @@ +{% extends 'master.twig' %} + +{% block content %} + {% if error is defined %} +
+

{{ error.title }}

+

{{ error.body|default('No further details provided.') }}

+
+ {% endif %} + + {% if not auth.success %} +
+

You must be logged in to use this website!

+

This website allows you to whitelist yourself on our Minecraft servers, for which you need to be logged in.

+

So it doesn't make sense to display the details either.

+
+ {% else %} + {% if auth.mc_whitelisted < 1 %} +
+

Add to Whitelist

+

This will give you access to the server.

+
+ + + +
+
+ {% endif %} + +
+

Servers

+ + + + + + {# #} + {# #} + {# #} + {# #} + + +
Name Address Java version Bedrock version Details
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!
+
+ + {% if auth.mc_whitelisted > 0 %} +
+

Remove from Whitelist

+

This will revoke your access to the server.

+

You are currently whitelisted as {{ auth.mc_username }} on {{ auth.mc_whitelisted|date('Y-m-d H:i:s T') }}.

+ {% if whitelist_pending %} +

The whitelists are synchronised once every 5 minutes, you'll be able to play soon!

+

If you're playing a modpack, take that time to start the game up; it'll take a while.

+ {% endif %} +
+ + +
+
+ {% endif %} + +
+

Bedrock versions

+

Through the black magic bestowed upon us by GeyserMC it's possible to play on the server through any of the updated Bedrock versions of Minecraft.

+

This should allow you to play on the server from a phone, a tablet or a console, provided you also have an account for the original version of the game.

+

You will need to link your Minecraft and Bedrock accounts, you can do this by connecting to link.geysermc.org in both versions of the game and following the on-screen instructions.

+

Do note that this only works for servers where both a Java and Bedrock version number is listed!

+
+ +
+

Rules

+

1. Observe Global Rules.

+

2. Don't be an asshole.

+

3. Don't flood.

+
+ {% endif %} +{% endblock %} diff --git a/src/HTML.php b/templates/master.twig similarity index 53% rename from src/HTML.php rename to templates/master.twig index 6645e86..6576211 100644 --- a/src/HTML.php +++ b/templates/master.twig @@ -1,20 +1,8 @@ -success) { - $userBar = 'Logged in as ' . $userInfo->username; - } else { - $userBar = 'Log in'; - } - - return << - {$title} + {{ title|default(global.title) }} @@ -27,23 +15,22 @@ final class HTML {
- {$userBar} + {% if auth.success %} + Logged in as {{ auth.username }} + {% else %} + Log in + {% endif %}
-HTML; - } - - public static function getFooter(): string { - return <<
-HTML; - } -}