MailPlugin.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. private mixed $shareWithGroupOnlyExcludeGroupsList = [],
  57. ) {
  58. $this->shareeEnumeration = $this->config->getAppValue('core', 'shareapi_allow_share_dialog_user_enumeration', 'yes') === 'yes';
  59. $this->shareWithGroupOnly = $this->config->getAppValue('core', 'shareapi_only_share_with_group_members', 'no') === 'yes';
  60. $this->shareeEnumerationInGroupOnly = $this->shareeEnumeration && $this->config->getAppValue('core', 'shareapi_restrict_user_enumeration_to_group', 'no') === 'yes';
  61. $this->shareeEnumerationPhone = $this->shareeEnumeration && $this->config->getAppValue('core', 'shareapi_restrict_user_enumeration_to_phone', 'no') === 'yes';
  62. $this->shareeEnumerationFullMatch = $this->config->getAppValue('core', 'shareapi_restrict_user_enumeration_full_match', 'yes') === 'yes';
  63. $this->shareeEnumerationFullMatchEmail = $this->config->getAppValue('core', 'shareapi_restrict_user_enumeration_full_match_email', 'yes') === 'yes';
  64. if ($this->shareWithGroupOnly) {
  65. $this->shareWithGroupOnlyExcludeGroupsList = json_decode($this->config->getAppValue('core', 'shareapi_only_share_with_group_members_exclude_group_list', ''), true) ?? [];
  66. }
  67. }
  68. /**
  69. * {@inheritdoc}
  70. */
  71. public function search($search, $limit, $offset, ISearchResult $searchResult): bool {
  72. if ($this->shareeEnumerationFullMatch && !$this->shareeEnumerationFullMatchEmail) {
  73. return false;
  74. }
  75. // Extract the email address from "Foo Bar <foo.bar@example.tld>" and then search with "foo.bar@example.tld" instead
  76. $result = preg_match('/<([^@]+@.+)>$/', $search, $matches);
  77. if ($result && filter_var($matches[1], FILTER_VALIDATE_EMAIL)) {
  78. return $this->search($matches[1], $limit, $offset, $searchResult);
  79. }
  80. $currentUserId = $this->userSession->getUser()->getUID();
  81. $result = $userResults = ['wide' => [], 'exact' => []];
  82. $userType = new SearchResultType('users');
  83. $emailType = new SearchResultType('emails');
  84. // Search in contacts
  85. $addressBookContacts = $this->contactsManager->search(
  86. $search,
  87. ['EMAIL', 'FN'],
  88. [
  89. 'limit' => $limit,
  90. 'offset' => $offset,
  91. 'enumeration' => $this->shareeEnumeration,
  92. 'fullmatch' => $this->shareeEnumerationFullMatch,
  93. ]
  94. );
  95. $lowerSearch = strtolower($search);
  96. foreach ($addressBookContacts as $contact) {
  97. if (isset($contact['EMAIL'])) {
  98. $emailAddresses = $contact['EMAIL'];
  99. if (\is_string($emailAddresses)) {
  100. $emailAddresses = [$emailAddresses];
  101. }
  102. foreach ($emailAddresses as $type => $emailAddress) {
  103. $displayName = $emailAddress;
  104. $emailAddressType = null;
  105. if (\is_array($emailAddress)) {
  106. $emailAddressData = $emailAddress;
  107. $emailAddress = $emailAddressData['value'];
  108. $emailAddressType = $emailAddressData['type'];
  109. }
  110. if (isset($contact['FN'])) {
  111. $displayName = $contact['FN'] . ' (' . $emailAddress . ')';
  112. }
  113. $exactEmailMatch = strtolower($emailAddress) === $lowerSearch;
  114. if (isset($contact['isLocalSystemBook'])) {
  115. if ($this->shareWithGroupOnly) {
  116. /*
  117. * Check if the user may share with the user associated with the e-mail of the just found contact
  118. */
  119. $userGroups = $this->groupManager->getUserGroupIds($this->userSession->getUser());
  120. // ShareWithGroupOnly filtering
  121. $userGroups = array_diff($userGroups, $this->shareWithGroupOnlyExcludeGroupsList);
  122. $found = false;
  123. foreach ($userGroups as $userGroup) {
  124. if ($this->groupManager->isInGroup($contact['UID'], $userGroup)) {
  125. $found = true;
  126. break;
  127. }
  128. }
  129. if (!$found) {
  130. continue;
  131. }
  132. }
  133. if ($exactEmailMatch && $this->shareeEnumerationFullMatch) {
  134. try {
  135. $cloud = $this->cloudIdManager->resolveCloudId($contact['CLOUD'][0] ?? '');
  136. } catch (\InvalidArgumentException $e) {
  137. continue;
  138. }
  139. if (!$this->isCurrentUser($cloud) && !$searchResult->hasResult($userType, $cloud->getUser())) {
  140. $singleResult = [[
  141. 'label' => $displayName,
  142. 'uuid' => $contact['UID'] ?? $emailAddress,
  143. 'name' => $contact['FN'] ?? $displayName,
  144. 'value' => [
  145. 'shareType' => IShare::TYPE_USER,
  146. 'shareWith' => $cloud->getUser(),
  147. ],
  148. 'shareWithDisplayNameUnique' => !empty($emailAddress) ? $emailAddress : $cloud->getUser()
  149. ]];
  150. $searchResult->addResultSet($userType, [], $singleResult);
  151. $searchResult->markExactIdMatch($emailType);
  152. }
  153. return false;
  154. }
  155. if ($this->shareeEnumeration) {
  156. try {
  157. $cloud = $this->cloudIdManager->resolveCloudId($contact['CLOUD'][0] ?? '');
  158. } catch (\InvalidArgumentException $e) {
  159. continue;
  160. }
  161. $addToWide = !($this->shareeEnumerationInGroupOnly || $this->shareeEnumerationPhone);
  162. if (!$addToWide && $this->shareeEnumerationPhone && $this->knownUserService->isKnownToUser($currentUserId, $contact['UID'])) {
  163. $addToWide = true;
  164. }
  165. if (!$addToWide && $this->shareeEnumerationInGroupOnly) {
  166. $addToWide = false;
  167. $userGroups = $this->groupManager->getUserGroupIds($this->userSession->getUser());
  168. foreach ($userGroups as $userGroup) {
  169. if ($this->groupManager->isInGroup($contact['UID'], $userGroup)) {
  170. $addToWide = true;
  171. break;
  172. }
  173. }
  174. }
  175. if ($addToWide && !$this->isCurrentUser($cloud) && !$searchResult->hasResult($userType, $cloud->getUser())) {
  176. $userResults['wide'][] = [
  177. 'label' => $displayName,
  178. 'uuid' => $contact['UID'] ?? $emailAddress,
  179. 'name' => $contact['FN'] ?? $displayName,
  180. 'value' => [
  181. 'shareType' => IShare::TYPE_USER,
  182. 'shareWith' => $cloud->getUser(),
  183. ],
  184. 'shareWithDisplayNameUnique' => !empty($emailAddress) ? $emailAddress : $cloud->getUser()
  185. ];
  186. continue;
  187. }
  188. }
  189. continue;
  190. }
  191. if ($exactEmailMatch
  192. || (isset($contact['FN']) && strtolower($contact['FN']) === $lowerSearch)) {
  193. if ($exactEmailMatch) {
  194. $searchResult->markExactIdMatch($emailType);
  195. }
  196. $result['exact'][] = [
  197. 'label' => $displayName,
  198. 'uuid' => $contact['UID'] ?? $emailAddress,
  199. 'name' => $contact['FN'] ?? $displayName,
  200. 'type' => $emailAddressType ?? '',
  201. 'value' => [
  202. 'shareType' => IShare::TYPE_EMAIL,
  203. 'shareWith' => $emailAddress,
  204. ],
  205. ];
  206. } else {
  207. $result['wide'][] = [
  208. 'label' => $displayName,
  209. 'uuid' => $contact['UID'] ?? $emailAddress,
  210. 'name' => $contact['FN'] ?? $displayName,
  211. 'type' => $emailAddressType ?? '',
  212. 'value' => [
  213. 'shareType' => IShare::TYPE_EMAIL,
  214. 'shareWith' => $emailAddress,
  215. ],
  216. ];
  217. }
  218. }
  219. }
  220. }
  221. $reachedEnd = true;
  222. if ($this->shareeEnumeration) {
  223. $reachedEnd = (count($result['wide']) < $offset + $limit) &&
  224. (count($userResults['wide']) < $offset + $limit);
  225. $result['wide'] = array_slice($result['wide'], $offset, $limit);
  226. $userResults['wide'] = array_slice($userResults['wide'], $offset, $limit);
  227. }
  228. if (!$searchResult->hasExactIdMatch($emailType) && $this->mailer->validateMailAddress($search)) {
  229. $result['exact'][] = [
  230. 'label' => $search,
  231. 'uuid' => $search,
  232. 'value' => [
  233. 'shareType' => IShare::TYPE_EMAIL,
  234. 'shareWith' => $search,
  235. ],
  236. ];
  237. }
  238. if (!empty($userResults['wide'])) {
  239. $searchResult->addResultSet($userType, $userResults['wide'], []);
  240. }
  241. $searchResult->addResultSet($emailType, $result['wide'], $result['exact']);
  242. return !$reachedEnd;
  243. }
  244. public function isCurrentUser(ICloudId $cloud): bool {
  245. $currentUser = $this->userSession->getUser();
  246. return $currentUser instanceof IUser && $currentUser->getUID() === $cloud->getUser();
  247. }
  248. }