VersionCheck.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. // Look up the cache - it is invalidated all 30 minutes
  50. if (((int)$this->config->getAppValue('core', 'lastupdatedat') + 1800) > time()) {
  51. return json_decode($this->config->getAppValue('core', 'lastupdateResult'), true);
  52. }
  53. $updaterUrl = $this->config->getSystemValue('updater.server.url', 'https://updates.nextcloud.com/updater_server/');
  54. $this->config->setAppValue('core', 'lastupdatedat', time());
  55. if ($this->config->getAppValue('core', 'installedat', '') === '') {
  56. $this->config->setAppValue('core', 'installedat', microtime(true));
  57. }
  58. $version = Util::getVersion();
  59. $version['installed'] = $this->config->getAppValue('core', 'installedat');
  60. $version['updated'] = $this->config->getAppValue('core', 'lastupdatedat');
  61. $version['updatechannel'] = \OC_Util::getChannel();
  62. $version['edition'] = '';
  63. $version['build'] = \OC_Util::getBuild();
  64. $version['php_major'] = PHP_MAJOR_VERSION;
  65. $version['php_minor'] = PHP_MINOR_VERSION;
  66. $version['php_release'] = PHP_RELEASE_VERSION;
  67. $versionString = implode('x', $version);
  68. //fetch xml data from updater
  69. $url = $updaterUrl . '?version=' . $versionString;
  70. $tmp = [];
  71. try {
  72. $xml = $this->getUrlContent($url);
  73. } catch (\Exception $e) {
  74. return false;
  75. }
  76. if ($xml) {
  77. $loadEntities = libxml_disable_entity_loader(true);
  78. $data = @simplexml_load_string($xml);
  79. libxml_disable_entity_loader($loadEntities);
  80. if ($data !== false) {
  81. $tmp['version'] = (string)$data->version;
  82. $tmp['versionstring'] = (string)$data->versionstring;
  83. $tmp['url'] = (string)$data->url;
  84. $tmp['web'] = (string)$data->web;
  85. $tmp['changes'] = isset($data->changes) ? (string)$data->changes : '';
  86. $tmp['autoupdater'] = (string)$data->autoupdater;
  87. $tmp['eol'] = isset($data->eol) ? (string)$data->eol : '0';
  88. } else {
  89. libxml_clear_errors();
  90. }
  91. }
  92. // Cache the result
  93. $this->config->setAppValue('core', 'lastupdateResult', json_encode($tmp));
  94. return $tmp;
  95. }
  96. /**
  97. * @codeCoverageIgnore
  98. * @param string $url
  99. * @return resource|string
  100. * @throws \Exception
  101. */
  102. protected function getUrlContent($url) {
  103. $client = $this->clientService->newClient();
  104. $response = $client->get($url);
  105. return $response->getBody();
  106. }
  107. }