| Total Complexity | 43 |
| Total Lines | 400 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Bootstrap 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 Bootstrap, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 60 | final class Bootstrap |
||
| 61 | { |
||
| 62 | /** |
||
| 63 | * @var string The current request path relative to the sysPass root (e.g. files/index.php) |
||
| 64 | */ |
||
| 65 | public static $WEBROOT = ''; |
||
| 66 | /** |
||
| 67 | * @var string The full URL to reach sysPass (e.g. https://sub.example.com/syspass/) |
||
| 68 | */ |
||
| 69 | public static $WEBURI = ''; |
||
| 70 | /** |
||
| 71 | * @var string |
||
| 72 | */ |
||
| 73 | public static $SUBURI = ''; |
||
| 74 | /** |
||
| 75 | * @var mixed |
||
| 76 | */ |
||
| 77 | public static $LOCK; |
||
| 78 | /** |
||
| 79 | * @var bool Indica si la versión de PHP es correcta |
||
| 80 | */ |
||
| 81 | public static $checkPhpVersion; |
||
| 82 | /** |
||
| 83 | * @var ContainerInterface |
||
| 84 | */ |
||
| 85 | private static $container; |
||
| 86 | /** |
||
| 87 | * @var Klein |
||
| 88 | */ |
||
| 89 | private $router; |
||
| 90 | /** |
||
| 91 | * @var Language |
||
| 92 | */ |
||
| 93 | private $language; |
||
| 94 | /** |
||
| 95 | * @var Request |
||
| 96 | */ |
||
| 97 | private $request; |
||
| 98 | /** |
||
| 99 | * @var Config |
||
| 100 | */ |
||
| 101 | private $config; |
||
| 102 | /** |
||
| 103 | * @var ConfigData |
||
| 104 | */ |
||
| 105 | private $configData; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Bootstrap constructor. |
||
| 109 | * |
||
| 110 | * @param Container $container |
||
| 111 | * |
||
| 112 | * @throws \DI\DependencyException |
||
| 113 | * @throws \DI\NotFoundException |
||
| 114 | */ |
||
| 115 | private final function __construct(Container $container) |
||
| 116 | { |
||
| 117 | self::$container = $container; |
||
| 118 | |||
| 119 | // Set the default language |
||
| 120 | Language::setLocales('en_US'); |
||
| 121 | |||
| 122 | $this->config = $container->get(Config::class); |
||
| 123 | $this->configData = $this->config->getConfigData(); |
||
| 124 | $this->router = $container->get(Klein::class); |
||
| 125 | $this->request = $container->get(Request::class); |
||
| 126 | $this->language = $container->get(Language::class); |
||
| 127 | |||
| 128 | $this->initRouter(); |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Inicializar router |
||
| 133 | */ |
||
| 134 | protected function initRouter() |
||
| 135 | { |
||
| 136 | $oops = "Oops, it looks like this content does not exist..."; |
||
| 137 | |||
| 138 | $this->router->onError(function ($router, $err_msg, $type, $err) { |
||
| 139 | logger('Routing error: ' . $err_msg); |
||
| 140 | |||
| 141 | /** @var Exception|\Throwable $err */ |
||
| 142 | logger('Routing error: ' . $err->getTraceAsString()); |
||
| 143 | |||
| 144 | /** @var Klein $router */ |
||
| 145 | $router->response()->body(__($err_msg)); |
||
| 146 | }); |
||
| 147 | |||
| 148 | // Manage requests for api module |
||
| 149 | $this->router->respond(['POST'], |
||
| 150 | '@/api\.php', |
||
| 151 | function ($request, $response, $service) use ($oops) { |
||
| 152 | try { |
||
| 153 | $apiRequest = self::$container->get(ApiRequest::class); |
||
| 154 | |||
| 155 | list($controller, $action) = explode('/', $apiRequest->getMethod()); |
||
| 156 | |||
| 157 | $controllerClass = 'SP\\Modules\\' . ucfirst(APP_MODULE) . '\\Controllers\\' . ucfirst($controller) . 'Controller'; |
||
| 158 | $method = $action . 'Action'; |
||
| 159 | |||
| 160 | if (!method_exists($controllerClass, $method)) { |
||
| 161 | logger($controllerClass . '::' . $method); |
||
| 162 | |||
| 163 | /** @var Response $response */ |
||
| 164 | $response->headers()->set('Content-type', 'application/json; charset=utf-8'); |
||
| 165 | return $response->body(JsonRpcResponse::getResponseError($oops, JsonRpcResponse::METHOD_NOT_FOUND, $apiRequest->getId())); |
||
| 166 | } |
||
| 167 | |||
| 168 | $this->initializeCommon(); |
||
| 169 | |||
| 170 | self::$container->get(InitApi::class) |
||
| 171 | ->initialize($controller); |
||
| 172 | |||
| 173 | logger('Routing call: ' . $controllerClass . '::' . $method); |
||
| 174 | |||
| 175 | return call_user_func([new $controllerClass(self::$container, $method, $apiRequest), $method]); |
||
| 176 | } catch (\Exception $e) { |
||
| 177 | processException($e); |
||
| 178 | |||
| 179 | /** @var Response $response */ |
||
| 180 | $response->headers()->set('Content-type', 'application/json; charset=utf-8'); |
||
| 181 | return $response->body(JsonRpcResponse::getResponseException($e, 0)); |
||
| 182 | |||
| 183 | } |
||
| 184 | } |
||
| 185 | ); |
||
| 186 | |||
| 187 | // Manage requests for web module |
||
| 188 | $this->router->respond(['GET', 'POST'], |
||
| 189 | '@/index\.php', |
||
| 190 | function ($request, $response, $service) use ($oops) { |
||
| 191 | try { |
||
| 192 | /** @var \Klein\Request $request */ |
||
| 193 | $route = Filter::getString($request->param('r', 'index/index')); |
||
| 194 | |||
| 195 | if (!preg_match_all('#(?P<controller>[a-zA-Z]+)(?:/(?P<action>[a-zA-Z]+))?(?P<params>(/[a-zA-Z\d\.]+)+)?#', $route, $matches)) { |
||
| 196 | throw new RuntimeException($oops); |
||
| 197 | } |
||
| 198 | |||
| 199 | // $app = $matches['app'][0] ?: 'web'; |
||
| 200 | $controller = $matches['controller'][0]; |
||
| 201 | $method = !empty($matches['action'][0]) ? $matches['action'][0] . 'Action' : 'indexAction'; |
||
| 202 | $params = !empty($matches['params'][0]) ? Filter::getArray(explode('/', trim($matches['params'][0], '/'))) : []; |
||
| 203 | |||
| 204 | $controllerClass = 'SP\\Modules\\' . ucfirst(APP_MODULE) . '\\Controllers\\' . ucfirst($controller) . 'Controller'; |
||
| 205 | |||
| 206 | $this->initializePluginClasses(); |
||
| 207 | |||
| 208 | if (!method_exists($controllerClass, $method)) { |
||
| 209 | logger($controllerClass . '::' . $method); |
||
| 210 | |||
| 211 | /** @var Response $response */ |
||
| 212 | $response->code(404); |
||
| 213 | |||
| 214 | throw new RuntimeException($oops); |
||
| 215 | } |
||
| 216 | |||
| 217 | $this->initializeCommon(); |
||
| 218 | |||
| 219 | switch (APP_MODULE) { |
||
| 220 | case 'web': |
||
| 221 | self::$container->get(InitWeb::class) |
||
| 222 | ->initialize($controller); |
||
| 223 | break; |
||
| 224 | } |
||
| 225 | |||
| 226 | logger('Routing call: ' . $controllerClass . '::' . $method . '::' . print_r($params, true)); |
||
| 227 | |||
| 228 | return call_user_func_array([new $controllerClass(self::$container, $method), $method], $params); |
||
| 229 | } catch (\Exception $e) { |
||
| 230 | processException($e); |
||
| 231 | |||
| 232 | /** @var Response $response */ |
||
| 233 | if ($response->status()->getCode() !== 404) { |
||
| 234 | $response->code(503); |
||
| 235 | } |
||
| 236 | |||
| 237 | return __($e->getMessage()); |
||
| 238 | } |
||
| 239 | } |
||
| 240 | ); |
||
| 241 | |||
| 242 | // Manejar URLs que no empiecen por '/admin' |
||
| 243 | // $this->Router->respond('GET', '!@^/(admin|public|service)', |
||
| 244 | // function ($request, $response) { |
||
| 245 | // /** @var Response $response */ |
||
| 246 | // $response->redirect('index.php'); |
||
| 247 | // } |
||
| 248 | // ); |
||
| 249 | } |
||
| 250 | |||
| 251 | /** |
||
| 252 | * @throws ConfigException |
||
| 253 | * @throws Core\Exceptions\CheckException |
||
| 254 | * @throws InitializationException |
||
| 255 | * @throws Services\Upgrade\UpgradeException |
||
| 256 | * @throws \DI\DependencyException |
||
| 257 | * @throws \DI\NotFoundException |
||
| 258 | * @throws Storage\File\FileException |
||
| 259 | */ |
||
| 260 | protected function initializeCommon() |
||
| 261 | { |
||
| 262 | logger(__FUNCTION__); |
||
| 263 | |||
| 264 | self::$checkPhpVersion = Checks::checkPhpVersion(); |
||
| 265 | |||
| 266 | // Initialize authentication variables |
||
| 267 | $this->initAuthVariables(); |
||
| 268 | |||
| 269 | // Initialize logging |
||
| 270 | $this->initPHPVars(); |
||
| 271 | |||
| 272 | // Set application paths |
||
| 273 | $this->initPaths(); |
||
| 274 | |||
| 275 | self::$container->get(PhpExtensionChecker::class)->checkMandatory(); |
||
| 276 | |||
| 277 | if (!self::$checkPhpVersion) { |
||
| 278 | throw new InitializationException( |
||
| 279 | sprintf(__('Required PHP version >= %s <= %s'), '7.0', '7.2'), |
||
| 280 | InitializationException::ERROR, |
||
| 281 | __u('Please update the PHP version to run sysPass') |
||
| 282 | ); |
||
| 283 | } |
||
| 284 | |||
| 285 | // Check and intitialize configuration |
||
| 286 | $this->initConfig(); |
||
| 287 | } |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Establecer variables de autentificación |
||
| 291 | */ |
||
| 292 | private function initAuthVariables() |
||
| 293 | { |
||
| 294 | $server = $this->router->request()->server(); |
||
| 295 | |||
| 296 | // Copiar la cabecera http de autentificación para apache+php-fcgid |
||
| 297 | if ($server->get('HTTP_XAUTHORIZATION') !== null |
||
| 298 | && $server->get('HTTP_AUTHORIZATION') === null) { |
||
| 299 | $server->set('HTTP_AUTHORIZATION', $server->get('HTTP_XAUTHORIZATION')); |
||
| 300 | } |
||
| 301 | |||
| 302 | // Establecer las cabeceras de autentificación para apache+php-cgi |
||
| 303 | // Establecer las cabeceras de autentificación para que apache+php-cgi funcione si la variable es renombrada por apache |
||
| 304 | if (($server->get('HTTP_AUTHORIZATION') !== null |
||
| 305 | && preg_match('/Basic\s+(.*)$/i', $server->get('HTTP_AUTHORIZATION'), $matches)) |
||
| 306 | || ($server->get('REDIRECT_HTTP_AUTHORIZATION') !== null |
||
| 307 | && preg_match('/Basic\s+(.*)$/i', $server->get('REDIRECT_HTTP_AUTHORIZATION'), $matches)) |
||
| 308 | ) { |
||
| 309 | list($name, $password) = explode(':', base64_decode($matches[1]), 2); |
||
| 310 | $server->set('PHP_AUTH_USER', strip_tags($name)); |
||
| 311 | $server->set('PHP_AUTH_PW', strip_tags($password)); |
||
| 312 | } |
||
| 313 | } |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Establecer el nivel de logging |
||
| 317 | */ |
||
| 318 | public function initPHPVars() |
||
| 319 | { |
||
| 320 | if (defined('DEBUG') && DEBUG) { |
||
| 321 | Debug::enable(); |
||
| 322 | } else { |
||
| 323 | // Set debug mode if an Xdebug session is active |
||
| 324 | if (($this->router->request()->cookies()->get('XDEBUG_SESSION') |
||
| 325 | || $this->configData->isDebug()) |
||
| 326 | && !defined('DEBUG') |
||
| 327 | ) { |
||
| 328 | define('DEBUG', true); |
||
| 329 | Debug::enable(); |
||
| 330 | } else { |
||
| 331 | error_reporting(E_ALL & ~(E_DEPRECATED | E_STRICT | E_NOTICE)); |
||
| 332 | ini_set('display_errors', 0); |
||
| 333 | } |
||
| 334 | } |
||
| 335 | |||
| 336 | if (!file_exists(LOG_FILE) |
||
| 337 | && touch(LOG_FILE) |
||
| 338 | && chmod(LOG_FILE, 0600) |
||
| 339 | ) { |
||
| 340 | logger('Setup log file: ' . LOG_FILE); |
||
| 341 | } |
||
| 342 | |||
| 343 | if (date_default_timezone_get() === 'UTC') { |
||
| 344 | date_default_timezone_set('UTC'); |
||
| 345 | } |
||
| 346 | |||
| 347 | // Avoid PHP session cookies from JavaScript |
||
| 348 | ini_set('session.cookie_httponly', '1'); |
||
| 349 | ini_set('session.save_handler', 'files'); |
||
| 350 | } |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Establecer las rutas de la aplicación. |
||
| 354 | * Esta función establece las rutas del sistema de archivos y web de la aplicación. |
||
| 355 | * La variables de clase definidas son $SERVERROOT, $WEBROOT y $SUBURI |
||
| 356 | */ |
||
| 357 | private function initPaths() |
||
| 358 | { |
||
| 359 | self::$SUBURI = '/' . basename($this->request->getServer('SCRIPT_FILENAME')); |
||
| 360 | |||
| 361 | $uri = $this->request->getServer('REQUEST_URI'); |
||
| 362 | |||
| 363 | $pos = strpos($uri, self::$SUBURI); |
||
| 364 | |||
| 365 | if ($pos > 0) { |
||
| 366 | self::$WEBROOT = substr($uri, 0, $pos); |
||
| 367 | } |
||
| 368 | |||
| 369 | self::$WEBURI = $this->request->getHttpHost() . self::$WEBROOT; |
||
| 370 | } |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Cargar la configuración |
||
| 374 | * |
||
| 375 | * @throws ConfigException |
||
| 376 | * @throws Services\Upgrade\UpgradeException |
||
| 377 | * @throws Storage\File\FileException |
||
| 378 | * @throws \DI\DependencyException |
||
| 379 | * @throws \DI\NotFoundException |
||
| 380 | */ |
||
| 381 | private function initConfig() |
||
| 382 | { |
||
| 383 | $this->checkConfigVersion(); |
||
| 384 | |||
| 385 | ConfigUtil::checkConfigDir(); |
||
| 386 | } |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Comprobar la versión de configuración y actualizarla |
||
| 390 | * |
||
| 391 | * @throws Services\Upgrade\UpgradeException |
||
| 392 | * @throws Storage\File\FileException |
||
| 393 | * @throws \DI\DependencyException |
||
| 394 | * @throws \DI\NotFoundException |
||
| 395 | */ |
||
| 396 | private function checkConfigVersion() |
||
| 410 | } |
||
| 411 | } |
||
| 412 | |||
| 413 | /** |
||
| 414 | * initializePluginClasses |
||
| 415 | */ |
||
| 416 | protected function initializePluginClasses() |
||
| 417 | { |
||
| 418 | $loader = require APP_ROOT . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php'; |
||
| 419 | |||
| 420 | foreach (PluginManager::getPlugins() as $name => $base) { |
||
| 421 | $loader->addPsr4($base['namespace'], $base['dir']); |
||
| 422 | } |
||
| 423 | } |
||
| 424 | |||
| 425 | /** |
||
| 426 | * @return ContainerInterface |
||
| 427 | */ |
||
| 428 | public static function getContainer() |
||
| 429 | { |
||
| 430 | return self::$container; |
||
| 431 | } |
||
| 432 | |||
| 433 | /** |
||
| 434 | * @param Container $container |
||
| 435 | * @param string $module |
||
| 436 | * |
||
| 437 | * @throws InitializationException |
||
| 438 | * @throws \DI\DependencyException |
||
| 439 | * @throws \DI\NotFoundException |
||
| 440 | */ |
||
| 441 | public static function run(Container $container, $module = APP_MODULE) |
||
| 460 | } |
||
| 461 | } |
||
| 462 | } |