@@ -34,113 +34,113 @@ |
||
| 34 | 34 | use OCP\Federation\ICloudIdManager; |
| 35 | 35 | |
| 36 | 36 | class CloudIdManager implements ICloudIdManager { |
| 37 | - /** @var IManager */ |
|
| 38 | - private $contactsManager; |
|
| 39 | - |
|
| 40 | - public function __construct(IManager $contactsManager) { |
|
| 41 | - $this->contactsManager = $contactsManager; |
|
| 42 | - } |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * @param string $cloudId |
|
| 46 | - * @return ICloudId |
|
| 47 | - * @throws \InvalidArgumentException |
|
| 48 | - */ |
|
| 49 | - public function resolveCloudId(string $cloudId): ICloudId { |
|
| 50 | - // TODO magic here to get the url and user instead of just splitting on @ |
|
| 51 | - |
|
| 52 | - if (!$this->isValidCloudId($cloudId)) { |
|
| 53 | - throw new \InvalidArgumentException('Invalid cloud id'); |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - // Find the first character that is not allowed in user names |
|
| 57 | - $id = $this->fixRemoteURL($cloudId); |
|
| 58 | - $posSlash = strpos($id, '/'); |
|
| 59 | - $posColon = strpos($id, ':'); |
|
| 60 | - |
|
| 61 | - if ($posSlash === false && $posColon === false) { |
|
| 62 | - $invalidPos = \strlen($id); |
|
| 63 | - } elseif ($posSlash === false) { |
|
| 64 | - $invalidPos = $posColon; |
|
| 65 | - } elseif ($posColon === false) { |
|
| 66 | - $invalidPos = $posSlash; |
|
| 67 | - } else { |
|
| 68 | - $invalidPos = min($posSlash, $posColon); |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - $lastValidAtPos = strrpos($id, '@', $invalidPos - strlen($id)); |
|
| 72 | - |
|
| 73 | - if ($lastValidAtPos !== false) { |
|
| 74 | - $user = substr($id, 0, $lastValidAtPos); |
|
| 75 | - $remote = substr($id, $lastValidAtPos + 1); |
|
| 76 | - if (!empty($user) && !empty($remote)) { |
|
| 77 | - return new CloudId($id, $user, $remote, $this->getDisplayNameFromContact($id)); |
|
| 78 | - } |
|
| 79 | - } |
|
| 80 | - throw new \InvalidArgumentException('Invalid cloud id'); |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - protected function getDisplayNameFromContact(string $cloudId): ?string { |
|
| 84 | - $addressBookEntries = $this->contactsManager->search($cloudId, ['CLOUD']); |
|
| 85 | - foreach ($addressBookEntries as $entry) { |
|
| 86 | - if (isset($entry['CLOUD'])) { |
|
| 87 | - foreach ($entry['CLOUD'] as $cloudID) { |
|
| 88 | - if ($cloudID === $cloudId) { |
|
| 89 | - return $entry['FN']; |
|
| 90 | - } |
|
| 91 | - } |
|
| 92 | - } |
|
| 93 | - } |
|
| 94 | - return null; |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - /** |
|
| 98 | - * @param string $user |
|
| 99 | - * @param string $remote |
|
| 100 | - * @return CloudId |
|
| 101 | - */ |
|
| 102 | - public function getCloudId(string $user, string $remote): ICloudId { |
|
| 103 | - // TODO check what the correct url is for remote (asking the remote) |
|
| 104 | - $fixedRemote = $this->fixRemoteURL($remote); |
|
| 105 | - if (strpos($fixedRemote, 'http://') === 0) { |
|
| 106 | - $host = substr($fixedRemote, strlen('http://')); |
|
| 107 | - } elseif (strpos($fixedRemote, 'https://') === 0) { |
|
| 108 | - $host = substr($fixedRemote, strlen('https://')); |
|
| 109 | - } else { |
|
| 110 | - $host = $fixedRemote; |
|
| 111 | - } |
|
| 112 | - $id = $user . '@' . $remote; |
|
| 113 | - $displayName = $this->getDisplayNameFromContact($user . '@' . $host); |
|
| 114 | - return new CloudId($id, $user, $fixedRemote, $displayName); |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - /** |
|
| 118 | - * Strips away a potential file names and trailing slashes: |
|
| 119 | - * - http://localhost |
|
| 120 | - * - http://localhost/ |
|
| 121 | - * - http://localhost/index.php |
|
| 122 | - * - http://localhost/index.php/s/{shareToken} |
|
| 123 | - * |
|
| 124 | - * all return: http://localhost |
|
| 125 | - * |
|
| 126 | - * @param string $remote |
|
| 127 | - * @return string |
|
| 128 | - */ |
|
| 129 | - protected function fixRemoteURL(string $remote): string { |
|
| 130 | - $remote = str_replace('\\', '/', $remote); |
|
| 131 | - if ($fileNamePosition = strpos($remote, '/index.php')) { |
|
| 132 | - $remote = substr($remote, 0, $fileNamePosition); |
|
| 133 | - } |
|
| 134 | - $remote = rtrim($remote, '/'); |
|
| 135 | - |
|
| 136 | - return $remote; |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - /** |
|
| 140 | - * @param string $cloudId |
|
| 141 | - * @return bool |
|
| 142 | - */ |
|
| 143 | - public function isValidCloudId(string $cloudId): bool { |
|
| 144 | - return strpos($cloudId, '@') !== false; |
|
| 145 | - } |
|
| 37 | + /** @var IManager */ |
|
| 38 | + private $contactsManager; |
|
| 39 | + |
|
| 40 | + public function __construct(IManager $contactsManager) { |
|
| 41 | + $this->contactsManager = $contactsManager; |
|
| 42 | + } |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * @param string $cloudId |
|
| 46 | + * @return ICloudId |
|
| 47 | + * @throws \InvalidArgumentException |
|
| 48 | + */ |
|
| 49 | + public function resolveCloudId(string $cloudId): ICloudId { |
|
| 50 | + // TODO magic here to get the url and user instead of just splitting on @ |
|
| 51 | + |
|
| 52 | + if (!$this->isValidCloudId($cloudId)) { |
|
| 53 | + throw new \InvalidArgumentException('Invalid cloud id'); |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + // Find the first character that is not allowed in user names |
|
| 57 | + $id = $this->fixRemoteURL($cloudId); |
|
| 58 | + $posSlash = strpos($id, '/'); |
|
| 59 | + $posColon = strpos($id, ':'); |
|
| 60 | + |
|
| 61 | + if ($posSlash === false && $posColon === false) { |
|
| 62 | + $invalidPos = \strlen($id); |
|
| 63 | + } elseif ($posSlash === false) { |
|
| 64 | + $invalidPos = $posColon; |
|
| 65 | + } elseif ($posColon === false) { |
|
| 66 | + $invalidPos = $posSlash; |
|
| 67 | + } else { |
|
| 68 | + $invalidPos = min($posSlash, $posColon); |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + $lastValidAtPos = strrpos($id, '@', $invalidPos - strlen($id)); |
|
| 72 | + |
|
| 73 | + if ($lastValidAtPos !== false) { |
|
| 74 | + $user = substr($id, 0, $lastValidAtPos); |
|
| 75 | + $remote = substr($id, $lastValidAtPos + 1); |
|
| 76 | + if (!empty($user) && !empty($remote)) { |
|
| 77 | + return new CloudId($id, $user, $remote, $this->getDisplayNameFromContact($id)); |
|
| 78 | + } |
|
| 79 | + } |
|
| 80 | + throw new \InvalidArgumentException('Invalid cloud id'); |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + protected function getDisplayNameFromContact(string $cloudId): ?string { |
|
| 84 | + $addressBookEntries = $this->contactsManager->search($cloudId, ['CLOUD']); |
|
| 85 | + foreach ($addressBookEntries as $entry) { |
|
| 86 | + if (isset($entry['CLOUD'])) { |
|
| 87 | + foreach ($entry['CLOUD'] as $cloudID) { |
|
| 88 | + if ($cloudID === $cloudId) { |
|
| 89 | + return $entry['FN']; |
|
| 90 | + } |
|
| 91 | + } |
|
| 92 | + } |
|
| 93 | + } |
|
| 94 | + return null; |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + /** |
|
| 98 | + * @param string $user |
|
| 99 | + * @param string $remote |
|
| 100 | + * @return CloudId |
|
| 101 | + */ |
|
| 102 | + public function getCloudId(string $user, string $remote): ICloudId { |
|
| 103 | + // TODO check what the correct url is for remote (asking the remote) |
|
| 104 | + $fixedRemote = $this->fixRemoteURL($remote); |
|
| 105 | + if (strpos($fixedRemote, 'http://') === 0) { |
|
| 106 | + $host = substr($fixedRemote, strlen('http://')); |
|
| 107 | + } elseif (strpos($fixedRemote, 'https://') === 0) { |
|
| 108 | + $host = substr($fixedRemote, strlen('https://')); |
|
| 109 | + } else { |
|
| 110 | + $host = $fixedRemote; |
|
| 111 | + } |
|
| 112 | + $id = $user . '@' . $remote; |
|
| 113 | + $displayName = $this->getDisplayNameFromContact($user . '@' . $host); |
|
| 114 | + return new CloudId($id, $user, $fixedRemote, $displayName); |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + /** |
|
| 118 | + * Strips away a potential file names and trailing slashes: |
|
| 119 | + * - http://localhost |
|
| 120 | + * - http://localhost/ |
|
| 121 | + * - http://localhost/index.php |
|
| 122 | + * - http://localhost/index.php/s/{shareToken} |
|
| 123 | + * |
|
| 124 | + * all return: http://localhost |
|
| 125 | + * |
|
| 126 | + * @param string $remote |
|
| 127 | + * @return string |
|
| 128 | + */ |
|
| 129 | + protected function fixRemoteURL(string $remote): string { |
|
| 130 | + $remote = str_replace('\\', '/', $remote); |
|
| 131 | + if ($fileNamePosition = strpos($remote, '/index.php')) { |
|
| 132 | + $remote = substr($remote, 0, $fileNamePosition); |
|
| 133 | + } |
|
| 134 | + $remote = rtrim($remote, '/'); |
|
| 135 | + |
|
| 136 | + return $remote; |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + /** |
|
| 140 | + * @param string $cloudId |
|
| 141 | + * @return bool |
|
| 142 | + */ |
|
| 143 | + public function isValidCloudId(string $cloudId): bool { |
|
| 144 | + return strpos($cloudId, '@') !== false; |
|
| 145 | + } |
|
| 146 | 146 | } |
@@ -109,8 +109,8 @@ |
||
| 109 | 109 | } else { |
| 110 | 110 | $host = $fixedRemote; |
| 111 | 111 | } |
| 112 | - $id = $user . '@' . $remote; |
|
| 113 | - $displayName = $this->getDisplayNameFromContact($user . '@' . $host); |
|
| 112 | + $id = $user.'@'.$remote; |
|
| 113 | + $displayName = $this->getDisplayNameFromContact($user.'@'.$host); |
|
| 114 | 114 | return new CloudId($id, $user, $fixedRemote, $displayName); |
| 115 | 115 | } |
| 116 | 116 | |