mailnotifications.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. use DateTime;
  23. class MailNotifications {
  24. /**
  25. * sender userId
  26. * @var null|string
  27. */
  28. private $senderId;
  29. /**
  30. * sender email address
  31. * @var string
  32. */
  33. private $from;
  34. /**
  35. * @var string
  36. */
  37. private $senderDisplayName;
  38. /**
  39. * @var \OC_L10N
  40. */
  41. private $l;
  42. /**
  43. *
  44. * @param string $sender user id (if nothing is set we use the currently logged-in user)
  45. */
  46. public function __construct($sender = null) {
  47. $this->l = \OC_L10N::get('core');
  48. $this->senderId = $sender;
  49. $this->from = \OCP\Util::getDefaultEmailAddress('sharing-noreply');
  50. if ($this->senderId) {
  51. $this->from = \OCP\Config::getUserValue($this->senderId, 'settings', 'email', $this->from);
  52. $this->senderDisplayName = \OCP\User::getDisplayName($this->senderId);
  53. } else {
  54. $this->senderDisplayName = \OCP\User::getDisplayName();
  55. }
  56. }
  57. /**
  58. * inform users if a file was shared with them
  59. *
  60. * @param array $recipientList list of recipients
  61. * @param string $itemSource shared item source
  62. * @param string $itemType shared item type
  63. * @return array list of user to whom the mail send operation failed
  64. */
  65. public function sendInternalShareMail($recipientList, $itemSource, $itemType) {
  66. $noMail = array();
  67. foreach ($recipientList as $recipient) {
  68. $recipientDisplayName = \OCP\User::getDisplayName($recipient);
  69. $to = \OC_Preferences::getValue($recipient, 'settings', 'email', '');
  70. if ($to === '') {
  71. $noMail[] = $recipientDisplayName;
  72. continue;
  73. }
  74. $items = \OCP\Share::getItemSharedWithUser($itemType, $itemSource, $recipient);
  75. $filename = trim($items[0]['file_target'], '/');
  76. $subject = (string) $this->l->t('%s shared »%s« with you', array($this->senderDisplayName, $filename));
  77. $expiration = null;
  78. if (isset($items[0]['expiration'])) {
  79. try {
  80. $date = new DateTime($items[0]['expiration']);
  81. $expiration = $date->getTimestamp();
  82. } catch (\Exception $e) {
  83. \OCP\Util::writeLog('sharing', "Couldn't read date: " . $e->getMessage(), \OCP\Util::ERROR);
  84. }
  85. }
  86. if ($itemType === 'folder') {
  87. $foldername = "/Shared/" . $filename;
  88. } else {
  89. // if it is a file we can just link to the Shared folder,
  90. // that's the place where the user will find the file
  91. $foldername = "/Shared";
  92. }
  93. $link = \OCP\Util::linkToAbsolute('files', 'index.php', array("dir" => $foldername));
  94. list($htmlMail, $alttextMail) = $this->createMailBody($filename, $link, $expiration);
  95. // send it out now
  96. try {
  97. \OCP\Util::sendMail($to, $recipientDisplayName, $subject, $htmlMail, $this->from, $this->senderDisplayName, 1, $alttextMail);
  98. } catch (\Exception $e) {
  99. \OCP\Util::writeLog('sharing', "Can't send mail to inform the user about an internal share: " . $e->getMessage() , \OCP\Util::ERROR);
  100. $noMail[] = $recipientDisplayName;
  101. }
  102. }
  103. return $noMail;
  104. }
  105. /**
  106. * inform recipient about public link share
  107. *
  108. * @param string $recipient recipient email address
  109. * @param string $filename the shared file
  110. * @param string $link the public link
  111. * @param int $expiration expiration date (timestamp)
  112. * @return array $result of failed recipients
  113. */
  114. public function sendLinkShareMail($recipient, $filename, $link, $expiration) {
  115. $subject = (string)$this->l->t('%s shared »%s« with you', array($this->senderDisplayName, $filename));
  116. list($htmlMail, $alttextMail) = $this->createMailBody($filename, $link, $expiration);
  117. $rs = explode(' ', $recipient);
  118. $failed = array();
  119. foreach ($rs as $r) {
  120. try {
  121. \OCP\Util::sendMail($r, $r, $subject, $htmlMail, $this->from, $this->senderDisplayName, 1, $alttextMail);
  122. } catch (\Exception $e) {
  123. \OCP\Util::writeLog('sharing', "Can't send mail with public link to $r: " . $e->getMessage(), \OCP\Util::ERROR);
  124. $failed[] = $r;
  125. }
  126. }
  127. return $failed;
  128. }
  129. /**
  130. * create mail body for plain text and html mail
  131. *
  132. * @param string $filename the shared file
  133. * @param string $link link to the shared file
  134. * @param int $expiration expiration date (timestamp)
  135. * @return array an array of the html mail body and the plain text mail body
  136. */
  137. private function createMailBody($filename, $link, $expiration) {
  138. $formatedDate = $expiration ? $this->l->l('date', $expiration) : null;
  139. $html = new \OC_Template("core", "mail", "");
  140. $html->assign ('link', $link);
  141. $html->assign ('user_displayname', $this->senderDisplayName);
  142. $html->assign ('filename', $filename);
  143. $html->assign('expiration', $formatedDate);
  144. $htmlMail = $html->fetchPage();
  145. $alttext = new \OC_Template("core", "altmail", "");
  146. $alttext->assign ('link', $link);
  147. $alttext->assign ('user_displayname', $this->senderDisplayName);
  148. $alttext->assign ('filename', $filename);
  149. $alttext->assign('expiration', $formatedDate);
  150. $alttextMail = $alttext->fetchPage();
  151. return array($htmlMail, $alttextMail);
  152. }
  153. }