1
0

AddressHandler.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\FederatedFileSharing;
  8. use OCP\Federation\ICloudIdManager;
  9. use OCP\HintException;
  10. use OCP\IL10N;
  11. use OCP\IURLGenerator;
  12. /**
  13. * Class AddressHandler - parse, modify and construct federated sharing addresses
  14. *
  15. * @package OCA\FederatedFileSharing
  16. */
  17. class AddressHandler {
  18. /** @var IL10N */
  19. private $l;
  20. /** @var IURLGenerator */
  21. private $urlGenerator;
  22. /** @var ICloudIdManager */
  23. private $cloudIdManager;
  24. /**
  25. * AddressHandler constructor.
  26. *
  27. * @param IURLGenerator $urlGenerator
  28. * @param IL10N $il10n
  29. * @param ICloudIdManager $cloudIdManager
  30. */
  31. public function __construct(
  32. IURLGenerator $urlGenerator,
  33. IL10N $il10n,
  34. ICloudIdManager $cloudIdManager
  35. ) {
  36. $this->l = $il10n;
  37. $this->urlGenerator = $urlGenerator;
  38. $this->cloudIdManager = $cloudIdManager;
  39. }
  40. /**
  41. * split user and remote from federated cloud id
  42. *
  43. * @param string $address federated share address
  44. * @return array<string> [user, remoteURL]
  45. * @throws HintException
  46. */
  47. public function splitUserRemote($address) {
  48. try {
  49. $cloudId = $this->cloudIdManager->resolveCloudId($address);
  50. return [$cloudId->getUser(), $cloudId->getRemote()];
  51. } catch (\InvalidArgumentException $e) {
  52. $hint = $this->l->t('Invalid Federated Cloud ID');
  53. throw new HintException('Invalid Federated Cloud ID', $hint, 0, $e);
  54. }
  55. }
  56. /**
  57. * generate remote URL part of federated ID
  58. *
  59. * @return string url of the current server
  60. */
  61. public function generateRemoteURL() {
  62. return $this->urlGenerator->getAbsoluteURL('/');
  63. }
  64. /**
  65. * check if two federated cloud IDs refer to the same user
  66. *
  67. * @param string $user1
  68. * @param string $server1
  69. * @param string $user2
  70. * @param string $server2
  71. * @return bool true if both users and servers are the same
  72. */
  73. public function compareAddresses($user1, $server1, $user2, $server2) {
  74. $normalizedServer1 = strtolower($this->removeProtocolFromUrl($server1));
  75. $normalizedServer2 = strtolower($this->removeProtocolFromUrl($server2));
  76. if (rtrim($normalizedServer1, '/') === rtrim($normalizedServer2, '/')) {
  77. // FIXME this should be a method in the user management instead
  78. \OCP\Util::emitHook(
  79. '\OCA\Files_Sharing\API\Server2Server',
  80. 'preLoginNameUsedAsUserName',
  81. ['uid' => &$user1]
  82. );
  83. \OCP\Util::emitHook(
  84. '\OCA\Files_Sharing\API\Server2Server',
  85. 'preLoginNameUsedAsUserName',
  86. ['uid' => &$user2]
  87. );
  88. if ($user1 === $user2) {
  89. return true;
  90. }
  91. }
  92. return false;
  93. }
  94. /**
  95. * remove protocol from URL
  96. *
  97. * @param string $url
  98. * @return string
  99. */
  100. public function removeProtocolFromUrl($url) {
  101. if (str_starts_with($url, 'https://')) {
  102. return substr($url, strlen('https://'));
  103. } elseif (str_starts_with($url, 'http://')) {
  104. return substr($url, strlen('http://'));
  105. }
  106. return $url;
  107. }
  108. /**
  109. * check if the url contain the protocol (http or https)
  110. *
  111. * @param string $url
  112. * @return bool
  113. */
  114. public function urlContainProtocol($url) {
  115. if (str_starts_with($url, 'https://') ||
  116. str_starts_with($url, 'http://')) {
  117. return true;
  118. }
  119. return false;
  120. }
  121. }