From fc370f74ea28a7080080f286a3f1cfd18a484950 Mon Sep 17 00:00:00 2001 From: nupplaPhil Date: Fri, 31 Jan 2020 19:32:17 +0100 Subject: [PATCH 1/3] Fix secure Mail addon --- src/Object/EMail/IEmail.php | 26 ++++++++++++++++- src/Object/Email.php | 58 ++++++++++++++++++++++--------------- src/Util/Emailer.php | 4 +++ 3 files changed, 64 insertions(+), 24 deletions(-) diff --git a/src/Object/EMail/IEmail.php b/src/Object/EMail/IEmail.php index 6a159c0ba..ec8ae8815 100644 --- a/src/Object/EMail/IEmail.php +++ b/src/Object/EMail/IEmail.php @@ -3,13 +3,14 @@ namespace Friendica\Object\EMail; use Friendica\Util\Emailer; +use JsonSerializable; /** * Interface for a single mail, which can be send through Emailer::send() * * @see Emailer::send() */ -interface IEmail +interface IEmail extends JsonSerializable { /** * Gets the senders name for this email @@ -68,4 +69,27 @@ interface IEmail * @return string */ function getAdditionalMailHeader(); + + /** + * Returns the current email with a new recipient + * + * @param string $address The email of the recipient + * @param int $uid The (optional) UID of the recipient for further infos + * + * @return static + */ + function withRecipient(string $address, int $uid); + + /** + * @param string $plaintext a new plaintext message for this email + * @param string $html a new html message for this email (optional) + * + * @return static + */ + function withMessage(string $plaintext, string $html = null); + + /** + * @return string + */ + function __toString(); } diff --git a/src/Object/Email.php b/src/Object/Email.php index 4f9c4e7bd..32d138283 100644 --- a/src/Object/Email.php +++ b/src/Object/Email.php @@ -23,7 +23,7 @@ class Email implements IEmail /** @var string */ private $subject; - /** @var string */ + /** @var string|null */ private $msgHtml; /** @var string */ private $msgText; @@ -117,40 +117,52 @@ class Email implements IEmail } /** - * Returns the current email with a new recipient - * - * @param string $email The email of the recipient - * @param int $uid The (optional) UID of the recipient for further infos - * - * @return static + * {@inheritDoc} */ - public function withRecipient(string $email, int $uid = null) + public function withRecipient(string $address, int $uid = null) { $newEmail = clone $this; - $newEmail->toAddress = $email; + $newEmail->toAddress = $address; $newEmail->toUid = $uid; return $newEmail; } /** - * Creates a new Email instance based on a given prototype - * - * @param static $prototype The base prototype - * @param array $data The delta-data (key must be an existing property) - * - * @return static The new email instance + * {@inheritDoc} */ - public static function createFromPrototype(Email $prototype, array $data = []) + public function withMessage(string $plaintext, string $html = null) { - $newMail = clone $prototype; - - foreach ($data as $key => $value) { - if (property_exists($newMail, $key)) { - $newMail->{$key} = $value; - } - } + $newMail = clone $this; + $newMail->msgText = $plaintext; + $newMail->msgHtml = $html; return $newMail; } + + /** + * Returns the properties of the email as an array + * + * @return array + */ + private function toArray() + { + return get_object_vars($this); + } + + /** + * @inheritDoc + */ + public function __toString() + { + return json_encode($this->toArray()); + } + + /** + * @inheritDoc + */ + public function jsonSerialize() + { + return $this->toArray(); + } } diff --git a/src/Util/Emailer.php b/src/Util/Emailer.php index 19755bebd..cb59d4839 100644 --- a/src/Util/Emailer.php +++ b/src/Util/Emailer.php @@ -45,8 +45,12 @@ class Emailer */ public function send(IEmail $email) { + $this->logger->warning('start', ['email' => $email]); + Hook::callAll('emailer_send_prepare', $email); + $this->logger->warning('end', ['email' => $email]); + if (empty($email)) { return true; } From 9ba8735b4a9ec8452254503bfb8a7d304b88fb25 Mon Sep 17 00:00:00 2001 From: nupplaPhil Date: Fri, 31 Jan 2020 19:40:50 +0100 Subject: [PATCH 2/3] fix logger settings --- src/Util/Emailer.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Util/Emailer.php b/src/Util/Emailer.php index cb59d4839..66ebcc307 100644 --- a/src/Util/Emailer.php +++ b/src/Util/Emailer.php @@ -45,11 +45,11 @@ class Emailer */ public function send(IEmail $email) { - $this->logger->warning('start', ['email' => $email]); + $this->logger->debug('start emailing', ['email' => $email]); Hook::callAll('emailer_send_prepare', $email); - $this->logger->warning('end', ['email' => $email]); + $this->logger->debug('End Hook call', ['email' => $email]); if (empty($email)) { return true; From 0c01512c5de5952329a3642641ae15d194ad8e17 Mon Sep 17 00:00:00 2001 From: nupplaPhil Date: Fri, 31 Jan 2020 19:43:48 +0100 Subject: [PATCH 3/3] Remove logging --- src/Util/Emailer.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/Util/Emailer.php b/src/Util/Emailer.php index 66ebcc307..19755bebd 100644 --- a/src/Util/Emailer.php +++ b/src/Util/Emailer.php @@ -45,12 +45,8 @@ class Emailer */ public function send(IEmail $email) { - $this->logger->debug('start emailing', ['email' => $email]); - Hook::callAll('emailer_send_prepare', $email); - $this->logger->debug('End Hook call', ['email' => $email]); - if (empty($email)) { return true; }