AddressHandler.php 3.8 KB

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