VersionCheck.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. if (\LIBXML_VERSION < 20900) {
  84. $loadEntities = libxml_disable_entity_loader(true);
  85. $data = @simplexml_load_string($xml);
  86. libxml_disable_entity_loader($loadEntities);
  87. } else {
  88. $data = @simplexml_load_string($xml);
  89. }
  90. if ($data !== false) {
  91. $tmp['version'] = (string)$data->version;
  92. $tmp['versionstring'] = (string)$data->versionstring;
  93. $tmp['url'] = (string)$data->url;
  94. $tmp['web'] = (string)$data->web;
  95. $tmp['changes'] = isset($data->changes) ? (string)$data->changes : '';
  96. $tmp['autoupdater'] = (string)$data->autoupdater;
  97. $tmp['eol'] = isset($data->eol) ? (string)$data->eol : '0';
  98. } else {
  99. libxml_clear_errors();
  100. }
  101. }
  102. // Cache the result
  103. $this->config->setAppValue('core', 'lastupdateResult', json_encode($tmp));
  104. return $tmp;
  105. }
  106. /**
  107. * @codeCoverageIgnore
  108. * @param string $url
  109. * @return resource|string
  110. * @throws \Exception
  111. */
  112. protected function getUrlContent($url) {
  113. $client = $this->clientService->newClient();
  114. $response = $client->get($url);
  115. return $response->getBody();
  116. }
  117. }