From 64e1af187e9c83659c4ce544a3e8972fa86392a2 Mon Sep 17 00:00:00 2001 From: Diego da Silva Date: Wed, 29 Jan 2020 15:24:56 +0100 Subject: [PATCH] Treat quote as value if already in quote using another delimiter --- CHANGELOG.md | 2 ++ src/Header/ListParser.php | 6 ++++++ test/AddressListTest.php | 22 ++++++++++++++++++++++ 3 files changed, 30 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8c3193e3..4d111211 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,6 +28,8 @@ All notable changes to this project will be documented in this file, in reverse - [#82](https://github.com/laminas/laminas-mail/pull/82) fixes numerous issues in `Storage\Maildir`. This storage adapter was not working before and unit tests were disabled. +- [#75](https://github.com/laminas/laminas-mail/pull/75) fixes how `Laminas\Mail\Header\ListParser::parse()` parses the string with quotes. + ## 2.10.0 - 2018-06-07 ### Added diff --git a/src/Header/ListParser.php b/src/Header/ListParser.php index f98f8cc8..e18c40f9 100644 --- a/src/Header/ListParser.php +++ b/src/Header/ListParser.php @@ -77,6 +77,12 @@ public static function parse($value, array $delims = self::CHAR_DELIMS) continue; } + // If already in quote and the character does not match the previously + // matched quote delimiter, we're done here. + if ($inQuote) { + continue; + } + // Otherwise, we're starting a quoted string. $inQuote = true; $currentQuoteDelim = $char; diff --git a/test/AddressListTest.php b/test/AddressListTest.php index ade87160..7b508c9b 100644 --- a/test/AddressListTest.php +++ b/test/AddressListTest.php @@ -200,4 +200,26 @@ public function testKey() $this->list->next(); $this->assertSame('test@example.org', $this->list->key()); } + + /** + * If name-field is quoted with "", then ' inside it should not treated as terminator, but as value. + */ + public function testMixedQuotesInName() + { + $header = '"Bob O\'Reilly" ,blah@example.com'; + + // In previous versions, this throws: + // 'Bob O'Reilly ,blah' can not be matched against dot-atom format + // hence the try/catch block, to allow finding the root cause. + try { + $to = Header\To::fromString('To:' . $header); + } catch (InvalidArgumentException $e) { + $this->fail('Header\To::fromString should not throw'); + } + + $addressList = $to->getAddressList(); + $this->assertTrue($addressList->has('bob@example.com')); + $this->assertTrue($addressList->has('blah@example.com')); + $this->assertEquals("Bob O'Reilly", $addressList->get('bob@example.com')->getName()); + } }