2014-02-17 17:26:52 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author Björn Schießle <schiessle@owncloud.com>
|
|
|
|
* @author Joas Schilling <nickvergessen@owncloud.com>
|
2015-06-25 09:43:55 +00:00
|
|
|
* @author Lukas Reschke <lukas@owncloud.com>
|
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>
|
|
|
|
* @author scolebrook <scolebrook@mac.com>
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
|
|
|
* @author Tom Needham <tom@owncloud.com>
|
|
|
|
*
|
2016-01-12 14:02:16 +00:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
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 users if a file was shared with them
|
2014-02-17 17:26:52 +00:00
|
|
|
*
|
2015-12-01 11:05:40 +00:00
|
|
|
* @param IUser[] $recipientList list of recipients
|
2014-05-11 20:51:30 +00:00
|
|
|
* @param string $itemSource shared item source
|
|
|
|
* @param string $itemType shared item type
|
2014-02-17 17:26:52 +00:00
|
|
|
* @return array list of user to whom the mail send operation failed
|
|
|
|
*/
|
|
|
|
public function sendInternalShareMail($recipientList, $itemSource, $itemType) {
|
2015-04-10 15:21:52 +00:00
|
|
|
$noMail = [];
|
2014-02-17 17:26:52 +00:00
|
|
|
|
|
|
|
foreach ($recipientList as $recipient) {
|
2015-12-01 11:05:40 +00:00
|
|
|
$recipientDisplayName = $recipient->getDisplayName();
|
|
|
|
$to = $recipient->getEMailAddress();
|
2014-02-17 17:26:52 +00:00
|
|
|
|
|
|
|
if ($to === '') {
|
|
|
|
$noMail[] = $recipientDisplayName;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-12-02 13:04:27 +00:00
|
|
|
$items = $this->getItemSharedWithUser($itemSource, $itemType, $recipient);
|
2014-02-17 17:26:52 +00:00
|
|
|
$filename = trim($items[0]['file_target'], '/');
|
|
|
|
$subject = (string) $this->l->t('%s shared »%s« with you', array($this->senderDisplayName, $filename));
|
|
|
|
$expiration = null;
|
|
|
|
if (isset($items[0]['expiration'])) {
|
|
|
|
try {
|
|
|
|
$date = new DateTime($items[0]['expiration']);
|
|
|
|
$expiration = $date->getTimestamp();
|
|
|
|
} catch (\Exception $e) {
|
2015-04-10 15:21:52 +00:00
|
|
|
$this->logger->error("Couldn't read date: ".$e->getMessage(), ['app' => 'sharing']);
|
2014-02-17 17:26:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-08 23:08:45 +00:00
|
|
|
// Link to folder, or root folder if a file
|
2014-08-08 23:21:29 +00:00
|
|
|
|
2014-02-17 17:26:52 +00:00
|
|
|
if ($itemType === 'folder') {
|
2014-08-08 23:21:29 +00:00
|
|
|
$args = array(
|
|
|
|
'dir' => $filename,
|
|
|
|
);
|
2015-05-06 13:44:19 +00:00
|
|
|
} else if (strpos($filename, '/')) {
|
|
|
|
$args = array(
|
|
|
|
'dir' => '/' . dirname($filename),
|
|
|
|
'scrollto' => basename($filename),
|
|
|
|
);
|
2014-02-17 17:26:52 +00:00
|
|
|
} else {
|
2014-08-08 23:21:29 +00:00
|
|
|
$args = array(
|
|
|
|
'dir' => '/',
|
|
|
|
'scrollto' => $filename,
|
|
|
|
);
|
2014-02-17 17:26:52 +00:00
|
|
|
}
|
|
|
|
|
2016-03-14 14:19:55 +00:00
|
|
|
$link = $this->urlGenerator->linkToRouteAbsolute(
|
|
|
|
'files.view.index',
|
|
|
|
$args
|
|
|
|
);
|
2014-02-17 17:26:52 +00:00
|
|
|
|
2014-10-10 13:29:38 +00:00
|
|
|
list($htmlBody, $textBody) = $this->createMailBody($filename, $link, $expiration, 'internal');
|
2014-02-17 17:26:52 +00:00
|
|
|
|
|
|
|
// send it out now
|
|
|
|
try {
|
2015-04-10 15:21:52 +00:00
|
|
|
$message = $this->mailer->createMessage();
|
|
|
|
$message->setSubject($subject);
|
|
|
|
$message->setTo([$to => $recipientDisplayName]);
|
|
|
|
$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]);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->mailer->send($message);
|
2014-02-17 17:26:52 +00:00
|
|
|
} catch (\Exception $e) {
|
2015-04-10 15:21:52 +00:00
|
|
|
$this->logger->error("Can't send mail to inform the user about an internal share: ".$e->getMessage(), ['app' => 'sharing']);
|
2014-02-17 17:26:52 +00:00
|
|
|
$noMail[] = $recipientDisplayName;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $noMail;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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
|
|
|
}
|
|
|
|
|
2015-12-01 11:05:40 +00:00
|
|
|
/**
|
2015-12-08 15:01:20 +00:00
|
|
|
* @param string $itemSource
|
|
|
|
* @param string $itemType
|
2015-12-01 11:05:40 +00:00
|
|
|
* @param IUser $recipient
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function getItemSharedWithUser($itemSource, $itemType, $recipient) {
|
|
|
|
return Share::getItemSharedWithUser($itemType, $itemSource, $recipient->getUID());
|
|
|
|
}
|
|
|
|
|
2014-02-17 17:26:52 +00:00
|
|
|
}
|