VersionCheck.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Lukas Reschke <lukas@statuscode.ch>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. * @author Thomas Müller <thomas.mueller@tmit.eu>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  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, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OC\Updater;
  28. use OCP\Http\Client\IClientService;
  29. use OCP\IConfig;
  30. use OCP\IUserManager;
  31. use OCP\Support\Subscription\IRegistry;
  32. use OCP\Util;
  33. use Psr\Log\LoggerInterface;
  34. class VersionCheck {
  35. public function __construct(
  36. private IClientService $clientService,
  37. private IConfig $config,
  38. private IUserManager $userManager,
  39. private IRegistry $registry,
  40. private LoggerInterface $logger,
  41. ) {
  42. }
  43. /**
  44. * Check if a new version is available
  45. *
  46. * @return array|bool
  47. */
  48. public function check() {
  49. // If this server is set to have no internet connection this is all not needed
  50. if (!$this->config->getSystemValueBool('has_internet_connection', true)) {
  51. return false;
  52. }
  53. // Look up the cache - it is invalidated all 30 minutes
  54. if (((int)$this->config->getAppValue('core', 'lastupdatedat') + 1800) > time()) {
  55. return json_decode($this->config->getAppValue('core', 'lastupdateResult'), true);
  56. }
  57. $updaterUrl = $this->config->getSystemValueString('updater.server.url', 'https://updates.nextcloud.com/updater_server/');
  58. $this->config->setAppValue('core', 'lastupdatedat', (string)time());
  59. if ($this->config->getAppValue('core', 'installedat', '') === '') {
  60. $this->config->setAppValue('core', 'installedat', (string)microtime(true));
  61. }
  62. $version = Util::getVersion();
  63. $version['installed'] = $this->config->getAppValue('core', 'installedat');
  64. $version['updated'] = $this->config->getAppValue('core', 'lastupdatedat');
  65. $version['updatechannel'] = \OC_Util::getChannel();
  66. $version['edition'] = '';
  67. $version['build'] = \OC_Util::getBuild();
  68. $version['php_major'] = PHP_MAJOR_VERSION;
  69. $version['php_minor'] = PHP_MINOR_VERSION;
  70. $version['php_release'] = PHP_RELEASE_VERSION;
  71. $version['category'] = $this->computeCategory();
  72. $version['isSubscriber'] = (int) $this->registry->delegateHasValidSubscription();
  73. $versionString = implode('x', $version);
  74. //fetch xml data from updater
  75. $url = $updaterUrl . '?version=' . $versionString;
  76. $tmp = [];
  77. try {
  78. $xml = $this->getUrlContent($url);
  79. } catch (\Exception $e) {
  80. $this->logger->info('Version could not be fetched from updater server: ' . $url, ['exception' => $e]);
  81. return false;
  82. }
  83. if ($xml) {
  84. if (\LIBXML_VERSION < 20900) {
  85. $loadEntities = libxml_disable_entity_loader(true);
  86. $data = @simplexml_load_string($xml);
  87. libxml_disable_entity_loader($loadEntities);
  88. } else {
  89. $data = @simplexml_load_string($xml);
  90. }
  91. if ($data !== false) {
  92. $tmp['version'] = (string)$data->version;
  93. $tmp['versionstring'] = (string)$data->versionstring;
  94. $tmp['url'] = (string)$data->url;
  95. $tmp['web'] = (string)$data->web;
  96. $tmp['changes'] = isset($data->changes) ? (string)$data->changes : '';
  97. $tmp['autoupdater'] = (string)$data->autoupdater;
  98. $tmp['eol'] = isset($data->eol) ? (string)$data->eol : '0';
  99. } else {
  100. libxml_clear_errors();
  101. }
  102. }
  103. // Cache the result
  104. $this->config->setAppValue('core', 'lastupdateResult', json_encode($tmp));
  105. return $tmp;
  106. }
  107. /**
  108. * @codeCoverageIgnore
  109. * @param string $url
  110. * @return resource|string
  111. * @throws \Exception
  112. */
  113. protected function getUrlContent($url) {
  114. $client = $this->clientService->newClient();
  115. $response = $client->get($url, [
  116. 'timeout' => 5,
  117. ]);
  118. return $response->getBody();
  119. }
  120. private function computeCategory(): int {
  121. $categoryBoundaries = [
  122. 100,
  123. 500,
  124. 1000,
  125. 5000,
  126. 10000,
  127. 100000,
  128. 1000000,
  129. ];
  130. $nbUsers = $this->userManager->countSeenUsers();
  131. foreach ($categoryBoundaries as $categoryId => $boundary) {
  132. if ($nbUsers <= $boundary) {
  133. return $categoryId;
  134. }
  135. }
  136. return count($categoryBoundaries);
  137. }
  138. }