Fix secure Mail addon

This commit is contained in:
Philipp Holzer 2020-01-31 19:32:17 +01:00
parent 4c07c725b6
commit fc370f74ea
No known key found for this signature in database
GPG Key ID: D8365C3D36B77D90
3 changed files with 64 additions and 24 deletions

View File

@ -3,13 +3,14 @@
namespace Friendica\Object\EMail; namespace Friendica\Object\EMail;
use Friendica\Util\Emailer; use Friendica\Util\Emailer;
use JsonSerializable;
/** /**
* Interface for a single mail, which can be send through Emailer::send() * Interface for a single mail, which can be send through Emailer::send()
* *
* @see Emailer::send() * @see Emailer::send()
*/ */
interface IEmail interface IEmail extends JsonSerializable
{ {
/** /**
* Gets the senders name for this email * Gets the senders name for this email
@ -68,4 +69,27 @@ interface IEmail
* @return string * @return string
*/ */
function getAdditionalMailHeader(); 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();
} }

View File

@ -23,7 +23,7 @@ class Email implements IEmail
/** @var string */ /** @var string */
private $subject; private $subject;
/** @var string */ /** @var string|null */
private $msgHtml; private $msgHtml;
/** @var string */ /** @var string */
private $msgText; private $msgText;
@ -117,40 +117,52 @@ class Email implements IEmail
} }
/** /**
* Returns the current email with a new recipient * {@inheritDoc}
*
* @param string $email The email of the recipient
* @param int $uid The (optional) UID of the recipient for further infos
*
* @return static
*/ */
public function withRecipient(string $email, int $uid = null) public function withRecipient(string $address, int $uid = null)
{ {
$newEmail = clone $this; $newEmail = clone $this;
$newEmail->toAddress = $email; $newEmail->toAddress = $address;
$newEmail->toUid = $uid; $newEmail->toUid = $uid;
return $newEmail; return $newEmail;
} }
/** /**
* Creates a new Email instance based on a given prototype * {@inheritDoc}
*
* @param static $prototype The base prototype
* @param array $data The delta-data (key must be an existing property)
*
* @return static The new email instance
*/ */
public static function createFromPrototype(Email $prototype, array $data = []) public function withMessage(string $plaintext, string $html = null)
{ {
$newMail = clone $prototype; $newMail = clone $this;
$newMail->msgText = $plaintext;
foreach ($data as $key => $value) { $newMail->msgHtml = $html;
if (property_exists($newMail, $key)) {
$newMail->{$key} = $value;
}
}
return $newMail; 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();
}
} }

View File

@ -45,8 +45,12 @@ class Emailer
*/ */
public function send(IEmail $email) public function send(IEmail $email)
{ {
$this->logger->warning('start', ['email' => $email]);
Hook::callAll('emailer_send_prepare', $email); Hook::callAll('emailer_send_prepare', $email);
$this->logger->warning('end', ['email' => $email]);
if (empty($email)) { if (empty($email)) {
return true; return true;
} }