You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 lines
3.9 KiB

3 years ago
  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\Util;
  31. class VersionCheck {
  32. /** @var IClientService */
  33. private $clientService;
  34. /** @var IConfig */
  35. private $config;
  36. /**
  37. * @param IClientService $clientService
  38. * @param IConfig $config
  39. */
  40. public function __construct(IClientService $clientService,
  41. IConfig $config) {
  42. $this->clientService = $clientService;
  43. $this->config = $config;
  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 (((int)$this->config->getAppValue('core', 'lastupdatedat') + 1800) > time()) {
  57. return json_decode($this->config->getAppValue('core', 'lastupdateResult'), true);
  58. }
  59. $updaterUrl = $this->config->getSystemValue('updater.server.url', 'https://updates.nextcloud.com/updater_server/');
  60. $this->config->setAppValue('core', 'lastupdatedat', time());
  61. if ($this->config->getAppValue('core', 'installedat', '') === '') {
  62. $this->config->setAppValue('core', 'installedat', microtime(true));
  63. }
  64. $version = Util::getVersion();
  65. $version['installed'] = $this->config->getAppValue('core', 'installedat');
  66. $version['updated'] = $this->config->getAppValue('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. $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. return false;
  81. }
  82. if ($xml) {
  83. $loadEntities = libxml_disable_entity_loader(true);
  84. $data = @simplexml_load_string($xml);
  85. libxml_disable_entity_loader($loadEntities);
  86. if ($data !== false) {
  87. $tmp['version'] = (string)$data->version;
  88. $tmp['versionstring'] = (string)$data->versionstring;
  89. $tmp['url'] = (string)$data->url;
  90. $tmp['web'] = (string)$data->web;
  91. $tmp['changes'] = isset($data->changes) ? (string)$data->changes : '';
  92. $tmp['autoupdater'] = (string)$data->autoupdater;
  93. $tmp['eol'] = isset($data->eol) ? (string)$data->eol : '0';
  94. } else {
  95. libxml_clear_errors();
  96. }
  97. }
  98. // Cache the result
  99. $this->config->setAppValue('core', 'lastupdateResult', json_encode($tmp));
  100. return $tmp;
  101. }
  102. /**
  103. * @codeCoverageIgnore
  104. * @param string $url
  105. * @return resource|string
  106. * @throws \Exception
  107. */
  108. protected function getUrlContent($url) {
  109. $client = $this->clientService->newClient();
  110. $response = $client->get($url);
  111. return $response->getBody();
  112. }
  113. }