. */ namespace SP\Modules\Api; use Klein\Klein; use SP\Core\Application; use SP\Core\Context\ContextException; use SP\Core\HttpModuleBase; use SP\Core\Language; use SP\Core\ProvidersHelper; use SP\Domain\Common\Providers\Http; use SP\Domain\Core\Exceptions\InitializationException; use SP\Domain\Core\Exceptions\SPException; use SP\Domain\Core\LanguageInterface; use SP\Domain\Http\Ports\RequestService; use SP\Infrastructure\Database\DatabaseUtil; use function SP\logger; /** * Class Init */ final class Init extends HttpModuleBase { private Language $language; private DatabaseUtil $databaseUtil; public function __construct( Application $application, ProvidersHelper $providersHelper, RequestService $request, Klein $router, LanguageInterface $language, DatabaseUtil $databaseUtil ) { parent::__construct( $application, $providersHelper, $request, $router ); $this->language = $language; $this->databaseUtil = $databaseUtil; } /** * @param string $controller * @throws ContextException * @throws InitializationException * @throws SPException */ public function initialize(string $controller): void { logger(__FUNCTION__); // Initialize context $this->context->initialize(); // Load language $this->language->setLanguage(); // Checks if it needs to switch the request over HTTPS Http::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 if ($this->checkUpgradeNeeded()) { throw new InitializationException('Upgrade needed'); } // 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'); } } public function getName(): string { return 'api'; } }