Completed
Push — master ( 4a5a36...bf8806 )
by Robin
27s
created

AddressHandler::splitUserRemote()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 9
Code Lines 7

Duplication

Lines 9
Ratio 100 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 3
nop 1
dl 9
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Björn Schießle <[email protected]>
6
 *
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\Federation\ICloudIdManager;
26
use OCP\IL10N;
27
use OCP\IURLGenerator;
28
29
/**
30
 * Class AddressHandler - parse, modify and construct federated sharing addresses
31
 *
32
 * @package OCA\FederatedFileSharing
33
 */
34
class AddressHandler {
35
36
	/** @var IL10N */
37
	private $l;
38
39
	/** @var IURLGenerator */
40
	private $urlGenerator;
41
42
	/** @var ICloudIdManager */
43
	private $cloudIdManager;
44
45
	/**
46
	 * AddressHandler constructor.
47
	 *
48
	 * @param IURLGenerator $urlGenerator
49
	 * @param IL10N $il10n
50
	 * @param ICloudIdManager $cloudIdManager
51
	 */
52
	public function __construct(
53
		IURLGenerator $urlGenerator,
54
		IL10N $il10n,
55
		ICloudIdManager $cloudIdManager
56
	) {
57
		$this->l = $il10n;
58
		$this->urlGenerator = $urlGenerator;
59
		$this->cloudIdManager = $cloudIdManager;
60
	}
61
62
	/**
63
	 * split user and remote from federated cloud id
64
	 *
65
	 * @param string $address federated share address
66
	 * @return array [user, remoteURL]
67
	 * @throws HintException
68
	 */
69 View Code Duplication
	public function splitUserRemote($address) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
70
		try {
71
			$cloudId = $this->cloudIdManager->resolveCloudId($address);
72
			return [$cloudId->getUser(), $cloudId->getRemote()];
73
		} catch (\InvalidArgumentException $e) {
74
			$hint = $this->l->t('Invalid Federated Cloud ID');
75
			throw new HintException('Invalid Federated Cloud ID', $hint, 0, $e);
76
		}
77
	}
78
79
	/**
80
	 * generate remote URL part of federated ID
81
	 *
82
	 * @return string url of the current server
83
	 */
84
	public function generateRemoteURL() {
85
		$url = $this->urlGenerator->getAbsoluteURL('/');
86
		return $url;
87
	}
88
89
	/**
90
	 * check if two federated cloud IDs refer to the same user
91
	 *
92
	 * @param string $user1
93
	 * @param string $server1
94
	 * @param string $user2
95
	 * @param string $server2
96
	 * @return bool true if both users and servers are the same
97
	 */
98
	public function compareAddresses($user1, $server1, $user2, $server2) {
99
		$normalizedServer1 = strtolower($this->removeProtocolFromUrl($server1));
100
		$normalizedServer2 = strtolower($this->removeProtocolFromUrl($server2));
101
102 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...
103
			// FIXME this should be a method in the user management instead
104
			\OCP\Util::emitHook(
105
				'\OCA\Files_Sharing\API\Server2Server',
106
				'preLoginNameUsedAsUserName',
107
				array('uid' => &$user1)
108
			);
109
			\OCP\Util::emitHook(
110
				'\OCA\Files_Sharing\API\Server2Server',
111
				'preLoginNameUsedAsUserName',
112
				array('uid' => &$user2)
113
			);
114
115
			if ($user1 === $user2) {
116
				return true;
117
			}
118
		}
119
120
		return false;
121
	}
122
123
	/**
124
	 * remove protocol from URL
125
	 *
126
	 * @param string $url
127
	 * @return string
128
	 */
129 View Code Duplication
	public function removeProtocolFromUrl($url) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
130
		if (strpos($url, 'https://') === 0) {
131
			return substr($url, strlen('https://'));
132
		} else if (strpos($url, 'http://') === 0) {
133
			return substr($url, strlen('http://'));
134
		}
135
136
		return $url;
137
	}
138
139
	/**
140
	 * check if the url contain the protocol (http or https)
141
	 *
142
	 * @param string $url
143
	 * @return bool
144
	 */
145
	public function urlContainProtocol($url) {
146 View Code Duplication
		if (strpos($url, 'https://') === 0 ||
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...
147
			strpos($url, 'http://') === 0) {
148
149
			return true;
150
		}
151
152
		return false;
153
	}
154
}
155