You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

119 lines
3.3 KiB

3 years ago
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2017, Robin Appelman <robin@icewind.nl>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Robin Appelman <robin@icewind.nl>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. *
  11. * @license GNU AGPL version 3 or any later version
  12. *
  13. * This program is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License as
  15. * published by the Free Software Foundation, either version 3 of the
  16. * License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. *
  26. */
  27. namespace OC\Federation;
  28. use OCP\Federation\ICloudId;
  29. use OCP\Federation\ICloudIdManager;
  30. class CloudIdManager implements ICloudIdManager {
  31. /**
  32. * @param string $cloudId
  33. * @return ICloudId
  34. * @throws \InvalidArgumentException
  35. */
  36. public function resolveCloudId(string $cloudId): ICloudId {
  37. // TODO magic here to get the url and user instead of just splitting on @
  38. if (!$this->isValidCloudId($cloudId)) {
  39. throw new \InvalidArgumentException('Invalid cloud id');
  40. }
  41. // Find the first character that is not allowed in user names
  42. $id = $this->fixRemoteURL($cloudId);
  43. $posSlash = strpos($id, '/');
  44. $posColon = strpos($id, ':');
  45. if ($posSlash === false && $posColon === false) {
  46. $invalidPos = \strlen($id);
  47. } elseif ($posSlash === false) {
  48. $invalidPos = $posColon;
  49. } elseif ($posColon === false) {
  50. $invalidPos = $posSlash;
  51. } else {
  52. $invalidPos = min($posSlash, $posColon);
  53. }
  54. // Find the last @ before $invalidPos
  55. $pos = $lastAtPos = 0;
  56. while ($lastAtPos !== false && $lastAtPos <= $invalidPos) {
  57. $pos = $lastAtPos;
  58. $lastAtPos = strpos($id, '@', $pos + 1);
  59. }
  60. if ($pos !== false) {
  61. $user = substr($id, 0, $pos);
  62. $remote = substr($id, $pos + 1);
  63. if (!empty($user) && !empty($remote)) {
  64. return new CloudId($id, $user, $remote);
  65. }
  66. }
  67. throw new \InvalidArgumentException('Invalid cloud id');
  68. }
  69. /**
  70. * @param string $user
  71. * @param string $remote
  72. * @return CloudId
  73. */
  74. public function getCloudId(string $user, string $remote): ICloudId {
  75. // TODO check what the correct url is for remote (asking the remote)
  76. return new CloudId($user. '@' . $remote, $user, $remote);
  77. }
  78. /**
  79. * Strips away a potential file names and trailing slashes:
  80. * - http://localhost
  81. * - http://localhost/
  82. * - http://localhost/index.php
  83. * - http://localhost/index.php/s/{shareToken}
  84. *
  85. * all return: http://localhost
  86. *
  87. * @param string $remote
  88. * @return string
  89. */
  90. protected function fixRemoteURL(string $remote): string {
  91. $remote = str_replace('\\', '/', $remote);
  92. if ($fileNamePosition = strpos($remote, '/index.php')) {
  93. $remote = substr($remote, 0, $fileNamePosition);
  94. }
  95. $remote = rtrim($remote, '/');
  96. return $remote;
  97. }
  98. /**
  99. * @param string $cloudId
  100. * @return bool
  101. */
  102. public function isValidCloudId(string $cloudId): bool {
  103. return strpos($cloudId, '@') !== false;
  104. }
  105. }