1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Viktar Dubiniuk <[email protected]> |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (c) 2018, ownCloud GmbH |
6
|
|
|
* @license AGPL-3.0 |
7
|
|
|
* |
8
|
|
|
* This code is free software: you can redistribute it and/or modify |
9
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
10
|
|
|
* as published by the Free Software Foundation. |
11
|
|
|
* |
12
|
|
|
* This program is distributed in the hope that it will be useful, |
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15
|
|
|
* GNU Affero General Public License for more details. |
16
|
|
|
* |
17
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
18
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
19
|
|
|
* |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
namespace OCA\FederatedFileSharing; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Class Address |
26
|
|
|
* |
27
|
|
|
* @package OCA\FederatedFileSharing |
28
|
|
|
*/ |
29
|
|
|
class Address { |
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
protected $cloudId; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
protected $displayName; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Address constructor. |
42
|
|
|
* |
43
|
|
|
* @param string $cloudId |
44
|
|
|
* @param string $displayName |
45
|
|
|
*/ |
46
|
|
|
public function __construct($cloudId, $displayName = '') { |
47
|
|
|
$this->cloudId = $cloudId; |
48
|
|
|
$this->displayName = $displayName; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Get user federated id |
53
|
|
|
* |
54
|
|
|
* @return string |
55
|
|
|
*/ |
56
|
|
|
public function getCloudId() { |
57
|
|
|
$origin = $this->getOrigin(); |
58
|
|
|
$userId = $this->getUserId(); |
59
|
|
|
return "{$userId}@{$origin}"; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Get user id |
64
|
|
|
* |
65
|
|
|
* @return string |
66
|
|
|
*/ |
67
|
|
|
public function getUserId() { |
68
|
|
|
// userId is everything except the last part |
69
|
|
|
$parts = \explode('@', $this->cloudId); |
70
|
|
|
\array_pop($parts); |
71
|
|
|
return \implode('@', $parts); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Get user host without protocol, index.php and a trailing slash |
76
|
|
|
* |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
|
|
public function getHostName() { |
80
|
|
|
// hostname is the last part |
81
|
|
|
$origin = $this->getCleanOrigin(); |
82
|
|
|
|
83
|
|
|
// replace all characters before :// and :// itself |
84
|
|
|
return \preg_replace('|^(.*?://)|', '', $origin); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Get user host with protocol but without trailing slash and index.php |
89
|
|
|
* |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
|
|
public function getOrigin() { |
93
|
|
|
return $this->getCleanOrigin(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Get user display name, fallback to userId if it is empty |
98
|
|
|
* |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
|
|
public function getDisplayName() { |
102
|
|
|
return ($this->displayName !== '') ? $this->displayName : $this->getUserId(); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Checks if the user and host is the same with another address |
107
|
|
|
* |
108
|
|
|
* @param Address $address |
109
|
|
|
* |
110
|
|
|
* @return bool |
111
|
|
|
*/ |
112
|
|
|
public function equalTo(Address $address) { |
113
|
|
|
$thisUserId = $this->translateUid($this->getUserId()); |
114
|
|
|
$otherUserId = $this->translateUid($address->getUserId()); |
115
|
|
|
return $this->getHostName() === $address->getHostName() |
116
|
|
|
&& $thisUserId === $otherUserId; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Some kind of ancient magic that was copypasted here and there |
121
|
|
|
* |
122
|
|
|
* @return mixed |
123
|
|
|
*/ |
124
|
|
|
public function toLocalUid() { |
125
|
|
|
return $this->translateUid($this->getUserId()); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Cut index.php and trailing slash from remote URL |
130
|
|
|
* |
131
|
|
|
* @return string |
132
|
|
|
*/ |
133
|
|
|
protected function getCleanOrigin() { |
134
|
|
|
//Origin is the last part |
135
|
|
|
$parts = \explode('@', $this->cloudId); |
136
|
|
|
$rawOrigin = \array_pop($parts); |
137
|
|
|
if ($fileNamePosition = \strpos($rawOrigin, '/index.php')) { |
138
|
|
|
$rawOrigin = \substr($rawOrigin, 0, $fileNamePosition); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
$normalizedOrigin = \rtrim( |
142
|
|
|
\strtolower($rawOrigin), |
143
|
|
|
'/' |
144
|
|
|
); |
145
|
|
|
|
146
|
|
|
return $normalizedOrigin; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param string $uid |
151
|
|
|
* @return mixed |
152
|
|
|
*/ |
153
|
|
|
protected function translateUid($uid) { |
154
|
|
|
// FIXME this should be a method in the user management instead |
155
|
|
|
// Move to a helper instead of C&P meanwhile? |
156
|
|
|
\OCP\Util::emitHook( |
157
|
|
|
'\OCA\Files_Sharing\API\Server2Server', |
158
|
|
|
'preLoginNameUsedAsUserName', |
159
|
|
|
['uid' => &$uid] |
160
|
|
|
); |
161
|
|
|
return $uid; |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|