2014-02-17 21:26:52 +04:00
|
|
|
<?php
|
|
|
|
/**
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Björn Schießle <schiessle@owncloud.com>
|
|
|
|
* @author Joas Schilling <nickvergessen@owncloud.com>
|
2015-06-25 12:43:55 +03:00
|
|
|
* @author Lukas Reschke <lukas@owncloud.com>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
|
|
|
* @author Robin McCorkell <rmccorkell@karoshi.org.uk>
|
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
|
|
|
* @author Tom Needham <tom@owncloud.com>
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2015, ownCloud, Inc.
|
|
|
|
* @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 13:37:37 +03:00
|
|
|
|
2014-02-17 21:26:52 +04:00
|
|
|
namespace OC\Share;
|
|
|
|
|
2014-05-13 14:21:04 +04:00
|
|
|
use DateTime;
|
2015-04-10 18:21:52 +03:00
|
|
|
use OCP\IL10N;
|
2015-12-01 14:05:40 +03:00
|
|
|
use OCP\IUser;
|
2015-04-10 18:21:52 +03:00
|
|
|
use OCP\Mail\IMailer;
|
|
|
|
use OCP\ILogger;
|
|
|
|
use OCP\Defaults;
|
2015-12-01 14:05:40 +03:00
|
|
|
use OCP\Util;
|
2014-05-13 14:21:04 +04:00
|
|
|
|
2015-04-10 18:21:52 +03:00
|
|
|
/**
|
|
|
|
* Class MailNotifications
|
|
|
|
*
|
|
|
|
* @package OC\Share
|
|
|
|
*/
|
2014-02-17 21:26:52 +04:00
|
|
|
class MailNotifications {
|
|
|
|
|
2015-12-01 14:05:40 +03:00
|
|
|
/** @var IUser sender userId */
|
|
|
|
private $user;
|
2015-04-10 18:21:52 +03:00
|
|
|
/** @var string sender email address */
|
|
|
|
private $replyTo;
|
|
|
|
/** @var string */
|
2014-02-17 21:26:52 +04:00
|
|
|
private $senderDisplayName;
|
2015-04-10 18:21:52 +03:00
|
|
|
/** @var IL10N */
|
2014-02-17 21:26:52 +04:00
|
|
|
private $l;
|
2015-04-10 18:21:52 +03:00
|
|
|
/** @var IMailer */
|
|
|
|
private $mailer;
|
|
|
|
/** @var Defaults */
|
|
|
|
private $defaults;
|
|
|
|
/** @var ILogger */
|
|
|
|
private $logger;
|
2014-02-17 21:26:52 +04:00
|
|
|
|
|
|
|
/**
|
2015-12-01 14:05:40 +03:00
|
|
|
* @param IUser $user
|
2015-04-10 18:21:52 +03:00
|
|
|
* @param IL10N $l10n
|
|
|
|
* @param IMailer $mailer
|
|
|
|
* @param ILogger $logger
|
|
|
|
* @param Defaults $defaults
|
2014-02-17 21:26:52 +04:00
|
|
|
*/
|
2015-12-01 14:05:40 +03:00
|
|
|
public function __construct(IUser $user,
|
2015-04-10 18:21:52 +03:00
|
|
|
IL10N $l10n,
|
|
|
|
IMailer $mailer,
|
|
|
|
ILogger $logger,
|
|
|
|
Defaults $defaults) {
|
|
|
|
$this->l = $l10n;
|
2015-12-01 14:05:40 +03:00
|
|
|
$this->user = $user;
|
2015-04-10 18:21:52 +03:00
|
|
|
$this->mailer = $mailer;
|
|
|
|
$this->logger = $logger;
|
|
|
|
$this->defaults = $defaults;
|
|
|
|
|
2015-12-01 14:05:40 +03:00
|
|
|
$this->replyTo = $this->user->getEMailAddress();
|
|
|
|
$this->senderDisplayName = $this->user->getDisplayName();
|
2014-02-17 21:26:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-19 19:50:53 +04:00
|
|
|
* inform users if a file was shared with them
|
2014-02-17 21:26:52 +04:00
|
|
|
*
|
2015-12-01 14:05:40 +03:00
|
|
|
* @param IUser[] $recipientList list of recipients
|
2014-05-12 00:51:30 +04:00
|
|
|
* @param string $itemSource shared item source
|
|
|
|
* @param string $itemType shared item type
|
2014-02-17 21:26:52 +04:00
|
|
|
* @return array list of user to whom the mail send operation failed
|
|
|
|
*/
|
|
|
|
public function sendInternalShareMail($recipientList, $itemSource, $itemType) {
|
2015-04-10 18:21:52 +03:00
|
|
|
$noMail = [];
|
2014-02-17 21:26:52 +04:00
|
|
|
|
|
|
|
foreach ($recipientList as $recipient) {
|
2015-12-01 14:05:40 +03:00
|
|
|
$recipientDisplayName = $recipient->getDisplayName();
|
|
|
|
$to = $recipient->getEMailAddress();
|
2014-02-17 21:26:52 +04:00
|
|
|
|
|
|
|
if ($to === '') {
|
|
|
|
$noMail[] = $recipientDisplayName;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-12-02 16:04:27 +03:00
|
|
|
$items = $this->getItemSharedWithUser($itemSource, $itemType, $recipient);
|
2014-02-17 21:26:52 +04: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 18:21:52 +03:00
|
|
|
$this->logger->error("Couldn't read date: ".$e->getMessage(), ['app' => 'sharing']);
|
2014-02-17 21:26:52 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-09 03:08:45 +04:00
|
|
|
// Link to folder, or root folder if a file
|
2014-08-09 03:21:29 +04:00
|
|
|
|
2014-02-17 21:26:52 +04:00
|
|
|
if ($itemType === 'folder') {
|
2014-08-09 03:21:29 +04:00
|
|
|
$args = array(
|
|
|
|
'dir' => $filename,
|
|
|
|
);
|
2015-05-06 16:44:19 +03:00
|
|
|
} else if (strpos($filename, '/')) {
|
|
|
|
$args = array(
|
|
|
|
'dir' => '/' . dirname($filename),
|
|
|
|
'scrollto' => basename($filename),
|
|
|
|
);
|
2014-02-17 21:26:52 +04:00
|
|
|
} else {
|
2014-08-09 03:21:29 +04:00
|
|
|
$args = array(
|
|
|
|
'dir' => '/',
|
|
|
|
'scrollto' => $filename,
|
|
|
|
);
|
2014-02-17 21:26:52 +04:00
|
|
|
}
|
|
|
|
|
2015-12-02 16:04:27 +03:00
|
|
|
$link = Util::linkToAbsolute('files', 'index.php', $args);
|
2014-02-17 21:26:52 +04:00
|
|
|
|
2014-10-10 17:29:38 +04:00
|
|
|
list($htmlBody, $textBody) = $this->createMailBody($filename, $link, $expiration, 'internal');
|
2014-02-17 21:26:52 +04:00
|
|
|
|
|
|
|
// send it out now
|
|
|
|
try {
|
2015-04-10 18:21:52 +03:00
|
|
|
$message = $this->mailer->createMessage();
|
|
|
|
$message->setSubject($subject);
|
|
|
|
$message->setTo([$to => $recipientDisplayName]);
|
|
|
|
$message->setHtmlBody($htmlBody);
|
|
|
|
$message->setPlainBody($textBody);
|
|
|
|
$message->setFrom([
|
2015-12-02 16:04:27 +03:00
|
|
|
Util::getDefaultEmailAddress('sharing-noreply') =>
|
2015-04-10 18:21:52 +03: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 21:26:52 +04:00
|
|
|
} catch (\Exception $e) {
|
2015-04-10 18:21:52 +03:00
|
|
|
$this->logger->error("Can't send mail to inform the user about an internal share: ".$e->getMessage(), ['app' => 'sharing']);
|
2014-02-17 21:26:52 +04:00
|
|
|
$noMail[] = $recipientDisplayName;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $noMail;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-19 19:50:53 +04:00
|
|
|
* inform recipient about public link share
|
2014-02-17 21:26:52 +04:00
|
|
|
*
|
2014-03-11 00:54:23 +04:00
|
|
|
* @param string $recipient recipient email address
|
2014-02-17 21:26:52 +04:00
|
|
|
* @param string $filename the shared file
|
|
|
|
* @param string $link the public link
|
|
|
|
* @param int $expiration expiration date (timestamp)
|
2015-12-08 18:01:20 +03:00
|
|
|
* @return string[] $result of failed recipients
|
2014-02-17 21:26:52 +04:00
|
|
|
*/
|
|
|
|
public function sendLinkShareMail($recipient, $filename, $link, $expiration) {
|
2015-04-10 18:21:52 +03: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 12:49:34 +03:00
|
|
|
$recipient = str_replace([', ', '; ', ',', ';', ' '], ',', $recipient);
|
|
|
|
$recipients = explode(',', $recipient);
|
2015-04-10 18:21:52 +03:00
|
|
|
try {
|
|
|
|
$message = $this->mailer->createMessage();
|
|
|
|
$message->setSubject($subject);
|
2015-12-07 12:49:34 +03:00
|
|
|
$message->setTo($recipients);
|
2015-04-10 18:21:52 +03:00
|
|
|
$message->setHtmlBody($htmlBody);
|
|
|
|
$message->setPlainBody($textBody);
|
|
|
|
$message->setFrom([
|
2015-12-02 16:04:27 +03:00
|
|
|
Util::getDefaultEmailAddress('sharing-noreply') =>
|
2015-04-10 18:21:52 +03:00
|
|
|
(string)$this->l->t('%s via %s', [
|
|
|
|
$this->senderDisplayName,
|
|
|
|
$this->defaults->getName()
|
|
|
|
]),
|
|
|
|
]);
|
|
|
|
if(!is_null($this->replyTo)) {
|
|
|
|
$message->setReplyTo([$this->replyTo]);
|
2014-03-11 00:54:23 +04:00
|
|
|
}
|
2015-04-10 18:21:52 +03: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 21:26:52 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-05-19 19:50:53 +04:00
|
|
|
* create mail body for plain text and html mail
|
2014-02-17 21:26:52 +04:00
|
|
|
*
|
|
|
|
* @param string $filename the shared file
|
|
|
|
* @param string $link link to the shared file
|
|
|
|
* @param int $expiration expiration date (timestamp)
|
2015-12-02 16:04:27 +03:00
|
|
|
* @param string $prefix prefix of mail template files
|
2014-05-11 21:13:51 +04:00
|
|
|
* @return array an array of the html mail body and the plain text mail body
|
2014-02-17 21:26:52 +04:00
|
|
|
*/
|
2014-10-10 17:29:38 +04:00
|
|
|
private function createMailBody($filename, $link, $expiration, $prefix = '') {
|
2015-04-10 18:21:52 +03:00
|
|
|
$formattedDate = $expiration ? $this->l->l('date', $expiration) : null;
|
2014-02-17 21:26:52 +04:00
|
|
|
|
2014-10-10 17:29:38 +04:00
|
|
|
$html = new \OC_Template('core', $prefix . 'mail', '');
|
2014-02-17 21:26:52 +04:00
|
|
|
$html->assign ('link', $link);
|
|
|
|
$html->assign ('user_displayname', $this->senderDisplayName);
|
|
|
|
$html->assign ('filename', $filename);
|
2015-04-10 18:21:52 +03:00
|
|
|
$html->assign('expiration', $formattedDate);
|
2014-02-17 21:26:52 +04:00
|
|
|
$htmlMail = $html->fetchPage();
|
|
|
|
|
2014-10-10 17:29:38 +04:00
|
|
|
$plainText = new \OC_Template('core', $prefix . 'altmail', '');
|
2015-04-10 18:21:52 +03: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 21:26:52 +04:00
|
|
|
|
2015-04-10 18:21:52 +03:00
|
|
|
return [$htmlMail, $plainTextMail];
|
2014-02-17 21:26:52 +04:00
|
|
|
}
|
|
|
|
|
2015-12-01 14:05:40 +03:00
|
|
|
/**
|
2015-12-08 18:01:20 +03:00
|
|
|
* @param string $itemSource
|
|
|
|
* @param string $itemType
|
2015-12-01 14:05:40 +03:00
|
|
|
* @param IUser $recipient
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function getItemSharedWithUser($itemSource, $itemType, $recipient) {
|
|
|
|
return Share::getItemSharedWithUser($itemType, $itemSource, $recipient->getUID());
|
|
|
|
}
|
|
|
|
|
2014-02-17 21:26:52 +04:00
|
|
|
}
|