Skip to content

Commit b0bac1e

Browse files
authored
Add a fallback when aliasing encodings for iconv (#325)
1 parent 91f2bd6 commit b0bac1e

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

src/IMAP/EncodingAliases.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -465,17 +465,20 @@ class EncodingAliases {
465465
];
466466

467467
/**
468-
* Returns proper encoding mapping, if exsists. If it doesn't, return unchanged $encoding
468+
* Returns proper encoding mapping, if it exists. If it doesn't, return $fallback if it is not null.
469+
* If neither a mapping was found nor $fallback was given return the unchanged $encoding
469470
*
470471
* @param string $encoding
471472
* @return string
472473
*/
473-
public static function get($encoding) {
474+
public static function get($encoding, $fallback = null) {
474475
if (isset(self::$aliases[strtolower($encoding)])) {
475476
return self::$aliases[strtolower($encoding)];
476-
} else {
477-
return $encoding;
478477
}
478+
if ($fallback !== null) {
479+
return $fallback;
480+
}
481+
return $encoding;
479482
}
480483

481484
}

src/IMAP/Message.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
* @property array reply_to
4646
* @property array in_reply_to
4747
* @property array sender
48+
* @property string fallback_encoding
4849
*
4950
* @method integer getMsglist()
5051
* @method integer setMsglist(integer $msglist)
@@ -176,6 +177,12 @@ class Message {
176177
public $attachments = [];
177178
public $flags = [];
178179

180+
/**
181+
* Fallback Encoding
182+
* @var string
183+
*/
184+
public $fallback_encoding = 'UTF-8';
185+
179186
/**
180187
* A list of all available and supported flags
181188
*
@@ -590,8 +597,7 @@ private function parseAddresses($list) {
590597
if(is_array($personalParts)) {
591598
$address->personal = '';
592599
foreach ($personalParts as $p) {
593-
$encoding = (property_exists($p, 'charset')) ? $p->charset : $this->getEncoding($p->text);
594-
$address->personal .= $this->convertEncoding($p->text, $encoding);
600+
$address->personal .= $this->convertEncoding($p->text, $this->getEncoding($p));
595601
}
596602
}
597603
}
@@ -853,8 +859,8 @@ public function decodeString($string, $encoding) {
853859
*/
854860
public function convertEncoding($str, $from = "ISO-8859-2", $to = "UTF-8") {
855861

856-
$from = EncodingAliases::get($from);
857-
$to = EncodingAliases::get($to);
862+
$from = EncodingAliases::get($from, $this->fallback_encoding);
863+
$to = EncodingAliases::get($to, $this->fallback_encoding);
858864

859865
if ($from === $to) {
860866
return $str;
@@ -896,9 +902,11 @@ public function getEncoding($structure) {
896902
if (property_exists($structure, 'parameters')) {
897903
foreach ($structure->parameters as $parameter) {
898904
if (strtolower($parameter->attribute) == "charset") {
899-
return EncodingAliases::get($parameter->value);
905+
return EncodingAliases::get($parameter->value, $this->fallback_encoding);
900906
}
901907
}
908+
}elseif (property_exists($structure, 'charset')) {
909+
return EncodingAliases::get($structure->charset, $this->fallback_encoding);
902910
}elseif (is_string($structure) === true){
903911
return mb_detect_encoding($structure);
904912
}

0 commit comments

Comments
 (0)