|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright Copyright (c) 2017, Robin Appelman <[email protected]> |
|
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
|
|
|
|
|
21
|
|
|
namespace OC\Federation; |
|
22
|
|
|
|
|
23
|
|
|
use OCP\Federation\ICloudId; |
|
24
|
|
|
use OCP\Federation\ICloudIdManager; |
|
25
|
|
|
|
|
26
|
|
|
class CloudIdManager implements ICloudIdManager { |
|
27
|
|
|
/** |
|
28
|
|
|
* @param string $cloudId |
|
29
|
|
|
* @return ICloudId |
|
30
|
|
|
*/ |
|
31
|
|
|
public function resolveCloudId($cloudId) { |
|
32
|
|
|
// TODO magic here to get the url and user instead of just splitting on @ |
|
33
|
|
|
|
|
34
|
|
|
if (!$this->isValidCloudId($cloudId)) { |
|
35
|
|
|
throw new \InvalidArgumentException('Invalid cloud id'); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
// Find the first character that is not allowed in user names |
|
39
|
|
|
$id = $this->fixRemoteURL($cloudId); |
|
40
|
|
|
$posSlash = strpos($id, '/'); |
|
41
|
|
|
$posColon = strpos($id, ':'); |
|
42
|
|
|
|
|
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
|
|
|
|
|
53
|
|
|
// Find the last @ before $invalidPos |
|
54
|
|
|
$pos = $lastAtPos = 0; |
|
55
|
|
|
while ($lastAtPos !== false && $lastAtPos <= $invalidPos) { |
|
56
|
|
|
$pos = $lastAtPos; |
|
57
|
|
|
$lastAtPos = strpos($id, '@', $pos + 1); |
|
58
|
|
|
} |
|
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
|
|
|
/** |
|
71
|
|
|
* @param string $user |
|
72
|
|
|
* @param string $remote |
|
73
|
|
|
* @return CloudId |
|
74
|
|
|
*/ |
|
75
|
|
|
public function getCloudId($user, $remote) { |
|
76
|
|
|
// TODO check what the correct url is for remote (asking the remote) |
|
77
|
|
|
return new CloudId($user. '@' . $remote, $user, $remote); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Strips away a potential file names and trailing slashes: |
|
82
|
|
|
* - http://localhost |
|
83
|
|
|
* - http://localhost/ |
|
84
|
|
|
* - http://localhost/index.php |
|
85
|
|
|
* - http://localhost/index.php/s/{shareToken} |
|
86
|
|
|
* |
|
87
|
|
|
* all return: http://localhost |
|
88
|
|
|
* |
|
89
|
|
|
* @param string $remote |
|
90
|
|
|
* @return string |
|
91
|
|
|
*/ |
|
92
|
|
View Code Duplication |
protected function fixRemoteURL($remote) { |
|
|
|
|
|
|
93
|
|
|
$remote = str_replace('\\', '/', $remote); |
|
94
|
|
|
if ($fileNamePosition = strpos($remote, '/index.php')) { |
|
95
|
|
|
$remote = substr($remote, 0, $fileNamePosition); |
|
96
|
|
|
} |
|
97
|
|
|
$remote = rtrim($remote, '/'); |
|
98
|
|
|
|
|
99
|
|
|
return $remote; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param string $cloudId |
|
104
|
|
|
* @return bool |
|
105
|
|
|
*/ |
|
106
|
|
|
public function isValidCloudId($cloudId) { |
|
107
|
|
|
return strpos($cloudId, '@') !== false; |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.