VersionCheck.php 3.9 KB

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