diff --git a/dummy.php b/dummy.php index 0437e18..f5ff127 100644 --- a/dummy.php +++ b/dummy.php @@ -3,12 +3,3 @@ require 'vendor/autoload.php'; const ROOT = __DIR__; - -class Container -{ - static SokoWeb\Interfaces\Database\IConnection $dbConnection; - static SokoWeb\Interfaces\Database\IAuditLogger $auditLogger; - static SokoWeb\Routing\RouteCollection $routeCollection; - static SokoWeb\Interfaces\Session\ISessionHandler $sessionHandler; - static SokoWeb\Interfaces\Request\IRequest $request; -} diff --git a/src/Interfaces/PersistentData/IPersistentDataManager.php b/src/Interfaces/PersistentData/IPersistentDataManager.php new file mode 100644 index 0000000..c6c931d --- /dev/null +++ b/src/Interfaces/PersistentData/IPersistentDataManager.php @@ -0,0 +1,20 @@ +dbConnection = $dbConnection; + $this->auditLogger = $auditLogger; + } + public function selectFromDb(Select $select, string $type, bool $useRelations = false, array $withRelations = []) { $select = $this->createSelect($select, $type, $useRelations, $withRelations); @@ -39,7 +52,7 @@ class PersistentDataManager public function selectFromDbById($id, string $type, bool $useRelations = false) { - $select = new Select(\Container::$dbConnection); + $select = new Select($this->dbConnection); $select->whereId($id); return $this->selectFromDb($select, $type, $useRelations); @@ -117,7 +130,7 @@ class PersistentDataManager $modified = $model->toArray(); $id = $model->getId(); - $modify = new Modify(\Container::$dbConnection, $model::getTable(), \Container::$auditLogger); + $modify = new Modify($this->dbConnection, $model::getTable(), $this->auditLogger); if ($id !== null) { $original = $model->getSnapshot(); @@ -149,7 +162,7 @@ class PersistentDataManager public function deleteFromDb(Model $model): void { - $modify = new Modify(\Container::$dbConnection, $model::getTable(), \Container::$auditLogger); + $modify = new Modify($this->dbConnection, $model::getTable(), $this->auditLogger); $modify->setId($model->getId()); $modify->fill($model->toArray()); $modify->delete(); diff --git a/src/Response/HttpResponse.php b/src/Response/HttpResponse.php index 9a0aed8..140b2ea 100644 --- a/src/Response/HttpResponse.php +++ b/src/Response/HttpResponse.php @@ -64,7 +64,7 @@ class HttpResponse list($route, $params) = $match; $this->request->setParsedRouteParams($params); $handler = $route->getHandler(); - $controller = new $handler[0]($this->request); + $controller = new $handler[0](); if ( $controller instanceof IAuthenticationRequired && @@ -103,7 +103,7 @@ class HttpResponse header('Content-Type: ' . $response->getContentType() . '; charset=UTF-8'); $response->render(); } elseif ($response instanceof IRedirect) { - header('Location: ' . $response->getUrl(), true, $response->getHttpCode()); + header('Location: ' . $this->getRedirectUrl($response), true, $response->getHttpCode()); } else { $this->render404(); } @@ -113,7 +113,7 @@ class HttpResponse { $this->request->session()->set('redirect_after_login', $this->rawUrl); $response = new Redirect($this->routeCollection->getRoute($this->appConfig['loginRouteId'])->generateLink(), IRedirect::TEMPORARY); - header('Location: ' . $response->getUrl(), true, $response->getHttpCode()); + header('Location: ' . $this->getRedirectUrl($response), true, $response->getHttpCode()); } private function renderAntiCsrfError(): void @@ -142,4 +142,13 @@ class HttpResponse header('Content-Type: text/html; charset=UTF-8', true, 500); $content->render(); } + + private function getRedirectUrl(IRedirect $redirect): string + { + $url = $redirect->getTarget(); + if (!preg_match('/^http(s)?/', $url) === 1) { + $url = $this->request->getBase() . $url; + } + return $url; + } } diff --git a/src/Response/Redirect.php b/src/Response/Redirect.php index 5cc4994..147391a 100644 --- a/src/Response/Redirect.php +++ b/src/Response/Redirect.php @@ -14,15 +14,9 @@ class Redirect implements IRedirect $this->type = $type; } - public function getUrl(): string + public function getTarget(): string { - if (preg_match('/^http(s)?/', $this->target) === 1) { - $link = $this->target; - } else { - $link = \Container::$request->getBase() . $this->target; - } - - return $link; + return $this->target; } public function getHttpCode(): int diff --git a/src/Session/DatabaseSessionHandler.php b/src/Session/DatabaseSessionHandler.php index c094431..45cab2d 100644 --- a/src/Session/DatabaseSessionHandler.php +++ b/src/Session/DatabaseSessionHandler.php @@ -3,15 +3,23 @@ use DateTime; use SokoWeb\Database\Query\Modify; use SokoWeb\Database\Query\Select; +use SokoWeb\Interfaces\Database\IConnection; use SokoWeb\Interfaces\Database\IResultSet; use SokoWeb\Interfaces\Session\ISessionHandler; class DatabaseSessionHandler implements ISessionHandler { + private IConnection $dbConnection; + private bool $exists = false; private bool $written = false; + public function __construct(IConnection $dbConnection) + { + $this->dbConnection = $dbConnection; + } + public function open($savePath, $sessionName): bool { return true; @@ -24,7 +32,7 @@ class DatabaseSessionHandler implements ISessionHandler public function read($id): string { - $select = new Select(\Container::$dbConnection, 'sessions'); + $select = new Select($this->dbConnection, 'sessions'); $select->columns(['data']); $select->whereId(substr($id, 0, 32)); @@ -41,7 +49,7 @@ class DatabaseSessionHandler implements ISessionHandler public function write($id, $data): bool { - $modify = new Modify(\Container::$dbConnection, 'sessions'); + $modify = new Modify($this->dbConnection, 'sessions'); if ($this->exists) { $modify->setId(substr($id, 0, 32)); @@ -60,7 +68,7 @@ class DatabaseSessionHandler implements ISessionHandler public function destroy($id): bool { - $modify = new Modify(\Container::$dbConnection, 'sessions'); + $modify = new Modify($this->dbConnection, 'sessions'); $modify->setId(substr($id, 0, 32)); $modify->delete(); @@ -93,7 +101,7 @@ class DatabaseSessionHandler implements ISessionHandler return true; } - $modify = new Modify(\Container::$dbConnection, 'sessions'); + $modify = new Modify($this->dbConnection, 'sessions'); $modify->setId(substr($id, 0, 32)); $modify->set('updated', (new DateTime())->format('Y-m-d H:i:s')); diff --git a/templates/app.php.tpl b/templates/app.php.tpl index be77248..f3e7534 100644 --- a/templates/app.php.tpl +++ b/templates/app.php.tpl @@ -16,10 +16,12 @@ class Container { static SokoWeb\Interfaces\Database\IConnection $dbConnection; static SokoWeb\Interfaces\Database\IAuditLogger $auditLogger; - static SokoWeb\Routing\RouteCollection $routeCollection; + static SokoWeb\Interfaces\PersistentData\IPersistentDataManager $persistentDataManager; + static SokoWeb\Interfaces\Routing\IRouteCollection $routeCollection; static SokoWeb\Interfaces\Session\ISessionHandler $sessionHandler; static SokoWeb\Interfaces\Request\IRequest $request; } Container::$dbConnection = new SokoWeb\Database\Mysql\Connection($_ENV['DB_HOST'], $_ENV['DB_USER'], $_ENV['DB_PASSWORD'], $_ENV['DB_NAME']); Container::$auditLogger = new {app}\Database\AuditLogger(Container::$dbConnection, 'audit_log'); +Container::$persistentDataManager = new SokoWeb\PersistentData\PersistentDataManager(Container::$dbConnection, Container::$auditLogger); diff --git a/templates/web.php.tpl b/templates/web.php.tpl index 1a97365..47fe1f7 100644 --- a/templates/web.php.tpl +++ b/templates/web.php.tpl @@ -14,7 +14,7 @@ Container::$routeCollection = new SokoWeb\Routing\RouteCollection(); Container::$routeCollection->get('index', '', [{app}\Controller\HomeController::class, 'getIndex']); if (isset($_COOKIE['COOKIES_CONSENT'])) { - Container::$sessionHandler = new SokoWeb\Session\DatabaseSessionHandler(); + Container::$sessionHandler = new SokoWeb\Session\DatabaseSessionHandler(Container::$dbConnection); session_set_save_handler(Container::$sessionHandler, true); session_start([