1
0

AddressHandler.php 3.9 KB

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