2020-05-17 22:30:22 +02:00
|
|
|
<?php
|
|
|
|
|
2020-06-13 20:42:07 +02:00
|
|
|
require '../web.php';
|
2020-05-17 22:30:22 +02:00
|
|
|
|
2020-05-31 20:44:45 +02:00
|
|
|
$method = strtolower($_SERVER['REQUEST_METHOD']);
|
|
|
|
$url = substr($_SERVER['REQUEST_URI'], strlen('/'));
|
2020-05-25 18:52:31 +02:00
|
|
|
if (($pos = strpos($url, '?')) !== false) {
|
|
|
|
$url = substr($url, 0, $pos);
|
|
|
|
}
|
2020-05-31 20:44:45 +02:00
|
|
|
$url = rawurldecode($url);
|
|
|
|
|
2020-06-21 15:44:40 +02:00
|
|
|
$match = Container::$routeCollection->match($method, $url == '' ? [] : explode('/', $url));
|
2020-05-19 03:19:23 +02:00
|
|
|
|
2020-05-31 20:44:45 +02:00
|
|
|
if ($match !== null) {
|
|
|
|
list($route, $params) = $match;
|
2020-05-19 03:19:23 +02:00
|
|
|
|
2020-06-14 17:11:48 +02:00
|
|
|
Container::$request->setParsedRouteParams($params);
|
2020-05-31 20:44:45 +02:00
|
|
|
|
2020-06-09 00:56:00 +02:00
|
|
|
$handler = $route->getHandler();
|
2020-06-14 17:11:48 +02:00
|
|
|
$controller = new $handler[0](Container::$request);
|
2020-06-07 23:37:01 +02:00
|
|
|
|
|
|
|
if ($controller instanceof MapGuesser\Interfaces\Authorization\ISecured) {
|
|
|
|
$authorized = $controller->authorize();
|
|
|
|
} else {
|
|
|
|
$authorized = true;
|
|
|
|
}
|
|
|
|
|
2020-06-14 17:11:48 +02:00
|
|
|
if ($method === 'post' && Container::$request->post('anti_csrf_token') !== Container::$request->session()->get('anti_csrf_token')) {
|
2020-06-13 22:38:30 +02:00
|
|
|
header('Content-Type: text/html; charset=UTF-8', true, 403);
|
|
|
|
echo json_encode(['error' => 'no_valid_anti_csrf_token']);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-06-07 23:37:01 +02:00
|
|
|
if ($authorized) {
|
2020-06-09 00:56:00 +02:00
|
|
|
$response = call_user_func([$controller, $handler[1]]);
|
2020-06-07 23:37:01 +02:00
|
|
|
|
|
|
|
if ($response instanceof MapGuesser\Interfaces\Response\IContent) {
|
|
|
|
header('Content-Type: ' . $response->getContentType() . '; charset=UTF-8');
|
2020-06-27 14:26:19 +02:00
|
|
|
$response->render();
|
2020-06-07 23:37:01 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
} elseif ($response instanceof MapGuesser\Interfaces\Response\IRedirect) {
|
2020-06-21 01:27:37 +02:00
|
|
|
header('Location: ' . $response->getUrl(), true, $response->getHttpCode());
|
2020-06-07 23:37:01 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2020-05-31 20:44:45 +02:00
|
|
|
}
|
|
|
|
}
|
2020-06-07 23:37:01 +02:00
|
|
|
|
2020-06-27 00:23:17 +02:00
|
|
|
$content = new MapGuesser\Response\HtmlContent('error/404');
|
2020-06-07 23:37:01 +02:00
|
|
|
header('Content-Type: text/html; charset=UTF-8', true, 404);
|
2020-06-27 14:26:19 +02:00
|
|
|
$content->render();
|