Skip to content
This repository

[mail] Fixes, criteria unification and optimization. #5291

Closed
wants to merge 3 commits into from

1 participant

Maks
Maks
Collaborator

I've created a new static method in GenericHeader for unify the criteria about split a header line in name and value.

This go rid with all issues about to force to have a space after colon (:) (RFC 2822 does not say nothing about to have an space)

Maks3w added some commits
Maks Maks3w referenced this pull request from a commit in Maks3w/zf2
Maks Maks3w [#5140] Adapt changes to actual master changes.
Adapt code to design changes added by #5291 and #5303
81a4ae2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 3 unique commits by 1 author.

Oct 18, 2013
Maks Maks3w [mail] Remove unnecessary case insensitive
lowercase and uppercase characters are already included in the range
61165f1
Maks Maks3w [mail] Fix assignment to protected methods 32b2859
Maks Maks3w [mail] Unify criteria for split fieldname and fieldvalue in the heade…
…r line.

Also adds @throws tag to interface method docblock.
a73d1f4
This page is out of date. Refresh to see the latest.
4 library/Zend/Mail/Header/AbstractAddressList.php
@@ -43,9 +43,7 @@ public static function fromString($headerLine)
43 43 {
44 44 $decodedLine = iconv_mime_decode($headerLine, ICONV_MIME_DECODE_CONTINUE_ON_ERROR, 'UTF-8');
45 45 // split into name/value
46   - list($fieldName, $fieldValue) = explode(':', $decodedLine, 2);
47   - $fieldName = trim($fieldName);
48   - $fieldValue = trim($fieldValue);
  46 + list($fieldName, $fieldValue) = GenericHeader::splitHeaderLine($decodedLine);
49 47
50 48 if (strtolower($fieldName) !== static::$type) {
51 49 throw new Exception\InvalidArgumentException(sprintf(
2  library/Zend/Mail/Header/ContentType.php
@@ -26,7 +26,7 @@ class ContentType implements HeaderInterface
26 26 public static function fromString($headerLine)
27 27 {
28 28 $headerLine = iconv_mime_decode($headerLine, ICONV_MIME_DECODE_CONTINUE_ON_ERROR, 'UTF-8');
29   - list($name, $value) = explode(': ', $headerLine, 2);
  29 + list($name, $value) = GenericHeader::splitHeaderLine($headerLine);
30 30
31 31 // check to ensure proper header type for this factory
32 32 if (strtolower($name) !== 'content-type') {
10 library/Zend/Mail/Header/Date.php
@@ -21,19 +21,23 @@ class Date implements HeaderInterface
21 21
22 22 public static function fromString($headerLine)
23 23 {
24   - list($name, $value) = explode(': ', $headerLine, 2);
  24 + list($name, $value) = GenericHeader::splitHeaderLine($headerLine);
25 25
26 26 // check to ensure proper header type for this factory
27 27 if (strtolower($name) !== 'date') {
28 28 throw new Exception\InvalidArgumentException('Invalid header line for Date string');
29 29 }
30 30
31   - $header = new static();
32   - $header->value= $value;
  31 + $header = new static($value);
33 32
34 33 return $header;
35 34 }
36 35
  36 + public function __construct($value)
  37 + {
  38 + $this->value = $value;
  39 + }
  40 +
37 41 public function getFieldName()
38 42 {
39 43 return 'Date';
28 library/Zend/Mail/Header/GenericHeader.php
@@ -31,11 +31,8 @@ class GenericHeader implements HeaderInterface, UnstructuredInterface
31 31 public static function fromString($headerLine)
32 32 {
33 33 $decodedLine = iconv_mime_decode($headerLine, ICONV_MIME_DECODE_CONTINUE_ON_ERROR, 'UTF-8');
34   - $parts = explode(':', $decodedLine, 2);
35   - if (count($parts) != 2) {
36   - throw new Exception\InvalidArgumentException('Header must match with the format "name: value"');
37   - }
38   - $header = new static($parts[0], ltrim($parts[1]));
  34 + list($name, $value) = GenericHeader::splitHeaderLine($decodedLine);
  35 + $header = new static($name, $value);
39 36 if ($decodedLine != $headerLine) {
40 37 $header->setEncoding('UTF-8');
41 38 }
@@ -43,6 +40,25 @@ public static function fromString($headerLine)
43 40 }
44 41
45 42 /**
  43 + * Splits the header line in `name` and `value` parts.
  44 + *
  45 + * @param string $headerLine
  46 + * @return string[] `name` in the first index and `value` in the second.
  47 + * @throws Exception\InvalidArgumentException If header does not match with the format ``name:value``
  48 + */
  49 + public static function splitHeaderLine($headerLine)
  50 + {
  51 + $parts = explode(':', $headerLine, 2);
  52 + if (count($parts) !== 2) {
  53 + throw new Exception\InvalidArgumentException('Header must match with the format "name:value"');
  54 + }
  55 +
  56 + $parts[1] = ltrim($parts[1]);
  57 +
  58 + return $parts;
  59 + }
  60 +
  61 + /**
46 62 * Constructor
47 63 *
48 64 * @param string $fieldName Optional
@@ -76,7 +92,7 @@ public function setFieldName($fieldName)
76 92 $fieldName = str_replace(' ', '-', ucwords(str_replace(array('_', '-'), ' ', $fieldName)));
77 93
78 94 // Validate what we have
79   - if (!preg_match('/^[\x21-\x39\x3B-\x7E]*$/i', $fieldName)) {
  95 + if (!preg_match('/^[\x21-\x39\x3B-\x7E]*$/', $fieldName)) {
80 96 throw new Exception\InvalidArgumentException(
81 97 'Header name must be composed of printable US-ASCII characters, except colon.'
82 98 );
6 library/Zend/Mail/Header/GenericMultiHeader.php
@@ -17,11 +17,7 @@ class GenericMultiHeader extends GenericHeader implements MultipleHeadersInterfa
17 17 public static function fromString($headerLine)
18 18 {
19 19 $decodedLine = iconv_mime_decode($headerLine, ICONV_MIME_DECODE_CONTINUE_ON_ERROR, 'UTF-8');
20   - $parts = explode(': ', $decodedLine, 2);
21   - if (count($parts) != 2) {
22   - throw new Exception\InvalidArgumentException('Header must match with the format "name: value"');
23   - }
24   - list($fieldName, $fieldValue) = $parts;
  20 + list($fieldName, $fieldValue) = GenericHeader::splitHeaderLine($decodedLine);
25 21
26 22 if (strpos($fieldValue, ',')) {
27 23 $headers = array();
2  library/Zend/Mail/Header/HeaderInterface.php
@@ -31,6 +31,8 @@
31 31 *
32 32 * @param string $headerLine
33 33 * @return self
  34 + * @throws Exception\InvalidArgumentException If the header does not match with RFC 2822 definition.
  35 + * @see http://tools.ietf.org/html/rfc2822#section-2.2
34 36 */
35 37 public static function fromString($headerLine);
36 38
2  library/Zend/Mail/Header/MessageId.php
@@ -20,7 +20,7 @@ class MessageId implements HeaderInterface
20 20
21 21 public static function fromString($headerLine)
22 22 {
23   - list($name, $value) = explode(': ', $headerLine, 2);
  23 + list($name, $value) = GenericHeader::splitHeaderLine($headerLine);
24 24
25 25 // check to ensure proper header type for this factory
26 26 if (strtolower($name) !== 'message-id') {
4 library/Zend/Mail/Header/MimeVersion.php
@@ -18,7 +18,7 @@ class MimeVersion implements HeaderInterface
18 18
19 19 public static function fromString($headerLine)
20 20 {
21   - list($name, $value) = explode(': ', $headerLine, 2);
  21 + list($name, $value) = GenericHeader::splitHeaderLine($headerLine);
22 22
23 23 // check to ensure proper header type for this factory
24 24 if (strtolower($name) !== 'mime-version') {
@@ -28,7 +28,7 @@ public static function fromString($headerLine)
28 28 // Check for version, and set if found
29 29 $header = new static();
30 30 if (preg_match('/^(?P<version>\d+\.\d+)$/', $value, $matches)) {
31   - $header->version = $matches['version'];
  31 + $header->setVersion($matches['version']);
32 32 }
33 33
34 34 return $header;
10 library/Zend/Mail/Header/Received.php
@@ -23,19 +23,23 @@ class Received implements HeaderInterface, MultipleHeadersInterface
23 23
24 24 public static function fromString($headerLine)
25 25 {
26   - list($name, $value) = explode(': ', $headerLine, 2);
  26 + list($name, $value) = GenericHeader::splitHeaderLine($headerLine);
27 27
28 28 // check to ensure proper header type for this factory
29 29 if (strtolower($name) !== 'received') {
30 30 throw new Exception\InvalidArgumentException('Invalid header line for Received string');
31 31 }
32 32
33   - $header = new static();
34   - $header->value= $value;
  33 + $header = new static($value);
35 34
36 35 return $header;
37 36 }
38 37
  38 + public function __construct($value = '')
  39 + {
  40 + $this->value = $value;
  41 + }
  42 +
39 43 public function getFieldName()
40 44 {
41 45 return 'Received';
2  library/Zend/Mail/Header/Sender.php
@@ -28,7 +28,7 @@ class Sender implements HeaderInterface
28 28 public static function fromString($headerLine)
29 29 {
30 30 $decodedLine = iconv_mime_decode($headerLine, ICONV_MIME_DECODE_CONTINUE_ON_ERROR, 'UTF-8');
31   - list($name, $value) = explode(': ', $decodedLine, 2);
  31 + list($name, $value) = GenericHeader::splitHeaderLine($decodedLine);
32 32
33 33 // check to ensure proper header type for this factory
34 34 if (strtolower($name) !== 'sender') {
3  library/Zend/Mail/Header/Subject.php
@@ -26,8 +26,7 @@ class Subject implements UnstructuredInterface
26 26 public static function fromString($headerLine)
27 27 {
28 28 $decodedLine = iconv_mime_decode($headerLine, ICONV_MIME_DECODE_CONTINUE_ON_ERROR, 'UTF-8');
29   - list($name, $value) = explode(':', $decodedLine, 2);
30   - $value = ltrim($value);
  29 + list($name, $value) = GenericHeader::splitHeaderLine($decodedLine);
31 30
32 31 // check to ensure proper header type for this factory
33 32 if (strtolower($name) !== 'subject') {
5 tests/ZendTest/Mail/HeadersTest.php
@@ -138,7 +138,10 @@ public function testHeadersAggregatesHeaderThroughAddHeaderLine()
138 138
139 139 public function testHeadersAddHeaderLineThrowsExceptionOnMissingFieldValue()
140 140 {
141   - $this->setExpectedException('Zend\Mail\Header\Exception\InvalidArgumentException', 'Header must match with the format "name: value"');
  141 + $this->setExpectedException(
  142 + 'Zend\Mail\Header\Exception\InvalidArgumentException',
  143 + 'Header must match with the format "name:value"'
  144 + );
142 145 $headers = new Mail\Headers();
143 146 $headers->addHeaderLine('Foo');
144 147 }

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.