InternetConnectivity.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2023 Côme Chilliet <come.chilliet@nextcloud.com>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Côme Chilliet <come.chilliet@nextcloud.com>
  8. * @author Lukas Reschke <lukas@statuscode.ch>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. *
  11. * @license GNU AGPL version 3 or any later version
  12. *
  13. * This program is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License as
  15. * published by the Free Software Foundation, either version 3 of the
  16. * License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. *
  26. */
  27. namespace OCA\Settings\SetupChecks;
  28. use OCP\Http\Client\IClientService;
  29. use OCP\IConfig;
  30. use OCP\IL10N;
  31. use OCP\SetupCheck\ISetupCheck;
  32. use OCP\SetupCheck\SetupResult;
  33. use Psr\Log\LoggerInterface;
  34. /**
  35. * Checks if the server can connect to the internet using HTTPS and HTTP
  36. */
  37. class InternetConnectivity implements ISetupCheck {
  38. public function __construct(
  39. private IL10N $l10n,
  40. private IConfig $config,
  41. private IClientService $clientService,
  42. private LoggerInterface $logger,
  43. ) {
  44. }
  45. public function getCategory(): string {
  46. return 'network';
  47. }
  48. public function getName(): string {
  49. return $this->l10n->t('Internet connectivity');
  50. }
  51. public function run(): SetupResult {
  52. if ($this->config->getSystemValue('has_internet_connection', true) === false) {
  53. return SetupResult::success($this->l10n->t('Internet connectivity is disabled in configuration file.'));
  54. }
  55. $siteArray = $this->config->getSystemValue('connectivity_check_domains', [
  56. 'www.nextcloud.com', 'www.startpage.com', 'www.eff.org', 'www.edri.org'
  57. ]);
  58. foreach ($siteArray as $site) {
  59. if ($this->isSiteReachable($site)) {
  60. return SetupResult::success();
  61. }
  62. }
  63. return SetupResult::warning($this->l10n->t('This server has no working internet connection: Multiple endpoints could not be reached. This means that some of the features like mounting external storage, notifications about updates or installation of third-party apps will not work. Accessing files remotely and sending of notification emails might not work, either. Establish a connection from this server to the internet to enjoy all features.'));
  64. }
  65. /**
  66. * Checks if the Nextcloud server can connect to a specific URL
  67. * @param string $site site domain or full URL with http/https protocol
  68. */
  69. private function isSiteReachable(string $site): bool {
  70. try {
  71. $client = $this->clientService->newClient();
  72. // if there is no protocol, test http:// AND https://
  73. if (preg_match('/^https?:\/\//', $site) !== 1) {
  74. $httpSite = 'http://' . $site . '/';
  75. $client->get($httpSite);
  76. $httpsSite = 'https://' . $site . '/';
  77. $client->get($httpsSite);
  78. } else {
  79. $client->get($site);
  80. }
  81. } catch (\Exception $e) {
  82. $this->logger->error('Cannot connect to: ' . $site, [
  83. 'app' => 'internet_connection_check',
  84. 'exception' => $e,
  85. ]);
  86. return false;
  87. }
  88. return true;
  89. }
  90. }