You are here

protected function PGPAnalyzer::findBody in Mailhandler 8

Analyzes the body part of the given message.

Parameters

\Drupal\inmail\MIME\MimeMessageInterface $message: The mail message.

\Drupal\inmail\DefaultAnalyzerResult $result: The analyzer result.

array $context: The array with context data.

Return value

string The analyzed message body.

1 call to PGPAnalyzer::findBody()
PGPAnalyzer::analyze in src/Plugin/inmail/Analyzer/PGPAnalyzer.php

File

src/Plugin/inmail/Analyzer/PGPAnalyzer.php, line 193

Class

PGPAnalyzer
An analyzer for PGP signed messages.

Namespace

Drupal\mailhandler\Plugin\inmail\Analyzer

Code

protected function findBody(MimeMessageInterface $message, DefaultAnalyzerResult $result, array &$context) {

  // By default, use original message body.
  $body = $message
    ->getBody();

  // Extract body from PGP/MIME messages.
  if ($result
    ->getContext('pgp')
    ->getContextValue()['pgp_type'] == 'mime') {

    /** @var \Drupal\inmail\MIME\MimeMultipartMessage $message */

    /** @var \Drupal\inmail\MIME\MimeMultipartEntity $signed_message_part */
    $signed_message_part = $message
      ->getPart($context['signed_text_index']);
    $body = '';
    foreach ($signed_message_part
      ->getParts() as $part) {

      // Extract the body from HTML messages.
      if ($part instanceof MimeMultipartEntity) {
        foreach ($part
          ->getParts() as $message_part) {
          if ($message_part
            ->getContentType()['subtype'] == 'html') {
            $body .= $message_part
              ->getBody();
          }
        }
      }
      else {
        $body .= $part
          ->getBody();
      }
    }
  }

  // Support for clear-text signed messages.
  if ($result
    ->getContext('pgp')
    ->getContextValue()['pgp_type'] == 'inline') {

    // Since the message was already checked for valid PGP signature, we
    // can use the analyzed result instead of the raw message body.
    $pgp_parts = explode("-----BEGIN PGP SIGNATURE-----", $result
      ->getContext('pgp')
      ->getContextValue()['signed_text']);

    // Get the message digest by following RFC 4880 recommendations.
    // See https://tools.ietf.org/html/rfc4880#section-7.
    // Remove PGP message header.
    $body = preg_replace("/^.*\n/", "", reset($pgp_parts));

    // In case there is a "Hash" header, remove it.
    $body = preg_replace("/Hash:.*\n/i", "", $body);

    // Remove empty line before the message digest.
    $body = preg_replace("/^.*\n/", "", $body);
  }

  // @todo: Support analysis of unsigned Multipart messages.
  $result
    ->setBody($body);
  return $body;
}