VersionCheck.php 4.4 KB

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