1
0

AddressHandler.php 3.0 KB

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