RemotePlugin.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 Arthur Schiwon <blizzz@arthur-schiwon.de>
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  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. namespace OC\Collaboration\Collaborators;
  24. use OCP\Collaboration\Collaborators\ISearchPlugin;
  25. use OCP\Collaboration\Collaborators\ISearchResult;
  26. use OCP\Collaboration\Collaborators\SearchResultType;
  27. use OCP\Contacts\IManager;
  28. use OCP\Federation\ICloudIdManager;
  29. use OCP\IConfig;
  30. use OCP\Share;
  31. class RemotePlugin implements ISearchPlugin {
  32. protected $shareeEnumeration;
  33. /** @var IManager */
  34. private $contactsManager;
  35. /** @var ICloudIdManager */
  36. private $cloudIdManager;
  37. /** @var IConfig */
  38. private $config;
  39. public function __construct(IManager $contactsManager, ICloudIdManager $cloudIdManager, IConfig $config) {
  40. $this->contactsManager = $contactsManager;
  41. $this->cloudIdManager = $cloudIdManager;
  42. $this->config = $config;
  43. $this->shareeEnumeration = $this->config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes') === 'yes';
  44. }
  45. public function search($search, $limit, $offset, ISearchResult $searchResult) {
  46. $result = ['wide' => [], 'exact' => []];
  47. $resultType = new SearchResultType('remotes');
  48. // Search in contacts
  49. //@todo Pagination missing
  50. $addressBookContacts = $this->contactsManager->search($search, ['CLOUD', 'FN']);
  51. foreach ($addressBookContacts as $contact) {
  52. if (isset($contact['isLocalSystemBook'])) {
  53. continue;
  54. }
  55. if (isset($contact['CLOUD'])) {
  56. $cloudIds = $contact['CLOUD'];
  57. if (!is_array($cloudIds)) {
  58. $cloudIds = [$cloudIds];
  59. }
  60. $lowerSearch = strtolower($search);
  61. foreach ($cloudIds as $cloudId) {
  62. try {
  63. list(, $serverUrl) = $this->splitUserRemote($cloudId);
  64. } catch (\InvalidArgumentException $e) {
  65. continue;
  66. }
  67. if (strtolower($contact['FN']) === $lowerSearch || strtolower($cloudId) === $lowerSearch) {
  68. if (strtolower($cloudId) === $lowerSearch) {
  69. $searchResult->markExactIdMatch($resultType);
  70. }
  71. $result['exact'][] = [
  72. 'label' => $contact['FN'] . " ($cloudId)",
  73. 'value' => [
  74. 'shareType' => Share::SHARE_TYPE_REMOTE,
  75. 'shareWith' => $cloudId,
  76. 'server' => $serverUrl,
  77. ],
  78. ];
  79. } else {
  80. $result['wide'][] = [
  81. 'label' => $contact['FN'] . " ($cloudId)",
  82. 'value' => [
  83. 'shareType' => Share::SHARE_TYPE_REMOTE,
  84. 'shareWith' => $cloudId,
  85. 'server' => $serverUrl,
  86. ],
  87. ];
  88. }
  89. }
  90. }
  91. }
  92. if (!$this->shareeEnumeration) {
  93. $result['wide'] = [];
  94. } else {
  95. $result['wide'] = array_slice($result['wide'], $offset, $limit);
  96. }
  97. if (!$searchResult->hasExactIdMatch($resultType) && $this->cloudIdManager->isValidCloudId($search) && $offset === 0) {
  98. $result['exact'][] = [
  99. 'label' => $search,
  100. 'value' => [
  101. 'shareType' => Share::SHARE_TYPE_REMOTE,
  102. 'shareWith' => $search,
  103. ],
  104. ];
  105. }
  106. $searchResult->addResultSet($resultType, $result['wide'], $result['exact']);
  107. return true;
  108. }
  109. /**
  110. * split user and remote from federated cloud id
  111. *
  112. * @param string $address federated share address
  113. * @return array [user, remoteURL]
  114. * @throws \InvalidArgumentException
  115. */
  116. public function splitUserRemote($address) {
  117. try {
  118. $cloudId = $this->cloudIdManager->resolveCloudId($address);
  119. return [$cloudId->getUser(), $cloudId->getRemote()];
  120. } catch (\InvalidArgumentException $e) {
  121. throw new \InvalidArgumentException('Invalid Federated Cloud ID', 0, $e);
  122. }
  123. }
  124. }