| 1 | <?php |
| 2 | |
| 3 | function decodeBASE64($s) { |
| 4 | $B64Values = array( |
| 5 | "A" => 0, "B" => 1, "C" => 2, "D" => 3, "E" => 4, "F" => 5, |
| 6 | "G" => 6, "H" => 7, "I" => 8, "J" => 9, "K" => 10, "L" => 11, |
| 7 | "M" => 12, "N" => 13, "O" => 14, "P" => 15, "Q" => 16, "R" => 17, |
| 8 | "S" => 18, "T" => 19, "U" => 20, "V" => 21, "W" => 22, "X" => 23, |
| 9 | "Y" => 24, "Z" => 25, |
| 10 | "a" => 26, "b" => 27, "c" => 28, "d" => 29, "e" => 30, "f" => 31, |
| 11 | "g" => 32, "h" => 33, "i" => 34, "j" => 35, "k" => 36, "l" => 37, |
| 12 | "m" => 38, "n" => 39, "o" => 40, "p" => 41, "q" => 42, "r" => 43, |
| 13 | "s" => 44, "t" => 45, "u" => 46, "v" => 47, "w" => 48, "x" => 49, |
| 14 | "y" => 50, "z" => 51, |
| 15 | "0" => 52, "1" => 53, "2" => 54, "3" => 55, "4" => 56, "5" => 57, |
| 16 | "6" => 58, "7" => 59, "8" => 60, "9" => 61, "+" => 62, "," => 63 |
| 17 | ); |
| 18 | $p = 0; |
| 19 | $d = ''; |
| 20 | $unicodeNullByteToggle = 0; |
| 21 | for ($i = 0; $i < strlen($s); $i++) { |
| 22 | $c = $s[$i]; |
| 23 | if ($p == 0) { |
| 24 | $t = $B64Values[$c]; |
| 25 | $p = 1; |
| 26 | } elseif ($p == 1) { |
| 27 | if ($unicodeNullByteToggle) { |
| 28 | $d = $d . chr(($t << 2) + (($B64Values[$c] & 48) >> 4)); |
| 29 | $unicodeNullByteToggle = 0; |
| 30 | } else { |
| 31 | $unicodeNullByteToggle = 1; |
| 32 | } |
| 33 | $t = ($B64Values[$c] & 15); |
| 34 | $p = 2; |
| 35 | } elseif ($p == 2) { |
| 36 | if ($unicodeNullByteToggle) { |
| 37 | $d = $d . chr(($t << 4) + (($B64Values[$c] & 60) >> 2)); |
| 38 | $unicodeNullByteToggle = 0; |
| 39 | } else { |
| 40 | $unicodeNullByteToggle = 1; |
| 41 | } |
| 42 | $t = ($B64Values[$c] & 3); |
| 43 | $p = 3; |
| 44 | } elseif ($p == 3) { |
| 45 | if ($unicodeNullByteToggle) { |
| 46 | $d = $d . chr(($t << 6) + $B64Values[$c]); |
| 47 | $unicodeNullByteToggle = 0; |
| 48 | } else { |
| 49 | $unicodeNullByteToggle = 1; |
| 50 | } |
| 51 | $t = ($B64Values[$c] & 3); |
| 52 | $p = 0; |
| 53 | } |
| 54 | } |
| 55 | return $d; |
| 56 | } |
| 57 | |
| 58 | function imap_utf7_decode_local($s) { |
| 59 | $b64_s = ''; |
| 60 | $iso_8859_1_s = ''; |
| 61 | for ($i = 0; $i < strlen($s); $i++) { |
| 62 | $c = $s[$i]; |
| 63 | if (strlen($b64_s) > 0) { |
| 64 | if ($c == '-') { |
| 65 | if ($b64_s == '&') { |
| 66 | $iso_8859_1_s = $iso_8859_1_s . '&'; |
| 67 | } else { |
| 68 | $iso_8859_1_s = $iso_8859_1_s . |
| 69 | decodeBASE64(substr($b64_s, 1)); |
| 70 | } |
| 71 | $b64_s = ''; |
| 72 | } else { |
| 73 | $b64_s = $b64_s . $c; |
| 74 | } |
| 75 | } else { |
| 76 | if ($c == '&') { |
| 77 | $b64_s = '&'; |
| 78 | } else { |
| 79 | $iso_8859_1_s = $iso_8859_1_s . $c; |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | return $iso_8859_1_s; |
| 84 | } |
| 85 | |
| 86 | ?> |