Completed
Push — master ( 5412c2...2d62f9 )
by Blizzz
45:23 queued 29:26
created

RemotePlugin::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 7
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 3
dl 7
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2017 Arthur Schiwon <[email protected]>
4
 *
5
 * @author Arthur Schiwon <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OC\Collaboration\Collaborators;
25
26
27
use OCP\Collaboration\Collaborators\ISearchPlugin;
28
use OCP\Collaboration\Collaborators\ISearchResult;
29
use OCP\Collaboration\Collaborators\SearchResultType;
30
use OCP\Contacts\IManager;
31
use OCP\Federation\ICloudIdManager;
32
use OCP\IConfig;
33
use OCP\Share;
34
35
class RemotePlugin implements ISearchPlugin {
36
	protected $shareeEnumeration;
37
38
	/** @var IManager */
39
	private $contactsManager;
40
	/** @var ICloudIdManager */
41
	private $cloudIdManager;
42
	/** @var IConfig */
43
	private $config;
44
45 View Code Duplication
	public function __construct(IManager $contactsManager, ICloudIdManager $cloudIdManager, IConfig $config) {
46
		$this->contactsManager = $contactsManager;
47
		$this->cloudIdManager = $cloudIdManager;
48
		$this->config = $config;
49
50
		$this->shareeEnumeration = $this->config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes') === 'yes';
51
	}
52
53
	public function search($search, $limit, $offset, ISearchResult $searchResult) {
54
		$result = ['wide' => [], 'exact' => []];
55
		$resultType = new SearchResultType('remotes');
56
57
		// Search in contacts
58
		//@todo Pagination missing
59
		$addressBookContacts = $this->contactsManager->search($search, ['CLOUD', 'FN']);
60
		foreach ($addressBookContacts as $contact) {
61
			if (isset($contact['isLocalSystemBook'])) {
62
				continue;
63
			}
64
			if (isset($contact['CLOUD'])) {
65
				$cloudIds = $contact['CLOUD'];
66
				if (!is_array($cloudIds)) {
67
					$cloudIds = [$cloudIds];
68
				}
69
				$lowerSearch = strtolower($search);
70
				foreach ($cloudIds as $cloudId) {
71
					try {
72
						list(, $serverUrl) = $this->splitUserRemote($cloudId);
73
					} catch (\InvalidArgumentException $e) {
74
						continue;
75
					}
76
77
					if (strtolower($contact['FN']) === $lowerSearch || strtolower($cloudId) === $lowerSearch) {
78
						if (strtolower($cloudId) === $lowerSearch) {
79
							$searchResult->markExactIdMatch($resultType);
80
						}
81
						$result['exact'][] = [
82
							'label' => $contact['FN'] . " ($cloudId)",
83
							'value' => [
84
								'shareType' => Share::SHARE_TYPE_REMOTE,
85
								'shareWith' => $cloudId,
86
								'server' => $serverUrl,
87
							],
88
						];
89 View Code Duplication
					} else {
90
						$result['wide'][] = [
91
							'label' => $contact['FN'] . " ($cloudId)",
92
							'value' => [
93
								'shareType' => Share::SHARE_TYPE_REMOTE,
94
								'shareWith' => $cloudId,
95
								'server' => $serverUrl,
96
							],
97
						];
98
					}
99
				}
100
			}
101
		}
102
103
		if (!$this->shareeEnumeration) {
104
			$result['wide'] = [];
105
		}
106
107
		if (!$searchResult->hasExactIdMatch($resultType) && $this->cloudIdManager->isValidCloudId($search) && $offset === 0) {
108
			$result['exact'][] = [
109
				'label' => $search,
110
				'value' => [
111
					'shareType' => Share::SHARE_TYPE_REMOTE,
112
					'shareWith' => $search,
113
				],
114
			];
115
		}
116
117
		$searchResult->addResultSet($resultType, $result['wide'], $result['exact']);
118
119
		return true;
120
	}
121
122
	/**
123
	 * split user and remote from federated cloud id
124
	 *
125
	 * @param string $address federated share address
126
	 * @return array [user, remoteURL]
127
	 * @throws \InvalidArgumentException
128
	 */
129 View Code Duplication
	public function splitUserRemote($address) {
130
		try {
131
			$cloudId = $this->cloudIdManager->resolveCloudId($address);
132
			return [$cloudId->getUser(), $cloudId->getRemote()];
133
		} catch (\InvalidArgumentException $e) {
134
			throw new \InvalidArgumentException('Invalid Federated Cloud ID', 0, $e);
135
		}
136
	}
137
}
138