fixed parsing of mime headers

This commit is contained in:
Andreas Schamberger 2011-10-31 11:54:41 +01:00
parent a1972a209c
commit 4becca0208
1 changed files with 17 additions and 15 deletions

View File

@ -49,8 +49,9 @@ class Parser
$currentPart = $multipart; $currentPart = $multipart;
$lines = preg_split("/\r\n|\n/", $mimeMessage); $lines = preg_split("/\r\n|\n/", $mimeMessage);
foreach ($lines as $line) { foreach ($lines as $line) {
// ignore http status code // ignore http status code and POST *
if (substr($line, 0, 5) == 'HTTP/') { if ( substr( $line, 0, 5 ) == 'HTTP/' || substr( $line, 0, 4 ) == 'POST')
{
continue; continue;
} }
if (isset($currentHeader)) { if (isset($currentHeader)) {
@ -58,14 +59,16 @@ class Parser
$currentHeader .= $line; $currentHeader .= $line;
continue; continue;
} }
list($headerName, $headerValue) = explode(':', $currentHeader, 2); if (strpos($currentHeader,':') !== false) {
$headerValue = iconv_mime_decode($headerValue, 0, 'utf-8'); list($headerName, $headerValue) = explode(':', $currentHeader, 2);
if (strpos($headerValue, ';') !== false) { $headerValue = iconv_mime_decode($headerValue, 0, 'utf-8');
self::parseContentTypeHeader($currentPart, $headerName, $headerValue); if (strpos($headerValue, ';') !== false) {
$boundary = $multipart->getHeader('Content-Type', 'boundary'); self::parseContentTypeHeader($currentPart, $headerName, $headerValue);
$start = $multipart->getHeader('Content-Type', 'start'); $boundary = $multipart->getHeader('Content-Type', 'boundary');
} else { $start = $multipart->getHeader('Content-Type', 'start');
$currentPart->setHeader($headerName, trim($headerValue)); } else {
$currentPart->setHeader($headerName, trim($headerValue));
}
} }
unset($currentHeader); unset($currentHeader);
} }
@ -123,7 +126,7 @@ class Parser
/** /**
* Parse a "Content-Type" header with multiple sub values. * Parse a "Content-Type" header with multiple sub values.
* e.g. Content-Type: Multipart/Related; boundary=boundary; type=text/xml; * e.g. Content-Type: multipart/related; boundary=boundary; type=text/xml;
* start="<123@abc>" * start="<123@abc>"
* *
* @see https://labs.omniti.com/alexandria/trunk/OmniTI/Mail/Parser.php * @see https://labs.omniti.com/alexandria/trunk/OmniTI/Mail/Parser.php
@ -140,18 +143,17 @@ class Parser
$part->setHeader($headerName, $value); $part->setHeader($headerName, $value);
$remainder = trim($remainder); $remainder = trim($remainder);
while (strlen($remainder) > 0) { while (strlen($remainder) > 0) {
if (!preg_match('/^([a-zA-Z0-9_-]+)=(.)/', $remainder, $matches)) { if (!preg_match('/^([a-zA-Z0-9_-]+)=(.{1})/', $remainder, $matches)) {
break; break;
} }
$name = $matches[1]; $name = $matches[1];
$delimiter = $matches[2]; $delimiter = $matches[2];
$pattern = '/(\S+)(\s|$)/';
$remainder = substr($remainder, strlen($name)+1); $remainder = substr($remainder, strlen($name)+1);
if (!preg_match($pattern, $remainder, $matches)) { if (!preg_match('/([^;]+)(;)?(\s|$)?/', $remainder, $matches)) {
break; break;
} }
$value = rtrim($matches[1], ';'); $value = rtrim($matches[1], ';');
if ($delimiter == '\'' || $delimiter == '"') { if ($delimiter == "'" || $delimiter == '"') {
$value = trim($value, $delimiter); $value = trim($value, $delimiter);
} }
$part->setHeader($headerName, $name, $value); $part->setHeader($headerName, $name, $value);