]> source.dussan.org Git - nextcloud-server.git/commitdiff
send mail for share-by-mail shares
authorBjoern Schiessle <bjoern@schiessle.org>
Wed, 26 Oct 2016 09:04:00 +0000 (11:04 +0200)
committerBjoern Schiessle <bjoern@schiessle.org>
Tue, 1 Nov 2016 18:54:40 +0000 (19:54 +0100)
Signed-off-by: Bjoern Schiessle <bjoern@schiessle.org>
apps/sharebymail/lib/ShareByMailProvider.php
apps/sharebymail/templates/altmail.php [new file with mode: 0644]
apps/sharebymail/templates/mail.php [new file with mode: 0644]
lib/private/Share20/ProviderFactory.php

index e432ab139edca44e369037ea44fc09aec31f3907..c0c21bdb16cbe96f9729d3d8520ef212f523c869 100644 (file)
 
 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
+                       $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 $hintException;
                } catch (\Exception $e) {
-                       $this->logger->error('Failed to notify remote server of federated share, removing share (' . $e->getMessage() . ')');
+                       $this->logger->error('Failed to send share by mail: ' . $e->getMessage());
                        $this->removeShareFromTable($shareId);
-                       throw $e;
+                       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
         *
diff --git a/apps/sharebymail/templates/altmail.php b/apps/sharebymail/templates/altmail.php
new file mode 100644 (file)
index 0000000..02d262d
--- /dev/null
@@ -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());
diff --git a/apps/sharebymail/templates/mail.php b/apps/sharebymail/templates/mail.php
new file mode 100644 (file)
index 0000000..48f7dcb
--- /dev/null
@@ -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>
index e1ed64d66adfb5890cc84d9a5aa66e9b39c9c8a8..457cf117c6994e6278b44d387aaea3bde4bcebb4 100644 (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()
                        );
                }