You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mailnotifications.php 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Bjoern Schiessle
  6. * @copyright 2014 Bjoern Schiessle <schiessle@owncloud.com>
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. namespace OC\Share;
  22. class MailNotifications {
  23. private $senderId; // sender userId
  24. private $from; // sender email address
  25. private $senderDisplayName;
  26. private $l;
  27. /**
  28. *
  29. * @param string $sender user id (if nothing is set we use the currently logged-in user)
  30. */
  31. public function __construct($sender = null) {
  32. $this->l = \OC_L10N::get('core');
  33. $this->senderId = $sender;
  34. $this->from = \OCP\Util::getDefaultEmailAddress('sharing-noreply');
  35. if ($this->senderId) {
  36. $this->from = \OCP\Config::getUserValue($this->senderId, 'settings', 'email', $this->from);
  37. $this->senderDisplayName = \OCP\User::getDisplayName($this->senderId);
  38. } else {
  39. $this->senderDisplayName = \OCP\User::getDisplayName();
  40. }
  41. }
  42. /**
  43. * @brief inform users if a file was shared with them
  44. *
  45. * @param array $recipientList list of recipients
  46. * @param type $itemSource shared item source
  47. * @param type $itemType shared item type
  48. * @return array list of user to whom the mail send operation failed
  49. */
  50. public function sendInternalShareMail($recipientList, $itemSource, $itemType) {
  51. $noMail = array();
  52. foreach ($recipientList as $recipient) {
  53. $recipientDisplayName = \OCP\User::getDisplayName($recipient);
  54. $to = \OC_Preferences::getValue($recipient, 'settings', 'email', '');
  55. if ($to === '') {
  56. $noMail[] = $recipientDisplayName;
  57. continue;
  58. }
  59. $items = \OCP\Share::getItemSharedWithUser($itemType, $itemSource, $recipient);
  60. $filename = trim($items[0]['file_target'], '/');
  61. $subject = (string) $this->l->t('%s shared »%s« with you', array($this->senderDisplayName, $filename));
  62. $expiration = null;
  63. if (isset($items[0]['expiration'])) {
  64. try {
  65. $date = new DateTime($items[0]['expiration']);
  66. $expiration = $date->getTimestamp();
  67. } catch (\Exception $e) {
  68. \OCP\Util::writeLog('sharing', "Couldn't read date: " . $e->getMessage(), \OCP\Util::ERROR);
  69. }
  70. }
  71. if ($itemType === 'folder') {
  72. $foldername = "/Shared/" . $filename;
  73. } else {
  74. // if it is a file we can just link to the Shared folder,
  75. // that's the place where the user will find the file
  76. $foldername = "/Shared";
  77. }
  78. $link = \OCP\Util::linkToAbsolute('files', 'index.php', array("dir" => $foldername));
  79. list($htmlMail, $alttextMail) = $this->createMailBody($filename, $link, $expiration);
  80. // send it out now
  81. try {
  82. \OCP\Util::sendMail($to, $recipientDisplayName, $subject, $htmlMail, $this->from, $this->senderDisplayName, 1, $alttextMail);
  83. } catch (\Exception $e) {
  84. \OCP\Util::writeLog('sharing', "Can't send mail to inform the user about an internal share: " . $e->getMessage() , \OCP\Util::ERROR);
  85. $noMail[] = $recipientDisplayName;
  86. }
  87. }
  88. return $noMail;
  89. }
  90. /**
  91. * @brief inform recipient about public link share
  92. *
  93. * @param string $recipient recipient email address
  94. * @param string $filename the shared file
  95. * @param string $link the public link
  96. * @param int $expiration expiration date (timestamp)
  97. * @return array $result of failed recipients
  98. */
  99. public function sendLinkShareMail($recipient, $filename, $link, $expiration) {
  100. $subject = (string)$this->l->t('%s shared »%s« with you', array($this->senderDisplayName, $filename));
  101. list($htmlMail, $alttextMail) = $this->createMailBody($filename, $link, $expiration);
  102. $rs = explode(' ', $recipient);
  103. $failed = array();
  104. foreach ($rs as $r) {
  105. try {
  106. \OCP\Util::sendMail($r, $r, $subject, $htmlMail, $this->from, $this->senderDisplayName, 1, $alttextMail);
  107. } catch (\Exception $e) {
  108. \OCP\Util::writeLog('sharing', "Can't send mail with public link to $r: " . $e->getMessage(), \OCP\Util::ERROR);
  109. $failed[] = $r;
  110. }
  111. }
  112. return $failed;
  113. }
  114. /**
  115. * @brief create mail body for plain text and html mail
  116. *
  117. * @param string $filename the shared file
  118. * @param string $link link to the shared file
  119. * @param int $expiration expiration date (timestamp)
  120. * @return array with the html mail body and the plain text mail body
  121. */
  122. private function createMailBody($filename, $link, $expiration) {
  123. $formatedDate = $expiration ? $this->l->l('date', $expiration) : null;
  124. $html = new \OC_Template("core", "mail", "");
  125. $html->assign ('link', $link);
  126. $html->assign ('user_displayname', $this->senderDisplayName);
  127. $html->assign ('filename', $filename);
  128. $html->assign('expiration', $formatedDate);
  129. $htmlMail = $html->fetchPage();
  130. $alttext = new \OC_Template("core", "altmail", "");
  131. $alttext->assign ('link', $link);
  132. $alttext->assign ('user_displayname', $this->senderDisplayName);
  133. $alttext->assign ('filename', $filename);
  134. $alttext->assign('expiration', $formatedDate);
  135. $alttextMail = $alttext->fetchPage();
  136. return array($htmlMail, $alttextMail);
  137. }
  138. }