mince/public/index.php
2023-02-25 20:55:38 +00:00

192 lines
9.4 KiB
PHP

<?php
namespace Mince;
use Index\Performance\Timings;
use Index\Data\DbType;
use Index\Http\HttpFx;
use Mince\HTML;
require_once __DIR__ . '/../mince.php';
$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->use('/', function($response) {
$response->setPoweredBy('Mince');
});
$router->get('/index.php', function($response) {
$response->redirect('/', true);
});
$router->get('/', function($response, $request) use ($db, $remote, $loginUrl, $userInfo, $sVerification) {
$name = (string)$request->getParam('name');
$error = (string)$request->getParam('error');
if(!empty($error) && ctype_lower($error)) {
$errors = [
'request' => ['Invalid request type.', 'Try to reload the page and try again.'],
'verify' => ['Request verification failed.', 'Try to reload the page and try again.'],
'itainthappenin' => ['Haha', 'No'],
'short' => ['Invalid username', 'The provided name is too short.'],
'long' => ['Invalid username', 'The provided name is too long.'],
'invalid' => ['Invalid username', 'The provided name contains invalid characters.'],
'conflict' => ['Username conflict', 'This username is already whitelisted with someone, contact flashwave if this is unexpected.'],
'connect' => ['Failed to connect to the server', 'The server is probably offline, pope flashwave if this is not expected.'],
'not-listed' => ['You have not been whitelisted yet', 'Add yourself to the whitelist before trying to remove yourself from it.'],
];
if(array_key_exists($error, $errors)) {
$mErrorTitle = $errors[$error][0];
$mErrorComment = $errors[$error][1];
} else {
$mErrorTitle = 'Unexpected response from server';
$mErrorComment = $error;
}
}
$body = HTML::getHeader($userInfo, $loginUrl);
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;
});
$router->use('/whitelist', function($response, $request) use ($sVerification) {
if(!$request->isFormContent()) {
$response->redirect('/?error=request');
return true;
}
$body = $request->getContent();
if(!$body->hasParam('boob') || !hash_equals($sVerification, (string)$body->getParam('boob'))) {
$response->redirect('/?error=verify');
return true;
}
});
$router->post('/whitelist/add', function($response, $request) use ($db, $userInfo) {
if($userInfo->user_id == 45) {
$response->redirect('/?error=itainthappenin');
return true;
}
$body = $request->getContent();
$name = (string)$body->getParam('name');
$resp = (new Whitelist($db))->add($userInfo, $name);
if($resp === '')
$response->redirect('/');
else {
if($resp === 'invalid')
$name = '';
$response->redirect("/?error={$resp}&name={$name}");
}
});
$router->post('/whitelist/remove', function($response) use ($db, $userInfo) {
$resp = (new Whitelist($db))->remove($userInfo);
if($resp === '')
$response->redirect('/');
else
$response->redirect("/?error={$resp}");
});
$router->get('/errors/:code', function($res, $req, $code) {
$code = intval($code);
if($code < 100 || $code >= 600)
$code = 400;
return $code;
});
$router->dispatch();