1
0

ShareByMailProvider.php 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\ShareByMail;
  7. use OC\Share20\DefaultShareProvider;
  8. use OC\Share20\Exception\InvalidShare;
  9. use OC\Share20\Share;
  10. use OC\User\NoUserException;
  11. use OCA\ShareByMail\Settings\SettingsManager;
  12. use OCP\Activity\IManager;
  13. use OCP\DB\QueryBuilder\IQueryBuilder;
  14. use OCP\Defaults;
  15. use OCP\EventDispatcher\IEventDispatcher;
  16. use OCP\Files\Folder;
  17. use OCP\Files\IRootFolder;
  18. use OCP\Files\Node;
  19. use OCP\HintException;
  20. use OCP\IConfig;
  21. use OCP\IDBConnection;
  22. use OCP\IL10N;
  23. use OCP\IURLGenerator;
  24. use OCP\IUser;
  25. use OCP\IUserManager;
  26. use OCP\Mail\IMailer;
  27. use OCP\Security\Events\GenerateSecurePasswordEvent;
  28. use OCP\Security\IHasher;
  29. use OCP\Security\ISecureRandom;
  30. use OCP\Security\PasswordContext;
  31. use OCP\Share\Exceptions\GenericShareException;
  32. use OCP\Share\Exceptions\ShareNotFound;
  33. use OCP\Share\IAttributes;
  34. use OCP\Share\IManager as IShareManager;
  35. use OCP\Share\IShare;
  36. use OCP\Share\IShareProviderWithNotification;
  37. use OCP\Util;
  38. use Psr\Log\LoggerInterface;
  39. /**
  40. * Class ShareByMail
  41. *
  42. * @package OCA\ShareByMail
  43. */
  44. class ShareByMailProvider extends DefaultShareProvider implements IShareProviderWithNotification {
  45. /**
  46. * Return the identifier of this provider.
  47. *
  48. * @return string Containing only [a-zA-Z0-9]
  49. */
  50. public function identifier(): string {
  51. return 'ocMailShare';
  52. }
  53. public function __construct(
  54. private IConfig $config,
  55. private IDBConnection $dbConnection,
  56. private ISecureRandom $secureRandom,
  57. private IUserManager $userManager,
  58. private IRootFolder $rootFolder,
  59. private IL10N $l,
  60. private LoggerInterface $logger,
  61. private IMailer $mailer,
  62. private IURLGenerator $urlGenerator,
  63. private IManager $activityManager,
  64. private SettingsManager $settingsManager,
  65. private Defaults $defaults,
  66. private IHasher $hasher,
  67. private IEventDispatcher $eventDispatcher,
  68. private IShareManager $shareManager,
  69. ) {
  70. }
  71. /**
  72. * Share a path
  73. *
  74. * @throws ShareNotFound
  75. * @throws \Exception
  76. */
  77. public function create(IShare $share): IShare {
  78. $shareWith = $share->getSharedWith();
  79. // Check if file is not already shared with the given email,
  80. // if we have an email at all.
  81. $alreadyShared = $this->getSharedWith($shareWith, IShare::TYPE_EMAIL, $share->getNode(), 1, 0);
  82. if ($shareWith !== '' && !empty($alreadyShared)) {
  83. $message = 'Sharing %1$s failed, because this item is already shared with the account %2$s';
  84. $message_t = $this->l->t('Sharing %1$s failed, because this item is already shared with the account %2$s', [$share->getNode()->getName(), $shareWith]);
  85. $this->logger->debug(sprintf($message, $share->getNode()->getName(), $shareWith), ['app' => 'Federated File Sharing']);
  86. throw new \Exception($message_t);
  87. }
  88. // if the admin enforces a password for all mail shares we create a
  89. // random password and send it to the recipient
  90. $password = $share->getPassword() ?: '';
  91. $passwordEnforced = $this->shareManager->shareApiLinkEnforcePassword();
  92. if ($passwordEnforced && empty($password)) {
  93. $password = $this->autoGeneratePassword($share);
  94. }
  95. if (!empty($password)) {
  96. $share->setPassword($this->hasher->hash($password));
  97. }
  98. $shareId = $this->createMailShare($share);
  99. $this->createShareActivity($share);
  100. $data = $this->getRawShare($shareId);
  101. // Temporary set the clear password again to send it by mail
  102. // This need to be done after the share was created in the database
  103. // as the password is hashed in between.
  104. if (!empty($password)) {
  105. $data['password'] = $password;
  106. }
  107. return $this->createShareObject($data);
  108. }
  109. /**
  110. * auto generate password in case of password enforcement on mail shares
  111. *
  112. * @throws \Exception
  113. */
  114. protected function autoGeneratePassword(IShare $share): string {
  115. $initiatorUser = $this->userManager->get($share->getSharedBy());
  116. $initiatorEMailAddress = ($initiatorUser instanceof IUser) ? $initiatorUser->getEMailAddress() : null;
  117. $allowPasswordByMail = $this->settingsManager->sendPasswordByMail();
  118. if ($initiatorEMailAddress === null && !$allowPasswordByMail) {
  119. throw new \Exception(
  120. $this->l->t('We cannot send you the auto-generated password. Please set a valid email address in your personal settings and try again.')
  121. );
  122. }
  123. $passwordEvent = new GenerateSecurePasswordEvent(PasswordContext::SHARING);
  124. $this->eventDispatcher->dispatchTyped($passwordEvent);
  125. $password = $passwordEvent->getPassword();
  126. if ($password === null) {
  127. $password = $this->secureRandom->generate(8, ISecureRandom::CHAR_HUMAN_READABLE);
  128. }
  129. return $password;
  130. }
  131. /**
  132. * create activity if a file/folder was shared by mail
  133. */
  134. protected function createShareActivity(IShare $share, string $type = 'share'): void {
  135. $userFolder = $this->rootFolder->getUserFolder($share->getSharedBy());
  136. $this->publishActivity(
  137. $type === 'share' ? Activity::SUBJECT_SHARED_EMAIL_SELF : Activity::SUBJECT_UNSHARED_EMAIL_SELF,
  138. [$userFolder->getRelativePath($share->getNode()->getPath()), $share->getSharedWith()],
  139. $share->getSharedBy(),
  140. $share->getNode()->getId(),
  141. (string)$userFolder->getRelativePath($share->getNode()->getPath())
  142. );
  143. if ($share->getShareOwner() !== $share->getSharedBy()) {
  144. $ownerFolder = $this->rootFolder->getUserFolder($share->getShareOwner());
  145. $fileId = $share->getNode()->getId();
  146. $nodes = $ownerFolder->getById($fileId);
  147. $ownerPath = $nodes[0]->getPath();
  148. $this->publishActivity(
  149. $type === 'share' ? Activity::SUBJECT_SHARED_EMAIL_BY : Activity::SUBJECT_UNSHARED_EMAIL_BY,
  150. [$ownerFolder->getRelativePath($ownerPath), $share->getSharedWith(), $share->getSharedBy()],
  151. $share->getShareOwner(),
  152. $fileId,
  153. (string)$ownerFolder->getRelativePath($ownerPath)
  154. );
  155. }
  156. }
  157. /**
  158. * create activity if a file/folder was shared by mail
  159. */
  160. protected function createPasswordSendActivity(IShare $share, string $sharedWith, bool $sendToSelf): void {
  161. $userFolder = $this->rootFolder->getUserFolder($share->getSharedBy());
  162. if ($sendToSelf) {
  163. $this->publishActivity(
  164. Activity::SUBJECT_SHARED_EMAIL_PASSWORD_SEND_SELF,
  165. [$userFolder->getRelativePath($share->getNode()->getPath())],
  166. $share->getSharedBy(),
  167. $share->getNode()->getId(),
  168. (string)$userFolder->getRelativePath($share->getNode()->getPath())
  169. );
  170. } else {
  171. $this->publishActivity(
  172. Activity::SUBJECT_SHARED_EMAIL_PASSWORD_SEND,
  173. [$userFolder->getRelativePath($share->getNode()->getPath()), $sharedWith],
  174. $share->getSharedBy(),
  175. $share->getNode()->getId(),
  176. (string)$userFolder->getRelativePath($share->getNode()->getPath())
  177. );
  178. }
  179. }
  180. /**
  181. * publish activity if a file/folder was shared by mail
  182. */
  183. protected function publishActivity(string $subject, array $parameters, string $affectedUser, int $fileId, string $filePath): void {
  184. $event = $this->activityManager->generateEvent();
  185. $event->setApp('sharebymail')
  186. ->setType('shared')
  187. ->setSubject($subject, $parameters)
  188. ->setAffectedUser($affectedUser)
  189. ->setObject('files', $fileId, $filePath);
  190. $this->activityManager->publish($event);
  191. }
  192. /**
  193. * @throws \Exception
  194. */
  195. protected function createMailShare(IShare $share): int {
  196. $share->setToken($this->generateToken());
  197. return $this->addShareToDB(
  198. $share->getNodeId(),
  199. $share->getNodeType(),
  200. $share->getSharedWith(),
  201. $share->getSharedBy(),
  202. $share->getShareOwner(),
  203. $share->getPermissions(),
  204. $share->getToken(),
  205. $share->getPassword(),
  206. $share->getPasswordExpirationTime(),
  207. $share->getSendPasswordByTalk(),
  208. $share->getHideDownload(),
  209. $share->getLabel(),
  210. $share->getExpirationDate(),
  211. $share->getNote(),
  212. $share->getAttributes(),
  213. $share->getMailSend(),
  214. );
  215. }
  216. /**
  217. * @inheritDoc
  218. */
  219. public function sendMailNotification(IShare $share): bool {
  220. $shareId = $share->getId();
  221. $emails = $this->getSharedWithEmails($share);
  222. $validEmails = array_filter($emails, function (string $email) {
  223. return $this->mailer->validateMailAddress($email);
  224. });
  225. if (count($validEmails) === 0) {
  226. $this->removeShareFromTable((int)$shareId);
  227. $e = new HintException('Failed to send share by mail. Could not find a valid email address: ' . join(', ', $emails),
  228. $this->l->t('Failed to send share by email. Got an invalid email address'));
  229. $this->logger->error('Failed to send share by mail. Could not find a valid email address ' . join(', ', $emails), [
  230. 'app' => 'sharebymail',
  231. 'exception' => $e,
  232. ]);
  233. }
  234. try {
  235. $this->sendEmail($share, $validEmails);
  236. // If we have a password set, we send it to the recipient
  237. if ($share->getPassword() !== null) {
  238. // If share-by-talk password is enabled, we do not send the notification
  239. // to the recipient. They will have to request it to the owner after opening the link.
  240. // Secondly, if the password expiration is disabled, we send the notification to the recipient
  241. // Lastly, if the mail to recipient failed, we send the password to the owner as a fallback.
  242. // If a password expires, the recipient will still be able to request a new one via talk.
  243. $passwordExpire = $this->config->getSystemValue('sharing.enable_mail_link_password_expiration', false);
  244. $passwordEnforced = $this->shareManager->shareApiLinkEnforcePassword();
  245. if ($passwordExpire === false || $share->getSendPasswordByTalk()) {
  246. $send = $this->sendPassword($share, $share->getPassword(), $validEmails);
  247. if ($passwordEnforced && $send === false) {
  248. $this->sendPasswordToOwner($share, $share->getPassword());
  249. }
  250. }
  251. }
  252. return true;
  253. } catch (HintException $hintException) {
  254. $this->logger->error('Failed to send share by mail.', [
  255. 'app' => 'sharebymail',
  256. 'exception' => $hintException,
  257. ]);
  258. $this->removeShareFromTable((int)$shareId);
  259. throw $hintException;
  260. } catch (\Exception $e) {
  261. $this->logger->error('Failed to send share by mail.', [
  262. 'app' => 'sharebymail',
  263. 'exception' => $e,
  264. ]);
  265. $this->removeShareFromTable((int)$shareId);
  266. throw new HintException(
  267. 'Failed to send share by mail',
  268. $this->l->t('Failed to send share by email'),
  269. 0,
  270. $e,
  271. );
  272. }
  273. return false;
  274. }
  275. /**
  276. * @param IShare $share The share to send the email for
  277. * @param array $emails The email addresses to send the email to
  278. */
  279. protected function sendEmail(IShare $share, array $emails): void {
  280. $link = $this->urlGenerator->linkToRouteAbsolute('files_sharing.sharecontroller.showShare', [
  281. 'token' => $share->getToken()
  282. ]);
  283. $expiration = $share->getExpirationDate();
  284. $filename = $share->getNode()->getName();
  285. $initiator = $share->getSharedBy();
  286. $note = $share->getNote();
  287. $shareWith = $share->getSharedWith();
  288. $initiatorUser = $this->userManager->get($initiator);
  289. $initiatorDisplayName = ($initiatorUser instanceof IUser) ? $initiatorUser->getDisplayName() : $initiator;
  290. $message = $this->mailer->createMessage();
  291. $emailTemplate = $this->mailer->createEMailTemplate('sharebymail.RecipientNotification', [
  292. 'filename' => $filename,
  293. 'link' => $link,
  294. 'initiator' => $initiatorDisplayName,
  295. 'expiration' => $expiration,
  296. 'shareWith' => $shareWith,
  297. 'note' => $note
  298. ]);
  299. $emailTemplate->setSubject($this->l->t('%1$s shared %2$s with you', [$initiatorDisplayName, $filename]));
  300. $emailTemplate->addHeader();
  301. $emailTemplate->addHeading($this->l->t('%1$s shared %2$s with you', [$initiatorDisplayName, $filename]), false);
  302. $text = $this->l->t('%1$s shared %2$s with you.', [$initiatorDisplayName, $filename]);
  303. if ($note !== '') {
  304. $emailTemplate->addBodyListItem(
  305. htmlspecialchars($note),
  306. $this->l->t('Note:'),
  307. $this->getAbsoluteImagePath('caldav/description.png'),
  308. $note
  309. );
  310. }
  311. if ($expiration !== null) {
  312. $dateString = (string)$this->l->l('date', $expiration, ['width' => 'medium']);
  313. $emailTemplate->addBodyListItem(
  314. $this->l->t('This share is valid until %s at midnight', [$dateString]),
  315. $this->l->t('Expiration:'),
  316. $this->getAbsoluteImagePath('caldav/time.png'),
  317. );
  318. }
  319. $emailTemplate->addBodyText(
  320. htmlspecialchars($text . ' ' . $this->l->t('Click the button below to open it.')),
  321. $text
  322. );
  323. $emailTemplate->addBodyButton(
  324. $this->l->t('Open %s', [$filename]),
  325. $link
  326. );
  327. // If multiple recipients are given, we send the mail to all of them
  328. if (count($emails) > 1) {
  329. // We do not want to expose the email addresses of the other recipients
  330. $message->setBcc($emails);
  331. } else {
  332. $message->setTo($emails);
  333. }
  334. // The "From" contains the sharers name
  335. $instanceName = $this->defaults->getName();
  336. $senderName = $instanceName;
  337. if ($this->settingsManager->replyToInitiator()) {
  338. $senderName = $this->l->t(
  339. '%1$s via %2$s',
  340. [
  341. $initiatorDisplayName,
  342. $instanceName
  343. ]
  344. );
  345. }
  346. $message->setFrom([Util::getDefaultEmailAddress($instanceName) => $senderName]);
  347. // The "Reply-To" is set to the sharer if an mail address is configured
  348. // also the default footer contains a "Do not reply" which needs to be adjusted.
  349. if ($initiatorUser && $this->settingsManager->replyToInitiator()) {
  350. $initiatorEmail = $initiatorUser->getEMailAddress();
  351. if ($initiatorEmail !== null) {
  352. $message->setReplyTo([$initiatorEmail => $initiatorDisplayName]);
  353. $emailTemplate->addFooter($instanceName . ($this->defaults->getSlogan() !== '' ? ' - ' . $this->defaults->getSlogan() : ''));
  354. } else {
  355. $emailTemplate->addFooter();
  356. }
  357. } else {
  358. $emailTemplate->addFooter();
  359. }
  360. $message->useTemplate($emailTemplate);
  361. $failedRecipients = $this->mailer->send($message);
  362. if (!empty($failedRecipients)) {
  363. $this->logger->error('Share notification mail could not be sent to: ' . implode(', ', $failedRecipients));
  364. return;
  365. }
  366. }
  367. /**
  368. * Send password to recipient of a mail share
  369. * Will return false if
  370. * 1. the password is empty
  371. * 2. the setting to send the password by mail is disabled
  372. * 3. the share is set to send the password by talk
  373. *
  374. * @param IShare $share
  375. * @param string $password
  376. * @param array $emails
  377. * @return bool
  378. */
  379. protected function sendPassword(IShare $share, string $password, array $emails): bool {
  380. $filename = $share->getNode()->getName();
  381. $initiator = $share->getSharedBy();
  382. $shareWith = $share->getSharedWith();
  383. if ($password === '' || $this->settingsManager->sendPasswordByMail() === false || $share->getSendPasswordByTalk()) {
  384. return false;
  385. }
  386. $initiatorUser = $this->userManager->get($initiator);
  387. $initiatorDisplayName = ($initiatorUser instanceof IUser) ? $initiatorUser->getDisplayName() : $initiator;
  388. $initiatorEmailAddress = ($initiatorUser instanceof IUser) ? $initiatorUser->getEMailAddress() : null;
  389. $plainBodyPart = $this->l->t("%1\$s shared %2\$s with you.\nYou should have already received a separate mail with a link to access it.\n", [$initiatorDisplayName, $filename]);
  390. $htmlBodyPart = $this->l->t('%1$s shared %2$s with you. You should have already received a separate mail with a link to access it.', [$initiatorDisplayName, $filename]);
  391. $message = $this->mailer->createMessage();
  392. $emailTemplate = $this->mailer->createEMailTemplate('sharebymail.RecipientPasswordNotification', [
  393. 'filename' => $filename,
  394. 'password' => $password,
  395. 'initiator' => $initiatorDisplayName,
  396. 'initiatorEmail' => $initiatorEmailAddress,
  397. 'shareWith' => $shareWith,
  398. ]);
  399. $emailTemplate->setSubject($this->l->t('Password to access %1$s shared to you by %2$s', [$filename, $initiatorDisplayName]));
  400. $emailTemplate->addHeader();
  401. $emailTemplate->addHeading($this->l->t('Password to access %s', [$filename]), false);
  402. $emailTemplate->addBodyText(htmlspecialchars($htmlBodyPart), $plainBodyPart);
  403. $emailTemplate->addBodyText($this->l->t('It is protected with the following password:'));
  404. $emailTemplate->addBodyText($password);
  405. if ($this->config->getSystemValue('sharing.enable_mail_link_password_expiration', false) === true) {
  406. $expirationTime = new \DateTime();
  407. $expirationInterval = $this->config->getSystemValue('sharing.mail_link_password_expiration_interval', 3600);
  408. $expirationTime = $expirationTime->add(new \DateInterval('PT' . $expirationInterval . 'S'));
  409. $emailTemplate->addBodyText($this->l->t('This password will expire at %s', [$expirationTime->format('r')]));
  410. }
  411. // If multiple recipients are given, we send the mail to all of them
  412. if (count($emails) > 1) {
  413. // We do not want to expose the email addresses of the other recipients
  414. $message->setBcc($emails);
  415. } else {
  416. $message->setTo($emails);
  417. }
  418. // The "From" contains the sharers name
  419. $instanceName = $this->defaults->getName();
  420. $senderName = $instanceName;
  421. if ($this->settingsManager->replyToInitiator()) {
  422. $senderName = $this->l->t(
  423. '%1$s via %2$s',
  424. [
  425. $initiatorDisplayName,
  426. $instanceName
  427. ]
  428. );
  429. }
  430. $message->setFrom([Util::getDefaultEmailAddress($instanceName) => $senderName]);
  431. // The "Reply-To" is set to the sharer if an mail address is configured
  432. // also the default footer contains a "Do not reply" which needs to be adjusted.
  433. if ($initiatorUser && $this->settingsManager->replyToInitiator()) {
  434. $initiatorEmail = $initiatorUser->getEMailAddress();
  435. if ($initiatorEmail !== null) {
  436. $message->setReplyTo([$initiatorEmail => $initiatorDisplayName]);
  437. $emailTemplate->addFooter($instanceName . ($this->defaults->getSlogan() !== '' ? ' - ' . $this->defaults->getSlogan() : ''));
  438. } else {
  439. $emailTemplate->addFooter();
  440. }
  441. } else {
  442. $emailTemplate->addFooter();
  443. }
  444. $message->useTemplate($emailTemplate);
  445. $failedRecipients = $this->mailer->send($message);
  446. if (!empty($failedRecipients)) {
  447. $this->logger->error('Share password mail could not be sent to: ' . implode(', ', $failedRecipients));
  448. return false;
  449. }
  450. $this->createPasswordSendActivity($share, $shareWith, false);
  451. return true;
  452. }
  453. protected function sendNote(IShare $share): void {
  454. $recipient = $share->getSharedWith();
  455. $filename = $share->getNode()->getName();
  456. $initiator = $share->getSharedBy();
  457. $note = $share->getNote();
  458. $initiatorUser = $this->userManager->get($initiator);
  459. $initiatorDisplayName = ($initiatorUser instanceof IUser) ? $initiatorUser->getDisplayName() : $initiator;
  460. $initiatorEmailAddress = ($initiatorUser instanceof IUser) ? $initiatorUser->getEMailAddress() : null;
  461. $plainHeading = $this->l->t('%1$s shared %2$s with you and wants to add:', [$initiatorDisplayName, $filename]);
  462. $htmlHeading = $this->l->t('%1$s shared %2$s with you and wants to add', [$initiatorDisplayName, $filename]);
  463. $message = $this->mailer->createMessage();
  464. $emailTemplate = $this->mailer->createEMailTemplate('shareByMail.sendNote');
  465. $emailTemplate->setSubject($this->l->t('%s added a note to a file shared with you', [$initiatorDisplayName]));
  466. $emailTemplate->addHeader();
  467. $emailTemplate->addHeading(htmlspecialchars($htmlHeading), $plainHeading);
  468. $emailTemplate->addBodyText(htmlspecialchars($note), $note);
  469. $link = $this->urlGenerator->linkToRouteAbsolute('files_sharing.sharecontroller.showShare',
  470. ['token' => $share->getToken()]);
  471. $emailTemplate->addBodyButton(
  472. $this->l->t('Open %s', [$filename]),
  473. $link
  474. );
  475. // The "From" contains the sharers name
  476. $instanceName = $this->defaults->getName();
  477. $senderName = $instanceName;
  478. if ($this->settingsManager->replyToInitiator()) {
  479. $senderName = $this->l->t(
  480. '%1$s via %2$s',
  481. [
  482. $initiatorDisplayName,
  483. $instanceName
  484. ]
  485. );
  486. }
  487. $message->setFrom([Util::getDefaultEmailAddress($instanceName) => $senderName]);
  488. if ($this->settingsManager->replyToInitiator() && $initiatorEmailAddress !== null) {
  489. $message->setReplyTo([$initiatorEmailAddress => $initiatorDisplayName]);
  490. $emailTemplate->addFooter($instanceName . ' - ' . $this->defaults->getSlogan());
  491. } else {
  492. $emailTemplate->addFooter();
  493. }
  494. $message->setTo([$recipient]);
  495. $message->useTemplate($emailTemplate);
  496. $this->mailer->send($message);
  497. }
  498. /**
  499. * send auto generated password to the owner. This happens if the admin enforces
  500. * a password for mail shares and forbid to send the password by mail to the recipient
  501. *
  502. * @throws \Exception
  503. */
  504. protected function sendPasswordToOwner(IShare $share, string $password): bool {
  505. $filename = $share->getNode()->getName();
  506. $initiator = $this->userManager->get($share->getSharedBy());
  507. $initiatorEMailAddress = ($initiator instanceof IUser) ? $initiator->getEMailAddress() : null;
  508. $initiatorDisplayName = ($initiator instanceof IUser) ? $initiator->getDisplayName() : $share->getSharedBy();
  509. $shareWith = $share->getSharedWith();
  510. if ($initiatorEMailAddress === null) {
  511. throw new \Exception(
  512. $this->l->t('We cannot send you the auto-generated password. Please set a valid email address in your personal settings and try again.')
  513. );
  514. }
  515. $bodyPart = $this->l->t('You just shared %1$s with %2$s. The share was already sent to the recipient. Due to the security policies defined by the administrator of %3$s each share needs to be protected by password and it is not allowed to send the password directly to the recipient. Therefore you need to forward the password manually to the recipient.', [$filename, $shareWith, $this->defaults->getName()]);
  516. $message = $this->mailer->createMessage();
  517. $emailTemplate = $this->mailer->createEMailTemplate('sharebymail.OwnerPasswordNotification', [
  518. 'filename' => $filename,
  519. 'password' => $password,
  520. 'initiator' => $initiatorDisplayName,
  521. 'initiatorEmail' => $initiatorEMailAddress,
  522. 'shareWith' => $shareWith,
  523. ]);
  524. $emailTemplate->setSubject($this->l->t('Password to access %1$s shared by you with %2$s', [$filename, $shareWith]));
  525. $emailTemplate->addHeader();
  526. $emailTemplate->addHeading($this->l->t('Password to access %s', [$filename]), false);
  527. $emailTemplate->addBodyText($bodyPart);
  528. $emailTemplate->addBodyText($this->l->t('This is the password:'));
  529. $emailTemplate->addBodyText($password);
  530. if ($this->config->getSystemValue('sharing.enable_mail_link_password_expiration', false) === true) {
  531. $expirationTime = new \DateTime();
  532. $expirationInterval = $this->config->getSystemValue('sharing.mail_link_password_expiration_interval', 3600);
  533. $expirationTime = $expirationTime->add(new \DateInterval('PT' . $expirationInterval . 'S'));
  534. $emailTemplate->addBodyText($this->l->t('This password will expire at %s', [$expirationTime->format('r')]));
  535. }
  536. $emailTemplate->addBodyText($this->l->t('You can choose a different password at any time in the share dialog.'));
  537. $emailTemplate->addFooter();
  538. $instanceName = $this->defaults->getName();
  539. $senderName = $this->l->t(
  540. '%1$s via %2$s',
  541. [
  542. $initiatorDisplayName,
  543. $instanceName
  544. ]
  545. );
  546. $message->setFrom([Util::getDefaultEmailAddress($instanceName) => $senderName]);
  547. $message->setTo([$initiatorEMailAddress => $initiatorDisplayName]);
  548. $message->useTemplate($emailTemplate);
  549. $this->mailer->send($message);
  550. $this->createPasswordSendActivity($share, $shareWith, true);
  551. return true;
  552. }
  553. private function getAbsoluteImagePath(string $path):string {
  554. return $this->urlGenerator->getAbsoluteURL(
  555. $this->urlGenerator->imagePath('core', $path)
  556. );
  557. }
  558. /**
  559. * generate share token
  560. */
  561. protected function generateToken(int $size = 15): string {
  562. $token = $this->secureRandom->generate($size, ISecureRandom::CHAR_HUMAN_READABLE);
  563. return $token;
  564. }
  565. /**
  566. * Get all children of this share
  567. *
  568. * @return IShare[]
  569. */
  570. public function getChildren(IShare $parent): array {
  571. $children = [];
  572. $qb = $this->dbConnection->getQueryBuilder();
  573. $qb->select('*')
  574. ->from('share')
  575. ->where($qb->expr()->eq('parent', $qb->createNamedParameter($parent->getId())))
  576. ->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(IShare::TYPE_EMAIL)))
  577. ->orderBy('id');
  578. $cursor = $qb->executeQuery();
  579. while ($data = $cursor->fetch()) {
  580. $children[] = $this->createShareObject($data);
  581. }
  582. $cursor->closeCursor();
  583. return $children;
  584. }
  585. /**
  586. * Add share to the database and return the ID
  587. */
  588. protected function addShareToDB(
  589. ?int $itemSource,
  590. ?string $itemType,
  591. ?string $shareWith,
  592. ?string $sharedBy,
  593. ?string $uidOwner,
  594. ?int $permissions,
  595. ?string $token,
  596. ?string $password,
  597. ?\DateTimeInterface $passwordExpirationTime,
  598. ?bool $sendPasswordByTalk,
  599. ?bool $hideDownload,
  600. ?string $label,
  601. ?\DateTimeInterface $expirationTime,
  602. ?string $note = '',
  603. ?IAttributes $attributes = null,
  604. ?bool $mailSend = true,
  605. ): int {
  606. $qb = $this->dbConnection->getQueryBuilder();
  607. $qb->insert('share')
  608. ->setValue('share_type', $qb->createNamedParameter(IShare::TYPE_EMAIL))
  609. ->setValue('item_type', $qb->createNamedParameter($itemType))
  610. ->setValue('item_source', $qb->createNamedParameter($itemSource))
  611. ->setValue('file_source', $qb->createNamedParameter($itemSource))
  612. ->setValue('share_with', $qb->createNamedParameter($shareWith))
  613. ->setValue('uid_owner', $qb->createNamedParameter($uidOwner))
  614. ->setValue('uid_initiator', $qb->createNamedParameter($sharedBy))
  615. ->setValue('permissions', $qb->createNamedParameter($permissions))
  616. ->setValue('token', $qb->createNamedParameter($token))
  617. ->setValue('password', $qb->createNamedParameter($password))
  618. ->setValue('password_expiration_time', $qb->createNamedParameter($passwordExpirationTime, IQueryBuilder::PARAM_DATETIME_MUTABLE))
  619. ->setValue('password_by_talk', $qb->createNamedParameter($sendPasswordByTalk, IQueryBuilder::PARAM_BOOL))
  620. ->setValue('stime', $qb->createNamedParameter(time()))
  621. ->setValue('hide_download', $qb->createNamedParameter((int)$hideDownload, IQueryBuilder::PARAM_INT))
  622. ->setValue('label', $qb->createNamedParameter($label))
  623. ->setValue('note', $qb->createNamedParameter($note))
  624. ->setValue('mail_send', $qb->createNamedParameter((int)$mailSend, IQueryBuilder::PARAM_INT));
  625. // set share attributes
  626. $shareAttributes = $this->formatShareAttributes($attributes);
  627. $qb->setValue('attributes', $qb->createNamedParameter($shareAttributes));
  628. if ($expirationTime !== null) {
  629. $qb->setValue('expiration', $qb->createNamedParameter($expirationTime, IQueryBuilder::PARAM_DATETIME_MUTABLE));
  630. }
  631. $qb->executeStatement();
  632. return $qb->getLastInsertId();
  633. }
  634. /**
  635. * Update a share
  636. */
  637. public function update(IShare $share, ?string $plainTextPassword = null): IShare {
  638. $originalShare = $this->getShareById($share->getId());
  639. // a real password was given
  640. $validPassword = $plainTextPassword !== null && $plainTextPassword !== '';
  641. if ($validPassword && ($originalShare->getPassword() !== $share->getPassword() ||
  642. ($originalShare->getSendPasswordByTalk() && !$share->getSendPasswordByTalk()))) {
  643. $emails = $this->getSharedWithEmails($share);
  644. $validEmails = array_filter($emails, function ($email) {
  645. return $this->mailer->validateMailAddress($email);
  646. });
  647. $this->sendPassword($share, $plainTextPassword, $validEmails);
  648. }
  649. $shareAttributes = $this->formatShareAttributes($share->getAttributes());
  650. /*
  651. * We allow updating mail shares
  652. */
  653. $qb = $this->dbConnection->getQueryBuilder();
  654. $qb->update('share')
  655. ->where($qb->expr()->eq('id', $qb->createNamedParameter($share->getId())))
  656. ->set('item_source', $qb->createNamedParameter($share->getNodeId()))
  657. ->set('file_source', $qb->createNamedParameter($share->getNodeId()))
  658. ->set('share_with', $qb->createNamedParameter($share->getSharedWith()))
  659. ->set('permissions', $qb->createNamedParameter($share->getPermissions()))
  660. ->set('uid_owner', $qb->createNamedParameter($share->getShareOwner()))
  661. ->set('uid_initiator', $qb->createNamedParameter($share->getSharedBy()))
  662. ->set('password', $qb->createNamedParameter($share->getPassword()))
  663. ->set('password_expiration_time', $qb->createNamedParameter($share->getPasswordExpirationTime(), IQueryBuilder::PARAM_DATETIME_MUTABLE))
  664. ->set('label', $qb->createNamedParameter($share->getLabel()))
  665. ->set('password_by_talk', $qb->createNamedParameter($share->getSendPasswordByTalk(), IQueryBuilder::PARAM_BOOL))
  666. ->set('expiration', $qb->createNamedParameter($share->getExpirationDate(), IQueryBuilder::PARAM_DATETIME_MUTABLE))
  667. ->set('note', $qb->createNamedParameter($share->getNote()))
  668. ->set('hide_download', $qb->createNamedParameter((int)$share->getHideDownload(), IQueryBuilder::PARAM_INT))
  669. ->set('attributes', $qb->createNamedParameter($shareAttributes))
  670. ->set('mail_send', $qb->createNamedParameter((int)$share->getMailSend(), IQueryBuilder::PARAM_INT))
  671. ->set('reminder_sent', $qb->createNamedParameter($share->getReminderSent(), IQueryBuilder::PARAM_BOOL))
  672. ->executeStatement();
  673. if ($originalShare->getNote() !== $share->getNote() && $share->getNote() !== '') {
  674. $this->sendNote($share);
  675. }
  676. return $share;
  677. }
  678. /**
  679. * @inheritdoc
  680. */
  681. public function move(IShare $share, $recipient): IShare {
  682. /**
  683. * nothing to do here, mail shares are only outgoing shares
  684. */
  685. return $share;
  686. }
  687. /**
  688. * Delete a share (owner unShares the file)
  689. *
  690. * @param IShare $share
  691. */
  692. public function delete(IShare $share): void {
  693. try {
  694. $this->createShareActivity($share, 'unshare');
  695. } catch (\Exception $e) {
  696. }
  697. $this->removeShareFromTable((int)$share->getId());
  698. }
  699. /**
  700. * @inheritdoc
  701. */
  702. public function deleteFromSelf(IShare $share, $recipient): void {
  703. // nothing to do here, mail shares are only outgoing shares
  704. }
  705. public function restore(IShare $share, string $recipient): IShare {
  706. throw new GenericShareException('not implemented');
  707. }
  708. /**
  709. * @inheritdoc
  710. */
  711. public function getSharesBy($userId, $shareType, $node, $reshares, $limit, $offset): array {
  712. $qb = $this->dbConnection->getQueryBuilder();
  713. $qb->select('*')
  714. ->from('share');
  715. $qb->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(IShare::TYPE_EMAIL)));
  716. /**
  717. * Reshares for this user are shares where they are the owner.
  718. */
  719. if ($reshares === false) {
  720. //Special case for old shares created via the web UI
  721. $or1 = $qb->expr()->andX(
  722. $qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)),
  723. $qb->expr()->isNull('uid_initiator')
  724. );
  725. $qb->andWhere(
  726. $qb->expr()->orX(
  727. $qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId)),
  728. $or1
  729. )
  730. );
  731. } elseif ($node === null) {
  732. $qb->andWhere(
  733. $qb->expr()->orX(
  734. $qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)),
  735. $qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId))
  736. )
  737. );
  738. }
  739. if ($node !== null) {
  740. $qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId())));
  741. }
  742. if ($limit !== -1) {
  743. $qb->setMaxResults($limit);
  744. }
  745. $qb->setFirstResult($offset);
  746. $qb->orderBy('id');
  747. $cursor = $qb->executeQuery();
  748. $shares = [];
  749. while ($data = $cursor->fetch()) {
  750. $shares[] = $this->createShareObject($data);
  751. }
  752. $cursor->closeCursor();
  753. return $shares;
  754. }
  755. /**
  756. * @inheritdoc
  757. */
  758. public function getShareById($id, $recipientId = null): IShare {
  759. $qb = $this->dbConnection->getQueryBuilder();
  760. $qb->select('*')
  761. ->from('share')
  762. ->where($qb->expr()->eq('id', $qb->createNamedParameter($id)))
  763. ->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(IShare::TYPE_EMAIL)));
  764. $cursor = $qb->executeQuery();
  765. $data = $cursor->fetch();
  766. $cursor->closeCursor();
  767. if ($data === false) {
  768. throw new ShareNotFound();
  769. }
  770. try {
  771. $share = $this->createShareObject($data);
  772. } catch (InvalidShare $e) {
  773. throw new ShareNotFound();
  774. }
  775. return $share;
  776. }
  777. /**
  778. * Get shares for a given path
  779. *
  780. * @return IShare[]
  781. */
  782. public function getSharesByPath(Node $path): array {
  783. $qb = $this->dbConnection->getQueryBuilder();
  784. $cursor = $qb->select('*')
  785. ->from('share')
  786. ->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($path->getId())))
  787. ->andWhere($qb->expr()->eq('share_type', $qb->createNamedParameter(IShare::TYPE_EMAIL)))
  788. ->executeQuery();
  789. $shares = [];
  790. while ($data = $cursor->fetch()) {
  791. $shares[] = $this->createShareObject($data);
  792. }
  793. $cursor->closeCursor();
  794. return $shares;
  795. }
  796. /**
  797. * @inheritdoc
  798. */
  799. public function getSharedWith($userId, $shareType, $node, $limit, $offset): array {
  800. /** @var IShare[] $shares */
  801. $shares = [];
  802. //Get shares directly with this user
  803. $qb = $this->dbConnection->getQueryBuilder();
  804. $qb->select('*')
  805. ->from('share');
  806. // Order by id
  807. $qb->orderBy('id');
  808. // Set limit and offset
  809. if ($limit !== -1) {
  810. $qb->setMaxResults($limit);
  811. }
  812. $qb->setFirstResult($offset);
  813. $qb->where($qb->expr()->eq('share_type', $qb->createNamedParameter(IShare::TYPE_EMAIL)));
  814. $qb->andWhere($qb->expr()->eq('share_with', $qb->createNamedParameter($userId)));
  815. // Filter by node if provided
  816. if ($node !== null) {
  817. $qb->andWhere($qb->expr()->eq('file_source', $qb->createNamedParameter($node->getId())));
  818. }
  819. $cursor = $qb->executeQuery();
  820. while ($data = $cursor->fetch()) {
  821. $shares[] = $this->createShareObject($data);
  822. }
  823. $cursor->closeCursor();
  824. return $shares;
  825. }
  826. /**
  827. * Get a share by token
  828. *
  829. * @throws ShareNotFound
  830. */
  831. public function getShareByToken($token): IShare {
  832. $qb = $this->dbConnection->getQueryBuilder();
  833. $cursor = $qb->select('*')
  834. ->from('share')
  835. ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(IShare::TYPE_EMAIL)))
  836. ->andWhere($qb->expr()->eq('token', $qb->createNamedParameter($token)))
  837. ->executeQuery();
  838. $data = $cursor->fetch();
  839. if ($data === false) {
  840. throw new ShareNotFound('Share not found', $this->l->t('Could not find share'));
  841. }
  842. try {
  843. $share = $this->createShareObject($data);
  844. } catch (InvalidShare $e) {
  845. throw new ShareNotFound('Share not found', $this->l->t('Could not find share'));
  846. }
  847. return $share;
  848. }
  849. /**
  850. * remove share from table
  851. */
  852. protected function removeShareFromTable(int $shareId): void {
  853. $qb = $this->dbConnection->getQueryBuilder();
  854. $qb->delete('share')
  855. ->where($qb->expr()->eq('id', $qb->createNamedParameter($shareId)));
  856. $qb->executeStatement();
  857. }
  858. /**
  859. * Create a share object from a database row
  860. *
  861. * @throws InvalidShare
  862. * @throws ShareNotFound
  863. */
  864. protected function createShareObject(array $data): IShare {
  865. $share = new Share($this->rootFolder, $this->userManager);
  866. $share->setId((int)$data['id'])
  867. ->setShareType((int)$data['share_type'])
  868. ->setPermissions((int)$data['permissions'])
  869. ->setTarget($data['file_target'])
  870. ->setMailSend((bool)$data['mail_send'])
  871. ->setNote($data['note'])
  872. ->setToken($data['token']);
  873. $shareTime = new \DateTime();
  874. $shareTime->setTimestamp((int)$data['stime']);
  875. $share->setShareTime($shareTime);
  876. $share->setSharedWith($data['share_with'] ?? '');
  877. $share->setPassword($data['password']);
  878. $passwordExpirationTime = \DateTime::createFromFormat('Y-m-d H:i:s', $data['password_expiration_time'] ?? '');
  879. $share->setPasswordExpirationTime($passwordExpirationTime !== false ? $passwordExpirationTime : null);
  880. $share->setLabel($data['label']);
  881. $share->setSendPasswordByTalk((bool)$data['password_by_talk']);
  882. $share->setHideDownload((bool)$data['hide_download']);
  883. $share->setReminderSent((bool)$data['reminder_sent']);
  884. if ($data['uid_initiator'] !== null) {
  885. $share->setShareOwner($data['uid_owner']);
  886. $share->setSharedBy($data['uid_initiator']);
  887. } else {
  888. //OLD SHARE
  889. $share->setSharedBy($data['uid_owner']);
  890. $path = $this->getNode($share->getSharedBy(), (int)$data['file_source']);
  891. $owner = $path->getOwner();
  892. $share->setShareOwner($owner->getUID());
  893. }
  894. if ($data['expiration'] !== null) {
  895. $expiration = \DateTime::createFromFormat('Y-m-d H:i:s', $data['expiration']);
  896. if ($expiration !== false) {
  897. $share->setExpirationDate($expiration);
  898. }
  899. }
  900. $share = $this->updateShareAttributes($share, $data['attributes']);
  901. $share->setNodeId((int)$data['file_source']);
  902. $share->setNodeType($data['item_type']);
  903. $share->setProviderId($this->identifier());
  904. return $share;
  905. }
  906. /**
  907. * Get the node with file $id for $user
  908. *
  909. * @throws InvalidShare
  910. */
  911. private function getNode(string $userId, int $id): Node {
  912. try {
  913. $userFolder = $this->rootFolder->getUserFolder($userId);
  914. } catch (NoUserException $e) {
  915. throw new InvalidShare();
  916. }
  917. $nodes = $userFolder->getById($id);
  918. if (empty($nodes)) {
  919. throw new InvalidShare();
  920. }
  921. return $nodes[0];
  922. }
  923. /**
  924. * A user is deleted from the system
  925. * So clean up the relevant shares.
  926. */
  927. public function userDeleted($uid, $shareType): void {
  928. $qb = $this->dbConnection->getQueryBuilder();
  929. $qb->delete('share')
  930. ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(IShare::TYPE_EMAIL)))
  931. ->andWhere($qb->expr()->eq('uid_owner', $qb->createNamedParameter($uid)))
  932. ->executeStatement();
  933. }
  934. /**
  935. * This provider does not support group shares
  936. */
  937. public function groupDeleted($gid): void {
  938. }
  939. /**
  940. * This provider does not support group shares
  941. */
  942. public function userDeletedFromGroup($uid, $gid): void {
  943. }
  944. /**
  945. * get database row of a give share
  946. *
  947. * @throws ShareNotFound
  948. */
  949. protected function getRawShare(int $id): array {
  950. // Now fetch the inserted share and create a complete share object
  951. $qb = $this->dbConnection->getQueryBuilder();
  952. $qb->select('*')
  953. ->from('share')
  954. ->where($qb->expr()->eq('id', $qb->createNamedParameter($id)));
  955. $cursor = $qb->executeQuery();
  956. $data = $cursor->fetch();
  957. $cursor->closeCursor();
  958. if ($data === false) {
  959. throw new ShareNotFound;
  960. }
  961. return $data;
  962. }
  963. public function getSharesInFolder($userId, Folder $node, $reshares, $shallow = true): array {
  964. $qb = $this->dbConnection->getQueryBuilder();
  965. $qb->select('*')
  966. ->from('share', 's')
  967. ->andWhere($qb->expr()->orX(
  968. $qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
  969. $qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
  970. ))
  971. ->andWhere(
  972. $qb->expr()->eq('share_type', $qb->createNamedParameter(IShare::TYPE_EMAIL))
  973. );
  974. /**
  975. * Reshares for this user are shares where they are the owner.
  976. */
  977. if ($reshares === false) {
  978. $qb->andWhere($qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId)));
  979. } else {
  980. $qb->andWhere(
  981. $qb->expr()->orX(
  982. $qb->expr()->eq('uid_owner', $qb->createNamedParameter($userId)),
  983. $qb->expr()->eq('uid_initiator', $qb->createNamedParameter($userId))
  984. )
  985. );
  986. }
  987. $qb->innerJoin('s', 'filecache', 'f', $qb->expr()->eq('s.file_source', 'f.fileid'));
  988. $qb->andWhere($qb->expr()->eq('f.parent', $qb->createNamedParameter($node->getId())));
  989. $qb->orderBy('id');
  990. $cursor = $qb->executeQuery();
  991. $shares = [];
  992. while ($data = $cursor->fetch()) {
  993. $shares[$data['fileid']][] = $this->createShareObject($data);
  994. }
  995. $cursor->closeCursor();
  996. return $shares;
  997. }
  998. /**
  999. * @inheritdoc
  1000. */
  1001. public function getAccessList($nodes, $currentAccess): array {
  1002. $ids = [];
  1003. foreach ($nodes as $node) {
  1004. $ids[] = $node->getId();
  1005. }
  1006. $qb = $this->dbConnection->getQueryBuilder();
  1007. $qb->select('share_with', 'file_source', 'token')
  1008. ->from('share')
  1009. ->where($qb->expr()->eq('share_type', $qb->createNamedParameter(IShare::TYPE_EMAIL)))
  1010. ->andWhere($qb->expr()->in('file_source', $qb->createNamedParameter($ids, IQueryBuilder::PARAM_INT_ARRAY)))
  1011. ->andWhere($qb->expr()->orX(
  1012. $qb->expr()->eq('item_type', $qb->createNamedParameter('file')),
  1013. $qb->expr()->eq('item_type', $qb->createNamedParameter('folder'))
  1014. ));
  1015. $cursor = $qb->executeQuery();
  1016. $public = false;
  1017. $mail = [];
  1018. while ($row = $cursor->fetch()) {
  1019. $public = true;
  1020. if ($currentAccess === false) {
  1021. $mail[] = $row['share_with'];
  1022. } else {
  1023. $mail[$row['share_with']] = [
  1024. 'node_id' => $row['file_source'],
  1025. 'token' => $row['token']
  1026. ];
  1027. }
  1028. }
  1029. $cursor->closeCursor();
  1030. return ['public' => $public, 'mail' => $mail];
  1031. }
  1032. public function getAllShares(): iterable {
  1033. $qb = $this->dbConnection->getQueryBuilder();
  1034. $qb->select('*')
  1035. ->from('share')
  1036. ->where(
  1037. $qb->expr()->orX(
  1038. $qb->expr()->eq('share_type', $qb->createNamedParameter(IShare::TYPE_EMAIL))
  1039. )
  1040. );
  1041. $cursor = $qb->executeQuery();
  1042. while ($data = $cursor->fetch()) {
  1043. try {
  1044. $share = $this->createShareObject($data);
  1045. } catch (InvalidShare $e) {
  1046. continue;
  1047. } catch (ShareNotFound $e) {
  1048. continue;
  1049. }
  1050. yield $share;
  1051. }
  1052. $cursor->closeCursor();
  1053. }
  1054. /**
  1055. * Extract the emails from the share
  1056. * It can be a single email, from the share_with field
  1057. * or a list of emails from the emails attributes field.
  1058. * @param IShare $share
  1059. * @return string[]
  1060. */
  1061. protected function getSharedWithEmails(IShare $share): array {
  1062. $attributes = $share->getAttributes();
  1063. if ($attributes === null) {
  1064. return [$share->getSharedWith()];
  1065. }
  1066. $emails = $attributes->getAttribute('shareWith', 'emails');
  1067. if (isset($emails) && is_array($emails) && !empty($emails)) {
  1068. return $emails;
  1069. }
  1070. return [$share->getSharedWith()];
  1071. }
  1072. }