Skip to content

Commit 3fbee96

Browse files
committed
Merge pull request #147 from linniksa/patch-mime-decoder
Provide mime text decoder by RFC2047
2 parents ea3f1bb + a528129 commit 3fbee96

File tree

4 files changed

+105
-4
lines changed

4 files changed

+105
-4
lines changed

src/Fetch/Attachment.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,9 @@ public function __construct(Message $message, $structure, $partIdentifier = null
9393
$parameters = Message::getParametersFromStructure($structure);
9494

9595
if (isset($parameters['filename'])) {
96-
$this->filename = imap_utf8($parameters['filename']);
96+
$this->setFileName($parameters['filename']);
9797
} elseif (isset($parameters['name'])) {
98-
$this->filename = imap_utf8($parameters['name']);
98+
$this->setFileName($parameters['name']);
9999
}
100100

101101
$this->size = $structure->bytes;
@@ -231,4 +231,9 @@ public function saveAs($path)
231231

232232
return $result;
233233
}
234+
235+
protected function setFileName($text)
236+
{
237+
$this->filename = MIME::decode($text, Message::$charset);
238+
}
234239
}

src/Fetch/MIME.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Fetch package.
5+
*
6+
* (c) Robert Hafner <tedivm@tedivm.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Fetch;
13+
14+
/**
15+
* This library is a wrapper around the Imap library functions included in php.
16+
*
17+
* @package Fetch
18+
* @author Robert Hafner <tedivm@tedivm.com>
19+
* @author Sergey Linnik <linniksa@gmail.com>
20+
*/
21+
final class MIME
22+
{
23+
/**
24+
* @param string $text
25+
* @param string $targetCharset
26+
*
27+
* @return string
28+
*/
29+
public static function decode($text, $targetCharset = 'utf-8')
30+
{
31+
if (null === $text) {
32+
return null;
33+
}
34+
35+
$result = '';
36+
37+
foreach (imap_mime_header_decode($text) as $word) {
38+
$ch = 'default' === $word->charset ? 'ascii' : $word->charset;
39+
40+
$result .= iconv($ch, $targetCharset, $word->text);
41+
}
42+
43+
return $result;
44+
}
45+
}

src/Fetch/Message.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ protected function loadMessage()
233233

234234
return false;
235235

236-
$this->subject = isset($messageOverview->subject) ? imap_utf8($messageOverview->subject) : null;
236+
$this->subject = MIME::decode($messageOverview->subject, self::$charset);
237237
$this->date = strtotime($messageOverview->date);
238238
$this->size = $messageOverview->size;
239239

@@ -674,7 +674,7 @@ protected function processAddressObject($addresses)
674674
$currentAddress = array();
675675
$currentAddress['address'] = $address->mailbox . '@' . $address->host;
676676
if (isset($address->personal)) {
677-
$currentAddress['name'] = $address->personal;
677+
$currentAddress['name'] = MIME::decode($address->personal, self::$charset);
678678
}
679679
$outputAddresses[] = $currentAddress;
680680
}

tests/Fetch/Test/MIMETest.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Fetch library.
5+
*
6+
* (c) Robert Hafner <tedivm@tedivm.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Fetch\Test;
13+
14+
use Fetch\MIME;
15+
16+
/**
17+
* @package Fetch
18+
* @author Robert Hafner <tedivm@tedivm.com>
19+
* @author Sergey Linnik <linniksa@gmail.com>
20+
*/
21+
class MIMETest extends \PHPUnit_Framework_TestCase
22+
{
23+
public function decodeData()
24+
{
25+
return array(
26+
array(null, null),
27+
array('Just text', 'Just text'),
28+
array('Keith Moore <moore@cs.utk.edu>', '=?US-ASCII?Q?Keith_Moore?= <moore@cs.utk.edu>'),
29+
array('Keld Jørn Simonsen <keld@dkuug.dk>', '=?ISO-8859-1?Q?Keld_J=F8rn_Simonsen?= <keld@dkuug.dk>'),
30+
array('André Pirard <PIRARD@vm1.ulg.ac.be>', '=?ISO-8859-1?Q?Andr=E9?= Pirard <PIRARD@vm1.ulg.ac.be>'),
31+
array(
32+
'If you can read this you understand the example.',
33+
'=?ISO-8859-1?B?SWYgeW91IGNhbiByZWFkIHRoaXMgeW8=?='
34+
. PHP_EOL .
35+
'=?ISO-8859-2?B?dSB1bmRlcnN0YW5kIHRoZSBleGFtcGxlLg==?='
36+
),
37+
);
38+
}
39+
40+
/**
41+
* @dataProvider decodeData
42+
*
43+
* @param string $expected
44+
* @param string $text
45+
* @param string $charset
46+
*/
47+
public function testDecode($expected, $text, $charset = 'UTF-8')
48+
{
49+
self::assertSame($expected, MIME::decode($text, $charset));
50+
}
51+
}

0 commit comments

Comments
 (0)