CloudIdManager.php 3.0 KB

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