mirror of
https://github.com/retailcrm/Fetch.git
synced 2024-11-22 03:06:02 +03:00
Provide mime text decoder by RFC2047
Replace imap_utf8, and add decode address name
This commit is contained in:
parent
ea3f1bbde6
commit
a528129d37
@ -93,9 +93,9 @@ class Attachment
|
||||
$parameters = Message::getParametersFromStructure($structure);
|
||||
|
||||
if (isset($parameters['filename'])) {
|
||||
$this->filename = imap_utf8($parameters['filename']);
|
||||
$this->setFileName($parameters['filename']);
|
||||
} elseif (isset($parameters['name'])) {
|
||||
$this->filename = imap_utf8($parameters['name']);
|
||||
$this->setFileName($parameters['name']);
|
||||
}
|
||||
|
||||
$this->size = $structure->bytes;
|
||||
@ -231,4 +231,9 @@ class Attachment
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
protected function setFileName($text)
|
||||
{
|
||||
$this->filename = MIME::decode($text, Message::$charset);
|
||||
}
|
||||
}
|
||||
|
45
src/Fetch/MIME.php
Normal file
45
src/Fetch/MIME.php
Normal file
@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Fetch package.
|
||||
*
|
||||
* (c) Robert Hafner <tedivm@tedivm.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Fetch;
|
||||
|
||||
/**
|
||||
* This library is a wrapper around the Imap library functions included in php.
|
||||
*
|
||||
* @package Fetch
|
||||
* @author Robert Hafner <tedivm@tedivm.com>
|
||||
* @author Sergey Linnik <linniksa@gmail.com>
|
||||
*/
|
||||
final class MIME
|
||||
{
|
||||
/**
|
||||
* @param string $text
|
||||
* @param string $targetCharset
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function decode($text, $targetCharset = 'utf-8')
|
||||
{
|
||||
if (null === $text) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$result = '';
|
||||
|
||||
foreach (imap_mime_header_decode($text) as $word) {
|
||||
$ch = 'default' === $word->charset ? 'ascii' : $word->charset;
|
||||
|
||||
$result .= iconv($ch, $targetCharset, $word->text);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
@ -233,7 +233,7 @@ class Message
|
||||
|
||||
return false;
|
||||
|
||||
$this->subject = isset($messageOverview->subject) ? imap_utf8($messageOverview->subject) : null;
|
||||
$this->subject = MIME::decode($messageOverview->subject, self::$charset);
|
||||
$this->date = strtotime($messageOverview->date);
|
||||
$this->size = $messageOverview->size;
|
||||
|
||||
@ -674,7 +674,7 @@ class Message
|
||||
$currentAddress = array();
|
||||
$currentAddress['address'] = $address->mailbox . '@' . $address->host;
|
||||
if (isset($address->personal)) {
|
||||
$currentAddress['name'] = $address->personal;
|
||||
$currentAddress['name'] = MIME::decode($address->personal, self::$charset);
|
||||
}
|
||||
$outputAddresses[] = $currentAddress;
|
||||
}
|
||||
|
51
tests/Fetch/Test/MIMETest.php
Normal file
51
tests/Fetch/Test/MIMETest.php
Normal file
@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Fetch library.
|
||||
*
|
||||
* (c) Robert Hafner <tedivm@tedivm.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Fetch\Test;
|
||||
|
||||
use Fetch\MIME;
|
||||
|
||||
/**
|
||||
* @package Fetch
|
||||
* @author Robert Hafner <tedivm@tedivm.com>
|
||||
* @author Sergey Linnik <linniksa@gmail.com>
|
||||
*/
|
||||
class MIMETest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function decodeData()
|
||||
{
|
||||
return array(
|
||||
array(null, null),
|
||||
array('Just text', 'Just text'),
|
||||
array('Keith Moore <moore@cs.utk.edu>', '=?US-ASCII?Q?Keith_Moore?= <moore@cs.utk.edu>'),
|
||||
array('Keld Jørn Simonsen <keld@dkuug.dk>', '=?ISO-8859-1?Q?Keld_J=F8rn_Simonsen?= <keld@dkuug.dk>'),
|
||||
array('André Pirard <PIRARD@vm1.ulg.ac.be>', '=?ISO-8859-1?Q?Andr=E9?= Pirard <PIRARD@vm1.ulg.ac.be>'),
|
||||
array(
|
||||
'If you can read this you understand the example.',
|
||||
'=?ISO-8859-1?B?SWYgeW91IGNhbiByZWFkIHRoaXMgeW8=?='
|
||||
. PHP_EOL .
|
||||
'=?ISO-8859-2?B?dSB1bmRlcnN0YW5kIHRoZSBleGFtcGxlLg==?='
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider decodeData
|
||||
*
|
||||
* @param string $expected
|
||||
* @param string $text
|
||||
* @param string $charset
|
||||
*/
|
||||
public function testDecode($expected, $text, $charset = 'UTF-8')
|
||||
{
|
||||
self::assertSame($expected, MIME::decode($text, $charset));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user