|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Goetas\Mail\ToSwiftMailParser\Mime; |
|
4
|
|
|
|
|
5
|
|
|
class HeaderDecoder |
|
6
|
|
|
{ |
|
7
|
|
|
public static $decodeWindows1252 = false; |
|
8
|
|
|
|
|
9
|
4 |
|
public function decode($string) |
|
10
|
|
|
{ |
|
11
|
|
|
/* Take out any spaces between multiple encoded words. */ |
|
12
|
4 |
|
$string = preg_replace('|\?=\s+=\?|', '?==?', $string); |
|
13
|
|
|
|
|
14
|
4 |
|
$out = ''; |
|
15
|
4 |
|
$old_pos = 0; |
|
16
|
|
|
|
|
17
|
4 |
|
while (($pos = strpos($string, '=?', $old_pos)) !== false) { |
|
18
|
|
|
/* Save any preceding text. */ |
|
19
|
2 |
|
$out .= substr($string, $old_pos, $pos - $old_pos); |
|
20
|
|
|
|
|
21
|
|
|
/* Search for first delimiting question mark (charset). */ |
|
22
|
2 |
|
if (($d1 = strpos($string, '?', $pos + 2)) === false) { |
|
23
|
|
|
break; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
2 |
|
$orig_charset = substr($string, $pos + 2, $d1 - $pos - 2); |
|
27
|
2 |
|
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
|
2 |
|
if (($d2 = strpos($string, '?', $d1 + 1)) === false) { |
|
33
|
|
|
break; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
2 |
|
$encoding = substr($string, $d1 + 1, $d2 - $d1 - 1); |
|
37
|
|
|
|
|
38
|
|
|
/* Search for end of encoded data. */ |
|
39
|
2 |
|
if (($end = strpos($string, '?=', $d2 + 1)) === false) { |
|
40
|
|
|
break; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
2 |
|
$encoded_text = substr($string, $d2 + 1, $end - $d2 - 1); |
|
44
|
|
|
|
|
45
|
|
|
switch ($encoding) { |
|
46
|
2 |
|
case 'Q' : |
|
|
|
|
|
|
47
|
2 |
|
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
|
2 |
|
case 'B' : |
|
|
|
|
|
|
54
|
2 |
|
case 'b' : |
|
|
|
|
|
|
55
|
2 |
|
$out .= self::convertCharset(base64_decode($encoded_text), $orig_charset, 'UTF-8'); |
|
56
|
2 |
|
break; |
|
57
|
|
|
|
|
58
|
|
|
default : |
|
|
|
|
|
|
59
|
|
|
// Ignore unknown encoding. |
|
60
|
|
|
break; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
2 |
|
$old_pos = $end + 2; |
|
64
|
2 |
|
} |
|
65
|
|
|
|
|
66
|
4 |
|
return $out . substr($string, $old_pos); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
2 |
|
public static function convertCharset($str, $orig, $to) |
|
|
|
|
|
|
70
|
|
|
{ |
|
71
|
|
|
//@todo convert charset |
|
72
|
2 |
|
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.