ShareByMailProvider.php 36 KB

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