Moved routes into classes.
This commit is contained in:
parent
8b47c866ed
commit
a708067ba7
3 changed files with 127 additions and 91 deletions
|
@ -29,8 +29,11 @@ $templating->addVars([
|
|||
]);
|
||||
|
||||
$router = new HttpFx;
|
||||
$router->use('/', function($response, $request) {
|
||||
$response->setPoweredBy('Mince');
|
||||
});
|
||||
|
||||
$router->setDefaultErrorHandler(function($response, $request, $code, $text) use ($userInfo, $templating) {
|
||||
$router->setDefaultErrorHandler(function($response, $request, $code, $text) use ($templating) {
|
||||
$response->setContent($templating->render('http-error', [
|
||||
'error' => [
|
||||
'code' => sprintf('%03d', $code),
|
||||
|
@ -39,95 +42,7 @@ $router->setDefaultErrorHandler(function($response, $request, $code, $text) use
|
|||
]));
|
||||
});
|
||||
|
||||
$router->use('/', function($response, $request) {
|
||||
$response->setPoweredBy('Mince');
|
||||
});
|
||||
|
||||
$router->get('/index.php', function($response) {
|
||||
$response->redirect('/', true);
|
||||
});
|
||||
|
||||
$router->get('/', function($response, $request) use ($userInfo, $templating) {
|
||||
$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;
|
||||
}
|
||||
|
||||
$templating->addVars([
|
||||
'error' => [
|
||||
'title' => $mErrorTitle,
|
||||
'body' => $mErrorComment,
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
if($userInfo->mc_whitelisted > 0)
|
||||
$templating->setVar('whitelist_pending', floor($userInfo->mc_whitelisted / 300) === floor(time() / 300));
|
||||
|
||||
return $templating->render('index', [
|
||||
'wladdform_username' => $name,
|
||||
]);
|
||||
});
|
||||
|
||||
$router->use('/whitelist', function($response, $request) use ($csrfp) {
|
||||
if(!$request->isFormContent()) {
|
||||
$response->redirect('/?error=request');
|
||||
return true;
|
||||
}
|
||||
|
||||
$body = $request->getContent();
|
||||
|
||||
if(!$body->hasParam('csrfp') || !$csrfp->verifyToken((string)$body->getParam('csrfp'))) {
|
||||
$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}");
|
||||
});
|
||||
(new HomeRoutes($templating, $userInfo))->register($router);
|
||||
(new WhitelistRoutes(new Whitelist($db), $csrfp, $userInfo))->register($router);
|
||||
|
||||
$router->dispatch();
|
||||
|
|
60
src/HomeRoutes.php
Normal file
60
src/HomeRoutes.php
Normal file
|
@ -0,0 +1,60 @@
|
|||
<?php
|
||||
namespace Mince;
|
||||
|
||||
use Index\Routing\IRouter;
|
||||
|
||||
class HomeRoutes {
|
||||
public function __construct(
|
||||
private Templating $templating,
|
||||
private object $userInfo
|
||||
) {}
|
||||
|
||||
public function register(IRouter $router): void {
|
||||
$router->get('/', [$this, 'getIndex']);
|
||||
|
||||
$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', [
|
||||
'wladdform_username' => $name,
|
||||
]);
|
||||
}
|
||||
}
|
61
src/WhitelistRoutes.php
Normal file
61
src/WhitelistRoutes.php
Normal file
|
@ -0,0 +1,61 @@
|
|||
<?php
|
||||
namespace Mince;
|
||||
|
||||
use Index\Routing\IRouter;
|
||||
use Index\Security\CSRFP;
|
||||
|
||||
class WhitelistRoutes {
|
||||
public function __construct(
|
||||
private Whitelist $whitelist,
|
||||
private CSRFP $csrfp,
|
||||
private object $userInfo
|
||||
) {}
|
||||
|
||||
public function register(IRouter $router): void {
|
||||
$router->use('/whitelist', [$this, 'verifyRequest']);
|
||||
$router->post('/whitelist/add', [$this, 'postAdd']);
|
||||
$router->post('/whitelist/remove', [$this, 'postRemove']);
|
||||
}
|
||||
|
||||
public function verifyRequest($response, $request) {
|
||||
if(!$request->isFormContent()) {
|
||||
$response->redirect('/?error=request');
|
||||
return true;
|
||||
}
|
||||
|
||||
$body = $request->getContent();
|
||||
|
||||
if(!$body->hasParam('csrfp') || !$this->csrfp->verifyToken((string)$body->getParam('csrfp'))) {
|
||||
$response->redirect('/?error=verify');
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public function postAdd($response, $request) {
|
||||
if($this->userInfo->user_id == 45) {
|
||||
$response->redirect('/?error=itainthappenin');
|
||||
return true;
|
||||
}
|
||||
|
||||
$body = $request->getContent();
|
||||
$name = (string)$body->getParam('name');
|
||||
$resp = $this->whitelist->add($this->userInfo, $name);
|
||||
|
||||
if($resp === '')
|
||||
$response->redirect('/');
|
||||
else {
|
||||
if($resp === 'invalid')
|
||||
$name = '';
|
||||
$response->redirect("/?error={$resp}&name={$name}");
|
||||
}
|
||||
}
|
||||
|
||||
public function postRemove($response) {
|
||||
$resp = $this->whitelist->remove($this->userInfo);
|
||||
|
||||
if($resp === '')
|
||||
$response->redirect('/');
|
||||
else
|
||||
$response->redirect("/?error={$resp}");
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue