LookupPlugin.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 IConfig */
  40. private $config;
  41. /** @var IClientService */
  42. private $clientService;
  43. /** @var string remote part of the current user's cloud id */
  44. private $currentUserRemote;
  45. /** @var ICloudIdManager */
  46. private $cloudIdManager;
  47. /** @var LoggerInterface */
  48. private $logger;
  49. public function __construct(IConfig $config,
  50. IClientService $clientService,
  51. IUserSession $userSession,
  52. ICloudIdManager $cloudIdManager,
  53. LoggerInterface $logger) {
  54. $this->config = $config;
  55. $this->clientService = $clientService;
  56. $this->cloudIdManager = $cloudIdManager;
  57. $currentUserCloudId = $userSession->getUser()->getCloudId();
  58. $this->currentUserRemote = $cloudIdManager->resolveCloudId($currentUserCloudId)->getRemote();
  59. $this->logger = $logger;
  60. }
  61. public function search($search, $limit, $offset, ISearchResult $searchResult) {
  62. $isGlobalScaleEnabled = $this->config->getSystemValueBool('gs.enabled', false);
  63. $isLookupServerEnabled = $this->config->getAppValue('files_sharing', 'lookupServerEnabled', 'yes') === 'yes';
  64. $hasInternetConnection = $this->config->getSystemValueBool('has_internet_connection', true);
  65. // if case of Global Scale we always search the lookup server
  66. if (!$isGlobalScaleEnabled && (!$isLookupServerEnabled || !$hasInternetConnection)) {
  67. return false;
  68. }
  69. $lookupServerUrl = $this->config->getSystemValueString('lookup_server', 'https://lookup.nextcloud.com');
  70. if (empty($lookupServerUrl)) {
  71. return false;
  72. }
  73. $lookupServerUrl = rtrim($lookupServerUrl, '/');
  74. $result = [];
  75. try {
  76. $client = $this->clientService->newClient();
  77. $response = $client->get(
  78. $lookupServerUrl . '/users?search=' . urlencode($search),
  79. [
  80. 'timeout' => 10,
  81. 'connect_timeout' => 3,
  82. ]
  83. );
  84. $body = json_decode($response->getBody(), true);
  85. foreach ($body as $lookup) {
  86. try {
  87. $remote = $this->cloudIdManager->resolveCloudId($lookup['federationId'])->getRemote();
  88. } catch (\Exception $e) {
  89. $this->logger->error('Can not parse federated cloud ID "' . $lookup['federationId'] . '"', [
  90. 'exception' => $e,
  91. ]);
  92. continue;
  93. }
  94. if ($this->currentUserRemote === $remote) {
  95. continue;
  96. }
  97. $name = isset($lookup['name']['value']) ? $lookup['name']['value'] : '';
  98. $label = empty($name) ? $lookup['federationId'] : $name . ' (' . $lookup['federationId'] . ')';
  99. $result[] = [
  100. 'label' => $label,
  101. 'value' => [
  102. 'shareType' => IShare::TYPE_REMOTE,
  103. 'globalScale' => $isGlobalScaleEnabled,
  104. 'shareWith' => $lookup['federationId'],
  105. ],
  106. 'extra' => $lookup,
  107. ];
  108. }
  109. } catch (\Exception $e) {
  110. }
  111. $type = new SearchResultType('lookup');
  112. $searchResult->addResultSet($type, $result, []);
  113. return false;
  114. }
  115. }