Skip to content

Commit d4f62bf

Browse files
authored
Merge pull request #666 from barbushin/Issue-657-Fix-encoding-issues
Issue #657: Fix encoding issues
2 parents f1e316b + 24e4047 commit d4f62bf

File tree

3 files changed

+106
-7
lines changed

3 files changed

+106
-7
lines changed

src/PhpImap/DataPartInfo.php

+11-6
Original file line numberDiff line numberDiff line change
@@ -85,23 +85,23 @@ public function fetch(): string
8585
$this->data = Imap::fetchbody($this->mail->getImapStream(), $this->id, $this->part, $this->options);
8686
}
8787

88-
return $this->decodeAfterFetch();
88+
return $this->decodeAfterFetch($this->data);
8989
}
9090

91-
protected function decodeAfterFetch(): string
91+
public function decodeAfterFetch(string $data): string
9292
{
9393
switch ($this->encoding) {
9494
case ENC8BIT:
95-
$this->data = \imap_utf8((string) $this->data);
95+
$this->data = \imap_utf8((string) $data);
9696
break;
9797
case ENCBINARY:
98-
$this->data = \imap_binary((string) $this->data);
98+
$this->data = \imap_binary((string) $data);
9999
break;
100100
case ENCBASE64:
101-
$this->data = \base64_decode((string) $this->data, false);
101+
$this->data = \base64_decode((string) $data, false);
102102
break;
103103
case ENCQUOTEDPRINTABLE:
104-
$this->data = \quoted_printable_decode((string) $this->data);
104+
$this->data = \quoted_printable_decode((string) $data);
105105
break;
106106
}
107107

@@ -114,6 +114,11 @@ protected function convertEncodingAfterFetch(): string
114114
$this->data = $this->mail->decodeMimeStr(
115115
(string) $this->data // Data to convert
116116
);
117+
118+
$this->data = $this->mail->convertToUtf8(
119+
$this->data,
120+
$this->charset
121+
);
117122
}
118123

119124
return (null === $this->data) ? '' : $this->data;

tests/unit/Fixtures/DataPartInfo.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,12 @@
88

99
class DataPartInfo extends Base
1010
{
11+
/** @var string|null */
12+
protected $data;
13+
1114
public function fetch(): string
1215
{
13-
return $this->decodeAfterFetch();
16+
return $this->decodeAfterFetch($this->data);
1417
}
1518

1619
public function setData(string $data = null): void
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
/**
3+
* Live Mailbox - PHPUnit tests.
4+
*
5+
* Runs tests on a live mailbox
6+
*
7+
* @author Sebi94nbg
8+
*/
9+
declare(strict_types=1);
10+
11+
namespace PhpImap;
12+
13+
use const ENCQUOTEDPRINTABLE;
14+
use Generator;
15+
use PHPUnit\Framework\TestCase;
16+
17+
class LiveMailboxStringDecodingConvertingTest extends TestCase
18+
{
19+
/**
20+
* Provides data for testing string decoding.
21+
*/
22+
public function stringDecodeProvider(): Generator
23+
{
24+
yield 'Issue #250 iso-8859-1' => [
25+
ENCQUOTEDPRINTABLE,
26+
'iso-8859-1',
27+
'mountainguan',
28+
'mountainguan',
29+
'e94a37111edb29a8d3f6078dc4810953964f19562613cf2bd15e21b69d30822a',
30+
];
31+
32+
yield 'Issue #250 utf-7' => [
33+
ENCQUOTEDPRINTABLE,
34+
'utf-7',
35+
'+bUuL1Q-',
36+
'测试',
37+
'6aa8f49cc992dfd75a114269ed26de0ad6d4e7d7a70d9c8afb3d7a57a88a73ed',
38+
];
39+
40+
yield 'Issue #250 utf-7 with chinese' => [
41+
ENCQUOTEDPRINTABLE,
42+
'utf-7',
43+
'mountainguan+bUuL1Q-',
44+
'mountainguan测试',
45+
'62a5022b682b7e02bda8d18424fa06501cdd71cce2832e95129673f63da2e177',
46+
];
47+
48+
yield 'Issue #250 utf-8 with chinese' => [
49+
ENCQUOTEDPRINTABLE,
50+
'utf-8',
51+
'mountainguan=E6=B5=8B=E8=AF=95',
52+
'mountainguan测试',
53+
'62a5022b682b7e02bda8d18424fa06501cdd71cce2832e95129673f63da2e177',
54+
];
55+
56+
yield 'Issue #657' => [
57+
ENCQUOTEDPRINTABLE,
58+
'iso-8859-2',
59+
'=EC=B9=E8=F8=BE=FD=E1=ED=E9',
60+
'ěščřžýáíé',
61+
'a05e42c7e14de716cd501e135f3f5e49545f71069de316a1e9f7bb153f9a7356',
62+
];
63+
64+
yield 'Emoji utf-8' => [
65+
ENCQUOTEDPRINTABLE,
66+
'utf-8',
67+
'Some subject here =F0=9F=98=98',
68+
'Some subject here 😘',
69+
'da66c62e7e82316b8b543f52f1ecc4415c4dc93bc87e2239ee5f98bdf00a8c50',
70+
];
71+
}
72+
73+
/**
74+
* Test that string decoding and converting works as expected.
75+
*
76+
* @dataProvider stringDecodeProvider
77+
*/
78+
public function testStringDecode(int $encoding, string $charset, string $iso_8859_2, string $utf8, string $sha256): void
79+
{
80+
$mailbox = new Mailbox('', '', '');
81+
82+
$dataInfo = new DataPartInfo($mailbox, 1337, '', $encoding, 0);
83+
$dataInfo->charset = $charset;
84+
85+
$decoded = $dataInfo->decodeAfterFetch($iso_8859_2);
86+
87+
$this->assertSame($utf8, $decoded);
88+
89+
$this->assertSame($sha256, \hash('sha256', $decoded));
90+
}
91+
}

0 commit comments

Comments
 (0)