2017-08-31 16:47:02 -04:00
|
|
|
<?php
|
2025-06-30 09:04:05 -04:00
|
|
|
|
2017-08-31 16:47:02 -04:00
|
|
|
/**
|
2024-05-23 03:26:56 -04:00
|
|
|
* SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
2017-08-31 16:47:02 -04:00
|
|
|
*/
|
|
|
|
|
namespace OC\Collaboration\Collaborators;
|
|
|
|
|
|
|
|
|
|
use OCP\Collaboration\Collaborators\ISearchPlugin;
|
|
|
|
|
use OCP\Collaboration\Collaborators\ISearchResult;
|
2017-09-06 10:09:29 -04:00
|
|
|
use OCP\Collaboration\Collaborators\SearchResultType;
|
2017-08-31 16:47:02 -04:00
|
|
|
use OCP\Contacts\IManager;
|
|
|
|
|
use OCP\Federation\ICloudIdManager;
|
|
|
|
|
use OCP\IConfig;
|
2018-10-17 15:20:15 -04:00
|
|
|
use OCP\IUserManager;
|
2018-10-30 06:41:56 -04:00
|
|
|
use OCP\IUserSession;
|
2020-06-24 10:49:16 -04:00
|
|
|
use OCP\Share\IShare;
|
2017-08-31 16:47:02 -04:00
|
|
|
|
|
|
|
|
class RemotePlugin implements ISearchPlugin {
|
2023-07-03 03:36:55 -04:00
|
|
|
protected bool $shareeEnumeration;
|
2017-08-31 16:47:02 -04:00
|
|
|
|
2023-07-03 03:36:55 -04:00
|
|
|
private string $userId;
|
2017-08-31 16:47:02 -04:00
|
|
|
|
2023-07-03 03:36:55 -04:00
|
|
|
public function __construct(
|
|
|
|
|
private IManager $contactsManager,
|
|
|
|
|
private ICloudIdManager $cloudIdManager,
|
|
|
|
|
private IConfig $config,
|
|
|
|
|
private IUserManager $userManager,
|
|
|
|
|
IUserSession $userSession,
|
|
|
|
|
) {
|
|
|
|
|
$this->userId = $userSession->getUser()?->getUID() ?? '';
|
2017-08-31 16:47:02 -04:00
|
|
|
$this->shareeEnumeration = $this->config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes') === 'yes';
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-03 03:36:55 -04:00
|
|
|
public function search($search, $limit, $offset, ISearchResult $searchResult): bool {
|
2017-08-31 16:47:02 -04:00
|
|
|
$result = ['wide' => [], 'exact' => []];
|
2017-09-06 10:09:29 -04:00
|
|
|
$resultType = new SearchResultType('remotes');
|
2017-08-31 16:47:02 -04:00
|
|
|
|
|
|
|
|
// Search in contacts
|
2021-12-08 11:26:30 -05:00
|
|
|
$addressBookContacts = $this->contactsManager->search($search, ['CLOUD', 'FN'], [
|
|
|
|
|
'limit' => $limit,
|
|
|
|
|
'offset' => $offset,
|
|
|
|
|
'enumeration' => false,
|
|
|
|
|
'fullmatch' => false,
|
|
|
|
|
]);
|
2017-08-31 16:47:02 -04:00
|
|
|
foreach ($addressBookContacts as $contact) {
|
|
|
|
|
if (isset($contact['isLocalSystemBook'])) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (isset($contact['CLOUD'])) {
|
|
|
|
|
$cloudIds = $contact['CLOUD'];
|
2018-10-29 12:00:09 -04:00
|
|
|
if (is_string($cloudIds)) {
|
2017-08-31 16:47:02 -04:00
|
|
|
$cloudIds = [$cloudIds];
|
|
|
|
|
}
|
|
|
|
|
$lowerSearch = strtolower($search);
|
2018-10-29 12:00:09 -04:00
|
|
|
foreach ($cloudIds as $cloudId) {
|
|
|
|
|
$cloudIdType = '';
|
|
|
|
|
if (\is_array($cloudId)) {
|
|
|
|
|
$cloudIdData = $cloudId;
|
|
|
|
|
$cloudId = $cloudIdData['value'];
|
|
|
|
|
$cloudIdType = $cloudIdData['type'];
|
|
|
|
|
}
|
2017-08-31 16:47:02 -04:00
|
|
|
try {
|
2021-01-12 04:15:48 -05:00
|
|
|
[$remoteUser, $serverUrl] = $this->splitUserRemote($cloudId);
|
2017-08-31 16:47:02 -04:00
|
|
|
} catch (\InvalidArgumentException $e) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-17 15:20:15 -04:00
|
|
|
$localUser = $this->userManager->get($remoteUser);
|
2018-10-23 03:24:04 -04:00
|
|
|
/**
|
|
|
|
|
* Add local share if remote cloud id matches a local user ones
|
|
|
|
|
*/
|
2020-04-09 10:07:47 -04:00
|
|
|
if ($localUser !== null && $remoteUser !== $this->userId && $cloudId === $localUser->getCloudId()) {
|
2018-10-23 03:24:04 -04:00
|
|
|
$result['wide'][] = [
|
|
|
|
|
'label' => $contact['FN'],
|
|
|
|
|
'uuid' => $contact['UID'],
|
|
|
|
|
'value' => [
|
2020-06-24 10:49:16 -04:00
|
|
|
'shareType' => IShare::TYPE_USER,
|
2018-10-23 03:24:04 -04:00
|
|
|
'shareWith' => $remoteUser
|
2020-10-07 06:31:30 -04:00
|
|
|
],
|
|
|
|
|
'shareWithDisplayNameUnique' => $contact['EMAIL'] !== null && $contact['EMAIL'] !== '' ? $contact['EMAIL'] : $contact['UID'],
|
2018-10-23 03:24:04 -04:00
|
|
|
];
|
2018-10-17 15:20:15 -04:00
|
|
|
}
|
|
|
|
|
|
2017-08-31 16:47:02 -04:00
|
|
|
if (strtolower($contact['FN']) === $lowerSearch || strtolower($cloudId) === $lowerSearch) {
|
|
|
|
|
if (strtolower($cloudId) === $lowerSearch) {
|
2017-09-12 16:38:36 -04:00
|
|
|
$searchResult->markExactIdMatch($resultType);
|
2017-08-31 16:47:02 -04:00
|
|
|
}
|
|
|
|
|
$result['exact'][] = [
|
|
|
|
|
'label' => $contact['FN'] . " ($cloudId)",
|
2018-10-17 15:57:22 -04:00
|
|
|
'uuid' => $contact['UID'],
|
2018-10-26 07:46:47 -04:00
|
|
|
'name' => $contact['FN'],
|
2018-10-29 12:00:09 -04:00
|
|
|
'type' => $cloudIdType,
|
2017-08-31 16:47:02 -04:00
|
|
|
'value' => [
|
2020-06-24 10:49:16 -04:00
|
|
|
'shareType' => IShare::TYPE_REMOTE,
|
2017-08-31 16:47:02 -04:00
|
|
|
'shareWith' => $cloudId,
|
|
|
|
|
'server' => $serverUrl,
|
|
|
|
|
],
|
|
|
|
|
];
|
|
|
|
|
} else {
|
|
|
|
|
$result['wide'][] = [
|
|
|
|
|
'label' => $contact['FN'] . " ($cloudId)",
|
2018-10-17 15:57:22 -04:00
|
|
|
'uuid' => $contact['UID'],
|
2018-10-26 07:46:47 -04:00
|
|
|
'name' => $contact['FN'],
|
2018-10-29 12:00:09 -04:00
|
|
|
'type' => $cloudIdType,
|
2017-08-31 16:47:02 -04:00
|
|
|
'value' => [
|
2020-06-24 10:49:16 -04:00
|
|
|
'shareType' => IShare::TYPE_REMOTE,
|
2017-08-31 16:47:02 -04:00
|
|
|
'shareWith' => $cloudId,
|
|
|
|
|
'server' => $serverUrl,
|
|
|
|
|
],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!$this->shareeEnumeration) {
|
|
|
|
|
$result['wide'] = [];
|
2018-02-06 12:11:44 -05:00
|
|
|
} else {
|
|
|
|
|
$result['wide'] = array_slice($result['wide'], $offset, $limit);
|
2017-08-31 16:47:02 -04:00
|
|
|
}
|
|
|
|
|
|
2018-10-17 15:21:39 -04:00
|
|
|
/**
|
|
|
|
|
* Add generic share with remote item for valid cloud ids that are not users of the local instance
|
|
|
|
|
*/
|
2017-09-06 10:09:29 -04:00
|
|
|
if (!$searchResult->hasExactIdMatch($resultType) && $this->cloudIdManager->isValidCloudId($search) && $offset === 0) {
|
2018-10-17 15:21:39 -04:00
|
|
|
try {
|
2021-01-12 04:15:48 -05:00
|
|
|
[$remoteUser, $serverUrl] = $this->splitUserRemote($search);
|
2018-10-17 15:21:39 -04:00
|
|
|
$localUser = $this->userManager->get($remoteUser);
|
|
|
|
|
if ($localUser === null || $search !== $localUser->getCloudId()) {
|
|
|
|
|
$result['exact'][] = [
|
2019-05-23 11:03:04 -04:00
|
|
|
'label' => $remoteUser . " ($serverUrl)",
|
|
|
|
|
'uuid' => $remoteUser,
|
|
|
|
|
'name' => $remoteUser,
|
2018-10-17 15:21:39 -04:00
|
|
|
'value' => [
|
2020-06-24 10:49:16 -04:00
|
|
|
'shareType' => IShare::TYPE_REMOTE,
|
2018-10-17 15:21:39 -04:00
|
|
|
'shareWith' => $search,
|
2019-05-23 11:03:04 -04:00
|
|
|
'server' => $serverUrl,
|
2018-10-17 15:21:39 -04:00
|
|
|
],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
} catch (\InvalidArgumentException $e) {
|
|
|
|
|
}
|
2017-08-31 16:47:02 -04:00
|
|
|
}
|
|
|
|
|
|
2017-09-06 10:09:29 -04:00
|
|
|
$searchResult->addResultSet($resultType, $result['wide'], $result['exact']);
|
2017-08-31 16:47:02 -04:00
|
|
|
|
2017-09-12 16:38:36 -04:00
|
|
|
return true;
|
2017-08-31 16:47:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* split user and remote from federated cloud id
|
|
|
|
|
*
|
|
|
|
|
* @param string $address federated share address
|
|
|
|
|
* @return array [user, remoteURL]
|
|
|
|
|
* @throws \InvalidArgumentException
|
|
|
|
|
*/
|
2023-07-14 10:07:19 -04:00
|
|
|
public function splitUserRemote(string $address): array {
|
2017-08-31 16:47:02 -04:00
|
|
|
try {
|
|
|
|
|
$cloudId = $this->cloudIdManager->resolveCloudId($address);
|
2024-06-26 04:40:31 -04:00
|
|
|
return [$cloudId->getUser(), $this->cloudIdManager->removeProtocolFromUrl($cloudId->getRemote(), true)];
|
2017-08-31 16:47:02 -04:00
|
|
|
} catch (\InvalidArgumentException $e) {
|
|
|
|
|
throw new \InvalidArgumentException('Invalid Federated Cloud ID', 0, $e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|