Skip to content

Commit df3fa89

Browse files
committed
Merge remote-tracking branch 'upstream/master' into content-disposition
2 parents 325bd61 + 28e09b4 commit df3fa89

9 files changed

+288
-7
lines changed

CHANGELOG.md

+24-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,29 @@
22

33
All notable changes to this project will be documented in this file, in reverse chronological order by release.
44

5-
## 2.10.1 - TBD
5+
## 2.10.2 - TBD
6+
7+
### Added
8+
9+
- Nothing.
10+
11+
### Changed
12+
13+
- Nothing.
14+
15+
### Deprecated
16+
17+
- Nothing.
18+
19+
### Removed
20+
21+
- Nothing.
22+
23+
### Fixed
24+
25+
- Nothing.
26+
27+
## 2.10.1 - 2020-04-21
628

729
### Added
830

@@ -32,6 +54,7 @@ All notable changes to this project will be documented in this file, in reverse
3254

3355
- [#31](https://github.com/laminas/laminas-mail/pull/31) Properly encode `content-disposition` header.
3456

57+
- [#88](https://github.com/laminas/laminas-mail/pull/88) fixes recognising encoding of `Subject` and `GenericHeader` headers.
3558

3659
## 2.10.0 - 2018-06-07
3760

COPYRIGHT.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
Copyright (c) 2019-2020, Laminas Foundation.
2-
All rights reserved. (https://getlaminas.org/)
1+
Copyright (c) 2020 Laminas Project a Series of LF Projects, LLC. (https://getlaminas.org/)

LICENSE.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
Copyright (c) 2019-2020, Laminas Foundation
2-
All rights reserved.
1+
Copyright (c) 2020 Laminas Project a Series of LF Projects, LLC.
32

43
Redistribution and use in source and binary forms, with or without
54
modification, are permitted provided that the following conditions are met:

src/Header/GenericHeader.php

+22-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,28 @@ public function getFieldValue($format = HeaderInterface::FORMAT_RAW)
151151

152152
public function setEncoding($encoding)
153153
{
154-
$this->encoding = $encoding;
154+
if ($encoding === $this->encoding) {
155+
return $this;
156+
}
157+
158+
if ($encoding === null) {
159+
$this->encoding = null;
160+
return $this;
161+
}
162+
163+
$encoding = strtoupper($encoding);
164+
if ($encoding === 'UTF-8') {
165+
$this->encoding = $encoding;
166+
return $this;
167+
}
168+
169+
if ($encoding === 'ASCII' && Mime::isPrintable($this->fieldValue)) {
170+
$this->encoding = $encoding;
171+
return $this;
172+
}
173+
174+
$this->encoding = null;
175+
155176
return $this;
156177
}
157178

src/Header/Subject.php

+22-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,28 @@ public function getFieldValue($format = HeaderInterface::FORMAT_RAW)
6262

6363
public function setEncoding($encoding)
6464
{
65-
$this->encoding = $encoding;
65+
if ($encoding === $this->encoding) {
66+
return $this;
67+
}
68+
69+
if ($encoding === null) {
70+
$this->encoding = null;
71+
return $this;
72+
}
73+
74+
$encoding = strtoupper($encoding);
75+
if ($encoding === 'UTF-8') {
76+
$this->encoding = $encoding;
77+
return $this;
78+
}
79+
80+
if ($encoding === 'ASCII' && Mime::isPrintable($this->subject)) {
81+
$this->encoding = $encoding;
82+
return $this;
83+
}
84+
85+
$this->encoding = null;
86+
6687
return $this;
6788
}
6889

src/Message.php

+1
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,7 @@ public function setSubject($subject)
342342
$header = $headers->get('subject');
343343
}
344344
$header->setSubject($subject);
345+
$header->setEncoding($this->getEncoding());
345346
return $this;
346347
}
347348

test/Header/GenericHeaderTest.php

+52
Original file line numberDiff line numberDiff line change
@@ -189,4 +189,56 @@ public function testToStringThrowsWithoutFieldName()
189189
$this->expectExceptionMessage('Header name is not set, use setFieldName()');
190190
$header->toString();
191191
}
192+
193+
public function testChangeEncodingToAsciiNotAllowedWhenHeaderValueContainsUtf8Characters()
194+
{
195+
$subject = new GenericHeader();
196+
$subject->setFieldValue('Accents òàùèéì');
197+
198+
self::assertSame('UTF-8', $subject->getEncoding());
199+
200+
$subject->setEncoding('ASCII');
201+
self::assertSame('UTF-8', $subject->getEncoding());
202+
}
203+
204+
public function testChangeEncodingBackToAscii()
205+
{
206+
$subject = new GenericHeader('X-Test');
207+
$subject->setFieldValue('test');
208+
209+
self::assertSame('ASCII', $subject->getEncoding());
210+
211+
$subject->setEncoding('UTF-8');
212+
self::assertSame('UTF-8', $subject->getEncoding());
213+
214+
$subject->setEncoding('ASCII');
215+
self::assertSame('ASCII', $subject->getEncoding());
216+
}
217+
218+
public function testSetNullEncoding()
219+
{
220+
$subject = GenericHeader::fromString('X-Test: test');
221+
self::assertSame('ASCII', $subject->getEncoding());
222+
223+
$subject->setEncoding(null);
224+
self::assertSame('ASCII', $subject->getEncoding());
225+
}
226+
227+
public function testSettingFieldValueCanChangeEncoding()
228+
{
229+
$subject = GenericHeader::fromString('X-Test: test');
230+
self::assertSame('ASCII', $subject->getEncoding());
231+
232+
$subject->setFieldValue('Accents òàùèéì');
233+
self::assertSame('UTF-8', $subject->getEncoding());
234+
}
235+
236+
public function testSettingTheSameEncoding()
237+
{
238+
$subject = GenericHeader::fromString('X-Test: test');
239+
self::assertSame('ASCII', $subject->getEncoding());
240+
241+
$subject->setEncoding('ASCII');
242+
self::assertSame('ASCII', $subject->getEncoding());
243+
}
192244
}

