Instance.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 Robin Appelman <robin@icewind.nl>
  4. *
  5. * @license GNU AGPL version 3 or any later version
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as
  9. * published by the Free Software Foundation, either version 3 of the
  10. * License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. namespace OC\Remote;
  22. use OC\Remote\Api\NotFoundException;
  23. use OCP\Http\Client\IClientService;
  24. use OCP\ICache;
  25. use OCP\Remote\IInstance;
  26. /**
  27. * Provides some basic info about a remote Nextcloud instance
  28. */
  29. class Instance implements IInstance {
  30. /** @var string */
  31. private $url;
  32. /** @var ICache */
  33. private $cache;
  34. /** @var IClientService */
  35. private $clientService;
  36. /** @var array|null */
  37. private $status;
  38. /**
  39. * @param string $url
  40. * @param ICache $cache
  41. * @param IClientService $clientService
  42. */
  43. public function __construct($url, ICache $cache, IClientService $clientService) {
  44. $url = str_replace('https://', '', $url);
  45. $this->url = str_replace('http://', '', $url);
  46. $this->cache = $cache;
  47. $this->clientService = $clientService;
  48. }
  49. /**
  50. * @return string The url of the remote server without protocol
  51. */
  52. public function getUrl() {
  53. return $this->url;
  54. }
  55. /**
  56. * @return string The of of the remote server with protocol
  57. */
  58. public function getFullUrl() {
  59. return $this->getProtocol() . '://' . $this->getUrl();
  60. }
  61. /**
  62. * @return string The full version string in '13.1.2.3' format
  63. */
  64. public function getVersion() {
  65. $status = $this->getStatus();
  66. return $status['version'];
  67. }
  68. /**
  69. * @return string 'http' or 'https'
  70. */
  71. public function getProtocol() {
  72. $status = $this->getStatus();
  73. return $status['protocol'];
  74. }
  75. /**
  76. * Check that the remote server is installed and not in maintenance mode
  77. *
  78. * @return bool
  79. */
  80. public function isActive() {
  81. $status = $this->getStatus();
  82. return $status['installed'] && !$status['maintenance'];
  83. }
  84. /**
  85. * @return array
  86. * @throws NotFoundException
  87. * @throws \Exception
  88. */
  89. private function getStatus() {
  90. if ($this->status) {
  91. return $this->status;
  92. }
  93. $key = 'remote/' . $this->url . '/status';
  94. $httpsKey = 'remote/' . $this->url . '/https';
  95. $status = $this->cache->get($key);
  96. if (!$status) {
  97. $response = $this->downloadStatus('https://' . $this->getUrl() . '/status.php');
  98. $protocol = 'https';
  99. if (!$response) {
  100. if ($status = $this->cache->get($httpsKey)) {
  101. throw new \Exception('refusing to connect to remote instance(' . $this->url . ') over http that was previously accessible over https');
  102. }
  103. $response = $this->downloadStatus('http://' . $this->getUrl() . '/status.php');
  104. $protocol = 'http';
  105. } else {
  106. $this->cache->set($httpsKey, true, 60 * 60 * 24 * 365);
  107. }
  108. $status = json_decode($response, true);
  109. if ($status) {
  110. $status['protocol'] = $protocol;
  111. }
  112. if ($status) {
  113. $this->cache->set($key, $status, 5 * 60);
  114. $this->status = $status;
  115. } else {
  116. throw new NotFoundException('Remote server not found at address ' . $this->url);
  117. }
  118. }
  119. return $status;
  120. }
  121. /**
  122. * @param string $url
  123. * @return bool|string
  124. */
  125. private function downloadStatus($url) {
  126. try {
  127. $request = $this->clientService->newClient()->get($url);
  128. return $request->getBody();
  129. } catch (\Exception $e) {
  130. return false;
  131. }
  132. }
  133. }