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>
|
|
|
|
* @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>
|
2015-04-10 18:21:52 +03:00
|
|
|
* @author Lukas Reschke <lukas@owncloud.com>
|
2015-03-26 13:44:34 +03:00
|
|
|
*
|
|
|
|
* @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\IConfig;
|
|
|
|
use OCP\IL10N;
|
|
|
|
use OCP\Mail\IMailer;
|
|
|
|
use OCP\ILogger;
|
|
|
|
use OCP\Defaults;
|
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-04-10 18:21:52 +03:00
|
|
|
/** @var string sender userId */
|
|
|
|
private $userId;
|
|
|
|
/** @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 IConfig */
|
|
|
|
private $config;
|
|
|
|
/** @var IMailer */
|
|
|
|
private $mailer;
|
|
|
|
/** @var Defaults */
|
|
|
|
private $defaults;
|
|
|
|
/** @var ILogger */
|
|
|
|
private $logger;
|
2014-02-17 21:26:52 +04:00
|
|
|
|
|
|
|
/**
|
2015-04-10 18:21:52 +03:00
|
|
|
* @param string $uid user id
|
|
|
|
* @param IConfig $config
|
|
|
|
* @param IL10N $l10n
|
|
|
|
* @param IMailer $mailer
|
|
|
|
* @param ILogger $logger
|
|
|
|
* @param Defaults $defaults
|
2014-02-17 21:26:52 +04:00
|
|
|
*/
|
2015-04-10 18:21:52 +03:00
|
|
|
public function __construct($uid,
|
|
|
|
IConfig $config,
|
|
|
|
IL10N $l10n,
|
|
|
|
IMailer $mailer,
|
|
|
|
ILogger $logger,
|
|
|
|
Defaults $defaults) {
|
|
|
|
$this->l = $l10n;
|
|
|
|
$this->userId = $uid;
|
|
|
|
$this->config = $config;
|
|
|
|
$this->mailer = $mailer;
|
|
|
|
$this->logger = $logger;
|
|
|
|
$this->defaults = $defaults;
|
|
|
|
|
|
|
|
$this->replyTo = $this->config->getUserValue($this->userId, 'settings', 'email', null);
|
|
|
|
$this->senderDisplayName = \OCP\User::getDisplayName($this->userId);
|
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
|
|
|
*
|
|
|
|
* @param array $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) {
|
|
|
|
$recipientDisplayName = \OCP\User::getDisplayName($recipient);
|
2015-04-10 18:21:52 +03:00
|
|
|
$to = $this->config->getUserValue($recipient, 'settings', 'email', '');
|
2014-02-17 21:26:52 +04:00
|
|
|
|
|
|
|
if ($to === '') {
|
|
|
|
$noMail[] = $recipientDisplayName;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$items = \OCP\Share::getItemSharedWithUser($itemType, $itemSource, $recipient);
|
|
|
|
$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,
|
|
|
|
);
|
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
|
|
|
}
|
|
|
|
|
2014-08-09 03:21:29 +04:00
|
|
|
$link = \OCP\Util::linkToAbsolute('files', 'index.php', $args);
|
2014-02-17 21:26:52 +04:00
|
|
|
|
2015-04-10 18:21:52 +03:00
|
|
|
list($htmlBody, $textBody) = $this->createMailBody($filename, $link, $expiration);
|
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([
|
|
|
|
\OCP\Util::getDefaultEmailAddress('sharing-noreply') =>
|
|
|
|
(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)
|
2014-03-11 00:54:23 +04:00
|
|
|
* @return array $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);
|
|
|
|
|
|
|
|
try {
|
|
|
|
$message = $this->mailer->createMessage();
|
|
|
|
$message->setSubject($subject);
|
|
|
|
$message->setTo([$recipient]);
|
|
|
|
$message->setHtmlBody($htmlBody);
|
|
|
|
$message->setPlainBody($textBody);
|
|
|
|
$message->setFrom([
|
|
|
|
\OCP\Util::getDefaultEmailAddress('sharing-noreply') =>
|
|
|
|
(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)
|
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
|
|
|
*/
|
|
|
|
private function createMailBody($filename, $link, $expiration) {
|
|
|
|
|
2015-04-10 18:21:52 +03:00
|
|
|
$formattedDate = $expiration ? $this->l->l('date', $expiration) : null;
|
2014-02-17 21:26:52 +04:00
|
|
|
|
|
|
|
$html = new \OC_Template("core", "mail", "");
|
|
|
|
$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();
|
|
|
|
|
2015-04-10 18:21:52 +03:00
|
|
|
$plainText = new \OC_Template("core", "altmail", "");
|
|
|
|
$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
|
|
|
}
|
|
|
|
|
|
|
|
}
|