test/Header/SubjectTest.php

+52
Original file line numberDiff line numberDiff line change
@@ -128,4 +128,56 @@ public function invalidSubjectValuesProvider()
128128
'multiline' => ["xxx\r\ny\r\nyy", $invalidArgumentException, $invalidHeaderValueDetected],
129129
];
130130
}
131+
132+
public function testChangeEncodingToAsciiNotAllowedWhenSubjectContainsUtf8Characters()
133+
{
134+
$subject = new Header\Subject();
135+
$subject->setSubject('Accents òàùèéì');
136+
137+
self::assertSame('UTF-8', $subject->getEncoding());
138+
139+
$subject->setEncoding('ASCII');
140+
self::assertSame('UTF-8', $subject->getEncoding());
141+
}
142+
143+
public function testChangeEncodingBackToAscii()
144+
{
145+
$subject = new Header\Subject();
146+
$subject->setSubject('test');
147+
148+
self::assertSame('ASCII', $subject->getEncoding());
149+
150+
$subject->setEncoding('UTF-8');
151+
self::assertSame('UTF-8', $subject->getEncoding());
152+
153+
$subject->setEncoding('ASCII');
154+
self::assertSame('ASCII', $subject->getEncoding());
155+
}
156+
157+
public function testSetNullEncoding()
158+
{
159+
$subject = Header\Subject::fromString('Subject: test');
160+
self::assertSame('ASCII', $subject->getEncoding());
161+
162+
$subject->setEncoding(null);
163+
self::assertSame('ASCII', $subject->getEncoding());
164+
}
165+
166+
public function testSettingSubjectCanChangeEncoding()
167+
{
168+
$subject = Header\Subject::fromString('Subject: test');
169+
self::assertSame('ASCII', $subject->getEncoding());
170+
171+
$subject->setSubject('Accents òàùèéì');
172+
self::assertSame('UTF-8', $subject->getEncoding());
173+
}
174+
175+
public function testSettingTheSameEncoding()
176+
{
177+
$subject = Header\Subject::fromString('Subject: test');
178+
self::assertSame('ASCII', $subject->getEncoding());
179+
180+
$subject->setEncoding('ASCII');
181+
self::assertSame('ASCII', $subject->getEncoding());
182+
}
131183
}

test/MessageTest.php

