Skip to content

Commit

Permalink
UTF-8 checks added to prevent decoding of unencoded values #76
Browse files Browse the repository at this point in the history
  • Loading branch information
Webklex committed Jan 21, 2021
1 parent de50ce3 commit 27cb7e1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip
### Fixed
- Attachment detection updated #82 #90
- Timeout handling improved
- Additional utf-8 checks added to prevent decoding of unencoded values #76

### Added
- Auto reconnect option added to `Folder::idle()` #89
Expand All @@ -16,6 +17,7 @@ Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) princip
- [Folder::class](src/Folder.php)
- [Part::class](src/Part.php)
- [Client::class](src/Client.php)
- [Header::class](src/Header.php)

### Breaking changes
- NaN
Expand Down
21 changes: 17 additions & 4 deletions src/Header.php
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,16 @@ public function getEncoding($structure) {
return $this->fallback_encoding;
}

/**
* Test if a given value is utf-8 encoded
* @param $value
*
* @return bool
*/
private function is_uft8($value) {
return strpos(strtolower($value), '=?utf-8?') === 0;
}

/**
* Try to decode a specific header
* @param mixed $value
Expand All @@ -420,9 +430,12 @@ private function decode($value) {
$decoder = $this->config['decoder']['message'];

if ($value !== null) {
$is_utf8_base = $this->is_uft8($value);

if($decoder === 'utf-8' && extension_loaded('imap')) {
$value = \imap_utf8($value);
if (strpos(strtolower($value), '=?utf-8?') === 0) {
$is_utf8_base = $this->is_uft8($value);
if ($is_utf8_base) {
$value = mb_decode_mimeheader($value);
}
if ($this->notDecoded($original_value, $value)) {
Expand All @@ -433,13 +446,13 @@ private function decode($value) {
}
}
}
}elseif($decoder === 'iconv') {
}elseif($decoder === 'iconv' && $is_utf8_base) {
$value = iconv_mime_decode($value);
}else{
}elseif($is_utf8_base){
$value = mb_decode_mimeheader($value);
}

if (strpos(strtolower($value), '=?utf-8?') === 0) {
if ($this->is_uft8($value)) {
$value = mb_decode_mimeheader($value);
}

Expand Down

0 comments on commit 27cb7e1

Please sign in to comment.