VersionCheck.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OC\Updater;
  8. use OCP\Http\Client\IClientService;
  9. use OCP\IAppConfig;
  10. use OCP\IConfig;
  11. use OCP\IUserManager;
  12. use OCP\Support\Subscription\IRegistry;
  13. use OCP\Util;
  14. use Psr\Log\LoggerInterface;
  15. class VersionCheck {
  16. public function __construct(
  17. private IClientService $clientService,
  18. private IConfig $config,
  19. private IAppConfig $appConfig,
  20. private IUserManager $userManager,
  21. private IRegistry $registry,
  22. private LoggerInterface $logger,
  23. ) {
  24. }
  25. /**
  26. * Check if a new version is available
  27. *
  28. * @return array|bool
  29. */
  30. public function check() {
  31. // If this server is set to have no internet connection this is all not needed
  32. if (!$this->config->getSystemValueBool('has_internet_connection', true)) {
  33. return false;
  34. }
  35. // Look up the cache - it is invalidated all 30 minutes
  36. if (($this->appConfig->getValueInt('core', 'lastupdatedat') + 1800) > time()) {
  37. return json_decode($this->config->getAppValue('core', 'lastupdateResult'), true);
  38. }
  39. $updaterUrl = $this->config->getSystemValueString('updater.server.url', 'https://updates.nextcloud.com/updater_server/');
  40. $this->appConfig->setValueInt('core', 'lastupdatedat', time());
  41. if ($this->config->getAppValue('core', 'installedat', '') === '') {
  42. $this->config->setAppValue('core', 'installedat', (string)microtime(true));
  43. }
  44. $version = Util::getVersion();
  45. $version['installed'] = $this->config->getAppValue('core', 'installedat');
  46. $version['updated'] = $this->appConfig->getValueInt('core', 'lastupdatedat');
  47. $version['updatechannel'] = \OC_Util::getChannel();
  48. $version['edition'] = '';
  49. $version['build'] = \OC_Util::getBuild();
  50. $version['php_major'] = PHP_MAJOR_VERSION;
  51. $version['php_minor'] = PHP_MINOR_VERSION;
  52. $version['php_release'] = PHP_RELEASE_VERSION;
  53. $version['category'] = $this->computeCategory();
  54. $version['isSubscriber'] = (int) $this->registry->delegateHasValidSubscription();
  55. $versionString = implode('x', $version);
  56. //fetch xml data from updater
  57. $url = $updaterUrl . '?version=' . $versionString;
  58. $tmp = [];
  59. try {
  60. $xml = $this->getUrlContent($url);
  61. } catch (\Exception $e) {
  62. $this->logger->info('Version could not be fetched from updater server: ' . $url, ['exception' => $e]);
  63. return false;
  64. }
  65. if ($xml) {
  66. if (\LIBXML_VERSION < 20900) {
  67. $loadEntities = libxml_disable_entity_loader(true);
  68. $data = @simplexml_load_string($xml);
  69. libxml_disable_entity_loader($loadEntities);
  70. } else {
  71. $data = @simplexml_load_string($xml);
  72. }
  73. if ($data !== false) {
  74. $tmp['version'] = (string)$data->version;
  75. $tmp['versionstring'] = (string)$data->versionstring;
  76. $tmp['url'] = (string)$data->url;
  77. $tmp['web'] = (string)$data->web;
  78. $tmp['changes'] = isset($data->changes) ? (string)$data->changes : '';
  79. $tmp['autoupdater'] = (string)$data->autoupdater;
  80. $tmp['eol'] = isset($data->eol) ? (string)$data->eol : '0';
  81. } else {
  82. libxml_clear_errors();
  83. }
  84. }
  85. // Cache the result
  86. $this->config->setAppValue('core', 'lastupdateResult', json_encode($tmp));
  87. return $tmp;
  88. }
  89. /**
  90. * @codeCoverageIgnore
  91. * @param string $url
  92. * @return resource|string
  93. * @throws \Exception
  94. */
  95. protected function getUrlContent($url) {
  96. $client = $this->clientService->newClient();
  97. $response = $client->get($url, [
  98. 'timeout' => 5,
  99. ]);
  100. return $response->getBody();
  101. }
  102. private function computeCategory(): int {
  103. $categoryBoundaries = [
  104. 100,
  105. 500,
  106. 1000,
  107. 5000,
  108. 10000,
  109. 100000,
  110. 1000000,
  111. ];
  112. $nbUsers = $this->userManager->countSeenUsers();
  113. foreach ($categoryBoundaries as $categoryId => $boundary) {
  114. if ($nbUsers <= $boundary) {
  115. return $categoryId;
  116. }
  117. }
  118. return count($categoryBoundaries);
  119. }
  120. }