1
0

VersionCheck.php 4.6 KB

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