@@ -34,119 +34,119 @@ |
||
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 | - // Warning, if user decides to make his full name local only, |
|
90 | - // no FN is found on federated servers |
|
91 | - if (isset($entry['FN'])) { |
|
92 | - return $entry['FN']; |
|
93 | - } else { |
|
94 | - return $cloudID; |
|
95 | - } |
|
96 | - } |
|
97 | - } |
|
98 | - } |
|
99 | - } |
|
100 | - return null; |
|
101 | - } |
|
102 | - |
|
103 | - /** |
|
104 | - * @param string $user |
|
105 | - * @param string $remote |
|
106 | - * @return CloudId |
|
107 | - */ |
|
108 | - public function getCloudId(string $user, string $remote): ICloudId { |
|
109 | - // TODO check what the correct url is for remote (asking the remote) |
|
110 | - $fixedRemote = $this->fixRemoteURL($remote); |
|
111 | - if (strpos($fixedRemote, 'http://') === 0) { |
|
112 | - $host = substr($fixedRemote, strlen('http://')); |
|
113 | - } elseif (strpos($fixedRemote, 'https://') === 0) { |
|
114 | - $host = substr($fixedRemote, strlen('https://')); |
|
115 | - } else { |
|
116 | - $host = $fixedRemote; |
|
117 | - } |
|
118 | - $id = $user . '@' . $remote; |
|
119 | - $displayName = $this->getDisplayNameFromContact($user . '@' . $host); |
|
120 | - return new CloudId($id, $user, $fixedRemote, $displayName); |
|
121 | - } |
|
122 | - |
|
123 | - /** |
|
124 | - * Strips away a potential file names and trailing slashes: |
|
125 | - * - http://localhost |
|
126 | - * - http://localhost/ |
|
127 | - * - http://localhost/index.php |
|
128 | - * - http://localhost/index.php/s/{shareToken} |
|
129 | - * |
|
130 | - * all return: http://localhost |
|
131 | - * |
|
132 | - * @param string $remote |
|
133 | - * @return string |
|
134 | - */ |
|
135 | - protected function fixRemoteURL(string $remote): string { |
|
136 | - $remote = str_replace('\\', '/', $remote); |
|
137 | - if ($fileNamePosition = strpos($remote, '/index.php')) { |
|
138 | - $remote = substr($remote, 0, $fileNamePosition); |
|
139 | - } |
|
140 | - $remote = rtrim($remote, '/'); |
|
141 | - |
|
142 | - return $remote; |
|
143 | - } |
|
144 | - |
|
145 | - /** |
|
146 | - * @param string $cloudId |
|
147 | - * @return bool |
|
148 | - */ |
|
149 | - public function isValidCloudId(string $cloudId): bool { |
|
150 | - return strpos($cloudId, '@') !== false; |
|
151 | - } |
|
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 | + // Warning, if user decides to make his full name local only, |
|
90 | + // no FN is found on federated servers |
|
91 | + if (isset($entry['FN'])) { |
|
92 | + return $entry['FN']; |
|
93 | + } else { |
|
94 | + return $cloudID; |
|
95 | + } |
|
96 | + } |
|
97 | + } |
|
98 | + } |
|
99 | + } |
|
100 | + return null; |
|
101 | + } |
|
102 | + |
|
103 | + /** |
|
104 | + * @param string $user |
|
105 | + * @param string $remote |
|
106 | + * @return CloudId |
|
107 | + */ |
|
108 | + public function getCloudId(string $user, string $remote): ICloudId { |
|
109 | + // TODO check what the correct url is for remote (asking the remote) |
|
110 | + $fixedRemote = $this->fixRemoteURL($remote); |
|
111 | + if (strpos($fixedRemote, 'http://') === 0) { |
|
112 | + $host = substr($fixedRemote, strlen('http://')); |
|
113 | + } elseif (strpos($fixedRemote, 'https://') === 0) { |
|
114 | + $host = substr($fixedRemote, strlen('https://')); |
|
115 | + } else { |
|
116 | + $host = $fixedRemote; |
|
117 | + } |
|
118 | + $id = $user . '@' . $remote; |
|
119 | + $displayName = $this->getDisplayNameFromContact($user . '@' . $host); |
|
120 | + return new CloudId($id, $user, $fixedRemote, $displayName); |
|
121 | + } |
|
122 | + |
|
123 | + /** |
|
124 | + * Strips away a potential file names and trailing slashes: |
|
125 | + * - http://localhost |
|
126 | + * - http://localhost/ |
|
127 | + * - http://localhost/index.php |
|
128 | + * - http://localhost/index.php/s/{shareToken} |
|
129 | + * |
|
130 | + * all return: http://localhost |
|
131 | + * |
|
132 | + * @param string $remote |
|
133 | + * @return string |
|
134 | + */ |
|
135 | + protected function fixRemoteURL(string $remote): string { |
|
136 | + $remote = str_replace('\\', '/', $remote); |
|
137 | + if ($fileNamePosition = strpos($remote, '/index.php')) { |
|
138 | + $remote = substr($remote, 0, $fileNamePosition); |
|
139 | + } |
|
140 | + $remote = rtrim($remote, '/'); |
|
141 | + |
|
142 | + return $remote; |
|
143 | + } |
|
144 | + |
|
145 | + /** |
|
146 | + * @param string $cloudId |
|
147 | + * @return bool |
|
148 | + */ |
|
149 | + public function isValidCloudId(string $cloudId): bool { |
|
150 | + return strpos($cloudId, '@') !== false; |
|
151 | + } |
|
152 | 152 | } |