feature/adapt-to-new-soko-web #59

Merged
bence merged 3 commits from feature/adapt-to-new-soko-web into develop 2023-04-16 21:46:08 +02:00
6 changed files with 111 additions and 143 deletions

View File

@ -10,7 +10,7 @@
}
],
"require": {
"esoko/soko-web": "0.3",
"esoko/soko-web": "0.4",
"fzaninotto/faker": "^1.9"
},
"require-dev": {

8
composer.lock generated
View File

@ -4,15 +4,15 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "0f82e530dedc414cfc0b04daf3c2085c",
"content-hash": "5e355d5efeb34e7e0ad2a69b1ec109d6",
"packages": [
{
"name": "esoko/soko-web",
"version": "v0.3",
"version": "v0.4",
"source": {
"type": "git",
"url": "https://git.esoko.eu/esoko/soko-web.git",
"reference": "014a5480967c03c00dda5ee34c7eaf4be224b96e"
"reference": "948b36c80d324e07339a543d97b9e629487f3a45"
},
"require": {
"phpmailer/phpmailer": "^6.8",
@ -33,7 +33,7 @@
"GNU GPL 3.0"
],
"description": "Lightweight web framework",
"time": "2023-04-16T14:54:22+00:00"
"time": "2023-04-16T18:52:06+00:00"
},
{
"name": "fzaninotto/faker",

View File

@ -1,66 +1,3 @@
<?php
use SokoWeb\Interfaces\Response\IRedirect;
use SokoWeb\Interfaces\Response\IContent;
use SokoWeb\Interfaces\Authentication\IAuthenticationRequired;
use SokoWeb\Interfaces\Authorization\ISecured;
use SokoWeb\Response\Redirect;
use SokoWeb\Response\HtmlContent;
use SokoWeb\Response\JsonContent;
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 IAuthenticationRequired &&
$controller->isAuthenticationRequired() &&
Container::$request->user() === null
) {
Container::$request->session()->set('redirect_after_login', substr($_SERVER['REQUEST_URI'], strlen('/')));
$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 JsonContent(['error' => 'no_valid_anti_csrf_token']);
header('Content-Type: text/html; charset=UTF-8', true, 403);
$content->render();
return;
}
if (
!($controller instanceof ISecured) ||
$controller->authorize()
) {
$response = call_user_func([$controller, $handler[1]]);
if ($response instanceof IContent) {
header('Content-Type: ' . $response->getContentType() . '; charset=UTF-8');
$response->render();
return;
} elseif ($response instanceof IRedirect) {
header('Location: ' . $response->getUrl(), true, $response->getHttpCode());
return;
}
}
}
$content = new HtmlContent('error/404');
header('Content-Type: text/html; charset=UTF-8', true, 404);
$content->render();

View File

@ -58,7 +58,7 @@ class LoginController
return new Redirect($this->redirectUrl, IRedirect::TEMPORARY);
}
return new HtmlContent('login/login', ['redirectUrl' => '/' . $this->redirectUrl]);
return new HtmlContent('login/login', ['redirectUrl' => $this->redirectUrl]);
}
public function getGoogleLoginRedirect(): IRedirect
@ -72,7 +72,7 @@ class LoginController
$oAuth = new GoogleOAuth(new Request());
$url = $oAuth->getDialogUrl(
$state,
$this->request->getBase() . '/' . \Container::$routeCollection->getRoute('login-google-action')->generateLink(),
$this->request->getBase() . \Container::$routeCollection->getRoute('login-google-action')->generateLink(),
$nonce
);
@ -117,7 +117,7 @@ class LoginController
$user = $this->userRepository->getByEmail($userData['email']);
return new HtmlContent('login/google_signup', ['found' => $user !== null, 'email' => $userData['email'], 'redirectUrl' => '/' . $this->redirectUrl]);
return new HtmlContent('login/google_signup', ['found' => $user !== null, 'email' => $userData['email'], 'redirectUrl' => $this->redirectUrl]);
}
public function getRequestPasswordResetForm()
@ -151,7 +151,7 @@ class LoginController
$user = $this->userRepository->getById($resetter->getUserId());
return new HtmlContent('login/reset_password', ['success' => true, 'token' => $token, 'email' => $user->getEmail(), 'redirectUrl' => '/' . $this->redirectUrl]);
return new HtmlContent('login/reset_password', ['success' => true, 'token' => $token, 'email' => $user->getEmail(), 'redirectUrl' => $this->redirectUrl]);
}
public function login(): IContent
@ -182,7 +182,7 @@ class LoginController
return new JsonContent([
'redirect' => [
'target' => '/' . \Container::$routeCollection->getRoute('signup')->generateLink()
'target' => \Container::$routeCollection->getRoute('signup')->generateLink()
]
]);
}
@ -227,7 +227,7 @@ class LoginController
$oAuth = new GoogleOAuth(new Request());
$tokenData = $oAuth->getToken(
$this->request->query('code'),
$this->request->getBase() . '/' . \Container::$routeCollection->getRoute('login-google-action')->generateLink()
$this->request->getBase() . \Container::$routeCollection->getRoute('login-google-action')->generateLink()
);
if (!isset($tokenData['id_token'])) {
@ -270,7 +270,7 @@ class LoginController
{
if ($this->request->user() !== null) {
$this->deleteRedirectUrl();
return new JsonContent(['redirect' => ['target' => '/' . $this->redirectUrl]]);
return new JsonContent(['redirect' => ['target' => $this->redirectUrl]]);
}
$user = $this->userRepository->getByEmail($this->request->post('email'));
@ -290,7 +290,7 @@ class LoginController
$this->request->setUser($user);
$this->deleteRedirectUrl();
$data = ['redirect' => ['target' => '/' . $this->redirectUrl]];
$data = ['redirect' => ['target' => $this->redirectUrl]];
} else {
$data = [
'error' => [
@ -485,7 +485,7 @@ class LoginController
$this->deleteRedirectUrl();
return new JsonContent([
'redirect' => [
'target' => '/' . $this->redirectUrl
'target' => $this->redirectUrl
]
]);
}
@ -563,7 +563,7 @@ class LoginController
$this->deleteRedirectUrl();
return new JsonContent([
'redirect' => [
'target' => '/' . $this->redirectUrl
'target' => $this->redirectUrl
]
]);
}
@ -574,7 +574,7 @@ class LoginController
if ($resetter === null || $resetter->getExpiresDate() < new DateTime()) {
return new JsonContent([
'redirect' => [
'target' => '/' . \Container::$routeCollection->getRoute('password-reset')->generateLink(['token' => $token])
'target' => \Container::$routeCollection->getRoute('password-reset')->generateLink(['token' => $token])
]
]);
}
@ -615,9 +615,9 @@ class LoginController
$mail->setSubject('Welcome to ' . $_ENV['APP_NAME'] . ' - Activate your account');
$mail->setBodyFromTemplate('signup', [
'EMAIL' => $email,
'ACTIVATE_LINK' => $this->request->getBase() . '/' .
'ACTIVATE_LINK' => $this->request->getBase() .
\Container::$routeCollection->getRoute('signup.activate')->generateLink(['token' => $token]),
'CANCEL_LINK' => $this->request->getBase() . '/' .
'CANCEL_LINK' => $this->request->getBase() .
\Container::$routeCollection->getRoute('signup.cancel')->generateLink(['token' => $token]),
'ACTIVATABLE_UNTIL' => (clone $created)->add(new DateInterval('P1D'))->format('Y-m-d H:i T')
]);
@ -659,7 +659,7 @@ class LoginController
$mail->setSubject($_ENV['APP_NAME'] . ' - Password reset');
$mail->setBodyFromTemplate('password-reset', [
'EMAIL' => $email,
'RESET_LINK' => $this->request->getBase() . '/' .
'RESET_LINK' => $this->request->getBase() .
\Container::$routeCollection->getRoute('password-reset')->generateLink(['token' => $token]),
'EXPIRES' => $expires->format('Y-m-d H:i T')
]);

View File

@ -78,7 +78,7 @@ class UserController implements IAuthenticationRequired
$url = $oAuth->getDialogUrl(
$state,
$this->request->getBase() . '/' . \Container::$routeCollection->getRoute('account.googleAuthenticate-action')->generateLink(),
$this->request->getBase() . \Container::$routeCollection->getRoute('account.googleAuthenticate-action')->generateLink(),
$nonce,
$user->getEmail()
);
@ -100,7 +100,7 @@ class UserController implements IAuthenticationRequired
$oAuth = new GoogleOAuth(new Request());
$tokenData = $oAuth->getToken(
$this->request->query('code'),
$this->request->getBase() . '/' . \Container::$routeCollection->getRoute('account.googleAuthenticate-action')->generateLink()
$this->request->getBase() . \Container::$routeCollection->getRoute('account.googleAuthenticate-action')->generateLink()
);
if (!isset($tokenData['id_token'])) {

149
web.php
View File

@ -1,5 +1,19 @@
<?php
use Container;
use SokoWeb\Response\HttpResponse;
use SokoWeb\Routing\RouteCollection;
use SokoWeb\Session\DatabaseSessionHandler;
use SokoWeb\Request\Request;
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;
require 'main.php';
if (!empty($_ENV['DEV'])) {
@ -10,74 +24,74 @@ if (!empty($_ENV['DEV'])) {
ini_set('display_errors', '0');
}
Container::$routeCollection = new SokoWeb\Routing\RouteCollection();
Container::$routeCollection = new RouteCollection();
Container::$routeCollection->get('index', '', [MapGuesser\Controller\MapsController::class, 'getMaps']);
Container::$routeCollection->get('startSession', 'startSession.json', [MapGuesser\Controller\HomeController::class, 'startSession']);
Container::$routeCollection->group('login', function (SokoWeb\Routing\RouteCollection $routeCollection) {
$routeCollection->get('login', '', [MapGuesser\Controller\LoginController::class, 'getLoginForm']);
$routeCollection->post('login-action', '', [MapGuesser\Controller\LoginController::class, 'login']);
$routeCollection->get('login-google', 'google', [MapGuesser\Controller\LoginController::class, 'getGoogleLoginRedirect']);
$routeCollection->get('login-google-action', 'google/code', [MapGuesser\Controller\LoginController::class, 'loginWithGoogle']);
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']);
});
Container::$routeCollection->group('signup', function (SokoWeb\Routing\RouteCollection $routeCollection) {
$routeCollection->get('signup', '', [MapGuesser\Controller\LoginController::class, 'getSignupForm']);
$routeCollection->post('signup-action', '', [MapGuesser\Controller\LoginController::class, 'signup']);
$routeCollection->get('signup-google', 'google', [MapGuesser\Controller\LoginController::class, 'getSignupWithGoogleForm']);
$routeCollection->post('signup-google-action', 'google', [MapGuesser\Controller\LoginController::class, 'signupWithGoogle']);
$routeCollection->post('signup.reset', 'reset', [MapGuesser\Controller\LoginController::class, 'resetSignup']);
$routeCollection->post('signup-google.reset', 'google/reset', [MapGuesser\Controller\LoginController::class, 'resetGoogleSignup']);
$routeCollection->get('signup.success', 'success', [MapGuesser\Controller\LoginController::class, 'getSignupSuccess']);
$routeCollection->get('signup.activate', 'activate/{token}', [MapGuesser\Controller\LoginController::class, 'activate']);
$routeCollection->get('signup.cancel', 'cancel/{token}', [MapGuesser\Controller\LoginController::class, 'cancel']);
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-google-action', 'google', [LoginController::class, 'signupWithGoogle']);
$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']);
});
Container::$routeCollection->group('password', function (SokoWeb\Routing\RouteCollection $routeCollection) {
$routeCollection->get('password-requestReset', 'requestReset', [MapGuesser\Controller\LoginController::class, 'getRequestPasswordResetForm']);
$routeCollection->post('password-requestReset-action', 'requestReset', [MapGuesser\Controller\LoginController::class, 'requestPasswordReset']);
$routeCollection->get('password-requestReset.success', 'requestReset/success', [MapGuesser\Controller\LoginController::class, 'getRequestPasswordResetSuccess']);
$routeCollection->get('password-reset', 'reset/{token}', [MapGuesser\Controller\LoginController::class, 'getResetPasswordForm']);
$routeCollection->post('password-reset.action', 'reset/{token}', [MapGuesser\Controller\LoginController::class, 'resetPassword']);
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']);
});
Container::$routeCollection->get('logout', 'logout', [MapGuesser\Controller\LoginController::class, 'logout']);
Container::$routeCollection->group('account', function (SokoWeb\Routing\RouteCollection $routeCollection) {
$routeCollection->get('account', '', [MapGuesser\Controller\UserController::class, 'getAccount']);
$routeCollection->post('account-action', '', [MapGuesser\Controller\UserController::class, 'saveAccount']);
$routeCollection->get('account.delete', 'delete', [MapGuesser\Controller\UserController::class, 'getDeleteAccount']);
$routeCollection->post('account.delete-action', 'delete', [MapGuesser\Controller\UserController::class, 'deleteAccount']);
$routeCollection->get('account.googleAuthenticate', 'googleAuthenticate', [MapGuesser\Controller\UserController::class, 'getGoogleAuthenticateRedirect']);
$routeCollection->get('account.googleAuthenticate-action', 'googleAuthenticate/code', [MapGuesser\Controller\UserController::class, 'authenticateWithGoogle']);
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']);
});
//Container::$routeCollection->get('maps', 'maps', [MapGuesser\Controller\MapsController::class, 'getMaps']);
Container::$routeCollection->group('game', function (SokoWeb\Routing\RouteCollection $routeCollection) {
$routeCollection->get('game', '{mapId}', [MapGuesser\Controller\GameController::class, 'getGame']);
$routeCollection->post('game.prepare-json', '{mapId}/prepare.json', [MapGuesser\Controller\GameController::class, 'prepareGame']);
$routeCollection->post('game.initialData-json', '{mapId}/initialData.json', [MapGuesser\Controller\GameFlowController::class, 'initialData']);
$routeCollection->post('game.guess-json', '{mapId}/guess.json', [MapGuesser\Controller\GameFlowController::class, 'guess']);
//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']);
});
Container::$routeCollection->group('multiGame', function (SokoWeb\Routing\RouteCollection $routeCollection) {
$routeCollection->get('multiGame.new', 'new/{mapId}', [MapGuesser\Controller\GameController::class, 'getNewMultiGame']);
$routeCollection->get('multiGame', '{roomId}', [MapGuesser\Controller\GameController::class, 'getMultiGame']);
$routeCollection->post('multiGame.prepare-json', '{roomId}/prepare.json', [MapGuesser\Controller\GameController::class, 'prepareMultiGame']);
$routeCollection->post('multiGame.initialData-json', '{roomId}/initialData.json', [MapGuesser\Controller\GameFlowController::class, 'multiInitialData']);
$routeCollection->post('multiGame.nextRound-json', '{roomId}/nextRound.json', [MapGuesser\Controller\GameFlowController::class, 'multiNextRound']);
$routeCollection->post('multiGame.guess-json', '{roomId}/guess.json', [MapGuesser\Controller\GameFlowController::class, 'multiGuess']);
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']);
});
Container::$routeCollection->group('challenge', function (SokoWeb\Routing\RouteCollection $routeCollection) {
$routeCollection->post('challenge.create', 'create.json', [\MapGuesser\Controller\GameController::class, 'createNewChallenge']);
$routeCollection->get('challenge', '{challengeToken}', [MapGuesser\Controller\GameController::class, 'getChallenge']);
$routeCollection->post('challenge.prepare-json', '{challengeToken}/prepare.json', [MapGuesser\Controller\GameController::class, 'prepareChallenge']);
$routeCollection->post('challenge.initialData-json', '{challengeToken}/initialData.json', [MapGuesser\Controller\GameFlowController::class, 'challengeInitialData']);
$routeCollection->post('challenge.guess-json', '{challengeToken}/guess.json', [MapGuesser\Controller\GameFlowController::class, 'challengeGuess']);
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']);
});
Container::$routeCollection->group('admin', function (SokoWeb\Routing\RouteCollection $routeCollection) {
$routeCollection->get('admin.mapEditor', 'mapEditor/{mapId?}', [MapGuesser\Controller\MapAdminController::class, 'getMapEditor']);
$routeCollection->get('admin.place', 'place.json/{placeId}', [MapGuesser\Controller\MapAdminController::class, 'getPlace']);
$routeCollection->post('admin.saveMap', 'saveMap/{mapId}/json', [MapGuesser\Controller\MapAdminController::class, 'saveMap']);
$routeCollection->post('admin.deleteMap', 'deleteMap/{mapId}', [MapGuesser\Controller\MapAdminController::class, 'deleteMap']);
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'])) {
Container::$sessionHandler = new SokoWeb\Session\DatabaseSessionHandler();
Container::$sessionHandler = new DatabaseSessionHandler();
session_set_save_handler(Container::$sessionHandler, true);
session_start([
@ -106,15 +120,32 @@ if (isset($_COOKIE['COOKIES_CONSENT'])) {
$_SESSION = [];
}
Container::$request = new SokoWeb\Request\Request(
Container::$request = new Request(
$_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['HTTP_HOST'],
$_GET,
$_POST,
getallheaders(),
$_SESSION,
new MapGuesser\Repository\UserRepository()
new UserRepository()
);
if (!Container::$request->session()->has('anti_csrf_token')) {
Container::$request->session()->set('anti_csrf_token', bin2hex(random_bytes(16)));
}
$appConfig = [
'antiCsrfTokenName' => 'anti_csrf_token',
'antiCsrfTokenErrorResponse' => ['error' => 'no_valid_anti_csrf_token'],
'antiCsrfTokenExceptions' => [],
'loginRouteId' => 'login',
'error404View' => 'error/404'
];
$httpReponse = new HttpResponse(
Container::$request,
Container::$routeCollection,
$appConfig,
$_SERVER['REQUEST_METHOD'],
$_SERVER['REQUEST_URI']
);
$httpReponse->render();