Skip to content

Commit

Permalink
Fix actionlogs information emails containing HTML links
Browse files Browse the repository at this point in the history
This needed multiple changes:
* MailTemplate now supports an extra set of replacement values for the
  plain-text mode. This will only be used if set explicitely, and will
  fall back to the old behaviour otherwise.
  The 'default' data and the plain data are _not_ merged. Callers have
  to ensure that both sets of values contain the same keys, or otherwise
  output might be missing.
* ActionlogModel uses this new facility to provide values without HTML
  links.
* ActionlogsHelper::getHumanReadableLogMessage already had a parameter
  $generateLinks, that did not do what was documented. It only controlled
  the conversion from relative to absolute URLs. Now it will also remove
  link tags from the message templates, if set to false.
  • Loading branch information
ManuelHu committed Jun 30, 2023
1 parent 4454094 commit 9dab525
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,12 @@ public static function getHumanReadableLogMessage($log, $generateLinks = true)
$messageData['type'] = Text::_($messageData['type']);
}

// Remove links from the message template, if we should not generate links.
if (!$generateLinks) {
$message = preg_replace('/<a href=["\']{.+?}["\']>/', '', $message);
$message = str_replace('</a>', '', $message);
}

$linkMode = Factory::getApplication()->get('force_ssl', 0) >= 1 ? Route::TLS_FORCE : Route::TLS_IGNORE;

foreach ($messageData as $key => $value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ protected function sendNotificationEmails($messages, $username, $context)
$lang->load('com_actionlogs', JPATH_ADMINISTRATOR);
ActionlogsHelper::loadTranslationFiles($extension);
$temp = [];
$tempPlain = [];

foreach ($messages as $message) {
$m = [];
Expand All @@ -159,14 +160,23 @@ protected function sendNotificationEmails($messages, $username, $context)
$m['date'] = HTMLHelper::_('date', $message->log_date, 'Y-m-d H:i:s T', 'UTC');
$m['username'] = $username;
$temp[] = $m;

// copy replacement tags array and set non-HTML message.
$mPlain = array_merge([], $m);
$mPlain['message'] = ActionlogsHelper::getHumanReadableLogMessage($message, false);
$tempPlain[] = $mPlain;
}

$templateData = [
'messages' => $temp,
];
$templateDataPlain = [
'messages' => $tempPlain,
];

$mailer = new MailTemplate('com_actionlogs.notification', $app->getLanguage()->getTag());
$mailer->addTemplateData($templateData);
$mailer->addTemplateData($templateDataPlain, true);

foreach ($recipients as $recipient) {
$mailer->addRecipient($recipient);
Expand Down
20 changes: 17 additions & 3 deletions libraries/src/Mail/MailTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,13 @@ class MailTemplate
*/
protected $data = [];

/**
*
* @var string[]
* @since __DEPLOY_VERSION__
*/
protected $plain_data = [];

/**
*
* @var string[]
Expand Down Expand Up @@ -164,14 +171,19 @@ public function setReplyTo($mail, $name = '')
* Add data to replace in the template
*
* @param array $data Associative array of strings to replace
* @param bool $plain Only use the data for plain-text emails.
*
* @return void
*
* @since 4.0.0
*/
public function addTemplateData($data)
public function addTemplateData($data, $plain = false)
{
$this->data = array_merge($this->data, $data);
if (!$plain) {
$this->data = array_merge($this->data, $data);
} else {
$this->plain_data = array_merge($this->plain_data, $data);
}
}

/**
Expand Down Expand Up @@ -233,7 +245,9 @@ public function send()
$this->mailer->setSubject($subject);

$mailStyle = $config->get('mail_style', 'plaintext');
$plainBody = $this->replaceTags(Text::_($mail->body), $this->data);
// Use the plain-text replacement data, if specified.
$plainData = $this->plain_data ?: $this->data;
$plainBody = $this->replaceTags(Text::_($mail->body), $plainData);
$htmlBody = $this->replaceTags(Text::_($mail->htmlbody), $this->data);

if ($mailStyle === 'plaintext' || $mailStyle === 'both') {
Expand Down

0 comments on commit 9dab525

Please sign in to comment.