+113
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Laminas\Mail\AddressList;
1313
use Laminas\Mail\Exception;
1414
use Laminas\Mail\Header;
15+
use Laminas\Mail\Header\GenericHeader;
1516
use Laminas\Mail\Headers;
1617
use Laminas\Mail\Message;
1718
use Laminas\Mime\Message as MimeMessage;
@@ -873,4 +874,116 @@ public function testSecondCodeInjectionInFromHeader()
873874
$message->setFrom('user@xenial(tmp1 -be ${run{${substr{0}{1}{$spool_directory}}usr${substr{0}{1}{$spool_directory}}bin${substr{0}{1}{$spool_directory}}touch${substr{10}{1}{$tod_log}}${substr{0}{1}{$spool_directory}}tmp${substr{0}{1}{$spool_directory}}test}} tmp2)', 'Sender\'s name');
874875
// @codingStandardsIgnoreEnd
875876
}
877+
878+
public function testMessageSubjectFromString()
879+
{
880+
$rawMessage = 'Subject: =?UTF-8?Q?Non=20=E2=80=9Cascii=E2=80=9D=20characters=20like=20accented=20?=' . "\r\n"
881+
. ' =?UTF-8?Q?vowels=20=C3=B2=C3=A0=C3=B9=C3=A8=C3=A9=C3=AC?=';
882+
$mail = Message::fromString($rawMessage);
883+
884+
self::assertContains(
885+
'Subject: =?UTF-8?Q?Non=20=E2=80=9Cascii=E2=80=9D=20characters=20like=20accented=20?=' . "\r\n"
886+
. ' =?UTF-8?Q?vowels=20=C3=B2=C3=A0=C3=B9=C3=A8=C3=A9=C3=AC?=' . "\r\n",
887+
$mail->toString()
888+
);
889+
}
890+
891+
public function testMessageSubjectSetSubject()
892+
{
893+
$mail = new Message();
894+
$mail->setSubject('Non “ascii” characters like accented vowels òàùèéì');
895+
896+
self::assertContains(
897+
'Subject: =?UTF-8?Q?Non=20=E2=80=9Cascii=E2=80=9D=20characters=20like=20accented=20?=' . "\r\n"
898+
. ' =?UTF-8?Q?vowels=20=C3=B2=C3=A0=C3=B9=C3=A8=C3=A9=C3=AC?=' . "\r\n",
899+
$mail->toString()
900+
);
901+
}
902+
903+
public function testCorrectHeaderEncodingAddHeader()
904+
{
905+
$mail = new Message();
906+
$header = new GenericHeader('X-Test', 'Non “ascii” characters like accented vowels òàùèéì');
907+
$mail->getHeaders()->addHeader($header);
908+
909+
self::assertContains(
910+
'X-Test: =?UTF-8?Q?Non=20=E2=80=9Cascii=E2=80=9D=20characters=20like=20accented=20?=' . "\r\n"
911+
. ' =?UTF-8?Q?vowels=20=C3=B2=C3=A0=C3=B9=C3=A8=C3=A9=C3=AC?=' . "\r\n",
912+
$mail->toString()
913+
);
914+
}
915+
916+
public function testCorrectHeaderEncodingSetHeaders()
917+
{
918+
$mail = new Message();
919+
$header = new GenericHeader('X-Test', 'Non “ascii” characters like accented vowels òàùèéì');
920+
$headers = new Headers();
921+
$headers->addHeader($header);
922+
$mail->setHeaders($headers);
923+
924+
self::assertContains(
925+
'X-Test: =?UTF-8?Q?Non=20=E2=80=9Cascii=E2=80=9D=20characters=20like=20accented=20?=' . "\r\n"
926+
. ' =?UTF-8?Q?vowels=20=C3=B2=C3=A0=C3=B9=C3=A8=C3=A9=C3=AC?=' . "\r\n",
927+
$mail->toString()
928+
);
929+
}
930+
931+
public function testCorrectHeaderEncodingFromString()
932+
{
933+
$mail = new Message();
934+
$str = 'X-Test: =?UTF-8?Q?Non=20=E2=80=9Cascii=E2=80=9D=20characters=20like=20accented=20?=' . "\r\n"
935+
. ' =?UTF-8?Q?vowels=20=C3=B2=C3=A0=C3=B9=C3=A8=C3=A9=C3=AC?=';
936+
$header = GenericHeader::fromString($str);
937+
$mail->getHeaders()->addHeader($header);
938+
939+
self::assertContains(
940+
'X-Test: =?UTF-8?Q?Non=20=E2=80=9Cascii=E2=80=9D=20characters=20like=20accented=20?=' . "\r\n"
941+
. ' =?UTF-8?Q?vowels=20=C3=B2=C3=A0=C3=B9=C3=A8=C3=A9=C3=AC?=' . "\r\n",
942+
$mail->toString()
943+
);
944+
}
945+
946+
public function testCorrectHeaderEncodingFromStringAndSetHeaders()
947+
{
948+
$mail = new Message();
949+
$str = 'X-Test: =?UTF-8?Q?Non=20=E2=80=9Cascii=E2=80=9D=20characters=20like=20accented=20?=' . "\r\n"
950+
. ' =?UTF-8?Q?vowels=20=C3=B2=C3=A0=C3=B9=C3=A8=C3=A9=C3=AC?=';
951+
952+
$header = GenericHeader::fromString($str);
953+
$headers = new Headers();
954+
$headers->addHeader($header);
955+
$mail->setHeaders($headers);
956+
957+
self::assertContains(
958+
'X-Test: =?UTF-8?Q?Non=20=E2=80=9Cascii=E2=80=9D=20characters=20like=20accented=20?=' . "\r\n"
959+
. ' =?UTF-8?Q?vowels=20=C3=B2=C3=A0=C3=B9=C3=A8=C3=A9=C3=AC?=' . "\r\n",
960+
$mail->toString()
961+
);
962+
}
963+
964+
public function testMessageSubjectEncodingWhenEncodingSetAfterTheSubject()
965+
{
966+
$mail = new Message();
967+
$mail->setSubject('hello world');
968+
$mail->setEncoding('UTF-8');
969+
970+
self::assertSame('UTF-8', $mail->getHeaders()->get('subject')->getEncoding());
971+
self::assertSame(
972+
'Subject: =?UTF-8?Q?hello=20world?=',
973+
$mail->getHeaders()->get('subject')->toString()
974+
);
975+
}
976+
977+
public function testMessageSubjectEncodingWhenEcodingSetBeforeTheSubject()
978+
{
979+
$mail = new Message();
980+
$mail->setEncoding('UTF-8');
981+
$mail->setSubject('hello world');
982+
983+
self::assertSame('UTF-8', $mail->getHeaders()->get('subject')->getEncoding());
984+
self::assertSame(
985+
'Subject: =?UTF-8?Q?hello=20world?=',
986+
$mail->getHeaders()->get('subject')->toString()
987+
);
988+
}
876989
}

0 commit comments

Comments
 (0)