/apps/federatedfilesharing/lib/addresshandler.php

https://gitlab.com/wuhang2003/core · PHP · 184 lines · 89 code · 21 blank · 74 comment · 19 complexity · b0dad70377c249c63c60761377239c95 MD5 · raw file

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