diff --git a/app/modules/web/Controllers/BootstrapController.php b/app/modules/web/Controllers/BootstrapController.php index eba6e1a9..e0c27585 100644 --- a/app/modules/web/Controllers/BootstrapController.php +++ b/app/modules/web/Controllers/BootstrapController.php @@ -24,11 +24,15 @@ namespace SP\Modules\Web\Controllers; -use DI\DependencyException; -use DI\NotFoundException; use Exception; +use Klein\Klein; +use SP\Core\Acl\Acl; +use SP\Core\Application; use SP\Core\Bootstrap\BootstrapBase; use SP\Core\Crypt\CryptPKI; +use SP\Core\PhpExtensionChecker; +use SP\Core\UI\ThemeInterface; +use SP\Http\Request; use SP\Modules\Web\Controllers\Traits\JsonTrait; use SP\Plugin\PluginManager; use SP\Providers\Auth\Browser\Browser; @@ -44,12 +48,33 @@ final class BootstrapController extends SimpleControllerBase { use JsonTrait; + private CryptPKI $cryptPKI; + private PluginManager $pluginManager; + private Browser $browser; + + public function __construct( + Application $application, + ThemeInterface $theme, + Klein $router, + Acl $acl, + Request $request, + PhpExtensionChecker $extensionChecker, + CryptPKI $cryptPKI, + PluginManager $pluginManager, + Browser $browser + ) { + $this->cryptPKI = $cryptPKI; + $this->pluginManager = $pluginManager; + $this->browser = $browser; + + parent::__construct($application, $theme, $router, $acl, $request, $extensionChecker); + } + /** * Returns environment data * * @return bool - * @throws DependencyException - * @throws NotFoundException + * @throws \JsonException */ public function getEnvironmentAction(): bool { @@ -122,7 +147,7 @@ final class BootstrapController extends SimpleControllerBase private function getPlugins(): array { try { - return $this->dic->get(PluginManager::class)->getEnabledPlugins(); + return $this->pluginManager->getEnabledPlugins(); } catch (Exception $e) { processException($e); } @@ -132,25 +157,21 @@ final class BootstrapController extends SimpleControllerBase /** * @return bool - * @throws DependencyException - * @throws NotFoundException */ private function getAuthBasicAutologinEnabled(): bool { - return $this->dic->get(Browser::class)->getServerAuthUser() !== null + return $this->browser->getServerAuthUser() !== null && $this->configData->isAuthBasicAutoLoginEnabled(); } /** * @return string - * @throws DependencyException - * @throws NotFoundException */ private function getPublicKey(): string { try { return $this->session->getPublicKey() - ?: $this->dic->get(CryptPKI::class)->getPublicKey(); + ?: $this->cryptPKI->getPublicKey(); } catch (FileException $e) { processException($e); @@ -169,12 +190,4 @@ final class BootstrapController extends SimpleControllerBase return $this->session->getCSRF(); } - - /** - * @return void - */ - protected function initialize(): void - { - // TODO: Implement initialize() method. - } } \ No newline at end of file diff --git a/app/modules/web/Controllers/ControllerBase.php b/app/modules/web/Controllers/ControllerBase.php index 5e0e243f..9d60314f 100644 --- a/app/modules/web/Controllers/ControllerBase.php +++ b/app/modules/web/Controllers/ControllerBase.php @@ -32,6 +32,7 @@ use Psr\Container\ContainerInterface; use SP\Config\Config; use SP\Config\ConfigDataInterface; use SP\Core\Acl\Acl; +use SP\Core\Application; use SP\Core\Bootstrap\BootstrapBase; use SP\Core\Context\ContextInterface; use SP\Core\Crypt\Hash; @@ -84,9 +85,7 @@ abstract class ControllerBase * @throws \JsonException */ public function __construct( - EventDispatcher $eventDispatcher, - Config $config, - ContextInterface $session, + Application $application, ThemeInterface $theme, Klein $router, Acl $acl, @@ -100,10 +99,10 @@ abstract class ControllerBase $this->dic = BootstrapBase::getContainer(); $this->controllerName = $this->getControllerName(); - $this->configData = $config->getConfigData(); - $this->eventDispatcher = $eventDispatcher; - $this->config = $config; - $this->session = $session; + $this->config = $application->getConfig(); + $this->configData = $this->config->getConfigData(); + $this->eventDispatcher = $application->getEventDispatcher(); + $this->session = $application->getContext(); $this->theme = $theme; $this->router = $router; $this->acl = $acl; diff --git a/app/modules/web/Controllers/InstallController.php b/app/modules/web/Controllers/InstallController.php index 7764fbeb..76462f0c 100644 --- a/app/modules/web/Controllers/InstallController.php +++ b/app/modules/web/Controllers/InstallController.php @@ -26,10 +26,8 @@ namespace SP\Modules\Web\Controllers; use Exception; use Klein\Klein; -use SP\Config\Config; use SP\Core\Acl\Acl; -use SP\Core\Context\ContextInterface; -use SP\Core\Events\EventDispatcher; +use SP\Core\Application; use SP\Core\Exceptions\SPException; use SP\Core\Language; use SP\Core\PhpExtensionChecker; @@ -56,9 +54,7 @@ final class InstallController extends ControllerBase private Installer $installer; public function __construct( - EventDispatcher $eventDispatcher, - Config $config, - ContextInterface $session, + Application $application, ThemeInterface $theme, Klein $router, Acl $acl, @@ -70,9 +66,7 @@ final class InstallController extends ControllerBase Installer $installer ) { parent::__construct( - $eventDispatcher, - $config, - $session, + $application, $theme, $router, $acl, diff --git a/app/modules/web/Controllers/LoginController.php b/app/modules/web/Controllers/LoginController.php index a0c55549..64e8a1e3 100644 --- a/app/modules/web/Controllers/LoginController.php +++ b/app/modules/web/Controllers/LoginController.php @@ -28,12 +28,10 @@ use Exception; use Klein\Klein; use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; -use SP\Config\Config; use SP\Core\Acl\Acl; +use SP\Core\Application; use SP\Core\Context\ContextBase; -use SP\Core\Context\ContextInterface; use SP\Core\Events\Event; -use SP\Core\Events\EventDispatcher; use SP\Core\Events\EventMessage; use SP\Core\PhpExtensionChecker; use SP\Core\SessionUtil; @@ -58,9 +56,7 @@ final class LoginController extends ControllerBase private LoginService $loginService; public function __construct( - EventDispatcher $eventDispatcher, - Config $config, - ContextInterface $session, + Application $application, ThemeInterface $theme, Klein $router, Acl $acl, @@ -72,9 +68,7 @@ final class LoginController extends ControllerBase LoginService $loginService ) { parent::__construct( - $eventDispatcher, - $config, - $session, + $application, $theme, $router, $acl, diff --git a/app/modules/web/Controllers/SimpleControllerBase.php b/app/modules/web/Controllers/SimpleControllerBase.php index 95901c14..815893ea 100644 --- a/app/modules/web/Controllers/SimpleControllerBase.php +++ b/app/modules/web/Controllers/SimpleControllerBase.php @@ -29,6 +29,7 @@ use Psr\Container\ContainerInterface; use SP\Config\Config; use SP\Core\Acl\Acl; use SP\Core\Acl\UnauthorizedPageException; +use SP\Core\Application; use SP\Core\Bootstrap\BootstrapBase; use SP\Core\Context\ContextInterface; use SP\Core\Events\EventDispatcher; @@ -65,9 +66,7 @@ abstract class SimpleControllerBase * @throws \JsonException */ public function __construct( - EventDispatcher $eventDispatcher, - Config $config, - ContextInterface $session, + Application $application, ThemeInterface $theme, Klein $router, Acl $acl, @@ -78,10 +77,10 @@ abstract class SimpleControllerBase $this->dic = BootstrapBase::getContainer(); $this->controllerName = $this->getControllerName(); - $this->configData = $config->getConfigData(); - $this->eventDispatcher = $eventDispatcher; - $this->config = $config; - $this->session = $session; + $this->config = $application->getConfig(); + $this->configData = $this->config->getConfigData(); + $this->eventDispatcher = $application->getEventDispatcher(); + $this->session = $application->getContext(); $this->theme = $theme; $this->router = $router; $this->acl = $acl; @@ -106,8 +105,6 @@ abstract class SimpleControllerBase } } - abstract protected function initialize(): void; - /** * Comprobaciones * diff --git a/app/modules/web/Init.php b/app/modules/web/Init.php index 4f22e882..7cc951cb 100644 --- a/app/modules/web/Init.php +++ b/app/modules/web/Init.php @@ -167,7 +167,12 @@ final class Init extends HttpModuleBase // Comprobar si es necesario cambiar a HTTPS HttpUtil::checkHttps($this->configData, $this->request); - if (in_array($controller, self::PARTIAL_INIT, true) === false) { + $partialInit = in_array($controller, self::PARTIAL_INIT, true); + + // Initialize event handlers + $this->initEventHandlers($partialInit); + + if ($partialInit === false) { // Checks if sysPass is installed if (!$this->checkInstalled()) { logger('Not installed', 'ERROR'); @@ -225,9 +230,6 @@ final class Init extends HttpModuleBase return; } - // Initialize event handlers - $this->initEventHandlers(); - if (!in_array($controller, self::NO_SESSION_ACTIVITY)) { // Initialize user session context $this->initUserSession(); diff --git a/lib/SP/Core/Bootstrap/BootstrapBase.php b/lib/SP/Core/Bootstrap/BootstrapBase.php index 72a54887..1a94fc8d 100644 --- a/lib/SP/Core/Bootstrap/BootstrapBase.php +++ b/lib/SP/Core/Bootstrap/BootstrapBase.php @@ -37,10 +37,7 @@ use SP\Core\Language; use SP\Core\PhpExtensionChecker; use SP\Http\Request; use SP\Plugin\PluginManager; -use SP\Services\Upgrade\UpgradeConfigService; -use SP\Services\Upgrade\UpgradeUtil; use SP\Util\Checks; -use SP\Util\VersionUtil; use Symfony\Component\Debug\Debug; use Throwable; @@ -72,18 +69,24 @@ abstract class BootstrapBase protected Klein $router; protected Request $request; protected ConfigDataInterface $configData; + private UpgradeConfigChecker $upgradeConfigChecker; /** * Bootstrap constructor. */ - final public function __construct(ConfigDataInterface $configData, Klein $router, Request $request) - { + final public function __construct( + ConfigDataInterface $configData, + Klein $router, + Request $request, + UpgradeConfigChecker $upgradeConfigChecker + ) { // Set the default language Language::setLocales('en_US'); $this->configData = $configData; $this->router = $router; $this->request = $request; + $this->upgradeConfigChecker = $upgradeConfigChecker; $this->initRouter(); $this->configureRouter(); @@ -240,7 +243,7 @@ abstract class BootstrapBase /** * Establecer el nivel de logging */ - final public function initPHPVars(): void + private function initPHPVars(): void { if (defined('DEBUG') && DEBUG) { /** @noinspection ForgottenDebugOutputInspection */ @@ -302,48 +305,16 @@ abstract class BootstrapBase /** * Cargar la configuración * - * @throws \Psr\Container\ContainerExceptionInterface - * @throws \Psr\Container\NotFoundExceptionInterface * @throws \SP\Core\Exceptions\ConfigException * @throws \SP\Services\Upgrade\UpgradeException */ private function initConfig(): void { - $this->checkConfigVersion(); + $this->upgradeConfigChecker->checkConfigVersion(); ConfigUtil::checkConfigDir(); } - /** - * Comprobar la versión de configuración y actualizarla - * - * @throws \Psr\Container\ContainerExceptionInterface - * @throws \Psr\Container\NotFoundExceptionInterface - * @throws \SP\Services\Upgrade\UpgradeException - */ - private function checkConfigVersion(): void - { - // Do not check config version when testing - if (IS_TESTING) { - return; - } - - if (defined('OLD_CONFIG_FILE') - && file_exists(OLD_CONFIG_FILE)) { - $upgradeConfigService = self::$container->get(UpgradeConfigService::class); - $upgradeConfigService->upgradeOldConfigFile(VersionUtil::getVersionStringNormalized()); - } - - $configVersion = UpgradeUtil::fixVersionNumber($this->configData->getConfigVersion()); - - if ($this->configData->isInstalled() - && UpgradeConfigService::needsUpgrade($configVersion) - ) { - $upgradeConfigService = self::$container->get(UpgradeConfigService::class); - $upgradeConfigService->upgrade($configVersion, $this->configData); - } - } - final protected function initializePluginClasses(): void { PluginManager::getPlugins(); diff --git a/lib/SP/Core/Bootstrap/UpgradeConfigChecker.php b/lib/SP/Core/Bootstrap/UpgradeConfigChecker.php new file mode 100644 index 00000000..2493f67e --- /dev/null +++ b/lib/SP/Core/Bootstrap/UpgradeConfigChecker.php @@ -0,0 +1,73 @@ +. + */ + +namespace SP\Core\Bootstrap; + + +use SP\Config\ConfigDataInterface; +use SP\Services\Upgrade\UpgradeConfigService; +use SP\Services\Upgrade\UpgradeUtil; +use SP\Util\VersionUtil; + +/** + * Upgrade the config whenever is needed + */ +final class UpgradeConfigChecker +{ + private UpgradeConfigService $upgradeConfigService; + private ConfigDataInterface $configData; + + public function __construct(UpgradeConfigService $upgradeConfigService, ConfigDataInterface $configData) + { + $this->upgradeConfigService = $upgradeConfigService; + $this->configData = $configData; + } + + /** + * Comprobar la versión de configuración y actualizarla + * + * @throws \SP\Services\Upgrade\UpgradeException + */ + public function checkConfigVersion(): void + { + // TODO: remove + // Do not check config version when testing + if (IS_TESTING) { + return; + } + + if (defined('OLD_CONFIG_FILE') + && file_exists(OLD_CONFIG_FILE)) { + $this->upgradeConfigService->upgradeOldConfigFile(VersionUtil::getVersionStringNormalized()); + } + + $configVersion = UpgradeUtil::fixVersionNumber($this->configData->getConfigVersion()); + + if ($this->configData->isInstalled() + && UpgradeConfigService::needsUpgrade($configVersion) + ) { + $this->upgradeConfigService->upgrade($configVersion, $this->configData); + } + } +} \ No newline at end of file diff --git a/lib/SP/Core/ModuleBase.php b/lib/SP/Core/ModuleBase.php index 584036ff..68026679 100644 --- a/lib/SP/Core/ModuleBase.php +++ b/lib/SP/Core/ModuleBase.php @@ -59,56 +59,38 @@ abstract class ModuleBase $this->providersHelper = $providersHelper; } + abstract public function initialize(string $controller); + /** * Initializes event handlers */ - protected function initEventHandlers(): void + protected function initEventHandlers(bool $partialInit = false): void { if (DEBUG || $this->configData->isDebug()) { - $handler = $this->providersHelper->getFileLogHandler(); - $handler->initialize(); + $this->eventDispatcher->attach($this->providersHelper->getFileLogHandler()); + } - $this->eventDispatcher->attach($handler); + if ($partialInit) { + return; } if ($this->configData->isLogEnabled()) { - $handler = $this->providersHelper->getDatabaseLogHandler(); - $handler->initialize(); - - $this->eventDispatcher->attach($handler); + $this->eventDispatcher->attach($this->providersHelper->getDatabaseLogHandler()); } if ($this->configData->isMailEnabled()) { - $handler = $this->providersHelper->getMailHandler(); - $handler->initialize(); - - $this->eventDispatcher->attach($handler); + $this->eventDispatcher->attach($this->providersHelper->getMailHandler()); } if ($this->configData->isSyslogEnabled()) { - $handler = $this->providersHelper->getSyslogHandler(); - $handler->initialize(); - - $this->eventDispatcher->attach($handler); + $this->eventDispatcher->attach($this->providersHelper->getSyslogHandler()); } if ($this->configData->isSyslogRemoteEnabled()) { - $handler = $this->providersHelper->getRemoteSyslogHandler(); - $handler->initialize(); - - $this->eventDispatcher->attach($handler); + $this->eventDispatcher->attach($this->providersHelper->getRemoteSyslogHandler()); } - $aclHandler = $this->providersHelper->getAclHandler(); - $aclHandler->initialize(); - - $this->eventDispatcher->attach($aclHandler); - - $notificationHandler = $this->providersHelper->getNotificationHandler(); - $notificationHandler->initialize(); - - $this->eventDispatcher->attach($notificationHandler); + $this->eventDispatcher->attach($this->providersHelper->getAclHandler()); + $this->eventDispatcher->attach($this->providersHelper->getNotificationHandler()); } - - abstract public function initialize(string $controller); } \ No newline at end of file diff --git a/lib/SP/Core/ProvidersHelper.php b/lib/SP/Core/ProvidersHelper.php index 647451a8..070232a0 100644 --- a/lib/SP/Core/ProvidersHelper.php +++ b/lib/SP/Core/ProvidersHelper.php @@ -80,6 +80,10 @@ final class ProvidersHelper */ public function getFileLogHandler(): FileLogHandler { + if (!$this->fileLogHandler->isInitialized()) { + $this->fileLogHandler->initialize(); + } + return $this->fileLogHandler; } @@ -88,6 +92,10 @@ final class ProvidersHelper */ public function getDatabaseLogHandler(): DatabaseLogHandler { + if (!$this->databaseLogHandler->isInitialized()) { + $this->databaseLogHandler->initialize(); + } + return $this->databaseLogHandler; } @@ -96,6 +104,10 @@ final class ProvidersHelper */ public function getMailHandler(): MailHandler { + if (!$this->mailHandler->isInitialized()) { + $this->mailHandler->initialize(); + } + return $this->mailHandler; } @@ -104,6 +116,10 @@ final class ProvidersHelper */ public function getSyslogHandler(): SyslogHandler { + if (!$this->syslogHandler->isInitialized()) { + $this->syslogHandler->initialize(); + } + return $this->syslogHandler; } @@ -112,6 +128,10 @@ final class ProvidersHelper */ public function getRemoteSyslogHandler(): RemoteSyslogHandler { + if (!$this->remoteSyslogHandler->isInitialized()) { + $this->remoteSyslogHandler->initialize(); + } + return $this->remoteSyslogHandler; } @@ -120,6 +140,10 @@ final class ProvidersHelper */ public function getAclHandler(): AclHandler { + if (!$this->aclHandler->isInitialized()) { + $this->aclHandler->initialize(); + } + return $this->aclHandler; } @@ -128,6 +152,10 @@ final class ProvidersHelper */ public function getNotificationHandler(): NotificationHandler { + if (!$this->notificationHandler->isInitialized()) { + $this->notificationHandler->initialize(); + } + return $this->notificationHandler; } } \ No newline at end of file diff --git a/lib/SP/Providers/Acl/AclHandler.php b/lib/SP/Providers/Acl/AclHandler.php index 2c299500..cc0e69b3 100644 --- a/lib/SP/Providers/Acl/AclHandler.php +++ b/lib/SP/Providers/Acl/AclHandler.php @@ -201,5 +201,6 @@ final class AclHandler extends Provider implements EventReceiver public function initialize(): void { $this->events = $this->parseEventsToRegex(self::EVENTS); + $this->initialized = true; } } \ No newline at end of file diff --git a/lib/SP/Providers/Log/DatabaseLogHandler.php b/lib/SP/Providers/Log/DatabaseLogHandler.php index 39b73ad8..99572833 100644 --- a/lib/SP/Providers/Log/DatabaseLogHandler.php +++ b/lib/SP/Providers/Log/DatabaseLogHandler.php @@ -161,5 +161,7 @@ final class DatabaseLogHandler extends Provider implements EventReceiver } else { $this->events = $this->parseEventsToRegex(array_merge($configEvents, LogInterface::EVENTS_FIXED)); } + + $this->initialized = true; } } \ No newline at end of file diff --git a/lib/SP/Providers/Log/LoggerBase.php b/lib/SP/Providers/Log/LoggerBase.php index 64174789..e9a515af 100644 --- a/lib/SP/Providers/Log/LoggerBase.php +++ b/lib/SP/Providers/Log/LoggerBase.php @@ -49,7 +49,7 @@ abstract class LoggerBase extends Provider implements EventReceiver protected Logger $logger; protected Language $language; protected Request $request; - protected string $events; + protected ?string $events = null; public function __construct( Config $config, @@ -77,6 +77,8 @@ abstract class LoggerBase extends Provider implements EventReceiver } else { $this->events = $this->parseEventsToRegex(array_merge($configEvents, LogInterface::EVENTS_FIXED)); } + + $this->initialized = true; } /** diff --git a/lib/SP/Providers/Mail/MailHandler.php b/lib/SP/Providers/Mail/MailHandler.php index 119974c2..860e7e65 100644 --- a/lib/SP/Providers/Mail/MailHandler.php +++ b/lib/SP/Providers/Mail/MailHandler.php @@ -208,5 +208,7 @@ final class MailHandler extends Provider implements EventReceiver } else { $this->events = $this->parseEventsToRegex(array_merge($configEvents, self::EVENTS_FIXED)); } + + $this->initialized = true; } } \ No newline at end of file diff --git a/lib/SP/Providers/Notification/NotificationHandler.php b/lib/SP/Providers/Notification/NotificationHandler.php index d51486b9..9f2385fc 100644 --- a/lib/SP/Providers/Notification/NotificationHandler.php +++ b/lib/SP/Providers/Notification/NotificationHandler.php @@ -172,5 +172,6 @@ final class NotificationHandler extends Provider implements EventReceiver public function initialize(): void { $this->events = $this->parseEventsToRegex(self::EVENTS); + $this->initialized = true; } } \ No newline at end of file diff --git a/lib/SP/Providers/Provider.php b/lib/SP/Providers/Provider.php index 53174431..a7406249 100644 --- a/lib/SP/Providers/Provider.php +++ b/lib/SP/Providers/Provider.php @@ -38,6 +38,7 @@ abstract class Provider implements ProviderInterface protected Config $config; protected ContextInterface $context; protected EventDispatcher $eventDispatcher; + protected bool $initialized = false; /** * Provider constructor. @@ -52,4 +53,12 @@ abstract class Provider implements ProviderInterface $this->context = $context; $this->eventDispatcher = $eventDispatcher; } + + /** + * @return bool + */ + public function isInitialized(): bool + { + return $this->initialized; + } } \ No newline at end of file