MailPlugin.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Julius Härtl <jus@bitgrid.net>
  9. * @author Tobia De Koninck <tobia@ledfan.be>
  10. *
  11. * @license GNU AGPL version 3 or any later version
  12. *
  13. * This program is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License as
  15. * published by the Free Software Foundation, either version 3 of the
  16. * License, or (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25. *
  26. */
  27. namespace OC\Collaboration\Collaborators;
  28. use OC\KnownUser\KnownUserService;
  29. use OCP\Collaboration\Collaborators\ISearchPlugin;
  30. use OCP\Collaboration\Collaborators\ISearchResult;
  31. use OCP\Collaboration\Collaborators\SearchResultType;
  32. use OCP\Contacts\IManager;
  33. use OCP\Federation\ICloudId;
  34. use OCP\Federation\ICloudIdManager;
  35. use OCP\IConfig;
  36. use OCP\IGroupManager;
  37. use OCP\IUser;
  38. use OCP\IUserSession;
  39. use OCP\Mail\IMailer;
  40. use OCP\Share\IShare;
  41. class MailPlugin implements ISearchPlugin {
  42. protected bool $shareWithGroupOnly;
  43. protected bool $shareeEnumeration;
  44. protected bool $shareeEnumerationInGroupOnly;
  45. protected bool $shareeEnumerationPhone;
  46. protected bool $shareeEnumerationFullMatch;
  47. protected bool $shareeEnumerationFullMatchEmail;
  48. public function __construct(
  49. private IManager $contactsManager,
  50. private ICloudIdManager $cloudIdManager,
  51. private IConfig $config,
  52. private IGroupManager $groupManager,
  53. private KnownUserService $knownUserService,
  54. private IUserSession $userSession,
  55. private IMailer $mailer,
  56. ) {
  57. $this->shareeEnumeration = $this->config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes') === 'yes';
  58. $this->shareWithGroupOnly = $this->config->getAppValue('core', 'shareapi_only_share_with_group_members', 'no') === 'yes';
  59. $this->shareeEnumerationInGroupOnly = $this->shareeEnumeration && $this->config->getAppValue('core', 'shareapi_restrict_user_enumeration_to_group', 'no') === 'yes';
  60. $this->shareeEnumerationPhone = $this->shareeEnumeration && $this->config->getAppValue('core', 'shareapi_restrict_user_enumeration_to_phone', 'no') === 'yes';
  61. $this->shareeEnumerationFullMatch = $this->config->getAppValue('core', 'shareapi_restrict_user_enumeration_full_match', 'yes') === 'yes';
  62. $this->shareeEnumerationFullMatchEmail = $this->config->getAppValue('core', 'shareapi_restrict_user_enumeration_full_match_email', 'yes') === 'yes';
  63. }
  64. /**
  65. * {@inheritdoc}
  66. */
  67. public function search($search, $limit, $offset, ISearchResult $searchResult): bool {
  68. if ($this->shareeEnumerationFullMatch && !$this->shareeEnumerationFullMatchEmail) {
  69. return false;
  70. }
  71. // Extract the email address from "Foo Bar <foo.bar@example.tld>" and then search with "foo.bar@example.tld" instead
  72. $result = preg_match('/<([^@]+@.+)>$/', $search, $matches);
  73. if ($result && filter_var($matches[1], FILTER_VALIDATE_EMAIL)) {
  74. return $this->search($matches[1], $limit, $offset, $searchResult);
  75. }
  76. $currentUserId = $this->userSession->getUser()->getUID();
  77. $result = $userResults = ['wide' => [], 'exact' => []];
  78. $userType = new SearchResultType('users');
  79. $emailType = new SearchResultType('emails');
  80. // Search in contacts
  81. $addressBookContacts = $this->contactsManager->search(
  82. $search,
  83. ['EMAIL', 'FN'],
  84. [
  85. 'limit' => $limit,
  86. 'offset' => $offset,
  87. 'enumeration' => $this->shareeEnumeration,
  88. 'fullmatch' => $this->shareeEnumerationFullMatch,
  89. ]
  90. );
  91. $lowerSearch = strtolower($search);
  92. foreach ($addressBookContacts as $contact) {
  93. if (isset($contact['EMAIL'])) {
  94. $emailAddresses = $contact['EMAIL'];
  95. if (\is_string($emailAddresses)) {
  96. $emailAddresses = [$emailAddresses];
  97. }
  98. foreach ($emailAddresses as $type => $emailAddress) {
  99. $displayName = $emailAddress;
  100. $emailAddressType = null;
  101. if (\is_array($emailAddress)) {
  102. $emailAddressData = $emailAddress;
  103. $emailAddress = $emailAddressData['value'];
  104. $emailAddressType = $emailAddressData['type'];
  105. }
  106. if (isset($contact['FN'])) {
  107. $displayName = $contact['FN'] . ' (' . $emailAddress . ')';
  108. }
  109. $exactEmailMatch = strtolower($emailAddress) === $lowerSearch;
  110. if (isset($contact['isLocalSystemBook'])) {
  111. if ($this->shareWithGroupOnly) {
  112. /*
  113. * Check if the user may share with the user associated with the e-mail of the just found contact
  114. */
  115. $userGroups = $this->groupManager->getUserGroupIds($this->userSession->getUser());
  116. $found = false;
  117. foreach ($userGroups as $userGroup) {
  118. if ($this->groupManager->isInGroup($contact['UID'], $userGroup)) {
  119. $found = true;
  120. break;
  121. }
  122. }
  123. if (!$found) {
  124. continue;
  125. }
  126. }
  127. if ($exactEmailMatch && $this->shareeEnumerationFullMatch) {
  128. try {
  129. $cloud = $this->cloudIdManager->resolveCloudId($contact['CLOUD'][0]);
  130. } catch (\InvalidArgumentException $e) {
  131. continue;
  132. }
  133. if (!$this->isCurrentUser($cloud) && !$searchResult->hasResult($userType, $cloud->getUser())) {
  134. $singleResult = [[
  135. 'label' => $displayName,
  136. 'uuid' => $contact['UID'] ?? $emailAddress,
  137. 'name' => $contact['FN'] ?? $displayName,
  138. 'value' => [
  139. 'shareType' => IShare::TYPE_USER,
  140. 'shareWith' => $cloud->getUser(),
  141. ],
  142. 'shareWithDisplayNameUnique' => !empty($emailAddress) ? $emailAddress : $cloud->getUser()
  143. ]];
  144. $searchResult->addResultSet($userType, [], $singleResult);
  145. $searchResult->markExactIdMatch($emailType);
  146. }
  147. return false;
  148. }
  149. if ($this->shareeEnumeration) {
  150. try {
  151. $cloud = $this->cloudIdManager->resolveCloudId($contact['CLOUD'][0]);
  152. } catch (\InvalidArgumentException $e) {
  153. continue;
  154. }
  155. $addToWide = !($this->shareeEnumerationInGroupOnly || $this->shareeEnumerationPhone);
  156. if (!$addToWide && $this->shareeEnumerationPhone && $this->knownUserService->isKnownToUser($currentUserId, $contact['UID'])) {
  157. $addToWide = true;
  158. }
  159. if (!$addToWide && $this->shareeEnumerationInGroupOnly) {
  160. $addToWide = false;
  161. $userGroups = $this->groupManager->getUserGroupIds($this->userSession->getUser());
  162. foreach ($userGroups as $userGroup) {
  163. if ($this->groupManager->isInGroup($contact['UID'], $userGroup)) {
  164. $addToWide = true;
  165. break;
  166. }
  167. }
  168. }
  169. if ($addToWide && !$this->isCurrentUser($cloud) && !$searchResult->hasResult($userType, $cloud->getUser())) {
  170. $userResults['wide'][] = [
  171. 'label' => $displayName,
  172. 'uuid' => $contact['UID'] ?? $emailAddress,
  173. 'name' => $contact['FN'] ?? $displayName,
  174. 'value' => [
  175. 'shareType' => IShare::TYPE_USER,
  176. 'shareWith' => $cloud->getUser(),
  177. ],
  178. 'shareWithDisplayNameUnique' => !empty($emailAddress) ? $emailAddress : $cloud->getUser()
  179. ];
  180. continue;
  181. }
  182. }
  183. continue;
  184. }
  185. if ($exactEmailMatch
  186. || (isset($contact['FN']) && strtolower($contact['FN']) === $lowerSearch)) {
  187. if ($exactEmailMatch) {
  188. $searchResult->markExactIdMatch($emailType);
  189. }
  190. $result['exact'][] = [
  191. 'label' => $displayName,
  192. 'uuid' => $contact['UID'] ?? $emailAddress,
  193. 'name' => $contact['FN'] ?? $displayName,
  194. 'type' => $emailAddressType ?? '',
  195. 'value' => [
  196. 'shareType' => IShare::TYPE_EMAIL,
  197. 'shareWith' => $emailAddress,
  198. ],
  199. ];
  200. } else {
  201. $result['wide'][] = [
  202. 'label' => $displayName,
  203. 'uuid' => $contact['UID'] ?? $emailAddress,
  204. 'name' => $contact['FN'] ?? $displayName,
  205. 'type' => $emailAddressType ?? '',
  206. 'value' => [
  207. 'shareType' => IShare::TYPE_EMAIL,
  208. 'shareWith' => $emailAddress,
  209. ],
  210. ];
  211. }
  212. }
  213. }
  214. }
  215. $reachedEnd = true;
  216. if ($this->shareeEnumeration) {
  217. $reachedEnd = (count($result['wide']) < $offset + $limit) &&
  218. (count($userResults['wide']) < $offset + $limit);
  219. $result['wide'] = array_slice($result['wide'], $offset, $limit);
  220. $userResults['wide'] = array_slice($userResults['wide'], $offset, $limit);
  221. }
  222. if (!$searchResult->hasExactIdMatch($emailType) && $this->mailer->validateMailAddress($search)) {
  223. $result['exact'][] = [
  224. 'label' => $search,
  225. 'uuid' => $search,
  226. 'value' => [
  227. 'shareType' => IShare::TYPE_EMAIL,
  228. 'shareWith' => $search,
  229. ],
  230. ];
  231. }
  232. if (!empty($userResults['wide'])) {
  233. $searchResult->addResultSet($userType, $userResults['wide'], []);
  234. }
  235. $searchResult->addResultSet($emailType, $result['wide'], $result['exact']);
  236. return !$reachedEnd;
  237. }
  238. public function isCurrentUser(ICloudId $cloud): bool {
  239. $currentUser = $this->userSession->getUser();
  240. return $currentUser instanceof IUser && $currentUser->getUID() === $cloud->getUser();
  241. }
  242. }