Instance.php 3.8 KB

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