Completed
Pull Request — master (#32218)
by Thomas
50:41 queued 37:30
created

AddressHandler::normalizeRemote()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Björn Schießle <[email protected]>
4
 * @author Thomas Müller <[email protected]>
5
 *
6
 * @copyright Copyright (c) 2018, ownCloud GmbH
7
 * @license AGPL-3.0
8
 *
9
 * This code is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License, version 3,
11
 * as published by the Free Software Foundation.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
 * GNU Affero General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public License, version 3,
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
20
 *
21
 */
22
23
namespace OCA\FederatedFileSharing;
24
use OC\HintException;
25
use OCP\IL10N;
26
use OCP\IURLGenerator;
27
28
/**
29
 * Class AddressHandler - parse, modify and construct federated sharing addresses
30
 *
31
 * @package OCA\FederatedFileSharing
32
 */
33
class AddressHandler {
34
35
	/** @var IL10N */
36
	private $l;
37
38
	/** @var IURLGenerator */
39
	private $urlGenerator;
40
41
	/**
42
	 * AddressHandler constructor.
43
	 *
44
	 * @param IURLGenerator $urlGenerator
45
	 * @param IL10N $il10n
46 256
	 */
47
	public function __construct(
48
		IURLGenerator $urlGenerator,
49
		IL10N $il10n
50 256
	) {
51 256
		$this->l = $il10n;
52 256
		$this->urlGenerator = $urlGenerator;
53
	}
54
55
	/**
56
	 * split user and remote from federated cloud id
57
	 *
58
	 * @param string $address federated share address
59
	 * @return array [user, remoteURL]
60
	 * @throws HintException
61 233
	 */
62 233
	public function splitUserRemote($address) {
63 4
		if (\strpos($address, '@') === false) {
64 4
			$hint = $this->l->t('Invalid Federated Cloud ID');
65
			throw new HintException('Invalid Federated Cloud ID', $hint);
66
		}
67
68 229
		// Find the first character that is not allowed in user names
69 229
		$id = \str_replace('\\', '/', $address);
70 229
		$posSlash = \strpos($id, '/');
71
		$posColon = \strpos($id, ':');
72 229
73 18 View Code Duplication
		if ($posSlash === false && $posColon === false) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
74 229
			$invalidPos = \strlen($id);
75 5
		} elseif ($posSlash === false) {
76 211
			$invalidPos = $posColon;
77 41
		} elseif ($posColon === false) {
78 41
			$invalidPos = $posSlash;
79 165
		} else {
80
			$invalidPos = \min($posSlash, $posColon);
81
		}
82
83 229
		// Find the last @ before $invalidPos
84 229
		$pos = $lastAtPos = 0;
85 229 View Code Duplication
		while ($lastAtPos !== false && $lastAtPos <= $invalidPos) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
86 229
			$pos = $lastAtPos;
87 229
			$lastAtPos = \strpos($id, '@', $pos + 1);
88
		}
89 229
90 229 View Code Duplication
		if ($pos !== false) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
91 229
			$user = \substr($id, 0, $pos);
92 229
			$remote = \substr($id, $pos + 1);
93 229
			$remote = $this->fixRemoteURL($remote);
94 225
			if (!empty($user) && !empty($remote)) {
95
				return [$user, $remote];
96 4
			}
97
		}
98 4
99 4
		$hint = $this->l->t('Invalid Federated Cloud ID');
100
		throw new HintException('Invalid Federated Cloud ID', $hint);
101
	}
102
103
	/**
104
	 * generate remote URL part of federated ID
105
	 *
106
	 * @return string url of the current server
107 9
	 */
108 9
	public function generateRemoteURL() {
109 9
		$url = $this->urlGenerator->getAbsoluteURL('/');
110
		return $url;
111
	}
112
113
	/**
114
	 * check if two federated cloud IDs refer to the same user
115
	 *
116
	 * @param string $user1
117
	 * @param string $server1
118
	 * @param string $user2
119
	 * @param string $server2
120
	 * @return bool true if both users and servers are the same
121 24
	 */
122 24
	public function compareAddresses($user1, $server1, $user2, $server2) {
123 24
		$normalizedServer1 = \strtolower($this->removeProtocolFromUrl($server1));
124
		$normalizedServer2 = \strtolower($this->removeProtocolFromUrl($server2));
125 24
126 View Code Duplication
		if (\rtrim($normalizedServer1, '/') === \rtrim($normalizedServer2, '/')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
127 11
			// FIXME this should be a method in the user management instead
128 11
			\OCP\Util::emitHook(
129 11
				'\OCA\Files_Sharing\API\Server2Server',
130 11
				'preLoginNameUsedAsUserName',
131 11
				['uid' => &$user1]
132 11
			);
133 11
			\OCP\Util::emitHook(
134 11
				'\OCA\Files_Sharing\API\Server2Server',
135 11
				'preLoginNameUsedAsUserName',
136 11
				['uid' => &$user2]
137
			);
138 11
139 6
			if ($user1 === $user2) {
140
				return true;
141 5
			}
142
		}
143 18
144
		return false;
145
	}
146
147
	/**
148
	 * remove protocol from URL
149
	 *
150
	 * @param string $url
151
	 * @return string
152 27
	 */
153 27
	public function removeProtocolFromUrl($url) {
154 4
		// replace all characters before :// and :// itself
155 26
		return \preg_replace('|^(.*?://)|', '', $url);
156 25
	}
157
158
	/**
159 12
	 * Get a remote name without a protocol, potential file names
160
	 * and a trailing slash
161
	 *
162
	 * @param string $remote
163
	 *
164
	 * @return string
165
	 */
166
	public function normalizeRemote($remote) {
167
		$fixedRemote = $this->fixRemoteURL($remote);
168
		return $this->removeProtocolFromUrl($fixedRemote);
169
	}
170
171
	/**
172
	 * Strips away a potential file names and trailing slashes:
173
	 * - http://localhost
174 233
	 * - http://localhost/
175 233
	 * - http://localhost/index.php
176 233
	 * - http://localhost/index.php/s/{shareToken}
177 110
	 *
178 110
	 * all return: http://localhost
179 233
	 *
180
	 * @param string $remote
181 233
	 * @return string
182
	 */
183 View Code Duplication
	protected function fixRemoteURL($remote) {
184
		$remote = \str_replace('\\', '/', $remote);
185
		if ($fileNamePosition = \strpos($remote, '/index.php')) {
186
			$remote = \substr($remote, 0, $fileNamePosition);
187
		}
188
		$remote = \rtrim($remote, '/');
189
190
		return $remote;
191
	}
192
}
193