From ef73720d2be115c9059ace3171bc45ff7b2d3522 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20D?= Date: Mon, 13 Jun 2022 08:02:42 +0200 Subject: [PATCH] refactor: Fix initialization errors. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Rubén D --- app/modules/web/Init.php | 81 ++++++++++++------------- lib/SP/Core/Acl/Acl.php | 22 +++---- lib/SP/Core/Bootstrap/BootstrapBase.php | 6 +- lib/SP/Core/Bootstrap/BootstrapWeb.php | 7 +-- lib/SP/Core/ModuleBase.php | 8 +-- 5 files changed, 55 insertions(+), 69 deletions(-) diff --git a/app/modules/web/Init.php b/app/modules/web/Init.php index 6ce7d33d..c5c0465c 100644 --- a/app/modules/web/Init.php +++ b/app/modules/web/Init.php @@ -29,6 +29,7 @@ use Exception; use Klein\Klein; use SP\Core\Application; use SP\Core\Bootstrap\BootstrapBase; +use SP\Core\Bootstrap\BootstrapWeb; use SP\Core\Context\ContextBase; use SP\Core\Context\SessionContext; use SP\Core\Crypt\CryptSessionHandler; @@ -57,6 +58,7 @@ use SP\Domain\User\Services\UserProfileService; use SP\Domain\User\UserProfileServiceInterface; use SP\Http\Address; use SP\Http\RequestInterface; +use SP\Http\Uri; use SP\Infrastructure\Database\DatabaseUtil; use SP\Infrastructure\File\FileException; use SP\Plugin\PluginManager; @@ -83,7 +85,14 @@ final class Init extends HttpModuleBase /** * List of controllers that don't need to update the user's session activity */ - private const NO_SESSION_ACTIVITY = ['items', 'login']; + private const NO_SESSION_ACTIVITY = ['items', 'login']; + public const ROUTE_INSTALL = 'install/index'; + public const ROUTE_ERROR_DATABASE_CONNECTION = 'error/databaseConnection'; + public const ROUTE_ERROR_MAINTENANCE = 'error/maintenanceError'; + public const ROUTE_ERROR_DATABASE = 'error/databaseError'; + public const ROUTE_UPGRADE = 'upgrade/index'; + + private CSRF $csrf; private ThemeInterface $theme; private Language $language; @@ -180,33 +189,27 @@ final class Init extends HttpModuleBase if (!$this->checkInstalled()) { logger('Not installed', 'ERROR'); - $this->router->response() - ->redirect('index.php?r=install/index') - ->send(); + $this->router->response()->redirect(self::getUriFor(self::ROUTE_INSTALL))->send(); - return; + throw new InitializationException('Not installed'); } // Checks if the database is set up if (!$this->databaseUtil->checkDatabaseConnection()) { logger('Database connection error', 'ERROR'); - $this->router->response() - ->redirect('index.php?r=error/databaseConnection') - ->send(); + $this->router->response()->redirect(self::getUriFor(self::ROUTE_ERROR_DATABASE_CONNECTION))->send(); - return; + throw new InitializationException('Database connection error'); } // Checks if maintenance mode is turned on if ($this->checkMaintenanceMode()) { logger('Maintenance mode', 'INFO'); - $this->router->response() - ->redirect('index.php?r=error/maintenanceError') - ->send(); + $this->router->response()->redirect(self::getUriFor(self::ROUTE_ERROR_MAINTENANCE))->send(); - return; + throw new InitializationException('Maintenance mode'); } // Checks if upgrade is needed @@ -215,22 +218,18 @@ final class Init extends HttpModuleBase $this->config->generateUpgradeKey(); - $this->router->response() - ->redirect('index.php?r=upgrade/index') - ->send(); + $this->router->response()->redirect(self::getUriFor(self::ROUTE_UPGRADE))->send(); - return; + throw new InitializationException('Upgrade needed'); } // Checks if the database is set up if (!$this->databaseUtil->checkDatabaseTables($this->configData->getDbName())) { logger('Database checking error', 'ERROR'); - $this->router->response() - ->redirect('index.php?r=error/databaseError') - ->send(); + $this->router->response()->redirect(self::getUriFor(self::ROUTE_ERROR_DATABASE))->send(); - return; + throw new InitializationException('Database checking error'); } if (!in_array($controller, self::NO_SESSION_ACTIVITY)) { @@ -260,8 +259,6 @@ final class Init extends HttpModuleBase // Initialize CSRF $this->csrf->initialize(); - - return; } // Do not keep the PHP's session opened @@ -285,9 +282,7 @@ final class Init extends HttpModuleBase try { $this->context->initialize(); } catch (Exception $e) { - $this->router - ->response() - ->header('HTTP/1.1', '500 Internal Server Error'); + $this->router->response()->header('HTTP/1.1', '500 Internal Server Error'); throw $e; } @@ -332,9 +327,7 @@ final class Init extends HttpModuleBase && time() > ($lastActivity + $this->getSessionLifeTime()) ) { if ($this->router->request()->cookies()->get(session_name()) !== null) { - $this->router - ->response() - ->cookie(session_name(), '', time() - 42000); + $this->router->response()->cookie(session_name(), '', time() - 42000); } SessionContext::restart(); @@ -345,20 +338,18 @@ final class Init extends HttpModuleBase if ($sidStartTime === 0) { // Try to set PHP's session lifetime @ini_set('session.gc_maxlifetime', $this->getSessionLifeTime()); - } else { - if (!$inMaintenance - && time() > ($sidStartTime + SessionContext::MAX_SID_TIME) - && $this->context->isLoggedIn() - ) { - try { - CryptSession::reKey($this->context); - } catch (CryptoException $e) { - logger($e->getMessage()); + } elseif (!$inMaintenance + && time() > ($sidStartTime + SessionContext::MAX_SID_TIME) + && $this->context->isLoggedIn() + ) { + try { + CryptSession::reKey($this->context); + } catch (CryptoException $e) { + logger($e->getMessage()); - SessionContext::restart(); + SessionContext::restart(); - return; - } + return; } } @@ -377,8 +368,7 @@ final class Init extends HttpModuleBase try { if ($this->isIndex || $timeout === null) { - $userTimeout = $this->getSessionTimeoutForUser($timeout) - ?: $this->configData->getSessionTimeout(); + $userTimeout = $this->getSessionTimeoutForUser($timeout) ?: $this->configData->getSessionTimeout(); logger('Session timeout: '.$userTimeout); @@ -417,4 +407,9 @@ final class Init extends HttpModuleBase return $default; } + + private static function getUriFor(string $route): string + { + return (new Uri(BootstrapWeb::$WEBROOT))->addParam('r', $route)->getUri(); + } } \ No newline at end of file diff --git a/lib/SP/Core/Acl/Acl.php b/lib/SP/Core/Acl/Acl.php index cf453990..c7d962dd 100644 --- a/lib/SP/Core/Acl/Acl.php +++ b/lib/SP/Core/Acl/Acl.php @@ -37,27 +37,27 @@ defined('APP_ROOT') || die(); */ class Acl implements ActionsInterface { - protected static ?Actions $action; - private ContextInterface $context; - private EventDispatcher $eventDispatcher; + protected static ?Actions $actions = null; + private ContextInterface $context; + private EventDispatcher $eventDispatcher; /** * Acl constructor. * - * @param ContextInterface $context - * @param EventDispatcher $eventDispatcher - * @param Actions|null $action + * @param ContextInterface $context + * @param EventDispatcher $eventDispatcher + * @param Actions|null $actions */ public function __construct( ContextInterface $context, EventDispatcher $eventDispatcher, - Actions $action = null + Actions $actions = null ) { $this->context = $context; $this->eventDispatcher = $eventDispatcher; - self::$action = $action; + self::$actions = $actions; } /** @@ -66,7 +66,7 @@ class Acl implements ActionsInterface public static function getActionRoute(string $actionId): string { try { - return self::$action !== null ? self::$action->getActionById($actionId)->getRoute() : ''; + return self::$actions !== null ? self::$actions->getActionById($actionId)->getRoute() : ''; } catch (ActionNotFoundException $e) { processException($e); } @@ -86,7 +86,7 @@ class Acl implements ActionsInterface public static function getActionInfo(int $actionId, bool $translate = true): string { try { - $text = self::$action->getActionById($actionId)->getText(); + $text = self::$actions->getActionById($actionId)->getText(); return $translate ? __($text) : $text; } catch (ActionNotFoundException $e) { @@ -275,7 +275,7 @@ class Acl implements ActionsInterface } try { - $actionName = self::$action->getActionById($action)->getName(); + $actionName = self::$actions->getActionById($action)->getName(); } catch (ActionNotFoundException $e) { $actionName = __u('N/A'); } diff --git a/lib/SP/Core/Bootstrap/BootstrapBase.php b/lib/SP/Core/Bootstrap/BootstrapBase.php index 64f7ae84..3ac7f2cc 100644 --- a/lib/SP/Core/Bootstrap/BootstrapBase.php +++ b/lib/SP/Core/Bootstrap/BootstrapBase.php @@ -105,11 +105,7 @@ abstract class BootstrapBase }); // Manage requests for options - $this->router->respond( - 'OPTIONS', - null, - $this->manageCorsRequest() - ); + $this->router->respond('OPTIONS', null, $this->manageCorsRequest()); } private function manageCorsRequest(): Closure diff --git a/lib/SP/Core/Bootstrap/BootstrapWeb.php b/lib/SP/Core/Bootstrap/BootstrapWeb.php index 8e9e6146..a067b2ba 100644 --- a/lib/SP/Core/Bootstrap/BootstrapWeb.php +++ b/lib/SP/Core/Bootstrap/BootstrapWeb.php @@ -96,7 +96,7 @@ final class BootstrapWeb extends BootstrapBase $route = Filter::getString($request->param('r', 'index/index')); if (!preg_match_all( - '#(?P[a-zA-Z]+)(?:/(?P[a-zA-Z]+))?(?P(/[a-zA-Z\d.]+)+)?#', + '#(?P[a-zA-Z]+)(?:/(?P[a-zA-Z]+))?(?P(/[a-zA-Z\d.]+)+)?#', $route, $matches )) { @@ -134,10 +134,7 @@ final class BootstrapWeb extends BootstrapBase $this->initializeCommon(); - // TODO: remove?? - if (APP_MODULE === 'web') { - $this->module->initialize($controllerName); - } + $this->module->initialize($controllerName); logger( sprintf( diff --git a/lib/SP/Core/ModuleBase.php b/lib/SP/Core/ModuleBase.php index c00a2a99..f61d9439 100644 --- a/lib/SP/Core/ModuleBase.php +++ b/lib/SP/Core/ModuleBase.php @@ -48,10 +48,8 @@ abstract class ModuleBase * @param \SP\Core\Application $application * @param \SP\Core\ProvidersHelper $providersHelper */ - public function __construct( - Application $application, - ProvidersHelper $providersHelper - ) { + public function __construct(Application $application, ProvidersHelper $providersHelper) + { $this->config = $application->getConfig(); $this->configData = $this->config->getConfigData(); $this->context = $application->getContext(); @@ -59,7 +57,7 @@ abstract class ModuleBase $this->providersHelper = $providersHelper; } - abstract public function initialize(string $controller); + abstract public function initialize(string $controller): void; /** * Initializes event handlers