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