. */ namespace SP\Modules\Api; use Defuse\Crypto\Exception\EnvironmentIsBrokenException; use Klein\Klein; use SP\Core\Application; use SP\Core\Exceptions\InitializationException; use SP\Core\HttpModuleBase; use SP\Core\Language; use SP\Core\ProvidersHelper; use SP\Domain\Core\LanguageInterface; use SP\Domain\Upgrade\Services\UpgradeAppService; use SP\Domain\Upgrade\Services\UpgradeDatabaseService; use SP\Domain\Upgrade\Services\UpgradeUtil; use SP\Http\RequestInterface; use SP\Infrastructure\Database\DatabaseUtil; use SP\Infrastructure\File\FileException; use SP\Util\HttpUtil; /** * Class Init */ final class Init extends HttpModuleBase { private Language $language; private DatabaseUtil $databaseUtil; public function __construct( Application $application, ProvidersHelper $providersHelper, RequestInterface $request, Klein $router, LanguageInterface $language, DatabaseUtil $databaseUtil ) { parent::__construct( $application, $providersHelper, $request, $router ); $this->language = $language; $this->databaseUtil = $databaseUtil; } /** * @throws \Defuse\Crypto\Exception\EnvironmentIsBrokenException * @throws \JsonException * @throws \SP\Core\Context\ContextException * @throws \SP\Core\Exceptions\InitializationException * @throws \SP\Infrastructure\File\FileException */ public function initialize(string $controller): void { logger(__FUNCTION__); // Initialize context $this->context->initialize(); // Load config $this->config->loadConfig(); // Load language $this->language->setLanguage(); // Checks if it needs to switch the request over HTTPS HttpUtil::checkHttps($this->configData, $this->request); // Checks if sysPass is installed $this->checkInstalled(); // Checks if maintenance mode is turned on if ($this->checkMaintenanceMode()) { throw new InitializationException('Maintenance mode'); } // Checks if upgrade is needed $this->checkUpgrade(); // Checks if the database is set up if (!$this->databaseUtil->checkDatabaseConnection()) { throw new InitializationException('Database connection error'); } if (!$this->databaseUtil->checkDatabaseTables($this->configData->getDbName())) { throw new InitializationException('Database checking error'); } // Initialize event handlers $this->initEventHandlers(); } /** * Comprueba que la aplicación esté instalada * Esta función comprueba si la aplicación está instalada. Si no lo está, redirige al instalador. * * @throws InitializationException */ private function checkInstalled(): void { if (!$this->configData->isInstalled()) { throw new InitializationException('Not installed'); } } /** * Comprobar si es necesario actualizar componentes * * @throws EnvironmentIsBrokenException * @throws FileException * @throws InitializationException */ private function checkUpgrade(): void { if (IS_TESTING) { return; } UpgradeUtil::fixAppUpgrade($this->configData, $this->config); if ($this->configData->getUpgradeKey() || (UpgradeDatabaseService::needsUpgrade($this->configData->getDatabaseVersion()) || UpgradeAppService::needsUpgrade($this->configData->getAppVersion())) ) { $this->config->generateUpgradeKey(); throw new InitializationException(__u('Updating needed')); } } }