handle($request); } /** * Handles an Install Tool request when nothing is there * * @param ServerRequestInterface $request * @return ResponseInterface * @throws \RuntimeException */ public function handle(ServerRequestInterface $request): ResponseInterface { $controller = new InstallerController(); $actionName = $request->getParsedBody()['install']['action'] ?? $request->getQueryParams()['install']['action'] ?? 'init'; $action = $actionName . 'Action'; if ($actionName === 'init' || $actionName === 'mainLayout') { $response = $controller->$action(); } elseif ($actionName === 'checkInstallerAvailable') { $response = new JsonResponse([ 'success' => $this->isInstallerAvailable(), ]); } elseif ($actionName === 'showInstallerNotAvailable') { $response = $controller->showInstallerNotAvailableAction(); } elseif ($actionName === 'checkEnvironmentAndFolders' || $actionName === 'showEnvironmentAndFolders' || $actionName === 'executeEnvironmentAndFolders' ) { $this->throwIfInstallerIsNotAvailable(); $response = $controller->$action($request); } else { $this->throwIfInstallerIsNotAvailable(); // With main folder layout available, sessions can be handled $session = new SessionService(); if (!$session->hasSession()) { $session->startSession(); } if ($session->isExpired()) { $session->refreshSession(); } $postValues = $request->getParsedBody()['install']; $sessionTokenOk = false; if (empty($postValues)) { // No post data is there, no token check necessary $sessionTokenOk = true; } if (isset($postValues['token'])) { // A token must be given as soon as there is POST data $formProtection = FormProtectionFactory::get(InstallToolFormProtection::class); if ($actionName === '') { throw new \RuntimeException('No POST action given for token check', 1505647681); } $sessionTokenOk = $formProtection->validateToken($postValues['token'], 'installTool', $actionName); } if (!$sessionTokenOk) { $session->resetSession(); $session->startSession(); throw new \RuntimeException('Invalid session token', 1505647737); } if (!method_exists($controller, $action)) { // Sanitize action method, preventing injecting whatever method name throw new \RuntimeException( 'Unknown action method ' . $action . ' in controller InstallerController', 1505687700 ); } $response = $controller->$action($request); if ($actionName === 'executeDefaultConfiguration') { // Executing last step cleans session $session->destroySession(); } } return $response; } /** * First installation is in progress, if LocalConfiguration does not exist, * or if FIRST_INSTALL file exists. * * @param ServerRequestInterface $request * @return bool Returns always TRUE */ public function canHandleRequest(ServerRequestInterface $request): bool { $localConfigurationFileLocation = (new ConfigurationManager())->getLocalConfigurationFileLocation(); return !@is_file($localConfigurationFileLocation) || EnableFileService::isFirstInstallAllowed(); } /** * Returns the priority - how eager the handler is to actually handle the request. * * @return int The priority of the request handler. */ public function getPriority(): int { return 20; } /** * @throws \RuntimeException If installer is not available due to missing FIRST_INSTALL */ protected function throwIfInstallerIsNotAvailable() { if (!$this->isInstallerAvailable()) { throw new \RuntimeException( 'Installer not available', 1505637427 ); } } /** * @return bool TRUE if FIRST_INSTALL file exists */ protected function isInstallerAvailable(): bool { if (EnableFileService::isFirstInstallAllowed()) { return true; } return false; } }