1
0

DiscoveryService.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2017 Bjoern Schiessle <bjoern@schiessle.org>
  5. *
  6. * @author Bjoern Schiessle <bjoern@schiessle.org>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OC\OCS;
  25. use OCP\AppFramework\Http;
  26. use OCP\Http\Client\IClient;
  27. use OCP\Http\Client\IClientService;
  28. use OCP\ICache;
  29. use OCP\ICacheFactory;
  30. use OCP\OCS\IDiscoveryService;
  31. class DiscoveryService implements IDiscoveryService {
  32. /** @var ICache */
  33. private $cache;
  34. /** @var IClient */
  35. private $client;
  36. /**
  37. * @param ICacheFactory $cacheFactory
  38. * @param IClientService $clientService
  39. */
  40. public function __construct(ICacheFactory $cacheFactory,
  41. IClientService $clientService
  42. ) {
  43. $this->cache = $cacheFactory->createDistributed('ocs-discovery');
  44. $this->client = $clientService->newClient();
  45. }
  46. /**
  47. * Discover OCS end-points
  48. *
  49. * If no valid discovery data is found the defaults are returned
  50. *
  51. * @param string $remote
  52. * @param string $service the service you want to discover
  53. * @param bool $skipCache We won't check if the data is in the cache. This is usefull if a background job is updating the status
  54. * @return array
  55. */
  56. public function discover(string $remote, string $service, bool $skipCache = false): array {
  57. // Check the cache first
  58. if ($skipCache === false) {
  59. $cacheData = $this->cache->get($remote . '#' . $service);
  60. if ($cacheData) {
  61. $data = json_decode($cacheData, true);
  62. if (\is_array($data)) {
  63. return $data;
  64. }
  65. }
  66. }
  67. $discoveredServices = [];
  68. // query the remote server for available services
  69. try {
  70. $response = $this->client->get($remote . '/ocs-provider/', [
  71. 'timeout' => 10,
  72. 'connect_timeout' => 10,
  73. ]);
  74. if($response->getStatusCode() === Http::STATUS_OK) {
  75. $decodedServices = json_decode($response->getBody(), true);
  76. if (\is_array($decodedServices)) {
  77. $discoveredServices = $this->getEndpoints($decodedServices, $service);
  78. }
  79. }
  80. } catch (\Exception $e) {
  81. // if we couldn't discover the service or any end-points we return a empty array
  82. }
  83. // Write into cache
  84. $this->cache->set($remote . '#' . $service, json_encode($discoveredServices), 60*60*24);
  85. return $discoveredServices;
  86. }
  87. /**
  88. * get requested end-points from the requested service
  89. *
  90. * @param array $decodedServices
  91. * @param string $service
  92. * @return array
  93. */
  94. protected function getEndpoints(array $decodedServices, string $service): array {
  95. $discoveredServices = [];
  96. if(isset($decodedServices['services'][$service]['endpoints'])) {
  97. foreach ($decodedServices['services'][$service]['endpoints'] as $endpoint => $url) {
  98. if($this->isSafeUrl($url)) {
  99. $discoveredServices[$endpoint] = $url;
  100. }
  101. }
  102. }
  103. return $discoveredServices;
  104. }
  105. /**
  106. * Returns whether the specified URL includes only safe characters, if not
  107. * returns false
  108. *
  109. * @param string $url
  110. * @return bool
  111. */
  112. protected function isSafeUrl(string $url): bool {
  113. return (bool)preg_match('/^[\/\.\-A-Za-z0-9]+$/', $url);
  114. }
  115. }