ShareByMailProvider.php 35 KB

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