mapguesser/web.php

147 lines
7.9 KiB
PHP
Raw Normal View History

<?php
2023-04-16 21:37:39 +02:00
use SokoWeb\Response\HttpResponse;
2023-04-16 21:44:11 +02:00
use SokoWeb\Routing\RouteCollection;
use SokoWeb\Session\DatabaseSessionHandler;
use SokoWeb\Request\Request;
2023-05-02 13:18:14 +02:00
use SokoWeb\Request\Session;
2023-04-16 21:44:11 +02:00
use MapGuesser\Controller\MapsController;
use MapGuesser\Controller\HomeController;
use MapGuesser\Controller\LoginController;
use MapGuesser\Controller\UserController;
use MapGuesser\Controller\GameController;
use MapGuesser\Controller\GameFlowController;
use MapGuesser\Controller\MapAdminController;
use MapGuesser\Repository\UserRepository;
2023-04-16 21:37:39 +02:00
require 'main.php';
if (!empty($_ENV['DEV'])) {
error_reporting(E_ALL);
ini_set('display_errors', '1');
} else {
ini_set('display_errors', '0');
}
2023-04-16 21:44:11 +02:00
Container::$routeCollection = new RouteCollection();
2023-04-16 21:44:11 +02:00
Container::$routeCollection->get('index', '', [MapsController::class, 'getMaps']);
Container::$routeCollection->get('startSession', 'startSession.json', [HomeController::class, 'startSession']);
Container::$routeCollection->group('login', function (RouteCollection $routeCollection) {
$routeCollection->get('login', '', [LoginController::class, 'getLoginForm']);
$routeCollection->post('login-action', '', [LoginController::class, 'login']);
$routeCollection->get('login-google', 'google', [LoginController::class, 'getGoogleLoginRedirect']);
$routeCollection->get('login-google-action', 'google/code', [LoginController::class, 'loginWithGoogle']);
});
2023-04-16 21:44:11 +02:00
Container::$routeCollection->group('signup', function (RouteCollection $routeCollection) {
$routeCollection->get('signup', '', [LoginController::class, 'getSignupForm']);
$routeCollection->post('signup-action', '', [LoginController::class, 'signup']);
$routeCollection->get('signup-google', 'google', [LoginController::class, 'getSignupWithGoogleForm']);
$routeCollection->post('signup.reset', 'reset', [LoginController::class, 'resetSignup']);
$routeCollection->post('signup-google.reset', 'google/reset', [LoginController::class, 'resetGoogleSignup']);
$routeCollection->get('signup.success', 'success', [LoginController::class, 'getSignupSuccess']);
$routeCollection->get('signup.activate', 'activate/{token}', [LoginController::class, 'activate']);
$routeCollection->get('signup.cancel', 'cancel/{token}', [LoginController::class, 'cancel']);
});
2023-04-16 21:44:11 +02:00
Container::$routeCollection->group('password', function (RouteCollection $routeCollection) {
$routeCollection->get('password-requestReset', 'requestReset', [LoginController::class, 'getRequestPasswordResetForm']);
$routeCollection->post('password-requestReset-action', 'requestReset', [LoginController::class, 'requestPasswordReset']);
$routeCollection->get('password-requestReset.success', 'requestReset/success', [LoginController::class, 'getRequestPasswordResetSuccess']);
$routeCollection->get('password-reset', 'reset/{token}', [LoginController::class, 'getResetPasswordForm']);
$routeCollection->post('password-reset.action', 'reset/{token}', [LoginController::class, 'resetPassword']);
});
2023-04-16 21:44:11 +02:00
Container::$routeCollection->get('logout', 'logout', [LoginController::class, 'logout']);
Container::$routeCollection->group('account', function (RouteCollection $routeCollection) {
$routeCollection->get('account', '', [UserController::class, 'getAccount']);
$routeCollection->post('account-action', '', [UserController::class, 'saveAccount']);
$routeCollection->get('account.delete', 'delete', [UserController::class, 'getDeleteAccount']);
$routeCollection->post('account.delete-action', 'delete', [UserController::class, 'deleteAccount']);
$routeCollection->get('account.googleAuthenticate', 'googleAuthenticate', [UserController::class, 'getGoogleAuthenticateRedirect']);
$routeCollection->get('account.googleAuthenticate-action', 'googleAuthenticate/code', [UserController::class, 'authenticateWithGoogle']);
});
2023-04-16 21:44:11 +02:00
//Container::$routeCollection->get('maps', 'maps', [MapsController::class, 'getMaps']);
Container::$routeCollection->group('game', function (RouteCollection $routeCollection) {
$routeCollection->get('game', '{mapId}', [GameController::class, 'getGame']);
$routeCollection->post('game.prepare-json', '{mapId}/prepare.json', [GameController::class, 'prepareGame']);
$routeCollection->post('game.initialData-json', '{mapId}/initialData.json', [GameFlowController::class, 'initialData']);
$routeCollection->post('game.guess-json', '{mapId}/guess.json', [GameFlowController::class, 'guess']);
});
2023-04-16 21:44:11 +02:00
Container::$routeCollection->group('multiGame', function (RouteCollection $routeCollection) {
$routeCollection->get('multiGame.new', 'new/{mapId}', [GameController::class, 'getNewMultiGame']);
$routeCollection->get('multiGame', '{roomId}', [GameController::class, 'getMultiGame']);
$routeCollection->post('multiGame.prepare-json', '{roomId}/prepare.json', [GameController::class, 'prepareMultiGame']);
$routeCollection->post('multiGame.initialData-json', '{roomId}/initialData.json', [GameFlowController::class, 'multiInitialData']);
$routeCollection->post('multiGame.nextRound-json', '{roomId}/nextRound.json', [GameFlowController::class, 'multiNextRound']);
$routeCollection->post('multiGame.guess-json', '{roomId}/guess.json', [GameFlowController::class, 'multiGuess']);
});
2023-04-16 21:44:11 +02:00
Container::$routeCollection->group('challenge', function (RouteCollection $routeCollection) {
$routeCollection->post('challenge.create', 'create.json', [GameController::class, 'createNewChallenge']);
$routeCollection->get('challenge', '{challengeToken}', [GameController::class, 'getChallenge']);
$routeCollection->post('challenge.prepare-json', '{challengeToken}/prepare.json', [GameController::class, 'prepareChallenge']);
$routeCollection->post('challenge.initialData-json', '{challengeToken}/initialData.json', [GameFlowController::class, 'challengeInitialData']);
$routeCollection->post('challenge.guess-json', '{challengeToken}/guess.json', [GameFlowController::class, 'challengeGuess']);
});
2023-04-16 21:44:11 +02:00
Container::$routeCollection->group('admin', function (RouteCollection $routeCollection) {
$routeCollection->get('admin.mapEditor', 'mapEditor/{mapId?}', [MapAdminController::class, 'getMapEditor']);
$routeCollection->get('admin.place', 'place.json/{placeId}', [MapAdminController::class, 'getPlace']);
$routeCollection->post('admin.saveMap', 'saveMap/{mapId}/json', [MapAdminController::class, 'saveMap']);
$routeCollection->post('admin.deleteMap', 'deleteMap/{mapId}', [MapAdminController::class, 'deleteMap']);
});
if (isset($_COOKIE['COOKIES_CONSENT'])) {
2023-05-02 13:18:14 +02:00
Container::$sessionHandler = new DatabaseSessionHandler(
Container::$dbConnection,
'sessions',
new DateTime('-7 days')
);
session_set_save_handler(Container::$sessionHandler, true);
session_start([
2020-07-05 16:48:37 +02:00
'gc_probability' => 0, // old sessions are deleted by MaintainDatabaseCommand
2023-05-02 13:18:37 +02:00
'cookie_lifetime' => 0,
'cookie_path' => '/',
'cookie_httponly' => true,
'cookie_samesite' => 'Lax'
]);
// this is needed to handle old type of session IDs
if (!Container::$sessionHandler->validateId(session_id())) {
session_regenerate_id(true);
}
} else {
$_SESSION = [];
}
2023-04-16 21:44:11 +02:00
Container::$request = new Request(
2023-04-07 20:28:14 +02:00
$_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['HTTP_HOST'],
$_GET,
$_POST,
2023-04-16 17:01:16 +02:00
getallheaders(),
2023-05-02 13:18:14 +02:00
new Session($_SESSION),
2023-04-16 21:44:11 +02:00
new UserRepository()
2023-04-07 20:28:14 +02:00
);
if (!Container::$request->session()->has('anti_csrf_token')) {
Container::$request->session()->set('anti_csrf_token', bin2hex(random_bytes(16)));
}
2023-04-16 21:37:39 +02:00
$appConfig = [
'antiCsrfTokenName' => 'anti_csrf_token',
'antiCsrfTokenErrorResponse' => ['error' => 'no_valid_anti_csrf_token'],
'antiCsrfTokenExceptions' => [],
'loginRouteId' => 'login',
2023-04-20 00:04:59 +02:00
'error404View' => 'error/404',
'error500View' => 'error/500'
2023-04-16 21:37:39 +02:00
];
$httpReponse = new HttpResponse(
Container::$request,
2023-04-20 00:04:10 +02:00
Container::$dbConnection,
2023-04-16 21:37:39 +02:00
Container::$routeCollection,
$appConfig,
$_SERVER['REQUEST_METHOD'],
$_SERVER['REQUEST_URI']
);
$httpReponse->render();