DiscoveryService.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 Bjoern Schiessle <bjoern@schiessle.org>
  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\OCS;
  22. use OCP\AppFramework\Http;
  23. use OCP\Http\Client\IClient;
  24. use OCP\Http\Client\IClientService;
  25. use OCP\ICache;
  26. use OCP\ICacheFactory;
  27. use OCP\OCS\IDiscoveryService;
  28. class DiscoveryService implements IDiscoveryService {
  29. /** @var ICache */
  30. private $cache;
  31. /** @var IClient */
  32. private $client;
  33. /**
  34. * @param ICacheFactory $cacheFactory
  35. * @param IClientService $clientService
  36. */
  37. public function __construct(ICacheFactory $cacheFactory,
  38. IClientService $clientService
  39. ) {
  40. $this->cache = $cacheFactory->create('ocs-discovery');
  41. $this->client = $clientService->newClient();
  42. }
  43. /**
  44. * Discover OCS end-points
  45. *
  46. * If no valid discovery data is found the defaults are returned
  47. *
  48. * @param string $remote
  49. * @param string $service the service you want to discover
  50. * @return array
  51. */
  52. public function discover($remote, $service) {
  53. // Check the cache first
  54. $cacheData = $this->cache->get($remote . '#' . $service);
  55. if($cacheData) {
  56. return json_decode($cacheData, true);
  57. }
  58. $discoveredServices = [];
  59. // query the remote server for available services
  60. try {
  61. $response = $this->client->get($remote . '/ocs-provider/', [
  62. 'timeout' => 10,
  63. 'connect_timeout' => 10,
  64. ]);
  65. if($response->getStatusCode() === Http::STATUS_OK) {
  66. $decodedServices = json_decode($response->getBody(), true);
  67. $discoveredServices = $this->getEndpoints($decodedServices, $service);
  68. }
  69. } catch (\Exception $e) {
  70. // if we couldn't discover the service or any end-points we return a empty array
  71. return [];
  72. }
  73. // Write into cache
  74. $this->cache->set($remote . '#' . $service, json_encode($discoveredServices));
  75. return $discoveredServices;
  76. }
  77. /**
  78. * get requested end-points from the requested service
  79. *
  80. * @param $decodedServices
  81. * @param $service
  82. * @return array
  83. */
  84. protected function getEndpoints($decodedServices, $service) {
  85. $discoveredServices = [];
  86. if(is_array($decodedServices) &&
  87. isset($decodedServices['services'][$service]['endpoints'])
  88. ) {
  89. foreach ($decodedServices['services'][$service]['endpoints'] as $endpoint => $url) {
  90. if($this->isSafeUrl($url)) {
  91. $discoveredServices[$endpoint] = $url;
  92. }
  93. }
  94. }
  95. return $discoveredServices;
  96. }
  97. /**
  98. * Returns whether the specified URL includes only safe characters, if not
  99. * returns false
  100. *
  101. * @param string $url
  102. * @return bool
  103. */
  104. protected function isSafeUrl($url) {
  105. return (bool)preg_match('/^[\/\.\-A-Za-z0-9]+$/', $url);
  106. }
  107. }