Skip to content

Commit

Permalink
Require ext-iconv (#248)
Browse files Browse the repository at this point in the history
  • Loading branch information
Slamdunk committed Oct 17, 2017
1 parent ac05f4a commit 58b1c7b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 34 deletions.
4 changes: 3 additions & 1 deletion README.md
Expand Up @@ -8,7 +8,9 @@

A PHP 7.0+ library to read and process e-mails over IMAP.

This library requires both [IMAP](https://secure.php.net/manual/en/book.imap.php) and [Multibyte String](https://secure.php.net/manual/en/book.mbstring.php) extensions installed.
This library requires [IMAP](https://secure.php.net/manual/en/book.imap.php),
[iconv](https://secure.php.net/manual/en/book.iconv.php) and
[Multibyte String](https://secure.php.net/manual/en/book.mbstring.php) extensions installed.

## Table of Contents

Expand Down
1 change: 1 addition & 0 deletions composer.json
Expand Up @@ -19,6 +19,7 @@
],
"require": {
"php": "^7.0",
"ext-iconv": "*",
"ext-imap": "*",
"ext-mbstring": "*"
},
Expand Down
42 changes: 9 additions & 33 deletions src/Message/Transcoder.php
Expand Up @@ -247,7 +247,15 @@ public static function decode(string $text, string $fromCharset): string
$fromCharset = self::$charsetAliases[$lowercaseFromCharset];
}

$iconvDecodedText = self::iconvDecode($text, $originalFromCharset, $fromCharset);
\set_error_handler(function () {});

$iconvDecodedText = \iconv($fromCharset, 'UTF-8', $text);
if (false === $iconvDecodedText) {
$iconvDecodedText = \iconv($originalFromCharset, 'UTF-8', $text);
}

\restore_error_handler();

if (false !== $iconvDecodedText) {
return $iconvDecodedText;
}
Expand All @@ -274,36 +282,4 @@ public static function decode(string $text, string $fromCharset): string

return $decodedText;
}

/**
* Decode text to UTF-8 with iconv.
*
* @param string $text Text to decode
* @param string $originalFromCharset Original charset
* @param string $fromCharset Aliased charset
*
* @return bool|string
*/
private static function iconvDecode($text, $originalFromCharset, $fromCharset)
{
static $iconvLoaded;
if (null === $iconvLoaded) {
$iconvLoaded = \function_exists('iconv');
}

if (false === $iconvLoaded) {
return false;
}

\set_error_handler(function () {});

$iconvDecodedText = \iconv($fromCharset, 'UTF-8', $text);
if (false === $iconvDecodedText) {
$iconvDecodedText = \iconv($originalFromCharset, 'UTF-8', $text);
}

\restore_error_handler();

return $iconvDecodedText;
}
}

0 comments on commit 58b1c7b

Please sign in to comment.