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

Commit

Permalink
Merge branch 'Maks3w-hotfix/many-mail-issues' into develop
Browse files Browse the repository at this point in the history
Forward port #5291
  • Loading branch information
Mike Willbanks committed Oct 20, 2013
2 parents 473401d + 544cc2d commit 2230479
Show file tree
Hide file tree
Showing 12 changed files with 50 additions and 28 deletions.
4 changes: 1 addition & 3 deletions library/Zend/Mail/Header/AbstractAddressList.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ public static function fromString($headerLine)
{
$decodedLine = iconv_mime_decode($headerLine, ICONV_MIME_DECODE_CONTINUE_ON_ERROR, 'UTF-8');
// split into name/value
list($fieldName, $fieldValue) = explode(':', $decodedLine, 2);
$fieldName = trim($fieldName);
$fieldValue = trim($fieldValue);
list($fieldName, $fieldValue) = GenericHeader::splitHeaderLine($decodedLine);

if (strtolower($fieldName) !== static::$type) {
throw new Exception\InvalidArgumentException(sprintf(
Expand Down
2 changes: 1 addition & 1 deletion library/Zend/Mail/Header/ContentType.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class ContentType implements HeaderInterface
public static function fromString($headerLine)
{
$headerLine = iconv_mime_decode($headerLine, ICONV_MIME_DECODE_CONTINUE_ON_ERROR, 'UTF-8');
list($name, $value) = explode(': ', $headerLine, 2);
list($name, $value) = GenericHeader::splitHeaderLine($headerLine);

// check to ensure proper header type for this factory
if (strtolower($name) !== 'content-type') {
Expand Down
10 changes: 7 additions & 3 deletions library/Zend/Mail/Header/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,23 @@ class Date implements HeaderInterface

public static function fromString($headerLine)
{
list($name, $value) = explode(': ', $headerLine, 2);
list($name, $value) = GenericHeader::splitHeaderLine($headerLine);

// check to ensure proper header type for this factory
if (strtolower($name) !== 'date') {
throw new Exception\InvalidArgumentException('Invalid header line for Date string');
}

$header = new static();
$header->value= $value;
$header = new static($value);

return $header;
}

public function __construct($value)
{
$this->value = $value;
}

public function getFieldName()
{
return 'Date';
Expand Down
28 changes: 22 additions & 6 deletions library/Zend/Mail/Header/GenericHeader.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,33 @@ class GenericHeader implements HeaderInterface, UnstructuredInterface
public static function fromString($headerLine)
{
$decodedLine = iconv_mime_decode($headerLine, ICONV_MIME_DECODE_CONTINUE_ON_ERROR, 'UTF-8');
$parts = explode(':', $decodedLine, 2);
if (count($parts) != 2) {
throw new Exception\InvalidArgumentException('Header must match with the format "name: value"');
}
$header = new static($parts[0], ltrim($parts[1]));
list($name, $value) = GenericHeader::splitHeaderLine($decodedLine);
$header = new static($name, $value);
if ($decodedLine != $headerLine) {
$header->setEncoding('UTF-8');
}
return $header;
}

/**
* Splits the header line in `name` and `value` parts.
*
* @param string $headerLine
* @return string[] `name` in the first index and `value` in the second.
* @throws Exception\InvalidArgumentException If header does not match with the format ``name:value``
*/
public static function splitHeaderLine($headerLine)
{
$parts = explode(':', $headerLine, 2);
if (count($parts) !== 2) {
throw new Exception\InvalidArgumentException('Header must match with the format "name:value"');
}

$parts[1] = ltrim($parts[1]);

return $parts;
}

/**
* Constructor
*
Expand Down Expand Up @@ -76,7 +92,7 @@ public function setFieldName($fieldName)
$fieldName = str_replace(' ', '-', ucwords(str_replace(array('_', '-'), ' ', $fieldName)));

// Validate what we have
if (!preg_match('/^[\x21-\x39\x3B-\x7E]*$/i', $fieldName)) {
if (!preg_match('/^[\x21-\x39\x3B-\x7E]*$/', $fieldName)) {
throw new Exception\InvalidArgumentException(
'Header name must be composed of printable US-ASCII characters, except colon.'
);
Expand Down
6 changes: 1 addition & 5 deletions library/Zend/Mail/Header/GenericMultiHeader.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@ class GenericMultiHeader extends GenericHeader implements MultipleHeadersInterfa
public static function fromString($headerLine)
{
$decodedLine = iconv_mime_decode($headerLine, ICONV_MIME_DECODE_CONTINUE_ON_ERROR, 'UTF-8');
$parts = explode(': ', $decodedLine, 2);
if (count($parts) != 2) {
throw new Exception\InvalidArgumentException('Header must match with the format "name: value"');
}
list($fieldName, $fieldValue) = $parts;
list($fieldName, $fieldValue) = GenericHeader::splitHeaderLine($decodedLine);

if (strpos($fieldValue, ',')) {
$headers = array();
Expand Down
2 changes: 2 additions & 0 deletions library/Zend/Mail/Header/HeaderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ interface HeaderInterface
*
* @param string $headerLine
* @return self
* @throws Exception\InvalidArgumentException If the header does not match with RFC 2822 definition.
* @see http://tools.ietf.org/html/rfc2822#section-2.2
*/
public static function fromString($headerLine);

Expand Down
2 changes: 1 addition & 1 deletion library/Zend/Mail/Header/MessageId.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class MessageId implements HeaderInterface

public static function fromString($headerLine)
{
list($name, $value) = explode(': ', $headerLine, 2);
list($name, $value) = GenericHeader::splitHeaderLine($headerLine);

// check to ensure proper header type for this factory
if (strtolower($name) !== 'message-id') {
Expand Down
4 changes: 2 additions & 2 deletions library/Zend/Mail/Header/MimeVersion.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class MimeVersion implements HeaderInterface

public static function fromString($headerLine)
{
list($name, $value) = explode(': ', $headerLine, 2);
list($name, $value) = GenericHeader::splitHeaderLine($headerLine);

// check to ensure proper header type for this factory
if (strtolower($name) !== 'mime-version') {
Expand All @@ -28,7 +28,7 @@ public static function fromString($headerLine)
// Check for version, and set if found
$header = new static();
if (preg_match('/^(?P<version>\d+\.\d+)$/', $value, $matches)) {
$header->version = $matches['version'];
$header->setVersion($matches['version']);
}

return $header;
Expand Down
10 changes: 7 additions & 3 deletions library/Zend/Mail/Header/Received.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,23 @@ class Received implements HeaderInterface, MultipleHeadersInterface

public static function fromString($headerLine)
{
list($name, $value) = explode(': ', $headerLine, 2);
list($name, $value) = GenericHeader::splitHeaderLine($headerLine);

// check to ensure proper header type for this factory
if (strtolower($name) !== 'received') {
throw new Exception\InvalidArgumentException('Invalid header line for Received string');
}

$header = new static();
$header->value= $value;
$header = new static($value);

return $header;
}

public function __construct($value = '')
{
$this->value = $value;
}

public function getFieldName()
{
return 'Received';
Expand Down
2 changes: 1 addition & 1 deletion library/Zend/Mail/Header/Sender.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Sender implements HeaderInterface
public static function fromString($headerLine)
{
$decodedLine = iconv_mime_decode($headerLine, ICONV_MIME_DECODE_CONTINUE_ON_ERROR, 'UTF-8');
list($name, $value) = explode(': ', $decodedLine, 2);
list($name, $value) = GenericHeader::splitHeaderLine($decodedLine);

// check to ensure proper header type for this factory
if (strtolower($name) !== 'sender') {
Expand Down
3 changes: 1 addition & 2 deletions library/Zend/Mail/Header/Subject.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ class Subject implements UnstructuredInterface
public static function fromString($headerLine)
{
$decodedLine = iconv_mime_decode($headerLine, ICONV_MIME_DECODE_CONTINUE_ON_ERROR, 'UTF-8');
list($name, $value) = explode(':', $decodedLine, 2);
$value = ltrim($value);
list($name, $value) = GenericHeader::splitHeaderLine($decodedLine);

// check to ensure proper header type for this factory
if (strtolower($name) !== 'subject') {
Expand Down
5 changes: 4 additions & 1 deletion tests/ZendTest/Mail/HeadersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,10 @@ public function testHeadersAggregatesHeaderThroughAddHeaderLine()

public function testHeadersAddHeaderLineThrowsExceptionOnMissingFieldValue()
{
$this->setExpectedException('Zend\Mail\Header\Exception\InvalidArgumentException', 'Header must match with the format "name: value"');
$this->setExpectedException(
'Zend\Mail\Header\Exception\InvalidArgumentException',
'Header must match with the format "name:value"'
);
$headers = new Mail\Headers();
$headers->addHeaderLine('Foo');
}
Expand Down

0 comments on commit 2230479

Please sign in to comment.