Completed
Pull Request — master (#32303)
by Victor
09:50
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
	 * @param string $uid
105
	 *
106
	 * @return Address
107 9
	 */
108 9
	public function getLocalUserFederatedAddress($uid) {
109 9
		$host = $this->generateRemoteURL();
110
		return new Address("{$uid}@{$host}");
111
	}
112
113
	/**
114
	 * generate remote URL part of federated ID
115
	 *
116
	 * @return string url of the current server
117
	 */
118
	public function generateRemoteURL() {
119
		$url = $this->urlGenerator->getAbsoluteURL('/');
120
		return $url;
121 24
	}
122 24
123 24
	/**
124
	 * remove protocol from URL
125 24
	 *
126
	 * @param string $url
127 11
	 * @return string
128 11
	 */
129 11
	public function removeProtocolFromUrl($url) {
130 11
		// replace all characters before :// and :// itself
131 11
		return \preg_replace('|^(.*?://)|', '', $url);
132 11
	}
133 11
134 11
	/**
135 11
	 * Strips away a potential file names and trailing slashes:
136 11
	 * - http://localhost
137
	 * - http://localhost/
138 11
	 * - http://localhost/index.php
139 6
	 * - http://localhost/index.php/s/{shareToken}
140
	 *
141 5
	 * all return: http://localhost
142
	 *
143 18
	 * @param string $remote
144
	 * @return string
145
	 */
146 View Code Duplication
	protected function fixRemoteURL($remote) {
147
		$remote = \str_replace('\\', '/', $remote);
148
		if ($fileNamePosition = \strpos($remote, '/index.php')) {
149
			$remote = \substr($remote, 0, $fileNamePosition);
150
		}
151
		$remote = \rtrim($remote, '/');
152 27
153 27
		return $remote;
154 4
	}
155
}
156