LookupPlugin.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 Arthur Schiwon <blizzz@arthur-schiwon.de>
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Bjoern Schiessle <bjoern@schiessle.org>
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author J0WI <J0WI@users.noreply.github.com>
  9. * @author Joas Schilling <coding@schilljs.com>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. *
  12. * @license GNU AGPL version 3 or any later version
  13. *
  14. * This program is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License as
  16. * published by the Free Software Foundation, either version 3 of the
  17. * License, or (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  26. *
  27. */
  28. namespace OC\Collaboration\Collaborators;
  29. use OCP\Collaboration\Collaborators\ISearchPlugin;
  30. use OCP\Collaboration\Collaborators\ISearchResult;
  31. use OCP\Collaboration\Collaborators\SearchResultType;
  32. use OCP\Federation\ICloudIdManager;
  33. use OCP\Http\Client\IClientService;
  34. use OCP\IConfig;
  35. use OCP\IUserSession;
  36. use OCP\Share\IShare;
  37. use Psr\Log\LoggerInterface;
  38. class LookupPlugin implements ISearchPlugin {
  39. /** @var string remote part of the current user's cloud id */
  40. private string $currentUserRemote;
  41. public function __construct(
  42. private IConfig $config,
  43. private IClientService $clientService,
  44. IUserSession $userSession,
  45. private ICloudIdManager $cloudIdManager,
  46. private LoggerInterface $logger,
  47. ) {
  48. $currentUserCloudId = $userSession->getUser()->getCloudId();
  49. $this->currentUserRemote = $cloudIdManager->resolveCloudId($currentUserCloudId)->getRemote();
  50. }
  51. public function search($search, $limit, $offset, ISearchResult $searchResult): bool {
  52. $isGlobalScaleEnabled = $this->config->getSystemValueBool('gs.enabled', false);
  53. $isLookupServerEnabled = $this->config->getAppValue('files_sharing', 'lookupServerEnabled', 'yes') === 'yes';
  54. $hasInternetConnection = $this->config->getSystemValueBool('has_internet_connection', true);
  55. // if case of Global Scale we always search the lookup server
  56. if (!$isGlobalScaleEnabled && (!$isLookupServerEnabled || !$hasInternetConnection)) {
  57. return false;
  58. }
  59. $lookupServerUrl = $this->config->getSystemValueString('lookup_server', 'https://lookup.nextcloud.com');
  60. if (empty($lookupServerUrl)) {
  61. return false;
  62. }
  63. $lookupServerUrl = rtrim($lookupServerUrl, '/');
  64. $result = [];
  65. try {
  66. $client = $this->clientService->newClient();
  67. $response = $client->get(
  68. $lookupServerUrl . '/users?search=' . urlencode($search),
  69. [
  70. 'timeout' => 10,
  71. 'connect_timeout' => 3,
  72. ]
  73. );
  74. $body = json_decode($response->getBody(), true);
  75. foreach ($body as $lookup) {
  76. try {
  77. $remote = $this->cloudIdManager->resolveCloudId($lookup['federationId'])->getRemote();
  78. } catch (\Exception $e) {
  79. $this->logger->error('Can not parse federated cloud ID "' . $lookup['federationId'] . '"', [
  80. 'exception' => $e,
  81. ]);
  82. continue;
  83. }
  84. if ($this->currentUserRemote === $remote) {
  85. continue;
  86. }
  87. $name = $lookup['name']['value'] ?? '';
  88. $label = empty($name) ? $lookup['federationId'] : $name . ' (' . $lookup['federationId'] . ')';
  89. $result[] = [
  90. 'label' => $label,
  91. 'value' => [
  92. 'shareType' => IShare::TYPE_REMOTE,
  93. 'globalScale' => $isGlobalScaleEnabled,
  94. 'shareWith' => $lookup['federationId'],
  95. ],
  96. 'extra' => $lookup,
  97. ];
  98. }
  99. } catch (\Exception $e) {
  100. }
  101. $type = new SearchResultType('lookup');
  102. $searchResult->addResultSet($type, $result, []);
  103. return false;
  104. }
  105. }