VersionCheck.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Lukas Reschke <lukas@statuscode.ch>
  6. * @author Thomas Müller <thomas.mueller@tmit.eu>
  7. *
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OC\Updater;
  24. use OC_Util;
  25. use OCP\Http\Client\IClientService;
  26. use OCP\IConfig;
  27. use OC\Setup;
  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. $xml = $this->getUrlContent($url);
  72. if ($xml) {
  73. $loadEntities = libxml_disable_entity_loader(true);
  74. $data = @simplexml_load_string($xml);
  75. libxml_disable_entity_loader($loadEntities);
  76. if ($data !== false) {
  77. $tmp['version'] = (string)$data->version;
  78. $tmp['versionstring'] = (string)$data->versionstring;
  79. $tmp['url'] = (string)$data->url;
  80. $tmp['web'] = (string)$data->web;
  81. $tmp['autoupdater'] = (string)$data->autoupdater;
  82. } else {
  83. libxml_clear_errors();
  84. }
  85. } else {
  86. $data = [];
  87. }
  88. // Cache the result
  89. $this->config->setAppValue('core', 'lastupdateResult', json_encode($data));
  90. return $tmp;
  91. }
  92. /**
  93. * @codeCoverageIgnore
  94. * @param string $url
  95. * @return bool|resource|string
  96. */
  97. protected function getUrlContent($url) {
  98. try {
  99. $client = $this->clientService->newClient();
  100. $response = $client->get($url);
  101. return $response->getBody();
  102. } catch (\Exception $e) {
  103. return false;
  104. }
  105. }
  106. }