DiscoveryManager.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bjoern Schiessle <bjoern@schiessle.org>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Lukas Reschke <lukas@statuscode.ch>
  8. * @author Vincent Petry <pvince81@owncloud.com>
  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 OCA\FederatedFileSharing;
  26. use GuzzleHttp\Exception\ClientException;
  27. use GuzzleHttp\Exception\ConnectException;
  28. use OCP\Http\Client\IClient;
  29. use OCP\Http\Client\IClientService;
  30. use OCP\ICache;
  31. use OCP\ICacheFactory;
  32. /**
  33. * Class DiscoveryManager handles the discovery of endpoints used by Federated
  34. * Cloud Sharing.
  35. *
  36. * @package OCA\FederatedFileSharing
  37. */
  38. class DiscoveryManager {
  39. /** @var ICache */
  40. private $cache;
  41. /** @var IClient */
  42. private $client;
  43. /**
  44. * @param ICacheFactory $cacheFactory
  45. * @param IClientService $clientService
  46. */
  47. public function __construct(ICacheFactory $cacheFactory,
  48. IClientService $clientService) {
  49. $this->cache = $cacheFactory->create('ocs-discovery');
  50. $this->client = $clientService->newClient();
  51. }
  52. /**
  53. * Returns whether the specified URL includes only safe characters, if not
  54. * returns false
  55. *
  56. * @param string $url
  57. * @return bool
  58. */
  59. private function isSafeUrl($url) {
  60. return (bool)preg_match('/^[\/\.A-Za-z0-9]+$/', $url);
  61. }
  62. /**
  63. * Discover the actual data and do some naive caching to ensure that the data
  64. * is not requested multiple times.
  65. *
  66. * If no valid discovery data is found the Nextcloud defaults are returned.
  67. *
  68. * @param string $remote
  69. * @return array
  70. */
  71. private function discover($remote) {
  72. // Check if something is in the cache
  73. if($cacheData = $this->cache->get($remote)) {
  74. return json_decode($cacheData, true);
  75. }
  76. // Default response body
  77. $discoveredServices = [
  78. 'webdav' => '/public.php/webdav',
  79. 'share' => '/ocs/v1.php/cloud/shares',
  80. ];
  81. // Read the data from the response body
  82. try {
  83. $response = $this->client->get($remote . '/ocs-provider/', [
  84. 'timeout' => 10,
  85. 'connect_timeout' => 10,
  86. ]);
  87. if($response->getStatusCode() === 200) {
  88. $decodedService = json_decode($response->getBody(), true);
  89. if(is_array($decodedService)) {
  90. $endpoints = [
  91. 'webdav',
  92. 'share',
  93. ];
  94. foreach($endpoints as $endpoint) {
  95. if(isset($decodedService['services']['FEDERATED_SHARING']['endpoints'][$endpoint])) {
  96. $endpointUrl = (string)$decodedService['services']['FEDERATED_SHARING']['endpoints'][$endpoint];
  97. if($this->isSafeUrl($endpointUrl)) {
  98. $discoveredServices[$endpoint] = $endpointUrl;
  99. }
  100. }
  101. }
  102. }
  103. }
  104. } catch (ClientException $e) {
  105. // Don't throw any exception since exceptions are handled before
  106. } catch (ConnectException $e) {
  107. // Don't throw any exception since exceptions are handled before
  108. }
  109. // Write into cache
  110. $this->cache->set($remote, json_encode($discoveredServices));
  111. return $discoveredServices;
  112. }
  113. /**
  114. * Return the public WebDAV endpoint used by the specified remote
  115. *
  116. * @param string $host
  117. * @return string
  118. */
  119. public function getWebDavEndpoint($host) {
  120. return $this->discover($host)['webdav'];
  121. }
  122. /**
  123. * Return the sharing endpoint used by the specified remote
  124. *
  125. * @param string $host
  126. * @return string
  127. */
  128. public function getShareEndpoint($host) {
  129. return $this->discover($host)['share'];
  130. }
  131. }