mince/public/index.php

159 lines
5 KiB
PHP
Raw Normal View History

2022-07-03 22:07:00 +00:00
<?php
namespace Mince;
2023-08-16 22:39:35 +00:00
use Index\XString;
2022-07-03 22:07:00 +00:00
use Index\Data\DbType;
use Index\Http\HttpFx;
2023-08-16 22:39:35 +00:00
use Twig\Environment as TwigEnvironment;
use Twig\Loader\FilesystemLoader as TwigLoaderFilesystem;
2022-07-03 22:07:00 +00:00
require_once __DIR__ . '/../mince.php';
2023-08-16 22:39:35 +00:00
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));
}
2022-07-03 22:07:00 +00:00
2023-08-16 22:39:35 +00:00
$router = new HttpFx;
2022-07-03 22:07:00 +00:00
2023-08-16 22:39:35 +00:00
$router->setDefaultErrorHandler(function($response, $request, $code, $text) use ($userInfo) {
$response->setContent(tpl_render('http-error', [
'error' => [
'code' => sprintf('%03d', $code),
'text' => $text,
],
]));
2022-07-03 22:07:00 +00:00
});
2023-02-25 20:17:06 +00:00
$router->use('/', function($response) {
$response->setPoweredBy('Mince');
2022-07-03 22:07:00 +00:00
});
$router->get('/index.php', function($response) {
$response->redirect('/', true);
});
2023-08-16 22:39:35 +00:00
$router->get('/', function($response, $request) use ($userInfo) {
2022-07-03 22:07:00 +00:00
$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;
}
2023-08-16 22:39:35 +00:00
tpl_vars([
'error' => [
'title' => $mErrorTitle,
'body' => $mErrorComment,
],
]);
2022-07-03 22:07:00 +00:00
}
2023-08-16 22:39:35 +00:00
if($userInfo->mc_whitelisted > 0)
tpl_vars(['whitelist_pending' => floor($userInfo->mc_whitelisted / 300) === floor(time() / 300)]);
2022-07-03 22:07:00 +00:00
2023-08-16 22:39:35 +00:00
return tpl_render('index', [
'wladdform_username' => $name,
]);
2022-07-03 22:07:00 +00:00
});
$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');
2023-02-25 20:17:06 +00:00
$resp = (new Whitelist($db))->add($userInfo, $name);
2022-07-03 22:07:00 +00:00
if($resp === '')
$response->redirect('/');
else {
if($resp === 'invalid')
$name = '';
$response->redirect("/?error={$resp}&name={$name}");
}
});
$router->post('/whitelist/remove', function($response) use ($db, $userInfo) {
2023-02-25 20:17:06 +00:00
$resp = (new Whitelist($db))->remove($userInfo);
2022-07-03 22:07:00 +00:00
if($resp === '')
$response->redirect('/');
else
$response->redirect("/?error={$resp}");
});
$router->get('/errors/:code', function($res, $req, $code) {
2023-08-16 22:39:35 +00:00
$code = (int)$code;
2022-07-03 22:07:00 +00:00
if($code < 100 || $code >= 600)
$code = 400;
return $code;
});
$router->dispatch();