VersionCheck.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Lukas Reschke <lukas@statuscode.ch>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Thomas Müller <thomas.mueller@tmit.eu>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OC\Updater;
  26. use OCP\Http\Client\IClientService;
  27. use OCP\IConfig;
  28. use OCP\Util;
  29. class VersionCheck {
  30. /** @var IClientService */
  31. private $clientService;
  32. /** @var IConfig */
  33. private $config;
  34. /**
  35. * @param IClientService $clientService
  36. * @param IConfig $config
  37. */
  38. public function __construct(IClientService $clientService,
  39. IConfig $config) {
  40. $this->clientService = $clientService;
  41. $this->config = $config;
  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->getSystemValue('updater.server.url', 'https://updates.nextcloud.com/updater_server/');
  58. $this->config->setAppValue('core', 'lastupdatedat', time());
  59. if ($this->config->getAppValue('core', 'installedat', '') === '') {
  60. $this->config->setAppValue('core', 'installedat', 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. $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. $loadEntities = libxml_disable_entity_loader(true);
  82. $data = @simplexml_load_string($xml);
  83. libxml_disable_entity_loader($loadEntities);
  84. if ($data !== false) {
  85. $tmp['version'] = (string)$data->version;
  86. $tmp['versionstring'] = (string)$data->versionstring;
  87. $tmp['url'] = (string)$data->url;
  88. $tmp['web'] = (string)$data->web;
  89. $tmp['changes'] = isset($data->changes) ? (string)$data->changes : '';
  90. $tmp['autoupdater'] = (string)$data->autoupdater;
  91. $tmp['eol'] = isset($data->eol) ? (string)$data->eol : '0';
  92. } else {
  93. libxml_clear_errors();
  94. }
  95. }
  96. // Cache the result
  97. $this->config->setAppValue('core', 'lastupdateResult', json_encode($tmp));
  98. return $tmp;
  99. }
  100. /**
  101. * @codeCoverageIgnore
  102. * @param string $url
  103. * @return resource|string
  104. * @throws \Exception
  105. */
  106. protected function getUrlContent($url) {
  107. $client = $this->clientService->newClient();
  108. $response = $client->get($url);
  109. return $response->getBody();
  110. }
  111. }