‪TYPO3CMS  9.5
InstallerRequestHandler.php
Go to the documentation of this file.
1 <?php
2 declare(strict_types = 1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
18 use Psr\Http\Message\ResponseInterface;
19 use Psr\Http\Message\ServerRequestInterface;
20 use Psr\Http\Server\RequestHandlerInterface as PsrRequestHandlerInterface;
21 use TYPO3\CMS\Core\Configuration\ConfigurationManager;
29 
34 class ‪InstallerRequestHandler implements ‪RequestHandlerInterface, PsrRequestHandlerInterface
35 {
43  public function ‪handleRequest(ServerRequestInterface $request): ResponseInterface
44  {
45  return $this->‪handle($request);
46  }
47 
55  public function ‪handle(ServerRequestInterface $request): ResponseInterface
56  {
57  $controller = new ‪InstallerController();
58  $actionName = $request->getParsedBody()['install']['action'] ?? $request->getQueryParams()['install']['action'] ?? 'init';
59  $action = $actionName . 'Action';
60 
61  if ($actionName === 'init' || $actionName === 'mainLayout') {
62  $response = $controller->$action();
63  } elseif ($actionName === 'checkInstallerAvailable') {
64  $response = new ‪JsonResponse([
65  'success' => $this->‪isInstallerAvailable(),
66  ]);
67  } elseif ($actionName === 'showInstallerNotAvailable') {
68  $response = $controller->showInstallerNotAvailableAction();
69  } elseif ($actionName === 'checkEnvironmentAndFolders'
70  || $actionName === 'showEnvironmentAndFolders'
71  || $actionName === 'executeEnvironmentAndFolders'
72  ) {
74  $response = $controller->$action($request);
75  } else {
77  // With main folder layout available, sessions can be handled
78  $session = new ‪SessionService();
79  if (!$session->hasSession()) {
80  $session->startSession();
81  }
82  if ($session->isExpired()) {
83  $session->refreshSession();
84  }
85  $postValues = $request->getParsedBody()['install'];
86  $sessionTokenOk = false;
87  if (empty($postValues)) {
88  // No post data is there, no token check necessary
89  $sessionTokenOk = true;
90  }
91  if (isset($postValues['token'])) {
92  // A token must be given as soon as there is POST data
93  $formProtection = ‪FormProtectionFactory::get(InstallToolFormProtection::class);
94  if ($actionName === '') {
95  throw new \RuntimeException('No POST action given for token check', 1505647681);
96  }
97  $sessionTokenOk = $formProtection->validateToken($postValues['token'], 'installTool', $actionName);
98  }
99  if (!$sessionTokenOk) {
100  $session->resetSession();
101  $session->startSession();
102  throw new \RuntimeException('Invalid session token', 1505647737);
103  }
104 
105  if (!method_exists($controller, $action)) {
106  // Sanitize action method, preventing injecting whatever method name
107  throw new \RuntimeException(
108  'Unknown action method ' . $action . ' in controller InstallerController',
109  1505687700
110  );
111  }
112 
113  $response = $controller->$action($request);
114 
115  if ($actionName === 'executeDefaultConfiguration') {
116  // Executing last step cleans session
117  $session->destroySession();
118  }
119  }
120 
121  return $response;
122  }
123 
131  public function ‪canHandleRequest(ServerRequestInterface $request): bool
132  {
133  $localConfigurationFileLocation = (new ConfigurationManager())->getLocalConfigurationFileLocation();
134  return !@is_file($localConfigurationFileLocation) || ‪EnableFileService::isFirstInstallAllowed();
135  }
136 
142  public function ‪getPriority(): int
143  {
144  return 20;
145  }
146 
151  {
152  if (!$this->‪isInstallerAvailable()) {
153  throw new \RuntimeException(
154  'Installer not available',
155  1505637427
156  );
157  }
158  }
159 
163  protected function ‪isInstallerAvailable(): bool
164  {
166  return true;
167  }
168  return false;
169  }
170 }
‪TYPO3\CMS\Install\Http\InstallerRequestHandler\isInstallerAvailable
‪bool isInstallerAvailable()
Definition: InstallerRequestHandler.php:163
‪TYPO3\CMS\Core\FormProtection\FormProtectionFactory\get
‪static TYPO3 CMS Core FormProtection AbstractFormProtection get($classNameOrType='default',... $constructorArguments)
Definition: FormProtectionFactory.php:72
‪TYPO3\CMS\Install\Http\InstallerRequestHandler\handle
‪ResponseInterface handle(ServerRequestInterface $request)
Definition: InstallerRequestHandler.php:55
‪TYPO3\CMS\Install\Http\InstallerRequestHandler
Definition: InstallerRequestHandler.php:35
‪TYPO3\CMS\Install\Service\EnableFileService
Definition: EnableFileService.php:24
‪TYPO3\CMS\Core\FormProtection\InstallToolFormProtection
Definition: InstallToolFormProtection.php:60
‪TYPO3\CMS\Install\Http
Definition: Application.php:2
‪TYPO3\CMS\Install\Http\InstallerRequestHandler\throwIfInstallerIsNotAvailable
‪throwIfInstallerIsNotAvailable()
Definition: InstallerRequestHandler.php:150
‪TYPO3\CMS\Core\Http\RequestHandlerInterface
Definition: RequestHandlerInterface.php:28
‪TYPO3\CMS\Install\Service\EnableFileService\isFirstInstallAllowed
‪static bool isFirstInstallAllowed()
Definition: EnableFileService.php:43
‪TYPO3\CMS\Install\Controller\InstallerController
Definition: InstallerController.php:64
‪TYPO3\CMS\Core\FormProtection\FormProtectionFactory
Definition: FormProtectionFactory.php:45
‪TYPO3\CMS\Core\Http\JsonResponse
Definition: JsonResponse.php:25
‪TYPO3\CMS\Install\Http\InstallerRequestHandler\handleRequest
‪ResponseInterface handleRequest(ServerRequestInterface $request)
Definition: InstallerRequestHandler.php:43
‪TYPO3\CMS\Install\Http\InstallerRequestHandler\getPriority
‪int getPriority()
Definition: InstallerRequestHandler.php:142
‪TYPO3\CMS\Install\Service\SessionService
Definition: SessionService.php:30
‪TYPO3\CMS\Install\Http\InstallerRequestHandler\canHandleRequest
‪bool canHandleRequest(ServerRequestInterface $request)
Definition: InstallerRequestHandler.php:131