DiscoveryManager.php 3.7 KB

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