Skip to content

Commit

Permalink
[fixed] RFC2231 Parameter value extended encoding is now supported.
Browse files Browse the repository at this point in the history
Faced that case with attachment with long name in Russian - got filename parameter encoded as per RFC2231 Section 4
See http://tools.ietf.org/html/rfc2231#section-4
  • Loading branch information
troggy committed Jul 25, 2013
1 parent 83df2b0 commit 2bcb40b
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions ImapMailbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ protected function initMailPart(IncomingMail $mail, $partStructure, $partNum) {
else {
$fileName = !empty($params['filename']) ? $params['filename'] : $params['name'];
$fileName = $this->decodeMimeStr($fileName, $this->serverEncoding);
$fileName = $this->decodeRFC2231($fileName, $this->serverEncoding);
}
$attachment = new IncomingMailAttachment();
$attachment->id = $attachmentId;
Expand Down Expand Up @@ -367,6 +368,27 @@ protected function initMailPart(IncomingMail $mail, $partStructure, $partNum) {
}
}

function isUrlEncoded($string) {
$string = preg_replace('/\%20/', '+', $string);
$decoded = urldecode($string);
return $decoded != $string && urlencode($decoded) == $string;
}

protected function decodeRFC2231($string, $charset = 'UTF-8') {
$parts = preg_split('/\'/', $string);
if (sizeof($parts) != 3) return $string; // no extended encoding

$encoding = $parts[0];
$data = $parts[2];

if (!$this->isUrlEncoded($data)) return $string;

$data = urldecode($data);
$data = iconv(strtoupper($encoding), $charset, $data);

return $data;
}

protected function decodeMimeStr($string, $charset = 'UTF-8') {
$newString = '';
$elements = imap_mime_header_decode($string);
Expand Down

0 comments on commit 2bcb40b

Please sign in to comment.