Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Merge remote-tracking branch 'Maks3w/use-headers-container-in-zend-mime'
Browse files Browse the repository at this point in the history
  • Loading branch information
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 38 deletions.
38 changes: 10 additions & 28 deletions src/Decode.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

namespace Zend\Mime;

use Zend\Mail\Headers;

/**
* @category Zend
* @package Zend_Mime
Expand Down Expand Up @@ -106,14 +108,17 @@ public static function splitMessageStruct($message, $boundary, $EOL = Mime::LINE
*
* The charset of the returned headers depend on your iconv settings.
*
* @param string $message raw message with header and optional content
* @param array $headers output param, array with headers as array(name => value)
* @param string $body output param, content of message
* @param string $EOL EOL string; defaults to {@link Zend_Mime::LINEEND}
* @param string|Headers $message raw message with header and optional content
* @param Headers $headers output param, headers container
* @param string $body output param, content of message
* @param string $EOL EOL string; defaults to {@link Zend_Mime::LINEEND}
* @return null
*/
public static function splitMessage($message, &$headers, &$body, $EOL = Mime::LINEEND)
{
if ($message instanceof Headers) {
$message = $message->toString();
}
// check for valid header at first line
$firstline = strtok($message, "\n");
if (!preg_match('%^[^\s]+[^:]*:%', $firstline)) {
Expand All @@ -138,30 +143,7 @@ public static function splitMessage($message, &$headers, &$body, $EOL = Mime::LI
@list($headers, $body) = @preg_split("%([\r\n]+)\\1%U", $message, 2);
}

$headers = iconv_mime_decode_headers($headers, ICONV_MIME_DECODE_CONTINUE_ON_ERROR, 'UTF-8');

if ($headers === false) {
// an error occurs during the decoding
return;
}

// normalize header names
foreach ($headers as $name => $header) {
$lower = strtolower($name);
if ($lower == $name) {
continue;
}
unset($headers[$name]);
if (!isset($headers[$lower])) {
$headers[$lower] = $header;
continue;
}
if (is_array($headers[$lower])) {
$headers[$lower][] = $header;
continue;
}
$headers[$lower] = array($headers[$lower], $header);
}
$headers = Headers::fromString($headers, $EOL);
}

/**
Expand Down
24 changes: 14 additions & 10 deletions src/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,34 +237,38 @@ public static function createFromMessage($message, $boundary, $EOL = Mime::LINEE
foreach ($parts as $part) {
// now we build a new MimePart for the current Message Part:
$newPart = new Part($part['body']);
foreach ($part['header'] as $key => $value) {
foreach ($part['header'] as $header) {
/** @var \Zend\Mail\Header\HeaderInterface $header */
/**
* @todo check for characterset and filename
*/
switch(strtolower($key)) {

$fieldName = $header->getFieldName();
$fieldValue = $header->getFieldValue();
switch (strtolower($fieldName)) {
case 'content-type':
$newPart->type = $value;
$newPart->type = $fieldValue;
break;
case 'content-transfer-encoding':
$newPart->encoding = $value;
$newPart->encoding = $fieldValue;
break;
case 'content-id':
$newPart->id = trim($value,'<>');
$newPart->id = trim($fieldValue,'<>');
break;
case 'content-disposition':
$newPart->disposition = $value;
$newPart->disposition = $fieldValue;
break;
case 'content-description':
$newPart->description = $value;
$newPart->description = $fieldValue;
break;
case 'content-location':
$newPart->location = $value;
$newPart->location = $fieldValue;
break;
case 'content-language':
$newPart->language = $value;
$newPart->language = $fieldValue;
break;
default:
throw new Exception\RuntimeException('Unknown header ignored for MimePart:' . $key);
throw new Exception\RuntimeException('Unknown header ignored for MimePart:' . $fieldName);
}
}
$res->addPart($newPart);
Expand Down

0 comments on commit aaa5805

Please sign in to comment.