AddressHandler.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bjoern Schiessle <bjoern@schiessle.org>
  6. * @author Björn Schießle <bjoern@schiessle.org>
  7. * @author Robin Appelman <robin@icewind.nl>
  8. *
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  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, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OCA\FederatedFileSharing;
  25. use OC\HintException;
  26. use OCP\Federation\ICloudIdManager;
  27. use OCP\IL10N;
  28. use OCP\IURLGenerator;
  29. /**
  30. * Class AddressHandler - parse, modify and construct federated sharing addresses
  31. *
  32. * @package OCA\FederatedFileSharing
  33. */
  34. class AddressHandler {
  35. /** @var IL10N */
  36. private $l;
  37. /** @var IURLGenerator */
  38. private $urlGenerator;
  39. /** @var ICloudIdManager */
  40. private $cloudIdManager;
  41. /**
  42. * AddressHandler constructor.
  43. *
  44. * @param IURLGenerator $urlGenerator
  45. * @param IL10N $il10n
  46. * @param ICloudIdManager $cloudIdManager
  47. */
  48. public function __construct(
  49. IURLGenerator $urlGenerator,
  50. IL10N $il10n,
  51. ICloudIdManager $cloudIdManager
  52. ) {
  53. $this->l = $il10n;
  54. $this->urlGenerator = $urlGenerator;
  55. $this->cloudIdManager = $cloudIdManager;
  56. }
  57. /**
  58. * split user and remote from federated cloud id
  59. *
  60. * @param string $address federated share address
  61. * @return array [user, remoteURL]
  62. * @throws HintException
  63. */
  64. public function splitUserRemote($address) {
  65. try {
  66. $cloudId = $this->cloudIdManager->resolveCloudId($address);
  67. return [$cloudId->getUser(), $cloudId->getRemote()];
  68. } catch (\InvalidArgumentException $e) {
  69. $hint = $this->l->t('Invalid Federated Cloud ID');
  70. throw new HintException('Invalid Federated Cloud ID', $hint, 0, $e);
  71. }
  72. }
  73. /**
  74. * generate remote URL part of federated ID
  75. *
  76. * @return string url of the current server
  77. */
  78. public function generateRemoteURL() {
  79. return $this->urlGenerator->getAbsoluteURL('/');
  80. }
  81. /**
  82. * check if two federated cloud IDs refer to the same user
  83. *
  84. * @param string $user1
  85. * @param string $server1
  86. * @param string $user2
  87. * @param string $server2
  88. * @return bool true if both users and servers are the same
  89. */
  90. public function compareAddresses($user1, $server1, $user2, $server2) {
  91. $normalizedServer1 = strtolower($this->removeProtocolFromUrl($server1));
  92. $normalizedServer2 = strtolower($this->removeProtocolFromUrl($server2));
  93. if (rtrim($normalizedServer1, '/') === rtrim($normalizedServer2, '/')) {
  94. // FIXME this should be a method in the user management instead
  95. \OCP\Util::emitHook(
  96. '\OCA\Files_Sharing\API\Server2Server',
  97. 'preLoginNameUsedAsUserName',
  98. array('uid' => &$user1)
  99. );
  100. \OCP\Util::emitHook(
  101. '\OCA\Files_Sharing\API\Server2Server',
  102. 'preLoginNameUsedAsUserName',
  103. array('uid' => &$user2)
  104. );
  105. if ($user1 === $user2) {
  106. return true;
  107. }
  108. }
  109. return false;
  110. }
  111. /**
  112. * remove protocol from URL
  113. *
  114. * @param string $url
  115. * @return string
  116. */
  117. public function removeProtocolFromUrl($url) {
  118. if (strpos($url, 'https://') === 0) {
  119. return substr($url, strlen('https://'));
  120. } else if (strpos($url, 'http://') === 0) {
  121. return substr($url, strlen('http://'));
  122. }
  123. return $url;
  124. }
  125. /**
  126. * check if the url contain the protocol (http or https)
  127. *
  128. * @param string $url
  129. * @return bool
  130. */
  131. public function urlContainProtocol($url) {
  132. if (strpos($url, 'https://') === 0 ||
  133. strpos($url, 'http://') === 0) {
  134. return true;
  135. }
  136. return false;
  137. }
  138. }