2023-04-08 00:42:55 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
use SokoWeb\Interfaces\Response\IRedirect;
|
|
|
|
use SokoWeb\Response\Redirect;
|
|
|
|
|
|
|
|
require '../web.php';
|
|
|
|
|
|
|
|
$method = strtolower($_SERVER['REQUEST_METHOD']);
|
|
|
|
$url = substr($_SERVER['REQUEST_URI'], strlen('/'));
|
|
|
|
if (($pos = strpos($url, '?')) !== false) {
|
|
|
|
$url = substr($url, 0, $pos);
|
|
|
|
}
|
|
|
|
$url = rawurldecode($url);
|
|
|
|
|
|
|
|
$match = Container::$routeCollection->match($method, $url == '' ? [] : explode('/', $url));
|
|
|
|
|
|
|
|
if ($match !== null) {
|
|
|
|
list($route, $params) = $match;
|
|
|
|
|
|
|
|
Container::$request->setParsedRouteParams($params);
|
|
|
|
|
|
|
|
$handler = $route->getHandler();
|
|
|
|
$controller = new $handler[0](Container::$request);
|
|
|
|
|
|
|
|
if ($controller instanceof SokoWeb\Interfaces\Authorization\ISecured) {
|
|
|
|
$authorized = $controller->authorize();
|
|
|
|
} else {
|
|
|
|
$authorized = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$authorized) {
|
2023-04-08 10:45:56 +02:00
|
|
|
Container::$request->session()->set('redirect_after_login', '/' . $url);
|
2023-04-08 00:42:55 +02:00
|
|
|
$response = new Redirect(Container::$routeCollection->getRoute('login')->generateLink(), IRedirect::TEMPORARY);
|
|
|
|
header('Location: ' . $response->getUrl(), true, $response->getHttpCode());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($method === 'post' && Container::$request->post('anti_csrf_token') !== Container::$request->session()->get('anti_csrf_token')) {
|
|
|
|
$content = new SokoWeb\Response\JsonContent(['error' => 'no_valid_anti_csrf_token']);
|
|
|
|
header('Content-Type: text/html; charset=UTF-8', true, 403);
|
|
|
|
$content->render();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$response = call_user_func([$controller, $handler[1]]);
|
|
|
|
if ($response instanceof SokoWeb\Interfaces\Response\IContent) {
|
|
|
|
header('Content-Type: ' . $response->getContentType() . '; charset=UTF-8');
|
|
|
|
$response->render();
|
|
|
|
return;
|
|
|
|
} elseif ($response instanceof SokoWeb\Interfaces\Response\IRedirect) {
|
|
|
|
header('Location: ' . $response->getUrl(), true, $response->getHttpCode());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$content = new SokoWeb\Response\HtmlContent('error/404');
|
|
|
|
header('Content-Type: text/html; charset=UTF-8', true, 404);
|
|
|
|
$content->render();
|