CloudIdManager.php 3.2 KB

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