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