| Total Complexity | 89 |
| Total Lines | 581 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like CheckSetupController 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 CheckSetupController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 65 | class CheckSetupController extends Controller { |
||
| 66 | /** @var IConfig */ |
||
| 67 | private $config; |
||
| 68 | /** @var IClientService */ |
||
| 69 | private $clientService; |
||
| 70 | /** @var \OC_Util */ |
||
| 71 | private $util; |
||
| 72 | /** @var IURLGenerator */ |
||
| 73 | private $urlGenerator; |
||
| 74 | /** @var IL10N */ |
||
| 75 | private $l10n; |
||
| 76 | /** @var Checker */ |
||
| 77 | private $checker; |
||
| 78 | /** @var ILogger */ |
||
| 79 | private $logger; |
||
| 80 | /** @var EventDispatcherInterface */ |
||
| 81 | private $dispatcher; |
||
| 82 | /** @var IDBConnection|Connection */ |
||
| 83 | private $db; |
||
| 84 | /** @var ILockingProvider */ |
||
| 85 | private $lockingProvider; |
||
| 86 | /** @var IDateTimeFormatter */ |
||
| 87 | private $dateTimeFormatter; |
||
| 88 | /** @var MemoryInfo */ |
||
| 89 | private $memoryInfo; |
||
| 90 | /** @var ISecureRandom */ |
||
| 91 | private $secureRandom; |
||
| 92 | |||
| 93 | public function __construct($AppName, |
||
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Checks if the server can connect to the internet using HTTPS and HTTP |
||
| 126 | * @return bool |
||
| 127 | */ |
||
| 128 | private function isInternetConnectionWorking() { |
||
| 129 | if ($this->config->getSystemValue('has_internet_connection', true) === false) { |
||
| 130 | return false; |
||
| 131 | } |
||
| 132 | |||
| 133 | $siteArray = $this->config->getSystemValue('connectivity_check_domains', [ |
||
| 134 | 'www.nextcloud.com', 'www.startpage.com', 'www.eff.org', 'www.edri.org' |
||
| 135 | ]); |
||
| 136 | |||
| 137 | foreach($siteArray as $site) { |
||
| 138 | if ($this->isSiteReachable($site)) { |
||
| 139 | return true; |
||
| 140 | } |
||
| 141 | } |
||
| 142 | return false; |
||
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Checks if the Nextcloud server can connect to a specific URL using both HTTPS and HTTP |
||
| 147 | * @return bool |
||
| 148 | */ |
||
| 149 | private function isSiteReachable($sitename) { |
||
| 150 | $httpSiteName = 'http://' . $sitename . '/'; |
||
| 151 | $httpsSiteName = 'https://' . $sitename . '/'; |
||
| 152 | |||
| 153 | try { |
||
| 154 | $client = $this->clientService->newClient(); |
||
| 155 | $client->get($httpSiteName); |
||
| 156 | $client->get($httpsSiteName); |
||
| 157 | } catch (\Exception $e) { |
||
| 158 | $this->logger->logException($e, ['app' => 'internet_connection_check']); |
||
| 159 | return false; |
||
| 160 | } |
||
| 161 | return true; |
||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Checks whether a local memcache is installed or not |
||
| 166 | * @return bool |
||
| 167 | */ |
||
| 168 | private function isMemcacheConfigured() { |
||
| 169 | return $this->config->getSystemValue('memcache.local', null) !== null; |
||
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Whether PHP can generate "secure" pseudorandom integers |
||
| 174 | * |
||
| 175 | * @return bool |
||
| 176 | */ |
||
| 177 | private function isRandomnessSecure() { |
||
| 178 | try { |
||
| 179 | $this->secureRandom->generate(1); |
||
| 180 | } catch (\Exception $ex) { |
||
| 181 | return false; |
||
| 182 | } |
||
| 183 | return true; |
||
| 184 | } |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Public for the sake of unit-testing |
||
| 188 | * |
||
| 189 | * @return array |
||
| 190 | */ |
||
| 191 | protected function getCurlVersion() { |
||
| 192 | return curl_version(); |
||
| 193 | } |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Check if the used SSL lib is outdated. Older OpenSSL and NSS versions do |
||
| 197 | * have multiple bugs which likely lead to problems in combination with |
||
| 198 | * functionality required by ownCloud such as SNI. |
||
| 199 | * |
||
| 200 | * @link https://github.com/owncloud/core/issues/17446#issuecomment-122877546 |
||
| 201 | * @link https://bugzilla.redhat.com/show_bug.cgi?id=1241172 |
||
| 202 | * @return string |
||
| 203 | */ |
||
| 204 | private function isUsedTlsLibOutdated() { |
||
| 205 | // Don't run check when: |
||
| 206 | // 1. Server has `has_internet_connection` set to false |
||
| 207 | // 2. AppStore AND S2S is disabled |
||
| 208 | if(!$this->config->getSystemValue('has_internet_connection', true)) { |
||
| 209 | return ''; |
||
| 210 | } |
||
| 211 | if(!$this->config->getSystemValue('appstoreenabled', true) |
||
| 212 | && $this->config->getAppValue('files_sharing', 'outgoing_server2server_share_enabled', 'yes') === 'no' |
||
| 213 | && $this->config->getAppValue('files_sharing', 'incoming_server2server_share_enabled', 'yes') === 'no') { |
||
| 214 | return ''; |
||
| 215 | } |
||
| 216 | |||
| 217 | $versionString = $this->getCurlVersion(); |
||
| 218 | if(isset($versionString['ssl_version'])) { |
||
| 219 | $versionString = $versionString['ssl_version']; |
||
| 220 | } else { |
||
| 221 | return ''; |
||
| 222 | } |
||
| 223 | |||
| 224 | $features = (string)$this->l10n->t('installing and updating apps via the app store or Federated Cloud Sharing'); |
||
| 225 | if(!$this->config->getSystemValue('appstoreenabled', true)) { |
||
| 226 | $features = (string)$this->l10n->t('Federated Cloud Sharing'); |
||
| 227 | } |
||
| 228 | |||
| 229 | // Check if at least OpenSSL after 1.01d or 1.0.2b |
||
| 230 | if(strpos($versionString, 'OpenSSL/') === 0) { |
||
| 231 | $majorVersion = substr($versionString, 8, 5); |
||
| 232 | $patchRelease = substr($versionString, 13, 6); |
||
| 233 | |||
| 234 | if(($majorVersion === '1.0.1' && ord($patchRelease) < ord('d')) || |
||
| 235 | ($majorVersion === '1.0.2' && ord($patchRelease) < ord('b'))) { |
||
| 236 | return $this->l10n->t('cURL is using an outdated %1$s version (%2$s). Please update your operating system or features such as %3$s will not work reliably.', ['OpenSSL', $versionString, $features]); |
||
| 237 | } |
||
| 238 | } |
||
| 239 | |||
| 240 | // Check if NSS and perform heuristic check |
||
| 241 | if(strpos($versionString, 'NSS/') === 0) { |
||
| 242 | try { |
||
| 243 | $firstClient = $this->clientService->newClient(); |
||
| 244 | $firstClient->get('https://nextcloud.com/'); |
||
| 245 | |||
| 246 | $secondClient = $this->clientService->newClient(); |
||
| 247 | $secondClient->get('https://nextcloud.com/'); |
||
| 248 | } catch (ClientException $e) { |
||
| 249 | if($e->getResponse()->getStatusCode() === 400) { |
||
| 250 | return $this->l10n->t('cURL is using an outdated %1$s version (%2$s). Please update your operating system or features such as %3$s will not work reliably.', ['NSS', $versionString, $features]); |
||
| 251 | } |
||
| 252 | } |
||
| 253 | } |
||
| 254 | |||
| 255 | return ''; |
||
| 256 | } |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Whether the version is outdated |
||
| 260 | * |
||
| 261 | * @return bool |
||
| 262 | */ |
||
| 263 | protected function isPhpOutdated() { |
||
| 264 | if (version_compare(PHP_VERSION, '7.1.0', '<')) { |
||
| 265 | return true; |
||
| 266 | } |
||
| 267 | |||
| 268 | return false; |
||
| 269 | } |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Whether the php version is still supported (at time of release) |
||
| 273 | * according to: https://secure.php.net/supported-versions.php |
||
| 274 | * |
||
| 275 | * @return array |
||
| 276 | */ |
||
| 277 | private function isPhpSupported() { |
||
| 278 | return ['eol' => $this->isPhpOutdated(), 'version' => PHP_VERSION]; |
||
| 279 | } |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Check if the reverse proxy configuration is working as expected |
||
| 283 | * |
||
| 284 | * @return bool |
||
| 285 | */ |
||
| 286 | private function forwardedForHeadersWorking() { |
||
| 295 | } |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Checks if the correct memcache module for PHP is installed. Only |
||
| 299 | * fails if memcached is configured and the working module is not installed. |
||
| 300 | * |
||
| 301 | * @return bool |
||
| 302 | */ |
||
| 303 | private function isCorrectMemcachedPHPModuleInstalled() { |
||
| 304 | if ($this->config->getSystemValue('memcache.distributed', null) !== '\OC\Memcache\Memcached') { |
||
| 305 | return true; |
||
| 306 | } |
||
| 307 | |||
| 308 | // there are two different memcached modules for PHP |
||
| 309 | // we only support memcached and not memcache |
||
| 310 | // https://code.google.com/p/memcached/wiki/PHPClientComparison |
||
| 311 | return !(!extension_loaded('memcached') && extension_loaded('memcache')); |
||
| 312 | } |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Checks if set_time_limit is not disabled. |
||
| 316 | * |
||
| 317 | * @return bool |
||
| 318 | */ |
||
| 319 | private function isSettimelimitAvailable() { |
||
| 320 | if (function_exists('set_time_limit') |
||
| 321 | && strpos(@ini_get('disable_functions'), 'set_time_limit') === false) { |
||
| 322 | return true; |
||
| 323 | } |
||
| 324 | |||
| 325 | return false; |
||
| 326 | } |
||
| 327 | |||
| 328 | /** |
||
| 329 | * @return RedirectResponse |
||
| 330 | */ |
||
| 331 | public function rescanFailedIntegrityCheck() { |
||
| 332 | $this->checker->runInstanceVerification(); |
||
| 333 | return new RedirectResponse( |
||
| 334 | $this->urlGenerator->linkToRoute('settings.AdminSettings.index') |
||
| 335 | ); |
||
| 336 | } |
||
| 337 | |||
| 338 | /** |
||
| 339 | * @NoCSRFRequired |
||
| 340 | * @return DataResponse |
||
| 341 | */ |
||
| 342 | public function getFailedIntegrityCheckFiles() { |
||
| 343 | if(!$this->checker->isCodeCheckEnforced()) { |
||
| 344 | return new DataDisplayResponse('Integrity checker has been disabled. Integrity cannot be verified.'); |
||
|
|
|||
| 345 | } |
||
| 346 | |||
| 347 | $completeResults = $this->checker->getResults(); |
||
| 348 | |||
| 349 | if(!empty($completeResults)) { |
||
| 350 | $formattedTextResponse = 'Technical information |
||
| 351 | ===================== |
||
| 352 | The following list covers which files have failed the integrity check. Please read |
||
| 353 | the previous linked documentation to learn more about the errors and how to fix |
||
| 354 | them. |
||
| 355 | |||
| 356 | Results |
||
| 357 | ======= |
||
| 358 | '; |
||
| 359 | foreach($completeResults as $context => $contextResult) { |
||
| 360 | $formattedTextResponse .= "- $context\n"; |
||
| 361 | |||
| 362 | foreach($contextResult as $category => $result) { |
||
| 363 | $formattedTextResponse .= "\t- $category\n"; |
||
| 364 | if($category !== 'EXCEPTION') { |
||
| 365 | foreach ($result as $key => $results) { |
||
| 366 | $formattedTextResponse .= "\t\t- $key\n"; |
||
| 367 | } |
||
| 368 | } else { |
||
| 369 | foreach ($result as $key => $results) { |
||
| 370 | $formattedTextResponse .= "\t\t- $results\n"; |
||
| 371 | } |
||
| 372 | } |
||
| 373 | |||
| 374 | } |
||
| 375 | } |
||
| 376 | |||
| 377 | $formattedTextResponse .= ' |
||
| 378 | Raw output |
||
| 379 | ========== |
||
| 380 | '; |
||
| 381 | $formattedTextResponse .= print_r($completeResults, true); |
||
| 382 | } else { |
||
| 383 | $formattedTextResponse = 'No errors have been found.'; |
||
| 384 | } |
||
| 385 | |||
| 386 | |||
| 387 | $response = new DataDisplayResponse( |
||
| 388 | $formattedTextResponse, |
||
| 389 | Http::STATUS_OK, |
||
| 390 | [ |
||
| 391 | 'Content-Type' => 'text/plain', |
||
| 392 | ] |
||
| 393 | ); |
||
| 394 | |||
| 395 | return $response; |
||
| 396 | } |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Checks whether a PHP opcache is properly set up |
||
| 400 | * @return bool |
||
| 401 | */ |
||
| 402 | protected function isOpcacheProperlySetup() { |
||
| 403 | $iniWrapper = new IniGetWrapper(); |
||
| 404 | |||
| 405 | if(!$iniWrapper->getBool('opcache.enable')) { |
||
| 406 | return false; |
||
| 407 | } |
||
| 408 | |||
| 409 | if(!$iniWrapper->getBool('opcache.save_comments')) { |
||
| 410 | return false; |
||
| 411 | } |
||
| 412 | |||
| 413 | if(!$iniWrapper->getBool('opcache.enable_cli')) { |
||
| 414 | return false; |
||
| 415 | } |
||
| 416 | |||
| 417 | if($iniWrapper->getNumeric('opcache.max_accelerated_files') < 10000) { |
||
| 418 | return false; |
||
| 419 | } |
||
| 420 | |||
| 421 | if($iniWrapper->getNumeric('opcache.memory_consumption') < 128) { |
||
| 422 | return false; |
||
| 423 | } |
||
| 424 | |||
| 425 | if($iniWrapper->getNumeric('opcache.interned_strings_buffer') < 8) { |
||
| 426 | return false; |
||
| 427 | } |
||
| 428 | |||
| 429 | return true; |
||
| 430 | } |
||
| 431 | |||
| 432 | /** |
||
| 433 | * Check if the required FreeType functions are present |
||
| 434 | * @return bool |
||
| 435 | */ |
||
| 436 | protected function hasFreeTypeSupport() { |
||
| 437 | return function_exists('imagettfbbox') && function_exists('imagettftext'); |
||
| 438 | } |
||
| 439 | |||
| 440 | protected function hasMissingIndexes(): array { |
||
| 447 | } |
||
| 448 | |||
| 449 | /** |
||
| 450 | * warn if outdated version of a memcache module is used |
||
| 451 | */ |
||
| 452 | protected function getOutdatedCaches(): array { |
||
| 453 | $caches = [ |
||
| 454 | 'apcu' => ['name' => 'APCu', 'version' => '4.0.6'], |
||
| 455 | 'redis' => ['name' => 'Redis', 'version' => '2.2.5'], |
||
| 456 | ]; |
||
| 457 | $outdatedCaches = []; |
||
| 458 | foreach ($caches as $php_module => $data) { |
||
| 459 | $isOutdated = extension_loaded($php_module) && version_compare(phpversion($php_module), $data['version'], '<'); |
||
| 460 | if ($isOutdated) { |
||
| 461 | $outdatedCaches[] = $data; |
||
| 462 | } |
||
| 463 | } |
||
| 464 | |||
| 465 | return $outdatedCaches; |
||
| 466 | } |
||
| 467 | |||
| 468 | protected function isSqliteUsed() { |
||
| 469 | return strpos($this->config->getSystemValue('dbtype'), 'sqlite') !== false; |
||
| 470 | } |
||
| 471 | |||
| 472 | protected function isReadOnlyConfig(): bool { |
||
| 473 | return \OC_Helper::isReadOnlyConfigEnabled(); |
||
| 474 | } |
||
| 475 | |||
| 476 | protected function hasValidTransactionIsolationLevel(): bool { |
||
| 477 | try { |
||
| 478 | if ($this->db->getDatabasePlatform() instanceof SqlitePlatform) { |
||
| 479 | return true; |
||
| 480 | } |
||
| 481 | |||
| 482 | return $this->db->getTransactionIsolation() === Connection::TRANSACTION_READ_COMMITTED; |
||
| 483 | } catch (DBALException $e) { |
||
| 484 | // ignore |
||
| 485 | } |
||
| 486 | |||
| 487 | return true; |
||
| 488 | } |
||
| 489 | |||
| 490 | protected function hasFileinfoInstalled(): bool { |
||
| 491 | return \OC_Util::fileInfoLoaded(); |
||
| 492 | } |
||
| 493 | |||
| 494 | protected function hasWorkingFileLocking(): bool { |
||
| 495 | return !($this->lockingProvider instanceof NoopLockingProvider); |
||
| 496 | } |
||
| 497 | |||
| 498 | protected function getSuggestedOverwriteCliURL(): string { |
||
| 499 | $suggestedOverwriteCliUrl = ''; |
||
| 500 | if ($this->config->getSystemValue('overwrite.cli.url', '') === '') { |
||
| 501 | $suggestedOverwriteCliUrl = $this->request->getServerProtocol() . '://' . $this->request->getInsecureServerHost() . \OC::$WEBROOT; |
||
| 502 | if (!$this->config->getSystemValue('config_is_read_only', false)) { |
||
| 503 | // Set the overwrite URL when it was not set yet. |
||
| 504 | $this->config->setSystemValue('overwrite.cli.url', $suggestedOverwriteCliUrl); |
||
| 505 | $suggestedOverwriteCliUrl = ''; |
||
| 506 | } |
||
| 507 | } |
||
| 508 | return $suggestedOverwriteCliUrl; |
||
| 509 | } |
||
| 510 | |||
| 511 | protected function getLastCronInfo(): array { |
||
| 512 | $lastCronRun = $this->config->getAppValue('core', 'lastcron', 0); |
||
| 513 | return [ |
||
| 514 | 'diffInSeconds' => time() - $lastCronRun, |
||
| 515 | 'relativeTime' => $this->dateTimeFormatter->formatTimeSpan($lastCronRun), |
||
| 516 | 'backgroundJobsUrl' => $this->urlGenerator->linkToRoute('settings.AdminSettings.index', ['section' => 'server']) . '#backgroundjobs', |
||
| 517 | ]; |
||
| 518 | } |
||
| 519 | |||
| 520 | protected function getCronErrors() { |
||
| 528 | } |
||
| 529 | |||
| 530 | protected function isPHPMailerUsed(): bool { |
||
| 531 | return $this->config->getSystemValue('mail_smtpmode', 'smtp') === 'php'; |
||
| 532 | } |
||
| 533 | |||
| 534 | protected function hasOpcacheLoaded(): bool { |
||
| 535 | return function_exists('opcache_get_status'); |
||
| 536 | } |
||
| 537 | |||
| 538 | /** |
||
| 539 | * Iterates through the configured app roots and |
||
| 540 | * tests if the subdirectories are owned by the same user than the current user. |
||
| 541 | * |
||
| 542 | * @return array |
||
| 543 | */ |
||
| 544 | protected function getAppDirsWithDifferentOwner(): array { |
||
| 545 | $currentUser = posix_getuid(); |
||
| 546 | $appDirsWithDifferentOwner = [[]]; |
||
| 547 | |||
| 548 | foreach (OC::$APPSROOTS as $appRoot) { |
||
| 549 | if ($appRoot['writable'] === true) { |
||
| 550 | $appDirsWithDifferentOwner[] = $this->getAppDirsWithDifferentOwnerForAppRoot($currentUser, $appRoot); |
||
| 551 | } |
||
| 552 | } |
||
| 553 | |||
| 554 | $appDirsWithDifferentOwner = array_merge(...$appDirsWithDifferentOwner); |
||
| 555 | sort($appDirsWithDifferentOwner); |
||
| 556 | |||
| 557 | return $appDirsWithDifferentOwner; |
||
| 558 | } |
||
| 559 | |||
| 560 | /** |
||
| 561 | * Tests if the directories for one apps directory are writable by the current user. |
||
| 562 | * |
||
| 563 | * @param int $currentUser The current user |
||
| 564 | * @param array $appRoot The app root config |
||
| 565 | * @return string[] The none writable directory paths inside the app root |
||
| 566 | */ |
||
| 567 | private function getAppDirsWithDifferentOwnerForAppRoot(int $currentUser, array $appRoot): array { |
||
| 583 | } |
||
| 584 | |||
| 585 | /** |
||
| 586 | * Checks for potential PHP modules that would improve the instance |
||
| 587 | * |
||
| 588 | * @return string[] A list of PHP modules that is recommended |
||
| 589 | */ |
||
| 590 | protected function hasRecommendedPHPModules(): array { |
||
| 591 | $recommendedPHPModules = []; |
||
| 592 | |||
| 593 | if (!function_exists('grapheme_strlen')) { |
||
| 594 | $recommendedPHPModules[] = 'intl'; |
||
| 595 | } |
||
| 596 | |||
| 597 | if ($this->config->getAppValue('theming', 'enabled', 'no') === 'yes') { |
||
| 598 | if (!extension_loaded('imagick')) { |
||
| 599 | $recommendedPHPModules[] = 'imagick'; |
||
| 600 | } |
||
| 601 | } |
||
| 602 | |||
| 603 | return $recommendedPHPModules; |
||
| 604 | } |
||
| 605 | |||
| 606 | /** |
||
| 607 | * @return DataResponse |
||
| 608 | */ |
||
| 609 | public function check() { |
||
| 650 |