mince/src/HomeRoutes.php

69 lines
2.7 KiB
PHP
Raw Normal View History

2023-08-16 23:45:46 +00:00
<?php
namespace Mince;
use Index\Routing\IRouter;
class HomeRoutes {
public function __construct(
2023-08-17 00:23:55 +00:00
private Servers $servers,
2023-08-16 23:45:46 +00:00
private Templating $templating,
2023-08-22 23:47:37 +00:00
private object $userInfo,
private string $loginUrl
2023-08-16 23:45:46 +00:00
) {}
public function register(IRouter $router): void {
$router->get('/', [$this, 'getIndex']);
2023-08-22 23:47:37 +00:00
$router->get('/downloads', [$this, 'getDownloads']);
$router->get('/login', fn($response) => $response->redirect($this->userInfo->success ? '/' : $this->loginUrl));
2023-08-16 23:45:46 +00:00
$router->get('/index.php', function($response) {
$response->redirect('/', true);
});
}
public function getIndex($response, $request) {
$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)) {
$errTitle = $errors[$error][0];
$errBody = $errors[$error][1];
} else {
$errTitle = 'Unexpected response from server';
$errBody = $error;
}
$this->templating->addVars([
'error' => [
'title' => $errTitle,
'body' => $errBody,
],
]);
}
if($this->userInfo->mc_whitelisted > 0)
$this->templating->setVar('whitelist_pending', floor($this->userInfo->mc_whitelisted / 300) === floor(time() / 300));
return $this->templating->render('index', [
2023-08-17 00:23:55 +00:00
'servers' => $this->servers->getServers(deleted: false),
2023-08-16 23:45:46 +00:00
'wladdform_username' => $name,
]);
}
2023-08-22 23:47:37 +00:00
public function getDownloads() {
return $this->templating->render('downloads');
}
2023-08-16 23:45:46 +00:00
}