Last active
November 9, 2017 10:36
-
-
Save thijzert/439b251b78fd31102cd865c1833dbb15 to your computer and use it in GitHub Desktop.
MIME encoded-word (RFC2047) decoder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @param string $value Raw MIME header field | |
* @return string UTF-8 subject | |
**/ | |
function decode_header_field( $value ) | |
{ | |
if ( preg_match( "/^\\=\\?([a-z0-9-]+)\\?([QB])\\?(.*)\\?\\=\$/i", $value, $sub ) ) | |
{ | |
$charset = strtoupper($sub[1]); | |
$encoding = strtoupper($sub[2]); | |
$raw = $sub[3]; | |
if ( $encoding == "B" ) | |
{ | |
$raw = base64_decode($raw); | |
} | |
elseif ( $encoding == "Q" ) | |
{ | |
$hexx = function( $h ) | |
{ | |
return hex2bin(substr($h[0],1)); | |
}; | |
$raw = preg_replace_callback( "/\\=[0-9a-f]{2}/i", $hexx, $raw ); | |
} | |
else | |
{ | |
return $value; | |
} | |
if ( $charset != "UTF-8" ) | |
{ | |
$raw = iconv( $charset, "UTF-8//IGNORE", $raw ); | |
} | |
return $raw; | |
} | |
return $value; | |
} | |
$test_cases = [ | |
'=?utf-8?B?UmU6IENoYWluJndpcmUgcm9wZSBhY2...?=', | |
'=?GB2312?B?UmU6TWFudWZhY3R1cmVyIGxvb2sgZ...?=', | |
'=?utf-8?B?UmU6QW5pbWFsIGVhciB0YWcgYW5kIG...?=', | |
'PCB supplier in China- RF-4, rogers, arl...', | |
'=?utf-8?B?UmU6IEFtbW9uaXVtIFBvbHlwaG9zcG...?=', | |
'the best price of glyphosate 41% SL From...', | |
'=?utf-8?B?UmU6U2V2ZXIgaGlnaCBxdWFsaXR5IE...?=', | |
'=?iso-8859-1?Q?=A1Hola,_se=F1or!?=', | |
]; | |
foreach ( $test_cases as $tc ) | |
{ | |
print( "{$tc}\n " . decode_header_field( $tc ) . "\n" ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment