Use Twig for HTML templates.

This commit is contained in:
flash 2023-08-16 22:39:35 +00:00
parent e5f3ee9d99
commit 1250659013
7 changed files with 403 additions and 133 deletions

View file

@ -2,7 +2,8 @@
"minimum-stability": "dev",
"prefer-stable": true,
"require": {
"flashwave/index": "*"
"flashwave/index": "*",
"twig/twig": "^3.7"
},
"autoload": {
"classmap": [

238
composer.lock generated
View file

@ -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": [],

View file

@ -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'));
}

View file

@ -1,25 +1,60 @@
<?php
namespace Mince;
use Index\Performance\Timings;
use Index\XString;
use Index\Data\DbType;
use Index\Http\HttpFx;
use Mince\HTML;
use Twig\Environment as TwigEnvironment;
use Twig\Loader\FilesystemLoader as TwigLoaderFilesystem;
require_once __DIR__ . '/../mince.php';
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'));
// need a more permanent solution for this
$twigLoader = new TwigLoaderFilesystem([MCR_DIR_TPL]);
$twigEnv = new TwigEnvironment($twigLoader, [
//'cache' => $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 .= '<div class="error">';
$body .= sprintf(' <h2>HTTP %03d</h2>', $code);
$body .= sprintf(' <p>%s</p>', $text);
$body .= '</div>';
$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 .= '<div class="error">';
$body .= sprintf(' <h2>%s</h2>', $mErrorTitle);
$body .= sprintf(' <p>%s</p>', $mErrorComment ?? 'No further details provided.');
$body .= '</div>';
}
if(!$userInfo->success) {
$body .= '<div class="section">';
$body .= ' <h2>You must be logged in to use this website!</h2>';
$body .= ' <p>This website allows you to whitelist yourself on our Minecraft servers, for which you need to be logged in.</p>';
$body .= ' <p>So it doesn\'t make sense to display the details either.</p>';
$body .= '</div>';
} else {
if($userInfo->mc_whitelisted < 1) {
$body .= '<div class="section whitelist">';
$body .= ' <h2>Add to Whitelist</h2>';
$body .= ' <p>This will give you access to the server.</p>';
$body .= ' <form method="post" action="/whitelist/add">';
$body .= sprintf(' <input type="hidden" name="boob" value="%s">', $sVerification);
$body .= ' <label>';
$body .= ' <div class="label-header">Username</div>';
$body .= sprintf(' <div class="label-input"><input type="text" name="name" value="%s"></div>', htmlspecialchars($name));
$body .= ' </label>';
$body .= ' <input type="submit" value="Add me to the Whitelist">';
$body .= ' </form>';
$body .= '</div>';
}
$body .= '<div class="section">';
$body .= ' <h2>Servers</h2>';
$body .= ' <table class="servers">';
$body .= ' <thead>';
$body .= ' <tr><th class="col-name">Name</th> <th class="col-address">Address</th> <th class="col-java">Java version</th> <th class="col-bedrock">Bedrock version</th> <th class="col-details">Details</th></tr>';
$body .= ' </thead>';
$body .= ' <tbody>';
// $body .= ' <tr><td class="col-name">Vanilla Survival</td> <td class="col-address"><code>mc-survival.flashii.net</code></td> <td class="col-java">1.19.2</td> <td class="col-bedrock">N/A</td> <td class="col-details">Regular Minecraft Survival with some server-side extensions.</td></tr>';
// $body .= ' <tr><td class="col-name">Beta Survival</td> <td class="col-address"><code>mc-beta.flashii.net</code></td> <td class="col-java">Beta 1.7.3</td> <td class="col-bedrock">N/A</td> <td class="col-details">Classic Minecraft Survival!</td></tr>';
// $body .= ' <tr><td class="col-name">Tekkit Classic</td> <td class="col-address"><code>mc-tekkit.flashii.net</code></td> <td class="col-java">1.2.5</td> <td class="col-bedrock">N/A</td> <td class="col-details"><a href="https://www.technicpack.net/modpack/tekkit.552560" target="_blank" rel="noopener">Page for this modpack on the Technic Platform</a></td></tr>';
$body .= ' <tr><td class="col-name">All of Fabric 6</td> <td class="col-address"><code>mc-aof6.flashii.net</code></td> <td class="col-java">1.19.2</td> <td class="col-bedrock">N/A</td> <td class="col-details"><a href="https://www.curseforge.com/minecraft/modpacks/all-of-fabric-6" target="_blank" rel="noopener">Page for this modpack on CurseForge</a></td></tr>';
$body .= ' </tbody>';
$body .= ' </table>';
$body .= '</div>';
if($userInfo->mc_whitelisted > 0) {
$body .= '<div class="section unwhitelist">';
$body .= ' <h2>Remove from Whitelist</h2>';
$body .= ' <p>This will revoke your access to the server.</p>';
$body .= sprintf(' <p>You are currently whitelisted as <b>%s</b> on <b>%s</b>.</p>', $userInfo->mc_username, date('Y-m-d H:i:s T', $userInfo->mc_whitelisted));
if(floor($userInfo->mc_whitelisted / 300) === floor(time() / 300)) {
$body .= ' <p><b style="font-size: 1.1em; line-height: 1.4em;">The whitelists are synchronised once every 5 minutes, you\'ll be able to play soon!</b></p>';
$body .= ' <p>If you\'re playing a modpack, take that time to start the game up; it\'ll take a while.</p>';
}
$body .= ' <form method="post" action="/whitelist/remove">';
$body .= sprintf(' <input type="hidden" name="boob" value="%s">', $sVerification);
$body .= ' <input type="submit" value="Remove me from the whitelist">';
$body .= ' </form>';
$body .= '</div>';
}
$body .= '<div class="section">';
$body .= ' <h2>Bedrock versions</h2>';
$body .= ' <p>Through the black magic bestowed upon us by <a href="https://geysermc.org/" target="_blank" rel="noopener">GeyserMC</a> it\'s possible to play on the server through any of the updated Bedrock versions of Minecraft.</p>';
$body .= ' <p>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.</p>';
$body .= ' <p>You will need to link your Minecraft and Bedrock accounts, you can do this by connecting to <code>link.geysermc.org</code> in both versions of the game and following the on-screen instructions.</p>';
$body .= ' <p>Do note that this only works for servers where both a Java and Bedrock version number is listed!</p>';
$body .= '</div>';
}
$body .= '<div class="section">';
$body .= ' <h2>Rules</h2>';
$body .= ' <p>1. Observe <a href="//fii.moe/rules">Global Rules</a>.</p>';
$body .= ' <p>2. Don\'t be an asshole.</p>';
$body .= ' <p>3. Don\'t flood.</p>';
$body .= '</div>';
$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;

10
templates/http-error.twig Normal file
View file

@ -0,0 +1,10 @@
{% extends 'master.twig' %}
{% set title = error.code ~ ' ' ~ error.text %}
{% block content %}
<div class="error">
<h2>HTTP {{ error.code }}</h2>
<p>{{ error.text }}</p>
</div>
{% endblock %}

80
templates/index.twig Normal file
View file

@ -0,0 +1,80 @@
{% extends 'master.twig' %}
{% block content %}
{% if error is defined %}
<div class="error">
<h2>{{ error.title }}</h2>
<p>{{ error.body|default('No further details provided.') }}</p>
</div>
{% endif %}
{% if not auth.success %}
<div class="section">
<h2>You must be logged in to use this website!</h2>
<p>This website allows you to whitelist yourself on our Minecraft servers, for which you need to be logged in.</p>
<p>So it doesn't make sense to display the details either.</p>
</div>
{% else %}
{% if auth.mc_whitelisted < 1 %}
<div class="section whitelist">
<h2>Add to Whitelist</h2>
<p>This will give you access to the server.</p>
<form method="post" action="/whitelist/add">
<input type="hidden" name="boob" value="{{ verification }}">
<label>
<div class="label-header">Username</div>
<div class="label-input"><input type="text" name="name" value="{{ wladdform_username }}"></div>
</label>
<input type="submit" value="Add me to the Whitelist">
</form>
</div>
{% endif %}
<div class="section">
<h2>Servers</h2>
<table class="servers">
<thead>
<tr><th class="col-name">Name</th> <th class="col-address">Address</th> <th class="col-java">Java version</th> <th class="col-bedrock">Bedrock version</th> <th class="col-details">Details</th></tr>
</thead>
<tbody>
{# <tr><td class="col-name">Vanilla Survival</td> <td class="col-address"><code>mc-survival.flashii.net</code></td> <td class="col-java">1.19.2</td> <td class="col-bedrock">N/A</td> <td class="col-details">Regular Minecraft Survival with some server-side extensions.</td></tr> #}
{# <tr><td class="col-name">Beta Survival</td> <td class="col-address"><code>mc-beta.flashii.net</code></td> <td class="col-java">Beta 1.7.3</td> <td class="col-bedrock">N/A</td> <td class="col-details">Classic Minecraft Survival!</td></tr> #}
{# <tr><td class="col-name">Tekkit Classic</td> <td class="col-address"><code>mc-tekkit.flashii.net</code></td> <td class="col-java">1.2.5</td> <td class="col-bedrock">N/A</td> <td class="col-details"><a href="https://www.technicpack.net/modpack/tekkit.552560" target="_blank" rel="noopener">Page for this modpack on the Technic Platform</a></td></tr> #}
{# <tr><td class="col-name">All of Fabric 6</td> <td class="col-address"><code>mc-aof6.flashii.net</code></td> <td class="col-java">1.19.2</td> <td class="col-bedrock">N/A</td> <td class="col-details"><a href="https://www.curseforge.com/minecraft/modpacks/all-of-fabric-6" target="_blank" rel="noopener">Page for this modpack on CurseForge</a></td></tr> #}
<tr><td class="col-java" colspan="5">There are currently no active servers, check back later!</td></tr>
</tbody>
</table>
</div>
{% if auth.mc_whitelisted > 0 %}
<div class="section unwhitelist">
<h2>Remove from Whitelist</h2>
<p>This will revoke your access to the server.</p>
<p>You are currently whitelisted as <b>{{ auth.mc_username }}</b> on <b>{{ auth.mc_whitelisted|date('Y-m-d H:i:s T') }}</b>.</p>
{% if whitelist_pending %}
<p><b style="font-size: 1.1em; line-height: 1.4em;">The whitelists are synchronised once every 5 minutes, you'll be able to play soon!</b></p>
<p>If you're playing a modpack, take that time to start the game up; it'll take a while.</p>
{% endif %}
<form method="post" action="/whitelist/remove">
<input type="hidden" name="boob" value="{{ verification }}">
<input type="submit" value="Remove me from the whitelist">
</form>
</div>
{% endif %}
<div class="section">
<h2>Bedrock versions</h2>
<p>Through the black magic bestowed upon us by <a href="https://geysermc.org/" target="_blank" rel="noopener">GeyserMC</a> it's possible to play on the server through any of the updated Bedrock versions of Minecraft.</p>
<p>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.</p>
<p>You will need to link your Minecraft and Bedrock accounts, you can do this by connecting to <code>link.geysermc.org</code> in both versions of the game and following the on-screen instructions.</p>
<p>Do note that this only works for servers where both a Java and Bedrock version number is listed!</p>
</div>
<div class="section">
<h2>Rules</h2>
<p>1. Observe <a href="//fii.moe/rules">Global Rules</a>.</p>
<p>2. Don't be an asshole.</p>
<p>3. Don't flood.</p>
</div>
{% endif %}
{% endblock %}

View file

@ -1,20 +1,8 @@
<?php
namespace Mince;
final class HTML {
public static function getHeader(object $userInfo, string $loginUrl, string $title = 'Flashii Minecraft Servers'): string {
if($userInfo->success) {
$userBar = 'Logged in as ' . $userInfo->username;
} else {
$userBar = '<a href="' . $loginUrl . '">Log in</a>';
}
return <<<HTML
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>{$title}</title>
<title>{{ title|default(global.title) }}</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="/mince.css" type="text/css" rel="stylesheet">
</head>
@ -27,23 +15,22 @@ final class HTML {
</div>
<div class="header-fat"></div>
<div class="header-user">
{$userBar}
{% if auth.success %}
Logged in as {{ auth.username }}
{% else %}
<a href="{{ global.loginUrl }}">Log in</a>
{% endif %}
</div>
</div>
</nav>
<div class="content">
HTML;
}
public static function getFooter(): string {
return <<<HTML
{% block content %}
There is nothing here!
{% endblock %}
</div>
<footer class="footer">
<a href="https://flash.moe">Flashwave</a> 2022-2023 | Site design "borrowed" from pre-Microsoft Mojang | "Minecraft" is a trademark of Mojang
<a href="https://flash.moe">Flashwave</a> 2022-{{ 'now'|date('Y') }} | Site design "borrowed" from pre-Microsoft Mojang | "Minecraft" is a trademark of Mojang
</footer>
</div>
</body>
</html>
HTML;
}
}