2014-02-17 21:26:52 +04:00
|
|
|
<?php
|
|
|
|
/**
|
2015-02-23 13:28:53 +03:00
|
|
|
* @author Bjoern Schiessle <schiessle@owncloud.com>
|
|
|
|
* @author Joas Schilling <nickvergessen@gmx.de>
|
|
|
|
* @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/>
|
|
|
|
*
|
|
|
|
*/
|
2014-02-17 21:26:52 +04:00
|
|
|
namespace OC\Share;
|
|
|
|
|
2014-05-13 14:21:04 +04:00
|
|
|
use DateTime;
|
|
|
|
|
2014-02-17 21:26:52 +04:00
|
|
|
class MailNotifications {
|
|
|
|
|
2014-05-13 14:21:04 +04:00
|
|
|
/**
|
|
|
|
* sender userId
|
|
|
|
* @var null|string
|
|
|
|
*/
|
|
|
|
private $senderId;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* sender email address
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $from;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
2014-02-17 21:26:52 +04:00
|
|
|
private $senderDisplayName;
|
2014-05-13 14:21:04 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \OC_L10N
|
|
|
|
*/
|
2014-02-17 21:26:52 +04:00
|
|
|
private $l;
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param string $sender user id (if nothing is set we use the currently logged-in user)
|
|
|
|
*/
|
|
|
|
public function __construct($sender = null) {
|
2014-09-04 13:23:23 +04:00
|
|
|
$this->l = \OC::$server->getL10N('lib');
|
2014-02-17 21:26:52 +04:00
|
|
|
|
|
|
|
$this->senderId = $sender;
|
|
|
|
|
|
|
|
$this->from = \OCP\Util::getDefaultEmailAddress('sharing-noreply');
|
|
|
|
if ($this->senderId) {
|
|
|
|
$this->from = \OCP\Config::getUserValue($this->senderId, 'settings', 'email', $this->from);
|
|
|
|
$this->senderDisplayName = \OCP\User::getDisplayName($this->senderId);
|
|
|
|
} else {
|
|
|
|
$this->senderDisplayName = \OCP\User::getDisplayName();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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) {
|
|
|
|
|
|
|
|
$noMail = array();
|
|
|
|
|
|
|
|
foreach ($recipientList as $recipient) {
|
|
|
|
$recipientDisplayName = \OCP\User::getDisplayName($recipient);
|
2014-12-04 18:48:07 +03:00
|
|
|
$to = \OC::$server->getConfig()->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) {
|
|
|
|
\OCP\Util::writeLog('sharing', "Couldn't read date: " . $e->getMessage(), \OCP\Util::ERROR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
list($htmlMail, $alttextMail) = $this->createMailBody($filename, $link, $expiration);
|
|
|
|
|
|
|
|
// send it out now
|
|
|
|
try {
|
|
|
|
\OCP\Util::sendMail($to, $recipientDisplayName, $subject, $htmlMail, $this->from, $this->senderDisplayName, 1, $alttextMail);
|
|
|
|
} catch (\Exception $e) {
|
2014-03-11 00:54:23 +04:00
|
|
|
\OCP\Util::writeLog('sharing', "Can't send mail to inform the user about an internal share: " . $e->getMessage() , \OCP\Util::ERROR);
|
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) {
|
|
|
|
$subject = (string)$this->l->t('%s shared »%s« with you', array($this->senderDisplayName, $filename));
|
|
|
|
list($htmlMail, $alttextMail) = $this->createMailBody($filename, $link, $expiration);
|
2014-03-11 00:54:23 +04:00
|
|
|
$rs = explode(' ', $recipient);
|
|
|
|
$failed = array();
|
|
|
|
foreach ($rs as $r) {
|
|
|
|
try {
|
|
|
|
\OCP\Util::sendMail($r, $r, $subject, $htmlMail, $this->from, $this->senderDisplayName, 1, $alttextMail);
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
\OCP\Util::writeLog('sharing', "Can't send mail with public link to $r: " . $e->getMessage(), \OCP\Util::ERROR);
|
|
|
|
$failed[] = $r;
|
|
|
|
}
|
2014-02-17 21:26:52 +04:00
|
|
|
}
|
2014-03-11 00:54:23 +04:00
|
|
|
return $failed;
|
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) {
|
|
|
|
|
|
|
|
$formatedDate = $expiration ? $this->l->l('date', $expiration) : null;
|
|
|
|
|
|
|
|
$html = new \OC_Template("core", "mail", "");
|
|
|
|
$html->assign ('link', $link);
|
|
|
|
$html->assign ('user_displayname', $this->senderDisplayName);
|
|
|
|
$html->assign ('filename', $filename);
|
|
|
|
$html->assign('expiration', $formatedDate);
|
|
|
|
$htmlMail = $html->fetchPage();
|
|
|
|
|
|
|
|
$alttext = new \OC_Template("core", "altmail", "");
|
|
|
|
$alttext->assign ('link', $link);
|
|
|
|
$alttext->assign ('user_displayname', $this->senderDisplayName);
|
|
|
|
$alttext->assign ('filename', $filename);
|
|
|
|
$alttext->assign('expiration', $formatedDate);
|
|
|
|
$alttextMail = $alttext->fetchPage();
|
|
|
|
|
|
|
|
return array($htmlMail, $alttextMail);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|