MailPlugin.php 10.0 KB

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