send mail for share-by-mail shares

Signed-off-by: Bjoern Schiessle <bjoern@schiessle.org>
This commit is contained in:
Bjoern Schiessle 2016-10-26 11:04:00 +02:00
parent 7436e45c91
commit 31c8c38bd6
No known key found for this signature in database
GPG key ID: 2378A753E2BF04F6
4 changed files with 181 additions and 8 deletions

View file

@ -21,12 +21,15 @@
namespace OCA\ShareByMail;
use OC\HintException;
use OCP\Files\IRootFolder;
use OCP\Files\Node;
use OCP\IDBConnection;
use OCP\IL10N;
use OCP\ILogger;
use OCP\IURLGenerator;
use OCP\IUserManager;
use OCP\Mail\IMailer;
use OCP\Security\ISecureRandom;
use OC\Share20\Share;
use OCP\Share\IShare;
@ -57,6 +60,12 @@ class ShareByMailProvider implements IShareProvider {
/** @var IL10N */
private $l;
/** @var IMailer */
private $mailer;
/** @var IURLGenerator */
private $urlGenerator;
/**
* Return the identifier of this provider.
*
@ -75,6 +84,8 @@ class ShareByMailProvider implements IShareProvider {
* @param IRootFolder $rootFolder
* @param IL10N $l
* @param ILogger $logger
* @param IMailer $mailer
* @param IURLGenerator $urlGenerator
*/
public function __construct(
IDBConnection $connection,
@ -82,7 +93,9 @@ class ShareByMailProvider implements IShareProvider {
IUserManager $userManager,
IRootFolder $rootFolder,
IL10N $l,
ILogger $logger
ILogger $logger,
IMailer $mailer,
IURLGenerator $urlGenerator
) {
$this->dbConnection = $connection;
$this->secureRandom = $secureRandom;
@ -90,6 +103,8 @@ class ShareByMailProvider implements IShareProvider {
$this->rootFolder = $rootFolder;
$this->l = $l;
$this->logger = $logger;
$this->mailer = $mailer;
$this->urlGenerator = $urlGenerator;
}
/**
@ -127,7 +142,7 @@ class ShareByMailProvider implements IShareProvider {
* @throws \Exception
*/
private function createMailShare(IShare $share) {
$token = $this->generateToken();
$share->setToken($this->generateToken());
$shareId = $this->addShareToDB(
$share->getNodeId(),
$share->getNodeType(),
@ -135,21 +150,77 @@ class ShareByMailProvider implements IShareProvider {
$share->getSharedBy(),
$share->getShareOwner(),
$share->getPermissions(),
$token
$share->getToken()
);
try {
// TODO: Send email with public link
} catch (\Exception $e) {
$this->logger->error('Failed to notify remote server of federated share, removing share (' . $e->getMessage() . ')');
$link = $this->urlGenerator->linkToRouteAbsolute('files_sharing.sharecontroller.showShare',
['token' => $share->getToken()]);
$this->sendMailNotification($share->getNode()->getName(),
$link,
$share->getShareOwner(),
$share->getSharedBy(), $share->getSharedWith());
} catch (HintException $hintException) {
$this->logger->error('Failed to send share by mail: ' . $hintException->getMessage());
$this->removeShareFromTable($shareId);
throw $e;
throw $hintException;
} catch (\Exception $e) {
$this->logger->error('Failed to send share by mail: ' . $e->getMessage());
$this->removeShareFromTable($shareId);
throw new HintException('Failed to send share by mail',
$this->l->t('Failed to send share by E-mail'));
}
return $shareId;
}
private function sendMailNotification($filename, $link, $owner, $initiator, $shareWith) {
if ($owner === $initiator) {
$subject = (string)$this->l->t('%s shared »%s« with you', array($owner, $filename));
} else {
$subject = (string)$this->l->t('%s shared »%s« with you on behalf of %s', array($owner, $filename, $initiator));
}
$message = $this->mailer->createMessage();
$htmlBody = $this->createMailBody('mail', $filename, $link, $owner, $initiator);
$textBody = $this->createMailBody('altmail', $filename, $link, $owner, $initiator);
$message->setTo([$shareWith]);
$message->setSubject($subject);
$message->setBody($textBody, 'text/plain');
$message->setHtmlBody($htmlBody);
$this->mailer->send($message);
}
/**
* create mail body
*
* @param $filename
* @param $link
* @param $owner
* @param $initiator
* @return string plain text mail
* @throws HintException
*/
protected function createMailBody($template, $filename, $link, $owner, $initiator) {
$mailBodyTemplate = new \OC_Template('sharebymail', $template, '');
$mailBodyTemplate->assign ('filename', $filename);
$mailBodyTemplate->assign ('link', $link);
$mailBodyTemplate->assign ('owner', $owner);
$mailBodyTemplate->assign ('initiator', $initiator);
$mailBodyTemplate->assign ('onBehalfOf', $initiator !== $owner);
$mailBody = $mailBodyTemplate->fetchPage();
if (is_string($mailBody)) {
return $mailBody;
}
throw new HintException('Failed to create the E-mail',
$this->l->t('Failed to create the E-mail'));
}
/**
* generate share token
*

View file

@ -0,0 +1,36 @@
<?php
/**
* @copyright Copyright (c) 2016 Bjoern Schiessle <bjoern@schiessle.org>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* 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
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/** @var OC_Theme $theme */
/** @var array $_ */
if ($_['onBehalfOf']) {
print_unescaped($l->t("Hey there,\n\n%s shared »%s« with you on behalf of %s.\n\n%s\n\n", [$_['owner'], $_['filename'], $_['initiator'], $_['link']]));
} else {
print_unescaped($l->t("Hey there,\n\n%s shared »%s« with you.\n\n%s\n\n", [$_['owner'], $_['filename'], $_['link']]));
}
// TRANSLATORS term at the end of a mail
p($l->t("Cheers!"));
print_unescaped("\n");
?>
--
<?php p($theme->getName() . ' - ' . $theme->getSlogan()); ?>
<?php print_unescaped("\n".$theme->getBaseUrl());

View file

@ -0,0 +1,64 @@
<?php
/**
* @copyright Copyright (c) 2016 Bjoern Schiessle <bjoern@schiessle.org>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* 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
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/** @var OC_Theme $theme */
/** @var array $_ */
?>
<table cellspacing="0" cellpadding="0" border="0" width="100%">
<tr><td>
<table cellspacing="0" cellpadding="0" border="0" width="600px">
<tr>
<td bgcolor="<?php p($theme->getMailHeaderColor());?>" width="20px">&nbsp;</td>
<td bgcolor="<?php p($theme->getMailHeaderColor());?>">
<img src="<?php p(\OC::$server->getURLGenerator()->getAbsoluteURL(image_path('', 'logo-mail.gif'))); ?>" alt="<?php p($theme->getName()); ?>"/>
</td>
</tr>
<tr><td colspan="2">&nbsp;</td></tr>
<tr>
<td width="20px">&nbsp;</td>
<td style="font-weight:normal; font-size:0.8em; line-height:1.2em; font-family:verdana,'arial',sans;">
<?php
if ($_['onBehalfOf']) {
print_unescaped($l->t('Hey there,<br><br>%s shared <a href="%s">%s</a> with you on behalf of %s.<br><br>', [$_['owner'], $_['link'], $_['filename'], $_['initiator']]));
} else {
print_unescaped($l->t('Hey there,<br><br>%s shared <a href="%s">%s</a> with you.<br><br>', [$_['owner'], $_['link'], $_['filename']]));
}
// TRANSLATORS term at the end of a mail
p($l->t('Cheers!'));
?>
</td>
</tr>
<tr><td colspan="2">&nbsp;</td></tr>
<tr>
<td width="20px">&nbsp;</td>
<td style="font-weight:normal; font-size:0.8em; line-height:1.2em; font-family:verdana,'arial',sans;">--<br>
<?php p($theme->getName()); ?> -
<?php p($theme->getSlogan()); ?>
<br><a href="<?php p($theme->getBaseUrl()); ?>"><?php p($theme->getBaseUrl());?></a>
</td>
</tr>
<tr>
<td colspan="2">&nbsp;</td>
</tr>
</table>
</td></tr>
</table>

View file

@ -151,7 +151,9 @@ class ProviderFactory implements IProviderFactory {
$this->serverContainer->getUserManager(),
$this->serverContainer->getLazyRootFolder(),
$l,
$this->serverContainer->getLogger()
$this->serverContainer->getLogger(),
$this->serverContainer->getMailer(),
$this->serverContainer->getURLGenerator()
);
}