2014-02-17 17:26:52 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 15:07:57 +00:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2016-05-26 17:56:05 +00:00
|
|
|
* @author Björn Schießle <bjoern@schiessle.org>
|
2016-07-21 15:07:57 +00:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2016-05-26 17:56:05 +00:00
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2016-01-12 14:02:16 +00:00
|
|
|
* @author Robin McCorkell <robin@mccorkell.me.uk>
|
2016-07-21 15:07:57 +00:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2016-01-12 14:02:16 +00:00
|
|
|
* @author scolebrook <scolebrook@mac.com>
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
2016-05-26 17:56:05 +00:00
|
|
|
* @author Vincent Petry <pvince81@owncloud.com>
|
2015-03-26 10:44:34 +00:00
|
|
|
*
|
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*
|
|
|
|
*/
|
2015-02-26 10:37:37 +00:00
|
|
|
|
2014-02-17 17:26:52 +00:00
|
|
|
namespace OC\Share;
|
|
|
|
|
2014-05-13 10:21:04 +00:00
|
|
|
use DateTime;
|
2015-04-10 15:21:52 +00:00
|
|
|
use OCP\IL10N;
|
2016-03-14 14:19:55 +00:00
|
|
|
use OCP\IURLGenerator;
|
2015-12-01 11:05:40 +00:00
|
|
|
use OCP\IUser;
|
2015-04-10 15:21:52 +00:00
|
|
|
use OCP\Mail\IMailer;
|
|
|
|
use OCP\ILogger;
|
|
|
|
use OCP\Defaults;
|
2015-12-01 11:05:40 +00:00
|
|
|
use OCP\Util;
|
2014-05-13 10:21:04 +00:00
|
|
|
|
2015-04-10 15:21:52 +00:00
|
|
|
/**
|
|
|
|
* Class MailNotifications
|
|
|
|
*
|
|
|
|
* @package OC\Share
|
|
|
|
*/
|
2014-02-17 17:26:52 +00:00
|
|
|
class MailNotifications {
|
|
|
|
|
2015-12-01 11:05:40 +00:00
|
|
|
/** @var IUser sender userId */
|
|
|
|
private $user;
|
2015-04-10 15:21:52 +00:00
|
|
|
/** @var string sender email address */
|
|
|
|
private $replyTo;
|
|
|
|
/** @var string */
|
2014-02-17 17:26:52 +00:00
|
|
|
private $senderDisplayName;
|
2015-04-10 15:21:52 +00:00
|
|
|
/** @var IL10N */
|
2014-02-17 17:26:52 +00:00
|
|
|
private $l;
|
2015-04-10 15:21:52 +00:00
|
|
|
/** @var IMailer */
|
|
|
|
private $mailer;
|
|
|
|
/** @var Defaults */
|
|
|
|
private $defaults;
|
|
|
|
/** @var ILogger */
|
|
|
|
private $logger;
|
2016-03-14 14:19:55 +00:00
|
|
|
/** @var IURLGenerator */
|
|
|
|
private $urlGenerator;
|
2014-02-17 17:26:52 +00:00
|
|
|
|
|
|
|
/**
|
2015-12-01 11:05:40 +00:00
|
|
|
* @param IUser $user
|
2015-04-10 15:21:52 +00:00
|
|
|
* @param IL10N $l10n
|
|
|
|
* @param IMailer $mailer
|
|
|
|
* @param ILogger $logger
|
|
|
|
* @param Defaults $defaults
|
2016-03-14 14:19:55 +00:00
|
|
|
* @param IURLGenerator $urlGenerator
|
2014-02-17 17:26:52 +00:00
|
|
|
*/
|
2015-12-01 11:05:40 +00:00
|
|
|
public function __construct(IUser $user,
|
2015-04-10 15:21:52 +00:00
|
|
|
IL10N $l10n,
|
|
|
|
IMailer $mailer,
|
|
|
|
ILogger $logger,
|
2016-03-14 14:19:55 +00:00
|
|
|
Defaults $defaults,
|
|
|
|
IURLGenerator $urlGenerator) {
|
2015-04-10 15:21:52 +00:00
|
|
|
$this->l = $l10n;
|
2015-12-01 11:05:40 +00:00
|
|
|
$this->user = $user;
|
2015-04-10 15:21:52 +00:00
|
|
|
$this->mailer = $mailer;
|
|
|
|
$this->logger = $logger;
|
|
|
|
$this->defaults = $defaults;
|
2016-03-14 14:19:55 +00:00
|
|
|
$this->urlGenerator = $urlGenerator;
|
2015-04-10 15:21:52 +00:00
|
|
|
|
2015-12-01 11:05:40 +00:00
|
|
|
$this->replyTo = $this->user->getEMailAddress();
|
|
|
|
$this->senderDisplayName = $this->user->getDisplayName();
|
2014-02-17 17:26:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* inform recipient about public link share
|
2014-02-17 17:26:52 +00:00
|
|
|
*
|
2014-03-10 20:54:23 +00:00
|
|
|
* @param string $recipient recipient email address
|
2014-02-17 17:26:52 +00:00
|
|
|
* @param string $filename the shared file
|
|
|
|
* @param string $link the public link
|
|
|
|
* @param int $expiration expiration date (timestamp)
|
2015-12-08 15:01:20 +00:00
|
|
|
* @return string[] $result of failed recipients
|
2014-02-17 17:26:52 +00:00
|
|
|
*/
|
|
|
|
public function sendLinkShareMail($recipient, $filename, $link, $expiration) {
|
2015-04-10 15:21:52 +00:00
|
|
|
$subject = (string)$this->l->t('%s shared »%s« with you', [$this->senderDisplayName, $filename]);
|
|
|
|
list($htmlBody, $textBody) = $this->createMailBody($filename, $link, $expiration);
|
|
|
|
|
2015-12-07 09:49:34 +00:00
|
|
|
$recipient = str_replace([', ', '; ', ',', ';', ' '], ',', $recipient);
|
|
|
|
$recipients = explode(',', $recipient);
|
2015-04-10 15:21:52 +00:00
|
|
|
try {
|
|
|
|
$message = $this->mailer->createMessage();
|
|
|
|
$message->setSubject($subject);
|
2015-12-07 09:49:34 +00:00
|
|
|
$message->setTo($recipients);
|
2015-04-10 15:21:52 +00:00
|
|
|
$message->setHtmlBody($htmlBody);
|
|
|
|
$message->setPlainBody($textBody);
|
|
|
|
$message->setFrom([
|
2015-12-02 13:04:27 +00:00
|
|
|
Util::getDefaultEmailAddress('sharing-noreply') =>
|
2015-04-10 15:21:52 +00:00
|
|
|
(string)$this->l->t('%s via %s', [
|
|
|
|
$this->senderDisplayName,
|
|
|
|
$this->defaults->getName()
|
|
|
|
]),
|
|
|
|
]);
|
|
|
|
if(!is_null($this->replyTo)) {
|
|
|
|
$message->setReplyTo([$this->replyTo]);
|
2014-03-10 20:54:23 +00:00
|
|
|
}
|
2015-04-10 15:21:52 +00:00
|
|
|
|
|
|
|
return $this->mailer->send($message);
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
$this->logger->error("Can't send mail with public link to $recipient: ".$e->getMessage(), ['app' => 'sharing']);
|
|
|
|
return [$recipient];
|
2014-02-17 17:26:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-19 15:50:53 +00:00
|
|
|
* create mail body for plain text and html mail
|
2014-02-17 17:26:52 +00:00
|
|
|
*
|
|
|
|
* @param string $filename the shared file
|
|
|
|
* @param string $link link to the shared file
|
|
|
|
* @param int $expiration expiration date (timestamp)
|
2015-12-02 13:04:27 +00:00
|
|
|
* @param string $prefix prefix of mail template files
|
2014-05-11 17:13:51 +00:00
|
|
|
* @return array an array of the html mail body and the plain text mail body
|
2014-02-17 17:26:52 +00:00
|
|
|
*/
|
2014-10-10 13:29:38 +00:00
|
|
|
private function createMailBody($filename, $link, $expiration, $prefix = '') {
|
2015-04-10 15:21:52 +00:00
|
|
|
$formattedDate = $expiration ? $this->l->l('date', $expiration) : null;
|
2014-02-17 17:26:52 +00:00
|
|
|
|
2014-10-10 13:29:38 +00:00
|
|
|
$html = new \OC_Template('core', $prefix . 'mail', '');
|
2014-02-17 17:26:52 +00:00
|
|
|
$html->assign ('link', $link);
|
|
|
|
$html->assign ('user_displayname', $this->senderDisplayName);
|
|
|
|
$html->assign ('filename', $filename);
|
2015-04-10 15:21:52 +00:00
|
|
|
$html->assign('expiration', $formattedDate);
|
2014-02-17 17:26:52 +00:00
|
|
|
$htmlMail = $html->fetchPage();
|
|
|
|
|
2014-10-10 13:29:38 +00:00
|
|
|
$plainText = new \OC_Template('core', $prefix . 'altmail', '');
|
2015-04-10 15:21:52 +00:00
|
|
|
$plainText->assign ('link', $link);
|
|
|
|
$plainText->assign ('user_displayname', $this->senderDisplayName);
|
|
|
|
$plainText->assign ('filename', $filename);
|
|
|
|
$plainText->assign('expiration', $formattedDate);
|
|
|
|
$plainTextMail = $plainText->fetchPage();
|
2014-02-17 17:26:52 +00:00
|
|
|
|
2015-04-10 15:21:52 +00:00
|
|
|
return [$htmlMail, $plainTextMail];
|
2014-02-17 17:26:52 +00:00
|
|
|
}
|
|
|
|
}
|