1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Goetas\Mail\ToSwiftMailParser\Mime; |
4
|
|
|
|
5
|
|
|
class HeaderDecoder |
6
|
|
|
{ |
7
|
|
|
public static $decodeWindows1252 = false; |
8
|
|
|
|
9
|
5 |
|
public function decode($string) |
10
|
|
|
{ |
11
|
|
|
/* Take out any spaces between multiple encoded words. */ |
12
|
5 |
|
$string = preg_replace('|\?=\s+=\?|', '?==?', $string); |
13
|
|
|
|
14
|
5 |
|
$out = ''; |
15
|
5 |
|
$old_pos = 0; |
16
|
|
|
|
17
|
5 |
|
while (($pos = strpos($string, '=?', $old_pos)) !== false) { |
18
|
|
|
/* Save any preceding text. */ |
19
|
3 |
|
$out .= substr($string, $old_pos, $pos - $old_pos); |
20
|
|
|
|
21
|
|
|
/* Search for first delimiting question mark (charset). */ |
22
|
3 |
|
if (($d1 = strpos($string, '?', $pos + 2)) === false) { |
23
|
|
|
break; |
24
|
|
|
} |
25
|
|
|
|
26
|
3 |
|
$orig_charset = substr($string, $pos + 2, $d1 - $pos - 2); |
27
|
3 |
|
if (self::$decodeWindows1252 && mb_strtolower($orig_charset) == 'iso-8859-1') { |
28
|
|
|
$orig_charset = 'windows-1252'; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/* Search for second delimiting question mark (encoding). */ |
32
|
3 |
|
if (($d2 = strpos($string, '?', $d1 + 1)) === false) { |
33
|
|
|
break; |
34
|
|
|
} |
35
|
|
|
|
36
|
3 |
|
$encoding = substr($string, $d1 + 1, $d2 - $d1 - 1); |
37
|
|
|
|
38
|
|
|
/* Search for end of encoded data. */ |
39
|
3 |
|
if (($end = strpos($string, '?=', $d2 + 1)) === false) { |
40
|
|
|
break; |
41
|
|
|
} |
42
|
|
|
|
43
|
3 |
|
$encoded_text = substr($string, $d2 + 1, $end - $d2 - 1); |
44
|
|
|
|
45
|
|
|
switch ($encoding) { |
46
|
3 |
|
case 'Q' : |
|
|
|
|
47
|
3 |
|
case 'q' : |
|
|
|
|
48
|
|
|
$out .= self::convertCharset(preg_replace_callback('/=([0-9a-f]{2})/i', function ($ord) { |
49
|
|
|
return chr(hexdec($ord [1])); |
50
|
|
|
}, str_replace('_', ' ', $encoded_text)), $orig_charset, 'UTF-8'); |
51
|
|
|
break; |
52
|
|
|
|
53
|
3 |
|
case 'B' : |
|
|
|
|
54
|
3 |
|
case 'b' : |
|
|
|
|
55
|
3 |
|
$out .= self::convertCharset(base64_decode($encoded_text), $orig_charset, 'UTF-8'); |
56
|
3 |
|
break; |
57
|
|
|
|
58
|
|
|
default : |
|
|
|
|
59
|
|
|
// Ignore unknown encoding. |
60
|
|
|
break; |
61
|
|
|
} |
62
|
|
|
|
63
|
3 |
|
$old_pos = $end + 2; |
64
|
3 |
|
} |
65
|
|
|
|
66
|
5 |
|
return $out . substr($string, $old_pos); |
67
|
|
|
} |
68
|
|
|
|
69
|
3 |
|
public static function convertCharset($str, $orig, $to) |
|
|
|
|
70
|
|
|
{ |
71
|
|
|
//@todo convert charset |
72
|
3 |
|
return $str; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
As per the PSR-2 coding standard, there must not be a space in front of the colon in case statements.
To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.