| Total Complexity | 49 |
| Total Lines | 325 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Maintenance often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Maintenance, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 55 | class Maintenance implements MiddlewareInterface |
||
| 56 | { |
||
| 57 | /** |
||
| 58 | * @var FailsafePackageManager |
||
| 59 | */ |
||
| 60 | protected $packageManager; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var ConfigurationManager |
||
| 64 | */ |
||
| 65 | protected $configurationManager; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var PasswordHashFactory |
||
| 69 | */ |
||
| 70 | protected $passwordHashFactory; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var ContainerInterface |
||
| 74 | */ |
||
| 75 | private $container; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var array List of valid controllers |
||
| 79 | */ |
||
| 80 | protected $controllers = [ |
||
| 81 | 'icon' => IconController::class, |
||
| 82 | 'layout' => LayoutController::class, |
||
| 83 | 'login' => LoginController::class, |
||
| 84 | 'maintenance' => MaintenanceController::class, |
||
| 85 | 'settings' => SettingsController::class, |
||
| 86 | 'upgrade' => UpgradeController::class, |
||
| 87 | 'environment' => EnvironmentController::class, |
||
| 88 | ]; |
||
| 89 | |||
| 90 | public function __construct( |
||
| 91 | FailsafePackageManager $packageManager, |
||
| 92 | ConfigurationManager $configurationManager, |
||
| 93 | PasswordHashFactory $passwordHashFactory, |
||
| 94 | ContainerInterface $container |
||
| 95 | ) { |
||
| 96 | $this->packageManager = $packageManager; |
||
| 97 | $this->configurationManager = $configurationManager; |
||
| 98 | $this->passwordHashFactory = $passwordHashFactory; |
||
| 99 | $this->container = $container; |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Handles an Install Tool request for normal operations |
||
| 104 | * |
||
| 105 | * @param ServerRequestInterface $request |
||
| 106 | * @param RequestHandlerInterface $handler |
||
| 107 | * @return ResponseInterface |
||
| 108 | */ |
||
| 109 | public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface |
||
| 110 | { |
||
| 111 | if (!$this->canHandleRequest($request)) { |
||
| 112 | return $handler->handle($request); |
||
| 113 | } |
||
| 114 | |||
| 115 | $controllerName = $request->getQueryParams()['install']['controller'] ?? 'layout'; |
||
| 116 | $actionName = $request->getParsedBody()['install']['action'] ?? $request->getQueryParams()['install']['action'] ?? 'init'; |
||
| 117 | |||
| 118 | if ($actionName === 'showEnableInstallToolFile' && EnableFileService::isInstallToolEnableFilePermanent()) { |
||
| 119 | $actionName = 'showLogin'; |
||
| 120 | } |
||
| 121 | |||
| 122 | $action = $actionName . 'Action'; |
||
| 123 | |||
| 124 | // not session related actions |
||
| 125 | if ($actionName === 'init') { |
||
| 126 | $controller = $this->container->get(LayoutController::class); |
||
| 127 | return $controller->initAction($request); |
||
| 128 | } |
||
| 129 | if ($actionName === 'checkEnableInstallToolFile') { |
||
| 130 | return new JsonResponse([ |
||
| 131 | 'success' => $this->checkEnableInstallToolFile(), |
||
| 132 | ]); |
||
| 133 | } |
||
| 134 | if ($actionName === 'showEnableInstallToolFile') { |
||
| 135 | $controller = $this->container->get(LoginController::class); |
||
| 136 | return $controller->showEnableInstallToolFileAction($request); |
||
| 137 | } |
||
| 138 | if ($actionName === 'showLogin') { |
||
| 139 | if (!$this->checkEnableInstallToolFile()) { |
||
| 140 | throw new \RuntimeException('Not authorized', 1505564888); |
||
| 141 | } |
||
| 142 | $controller = $this->container->get(LoginController::class); |
||
| 143 | return $controller->showLoginAction($request); |
||
| 144 | } |
||
| 145 | |||
| 146 | // session related actions |
||
| 147 | $session = new SessionService(); |
||
| 148 | if ($actionName === 'preAccessCheck') { |
||
| 149 | $response = new JsonResponse([ |
||
| 150 | 'installToolLocked' => !$this->checkEnableInstallToolFile(), |
||
| 151 | 'isAuthorized' => $session->isAuthorized() |
||
| 152 | ]); |
||
| 153 | } elseif ($actionName === 'checkLogin') { |
||
| 154 | if (!$this->checkEnableInstallToolFile() && !$session->isAuthorizedBackendUserSession()) { |
||
| 155 | throw new \RuntimeException('Not authorized', 1505563556); |
||
| 156 | } |
||
| 157 | if ($session->isAuthorized()) { |
||
| 158 | $session->refreshSession(); |
||
| 159 | $response = new JsonResponse([ |
||
| 160 | 'success' => true, |
||
| 161 | ]); |
||
| 162 | } else { |
||
| 163 | // Session expired, log out user, start new session |
||
| 164 | $session->resetSession(); |
||
| 165 | $session->startSession(); |
||
| 166 | $response = new JsonResponse([ |
||
| 167 | 'success' => false, |
||
| 168 | ]); |
||
| 169 | } |
||
| 170 | } elseif ($actionName === 'login') { |
||
| 171 | $session->initializeSession(); |
||
| 172 | if (!$this->checkEnableInstallToolFile()) { |
||
| 173 | throw new \RuntimeException('Not authorized', 1505567462); |
||
| 174 | } |
||
| 175 | $this->checkSessionToken($request, $session); |
||
| 176 | $this->checkSessionLifetime($session); |
||
| 177 | $password = $request->getParsedBody()['install']['password'] ?? null; |
||
| 178 | $authService = new AuthenticationService($session); |
||
| 179 | if ($authService->loginWithPassword($password, $request)) { |
||
| 180 | $response = new JsonResponse([ |
||
| 181 | 'success' => true, |
||
| 182 | ]); |
||
| 183 | } else { |
||
| 184 | if ($password === null || empty($password)) { |
||
| 185 | $messageQueue = (new FlashMessageQueue('install'))->enqueue( |
||
| 186 | new FlashMessage('Please enter the install tool password', '', FlashMessage::ERROR) |
||
| 187 | ); |
||
| 188 | } else { |
||
| 189 | $hashInstance = $this->passwordHashFactory->getDefaultHashInstance('BE'); |
||
| 190 | $hashedPassword = $hashInstance->getHashedPassword($password); |
||
| 191 | $messageQueue = (new FlashMessageQueue('install'))->enqueue( |
||
| 192 | new FlashMessage( |
||
| 193 | 'Given password does not match the install tool login password. Calculated hash: ' . $hashedPassword, |
||
| 194 | '', |
||
| 195 | FlashMessage::ERROR |
||
| 196 | ) |
||
| 197 | ); |
||
| 198 | } |
||
| 199 | $response = new JsonResponse([ |
||
| 200 | 'success' => false, |
||
| 201 | 'status' => $messageQueue, |
||
| 202 | ]); |
||
| 203 | } |
||
| 204 | } elseif ($actionName === 'logout') { |
||
| 205 | if (EnableFileService::installToolEnableFileExists() && !EnableFileService::isInstallToolEnableFilePermanent()) { |
||
| 206 | EnableFileService::removeInstallToolEnableFile(); |
||
| 207 | } |
||
| 208 | $formProtection = FormProtectionFactory::get( |
||
| 209 | InstallToolFormProtection::class |
||
| 210 | ); |
||
| 211 | $formProtection->clean(); |
||
| 212 | $session->destroySession(); |
||
| 213 | $response = new JsonResponse([ |
||
| 214 | 'success' => true, |
||
| 215 | ]); |
||
| 216 | } else { |
||
| 217 | $enforceReferrerResponse = $this->enforceReferrer($request); |
||
| 218 | if ($enforceReferrerResponse instanceof ResponseInterface) { |
||
|
|
|||
| 219 | return $enforceReferrerResponse; |
||
| 220 | } |
||
| 221 | $session->initializeSession(); |
||
| 222 | if ( |
||
| 223 | !$this->checkSessionToken($request, $session) |
||
| 224 | || !$this->checkSessionLifetime($session) |
||
| 225 | || !$session->isAuthorized() |
||
| 226 | ) { |
||
| 227 | return new HtmlResponse('', 403); |
||
| 228 | } |
||
| 229 | $session->refreshSession(); |
||
| 230 | if (!array_key_exists($controllerName, $this->controllers)) { |
||
| 231 | throw new \RuntimeException( |
||
| 232 | 'Unknown controller ' . $controllerName, |
||
| 233 | 1505215756 |
||
| 234 | ); |
||
| 235 | } |
||
| 236 | $this->recreatePackageStatesFileIfMissing(); |
||
| 237 | $className = $this->controllers[$controllerName]; |
||
| 238 | /** @var AbstractController $controller */ |
||
| 239 | $controller = $this->container->get($className); |
||
| 240 | if (!method_exists($controller, $action)) { |
||
| 241 | throw new \RuntimeException( |
||
| 242 | 'Unknown action method ' . $action . ' in controller ' . $controllerName, |
||
| 243 | 1505216027 |
||
| 244 | ); |
||
| 245 | } |
||
| 246 | $response = $controller->$action($request); |
||
| 247 | } |
||
| 248 | |||
| 249 | return $response; |
||
| 250 | } |
||
| 251 | |||
| 252 | /** |
||
| 253 | * This request handler can handle any request when not in CLI mode. |
||
| 254 | * Warning: Order of these methods is security relevant and interferes with different access |
||
| 255 | * conditions (new/existing installation). See the single method comments for details. |
||
| 256 | * |
||
| 257 | * @param ServerRequestInterface $request |
||
| 258 | * @return bool Returns always TRUE |
||
| 259 | */ |
||
| 260 | protected function canHandleRequest(ServerRequestInterface $request): bool |
||
| 261 | { |
||
| 262 | $basicIntegrity = $this->checkIfEssentialConfigurationExists() |
||
| 263 | && !empty($GLOBALS['TYPO3_CONF_VARS']['BE']['installToolPassword']) |
||
| 264 | && !EnableFileService::isFirstInstallAllowed(); |
||
| 265 | if (!$basicIntegrity) { |
||
| 266 | return false; |
||
| 267 | } |
||
| 268 | return true; |
||
| 269 | } |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Checks if ENABLE_INSTALL_TOOL exists. |
||
| 273 | * |
||
| 274 | * @return bool |
||
| 275 | */ |
||
| 276 | protected function checkEnableInstallToolFile() |
||
| 277 | { |
||
| 278 | return EnableFileService::checkInstallToolEnableFile(); |
||
| 279 | } |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Use form protection API to find out if protected POST forms are ok. |
||
| 283 | * |
||
| 284 | * @param ServerRequestInterface $request |
||
| 285 | * @param SessionService $session |
||
| 286 | * @return bool |
||
| 287 | */ |
||
| 288 | protected function checkSessionToken(ServerRequestInterface $request, SessionService $session): bool |
||
| 289 | { |
||
| 290 | $postValues = $request->getParsedBody()['install']; |
||
| 291 | // no post data is there, so no token check necessary |
||
| 292 | if (empty($postValues)) { |
||
| 293 | return true; |
||
| 294 | } |
||
| 295 | $tokenOk = false; |
||
| 296 | // A token must be given as soon as there is POST data |
||
| 297 | if (isset($postValues['token'])) { |
||
| 298 | $formProtection = FormProtectionFactory::get( |
||
| 299 | InstallToolFormProtection::class |
||
| 300 | ); |
||
| 301 | $action = (string)$postValues['action']; |
||
| 302 | if ($action === '') { |
||
| 303 | throw new \RuntimeException( |
||
| 304 | 'No POST action given for token check', |
||
| 305 | 1369326593 |
||
| 306 | ); |
||
| 307 | } |
||
| 308 | $tokenOk = $formProtection->validateToken($postValues['token'], 'installTool', $action); |
||
| 309 | } |
||
| 310 | if (!$tokenOk) { |
||
| 311 | $session->resetSession(); |
||
| 312 | $session->startSession(); |
||
| 313 | } |
||
| 314 | return $tokenOk; |
||
| 315 | } |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Check if session expired. |
||
| 319 | * If the session has expired, the login form is displayed. |
||
| 320 | * |
||
| 321 | * @param SessionService $session |
||
| 322 | * @return bool True if session lifetime is OK |
||
| 323 | */ |
||
| 324 | protected function checkSessionLifetime(SessionService $session): bool |
||
| 325 | { |
||
| 326 | $isExpired = $session->isExpired(); |
||
| 327 | if ($isExpired) { |
||
| 328 | // Session expired, log out user, start new session |
||
| 329 | $session->resetSession(); |
||
| 330 | $session->startSession(); |
||
| 331 | } |
||
| 332 | return !$isExpired; |
||
| 333 | } |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Check if LocalConfiguration.php and PackageStates.php exist |
||
| 337 | * |
||
| 338 | * @return bool TRUE when the essential configuration is available, otherwise FALSE |
||
| 339 | */ |
||
| 340 | protected function checkIfEssentialConfigurationExists(): bool |
||
| 343 | } |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Create PackageStates.php if missing and LocalConfiguration exists. |
||
| 347 | * |
||
| 348 | * It is fired if PackageStates.php is deleted on a running instance, |
||
| 349 | * all packages marked as "part of minimal system" are activated in this case. |
||
| 350 | */ |
||
| 351 | protected function recreatePackageStatesFileIfMissing(): void |
||
| 361 | } |
||
| 362 | } |
||
| 363 | |||
| 364 | /** |
||
| 365 | * Evaluates HTTP `Referer` header (which is denied by client to be a custom |
||
| 366 | * value) - attempts to ensure the value is given using a HTML client refresh. |
||
| 367 | * see: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Referer |
||
| 368 | * |
||
| 369 | * @param ServerRequestInterface $request |
||
| 370 | * @return ResponseInterface|null |
||
| 371 | */ |
||
| 372 | protected function enforceReferrer(ServerRequestInterface $request): ?ResponseInterface |
||
| 380 | ]); |
||
| 381 | } |
||
| 382 | } |
||
| 383 |