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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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::$server->getL10N('lib');
  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::$server->getConfig()->getUserValue($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. // Link to folder, or root folder if a file
  87. if ($itemType === 'folder') {
  88. $args = array(
  89. 'dir' => $filename,
  90. );
  91. } else {
  92. $args = array(
  93. 'dir' => '/',
  94. 'scrollto' => $filename,
  95. );
  96. }
  97. $link = \OCP\Util::linkToAbsolute('files', 'index.php', $args);
  98. list($htmlMail, $alttextMail) = $this->createMailBody($filename, $link, $expiration);
  99. // send it out now
  100. try {
  101. \OCP\Util::sendMail($to, $recipientDisplayName, $subject, $htmlMail, $this->from, $this->senderDisplayName, 1, $alttextMail);
  102. } catch (\Exception $e) {
  103. \OCP\Util::writeLog('sharing', "Can't send mail to inform the user about an internal share: " . $e->getMessage() , \OCP\Util::ERROR);
  104. $noMail[] = $recipientDisplayName;
  105. }
  106. }
  107. return $noMail;
  108. }
  109. /**
  110. * inform recipient about public link share
  111. *
  112. * @param string $recipient recipient email address
  113. * @param string $filename the shared file
  114. * @param string $link the public link
  115. * @param int $expiration expiration date (timestamp)
  116. * @return array $result of failed recipients
  117. */
  118. public function sendLinkShareMail($recipient, $filename, $link, $expiration) {
  119. $subject = (string)$this->l->t('%s shared »%s« with you', array($this->senderDisplayName, $filename));
  120. list($htmlMail, $alttextMail) = $this->createMailBody($filename, $link, $expiration);
  121. $rs = explode(' ', $recipient);
  122. $failed = array();
  123. foreach ($rs as $r) {
  124. try {
  125. \OCP\Util::sendMail($r, $r, $subject, $htmlMail, $this->from, $this->senderDisplayName, 1, $alttextMail);
  126. } catch (\Exception $e) {
  127. \OCP\Util::writeLog('sharing', "Can't send mail with public link to $r: " . $e->getMessage(), \OCP\Util::ERROR);
  128. $failed[] = $r;
  129. }
  130. }
  131. return $failed;
  132. }
  133. /**
  134. * create mail body for plain text and html mail
  135. *
  136. * @param string $filename the shared file
  137. * @param string $link link to the shared file
  138. * @param int $expiration expiration date (timestamp)
  139. * @return array an array of the html mail body and the plain text mail body
  140. */
  141. private function createMailBody($filename, $link, $expiration) {
  142. $formatedDate = $expiration ? $this->l->l('date', $expiration) : null;
  143. $html = new \OC_Template("core", "mail", "");
  144. $html->assign ('link', $link);
  145. $html->assign ('user_displayname', $this->senderDisplayName);
  146. $html->assign ('filename', $filename);
  147. $html->assign('expiration', $formatedDate);
  148. $htmlMail = $html->fetchPage();
  149. $alttext = new \OC_Template("core", "altmail", "");
  150. $alttext->assign ('link', $link);
  151. $alttext->assign ('user_displayname', $this->senderDisplayName);
  152. $alttext->assign ('filename', $filename);
  153. $alttext->assign('expiration', $formatedDate);
  154. $alttextMail = $alttext->fetchPage();
  155. return array($htmlMail, $alttextMail);
  156. }
  157. }