Skip to content

Commit

Permalink
Issue #321: Fixed PHP notice conversion failed / not allowed and prio…
Browse files Browse the repository at this point in the history
…rized mbstring before iconv
  • Loading branch information
Sebastian Krätzig committed May 15, 2019
1 parent 89be6bf commit f2cb352
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/PhpImap/DataPartInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ function fetch() {
break;
}

if(isset($this->charset)) {
if(isset($this->charset) AND !empty($this->charset)) {
$this->data = $this->mail->decodeMimeStr($this->data, $this->charset);
}

Expand Down
13 changes: 7 additions & 6 deletions src/PhpImap/Mailbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -889,15 +889,15 @@ protected function initMailPart(IncomingMail $mail, $partStructure, $partNum, $m
$attachment->name = $fileName;
$attachment->disposition = (isset($partStructure->disposition) ? $partStructure->disposition : null);
$attachment->charset = (isset($params['charset']) AND !empty($params['charset'])) ? $params['charset'] : null;
if($this->attachmentsDir) {
if($this->getAttachmentsDir() != null) {
$replace = [
'/\s/' => '_',
'/[^\w\.]/iu' => '',
'/_+/' => '_',
'/(^_)|(_$)/' => '',
];
$fileSysName = preg_replace('~[\\\\/]~', '', $mail->id . '_' . $attachmentId . '_' . preg_replace(array_keys($replace), $replace, $fileName));
$filePath = $this->attachmentsDir . DIRECTORY_SEPARATOR . $fileSysName;
$filePath = $this->getAttachmentsDir() . DIRECTORY_SEPARATOR . $fileSysName;

if(strlen($filePath) > 255) {
$ext = pathinfo($filePath, PATHINFO_EXTENSION);
Expand Down Expand Up @@ -1007,12 +1007,13 @@ public function parseDateTime($dateHeader) {
* @throws Exception
*/
public function convertStringEncoding($string, $fromEncoding, $toEncoding) {
if(!$string || $fromEncoding == $toEncoding) {
if(!$string || $fromEncoding == $toEncoding || preg_match("/default/i", $fromEncoding)) {
return $string;
}
$convertedString = function_exists('iconv') ? @iconv($fromEncoding, $toEncoding . '//IGNORE', $string) : null;
if(!$convertedString && extension_loaded('mbstring')) {
$convertedString = @mb_convert_encoding($string, $toEncoding, $fromEncoding);
if(extension_loaded('mbstring')) {
$convertedString = mb_convert_encoding($string, $toEncoding, $fromEncoding);
} elseif(function_exists('iconv')) {
$convertedString = iconv($fromEncoding, $toEncoding . '//IGNORE', $string);
}
if(!$convertedString) {
throw new Exception('Mime string encoding conversion failed');
Expand Down

0 comments on commit f2cb352

Please sign in to comment.