| Total Complexity | 41 |
| Total Lines | 340 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Init 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 Init, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 68 | final class Init extends ModuleBase |
||
| 69 | { |
||
| 70 | /** |
||
| 71 | * List of controllers that don't need to perform fully initialization |
||
| 72 | * like: install/database checks, session/event handlers initialization |
||
| 73 | */ |
||
| 74 | const PARTIAL_INIT = [ |
||
| 75 | 'resource', |
||
| 76 | 'install', |
||
| 77 | 'bootstrap', |
||
| 78 | 'status', |
||
| 79 | 'upgrade', |
||
| 80 | 'error', |
||
| 81 | 'task' |
||
| 82 | ]; |
||
| 83 | /** |
||
| 84 | * List of controllers that don't need to update the user's session activity |
||
| 85 | */ |
||
| 86 | const NO_SESSION_ACTIVITY = ['items', 'login']; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @var SessionContext |
||
| 90 | */ |
||
| 91 | private $context; |
||
| 92 | /** |
||
| 93 | * @var ThemeInterface |
||
| 94 | */ |
||
| 95 | private $theme; |
||
| 96 | /** |
||
| 97 | * @var Language |
||
| 98 | */ |
||
| 99 | private $language; |
||
| 100 | /** |
||
| 101 | * @var SecureSessionService |
||
| 102 | */ |
||
| 103 | private $secureSessionService; |
||
| 104 | /** |
||
| 105 | * @var PluginManager |
||
| 106 | */ |
||
| 107 | private $pluginManager; |
||
| 108 | /** |
||
| 109 | * @var ItemPresetService |
||
| 110 | */ |
||
| 111 | private $itemPresetService; |
||
| 112 | /** |
||
| 113 | * @var bool |
||
| 114 | */ |
||
| 115 | private $isIndex = false; |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Init constructor. |
||
| 119 | * |
||
| 120 | * @param ContainerInterface $container |
||
| 121 | */ |
||
| 122 | public function __construct(ContainerInterface $container) |
||
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Initialize Web App |
||
| 136 | * |
||
| 137 | * @param string $controller |
||
| 138 | * |
||
| 139 | * @throws DependencyException |
||
| 140 | * @throws NotFoundException |
||
| 141 | * @throws EnvironmentIsBrokenException |
||
| 142 | * @throws ConstraintException |
||
| 143 | * @throws QueryException |
||
| 144 | * @throws SPException |
||
| 145 | * @throws NoSuchItemException |
||
| 146 | * @throws Exception |
||
| 147 | */ |
||
| 148 | public function initialize($controller) |
||
| 149 | { |
||
| 150 | logger(__METHOD__); |
||
| 151 | |||
| 152 | $this->isIndex = $controller === 'index'; |
||
| 153 | |||
| 154 | // Iniciar la sesión de PHP |
||
| 155 | $this->initSession($this->configData->isEncryptSession()); |
||
| 156 | |||
| 157 | $isReload = $this->request->checkReload(); |
||
| 158 | |||
| 159 | // Volver a cargar la configuración si se recarga la página |
||
| 160 | if ($isReload) { |
||
| 161 | logger('Browser reload'); |
||
| 162 | |||
| 163 | $this->context->setAppStatus(SessionContext::APP_STATUS_RELOADED); |
||
| 164 | |||
| 165 | // Cargar la configuración |
||
| 166 | $this->config->loadConfig(true); |
||
| 167 | } |
||
| 168 | |||
| 169 | // Setup language |
||
| 170 | $this->language->setLanguage($isReload); |
||
| 171 | |||
| 172 | // Setup theme |
||
| 173 | $this->theme->initTheme($isReload); |
||
| 174 | |||
| 175 | // Comprobar si es necesario cambiar a HTTPS |
||
| 176 | HttpUtil::checkHttps($this->configData, $this->request); |
||
| 177 | |||
| 178 | if (in_array($controller, self::PARTIAL_INIT, true) === false) { |
||
| 179 | $databaseUtil = $this->container->get(DatabaseUtil::class); |
||
| 180 | |||
| 181 | // Checks if sysPass is installed |
||
| 182 | if (!$this->checkInstalled()) { |
||
| 183 | logger('Not installed', 'ERROR'); |
||
| 184 | |||
| 185 | $this->router->response() |
||
| 186 | ->redirect('index.php?r=install/index') |
||
| 187 | ->send(); |
||
| 188 | |||
| 189 | return; |
||
| 190 | } |
||
| 191 | |||
| 192 | // Checks if the database is set up |
||
| 193 | if (!$databaseUtil->checkDatabaseConnection()) { |
||
| 194 | logger('Database connection error', 'ERROR'); |
||
| 195 | |||
| 196 | $this->router->response() |
||
| 197 | ->redirect('index.php?r=error/databaseConnection') |
||
| 198 | ->send(); |
||
| 199 | |||
| 200 | return; |
||
| 201 | } |
||
| 202 | |||
| 203 | // Checks if maintenance mode is turned on |
||
| 204 | if ($this->checkMaintenanceMode($this->context)) { |
||
| 205 | logger('Maintenance mode', 'INFO'); |
||
| 206 | |||
| 207 | $this->router->response() |
||
| 208 | ->redirect('index.php?r=error/maintenanceError') |
||
| 209 | ->send(); |
||
| 210 | |||
| 211 | return; |
||
| 212 | } |
||
| 213 | |||
| 214 | // Checks if upgrade is needed |
||
| 215 | if ($this->checkUpgrade()) { |
||
| 216 | logger('Upgrade needed', 'ERROR'); |
||
| 217 | |||
| 218 | $this->config->generateUpgradeKey(); |
||
| 219 | |||
| 220 | $this->router->response() |
||
| 221 | ->redirect('index.php?r=upgrade/index') |
||
| 222 | ->send(); |
||
| 223 | |||
| 224 | return; |
||
| 225 | } |
||
| 226 | |||
| 227 | // Checks if the database is set up |
||
| 228 | if (!$databaseUtil->checkDatabaseTables($this->configData->getDbName())) { |
||
| 229 | logger('Database checking error', 'ERROR'); |
||
| 230 | |||
| 231 | $this->router->response() |
||
| 232 | ->redirect('index.php?r=error/databaseError') |
||
| 233 | ->send(); |
||
| 234 | |||
| 235 | return; |
||
| 236 | } |
||
| 237 | |||
| 238 | // Initialize event handlers |
||
| 239 | $this->initEventHandlers(); |
||
| 240 | |||
| 241 | if (!in_array($controller, self::NO_SESSION_ACTIVITY)) { |
||
| 242 | // Initialize user session context |
||
| 243 | $this->initUserSession(); |
||
| 244 | } |
||
| 245 | |||
| 246 | // Load plugins |
||
| 247 | $this->pluginManager->loadPlugins(); |
||
| 248 | |||
| 249 | if ($this->context->isLoggedIn() |
||
| 250 | && $this->context->getAppStatus() === SessionContext::APP_STATUS_RELOADED |
||
|
|
|||
| 251 | ) { |
||
| 252 | logger('Reload user profile'); |
||
| 253 | // Recargar los permisos del perfil de usuario |
||
| 254 | $this->context->setUserProfile( |
||
| 255 | $this->container->get(UserProfileService::class) |
||
| 256 | ->getById($this->context->getUserData() |
||
| 257 | ->getUserProfileId())->getProfile()); |
||
| 258 | } |
||
| 259 | |||
| 260 | return; |
||
| 261 | } |
||
| 262 | |||
| 263 | // Do not keep the PHP's session opened |
||
| 264 | SessionContext::close(); |
||
| 265 | } |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Iniciar la sesión PHP |
||
| 269 | * |
||
| 270 | * @param bool $encrypt Encriptar la sesión de PHP |
||
| 271 | * |
||
| 272 | * @throws Exception |
||
| 273 | */ |
||
| 274 | private function initSession($encrypt = false) |
||
| 275 | { |
||
| 276 | if ($encrypt === true |
||
| 277 | && Bootstrap::$checkPhpVersion |
||
| 278 | && ($key = $this->secureSessionService->getKey(UUIDCookie::factory($this->request))) !== false) { |
||
| 279 | session_set_save_handler(new CryptSessionHandler($key), true); |
||
| 280 | } |
||
| 281 | |||
| 282 | |||
| 283 | try { |
||
| 284 | $this->context->initialize(); |
||
| 285 | } catch (Exception $e) { |
||
| 286 | $this->router->response()->header('HTTP/1.1', '500 Internal Server Error'); |
||
| 287 | |||
| 288 | throw $e; |
||
| 289 | } |
||
| 290 | } |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Comprueba que la aplicación esté instalada |
||
| 294 | * Esta función comprueba si la aplicación está instalada. Si no lo está, redirige al instalador. |
||
| 295 | */ |
||
| 296 | private function checkInstalled() |
||
| 297 | { |
||
| 298 | return $this->configData->isInstalled() |
||
| 299 | && $this->router->request()->param('r') !== 'install/index'; |
||
| 300 | } |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Comprobar si es necesario actualizar componentes |
||
| 304 | * |
||
| 305 | * @throws FileException |
||
| 306 | */ |
||
| 307 | private function checkUpgrade() |
||
| 308 | { |
||
| 309 | UpgradeUtil::fixAppUpgrade($this->configData, $this->config); |
||
| 310 | |||
| 311 | return $this->configData->getUpgradeKey() |
||
| 312 | || (UpgradeDatabaseService::needsUpgrade($this->configData->getDatabaseVersion()) || |
||
| 313 | UpgradeAppService::needsUpgrade($this->configData->getAppVersion())); |
||
| 314 | } |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Inicializar la sesión de usuario |
||
| 318 | * |
||
| 319 | */ |
||
| 320 | private function initUserSession() |
||
| 321 | { |
||
| 322 | $lastActivity = $this->context->getLastActivity(); |
||
| 323 | $inMaintenance = $this->configData->isMaintenance(); |
||
| 324 | |||
| 325 | // Session timeout |
||
| 326 | if ($lastActivity > 0 |
||
| 327 | && !$inMaintenance |
||
| 328 | && time() > ($lastActivity + $this->getSessionLifeTime()) |
||
| 329 | ) { |
||
| 330 | if ($this->router->request()->cookies()->get(session_name()) !== null) { |
||
| 331 | $this->router->response()->cookie(session_name(), '', time() - 42000); |
||
| 332 | } |
||
| 333 | |||
| 334 | SessionContext::restart(); |
||
| 335 | } else { |
||
| 336 | $sidStartTime = $this->context->getSidStartTime(); |
||
| 337 | |||
| 338 | // Regenerate session's ID frequently to avoid fixation |
||
| 339 | if ($sidStartTime === 0) { |
||
| 340 | // Try to set PHP's session lifetime |
||
| 341 | @ini_set('session.gc_maxlifetime', $this->getSessionLifeTime()); |
||
| 342 | } else if (!$inMaintenance |
||
| 343 | && time() > ($sidStartTime + SessionContext::MAX_SID_TIME) |
||
| 344 | && $this->context->isLoggedIn() |
||
| 345 | ) { |
||
| 346 | try { |
||
| 347 | CryptSession::reKey($this->context); |
||
| 348 | } catch (CryptoException $e) { |
||
| 349 | logger($e->getMessage()); |
||
| 350 | |||
| 351 | SessionContext::restart(); |
||
| 352 | return; |
||
| 353 | } |
||
| 354 | } |
||
| 355 | |||
| 356 | $this->context->setLastActivity(time()); |
||
| 357 | } |
||
| 358 | } |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Obtener el timeout de sesión desde la configuración. |
||
| 362 | * |
||
| 363 | * @return int con el tiempo en segundos |
||
| 364 | */ |
||
| 365 | private function getSessionLifeTime() |
||
| 382 | } |
||
| 383 | |||
| 384 | /** |
||
| 385 | * @param int $default |
||
| 386 | * |
||
| 387 | * @return int |
||
| 388 | * @throws ConstraintException |
||
| 389 | * @throws InvalidArgumentException |
||
| 390 | * @throws NoSuchPropertyException |
||
| 391 | * @throws QueryException |
||
| 392 | */ |
||
| 393 | private function getSessionTimeoutForUser(int $default = null) |
||
| 408 | } |
||
| 409 | } |