| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * imap_general.php |
| 5 | * |
| 6 | * Copyright (c) 1999-2002 The SquirrelMail Project Team |
| 7 | * Licensed under the GNU GPL. For full terms see the file COPYING. |
| 8 | * |
| 9 | * This implements all functions that do general imap functions. |
| 10 | * |
| 11 | * $Id $ |
| 12 | */ |
| 13 | |
| 14 | function imap_utf7_encode_local($s) { |
| 15 | $b64_s = ''; // buffer for substring to be base64-encoded |
| 16 | $utf7_s = ''; // imap-utf7-encoded string |
| 17 | for ($i = 0; $i < strlen($s); $i++) { |
| 18 | $c = $s[$i]; |
| 19 | $ord_c = ord($c); |
| 20 | if ((($ord_c >= 0x20) and ($ord_c <= 0x25)) or |
| 21 | (($ord_c >= 0x27) and ($ord_c <= 0x7e))) { |
| 22 | if ($b64_s) { |
| 23 | $utf7_s = $utf7_s . '&' . encodeBASE64($b64_s) .'-'; |
| 24 | $b64_s = ''; |
| 25 | } |
| 26 | $utf7_s = $utf7_s . $c; |
| 27 | } elseif ($ord_c == 0x26) { |
| 28 | if ($b64_s) { |
| 29 | $utf7_s = $utf7_s . '&' . encodeBASE64($b64_s) . '-'; |
| 30 | $b64_s = ''; |
| 31 | } |
| 32 | $utf7_s = $utf7_s . '&-'; |
| 33 | } else { |
| 34 | $b64_s = $b64_s . chr(0) . $c; |
| 35 | } |
| 36 | } |
| 37 | // |
| 38 | // flush buffer |
| 39 | // |
| 40 | if ($b64_s) { |
| 41 | $utf7_s = $utf7_s . '&' . encodeBASE64($b64_s) . '-'; |
| 42 | $b64_s = ''; |
| 43 | } |
| 44 | return $utf7_s; |
| 45 | } |
| 46 | |
| 47 | function imap_utf7_decode_local($s) { |
| 48 | $b64_s = ''; |
| 49 | $iso_8859_1_s = ''; |
| 50 | for ($i = 0, $len = strlen($s); $i < $len; $i++) { |
| 51 | $c = $s[$i]; |
| 52 | if (strlen($b64_s) > 0) { |
| 53 | if ($c == '-') { |
| 54 | if ($b64_s == '&') { |
| 55 | $iso_8859_1_s = $iso_8859_1_s . '&'; |
| 56 | } else { |
| 57 | $iso_8859_1_s = $iso_8859_1_s . |
| 58 | decodeBASE64(substr($b64_s, 1)); |
| 59 | } |
| 60 | $b64_s = ''; |
| 61 | } else { |
| 62 | $b64_s = $b64_s . $c; |
| 63 | } |
| 64 | } else { |
| 65 | if ($c == '&') { |
| 66 | $b64_s = '&'; |
| 67 | } else { |
| 68 | $iso_8859_1_s = $iso_8859_1_s . $c; |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | return $iso_8859_1_s; |
| 73 | } |
| 74 | |
| 75 | function encodeBASE64($s) { |
| 76 | $B64Chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+,'; |
| 77 | $p = 0; // phase: 1 / 2 / 3 / 1 / 2 / 3... |
| 78 | $e = ''; // base64-encoded string |
| 79 | //foreach($s as $c) { |
| 80 | for ($i = 0; $i < strlen($s); $i++) { |
| 81 | $c = $s[$i]; |
| 82 | if ($p == 0) { |
| 83 | $e = $e . substr($B64Chars, ((ord($c) & 252) >> 2), 1); |
| 84 | $t = (ord($c) & 3); |
| 85 | $p = 1; |
| 86 | } elseif ($p == 1) { |
| 87 | $e = $e . $B64Chars[($t << 4) + ((ord($c) & 240) >> 4)]; |
| 88 | $t = (ord($c) & 15); |
| 89 | $p = 2; |
| 90 | } elseif ($p == 2) { |
| 91 | $e = $e . $B64Chars[($t << 2) + ((ord($c) & 192) >> 6)]; |
| 92 | $e = $e . $B64Chars[ord($c) & 63]; |
| 93 | $p = 0; |
| 94 | } |
| 95 | } |
| 96 | // |
| 97 | // flush buffer |
| 98 | // |
| 99 | if ($p == 1) { |
| 100 | $e = $e . $B64Chars[$t << 4]; |
| 101 | } elseif ($p == 2) { |
| 102 | $e = $e . $B64Chars[$t << 2]; |
| 103 | } |
| 104 | return $e; |
| 105 | } |
| 106 | |
| 107 | function decodeBASE64($s) { |
| 108 | $B64Values = array( |
| 109 | 'A' => 0, 'B' => 1, 'C' => 2, 'D' => 3, 'E' => 4, 'F' => 5, |
| 110 | 'G' => 6, 'H' => 7, 'I' => 8, 'J' => 9, 'K' => 10, 'L' => 11, |
| 111 | 'M' => 12, 'N' => 13, 'O' => 14, 'P' => 15, 'Q' => 16, 'R' => 17, |
| 112 | 'S' => 18, 'T' => 19, 'U' => 20, 'V' => 21, 'W' => 22, 'X' => 23, |
| 113 | 'Y' => 24, 'Z' => 25, |
| 114 | 'a' => 26, 'b' => 27, 'c' => 28, 'd' => 29, 'e' => 30, 'f' => 31, |
| 115 | 'g' => 32, 'h' => 33, 'i' => 34, 'j' => 35, 'k' => 36, 'l' => 37, |
| 116 | 'm' => 38, 'n' => 39, 'o' => 40, 'p' => 41, 'q' => 42, 'r' => 43, |
| 117 | 's' => 44, 't' => 45, 'u' => 46, 'v' => 47, 'w' => 48, 'x' => 49, |
| 118 | 'y' => 50, 'z' => 51, |
| 119 | '0' => 52, '1' => 53, '2' => 54, '3' => 55, '4' => 56, '5' => 57, |
| 120 | '6' => 58, '7' => 59, '8' => 60, '9' => 61, '+' => 62, ',' => 63 |
| 121 | ); |
| 122 | $p = 0; |
| 123 | $d = ''; |
| 124 | $unicodeNullByteToggle = 0; |
| 125 | for ($i = 0, $len = strlen($s); $i < $len; $i++) { |
| 126 | $c = $s[$i]; |
| 127 | if ($p == 0) { |
| 128 | $t = $B64Values[$c]; |
| 129 | $p = 1; |
| 130 | } elseif ($p == 1) { |
| 131 | if ($unicodeNullByteToggle) { |
| 132 | $d = $d . chr(($t << 2) + (($B64Values[$c] & 48) >> 4)); |
| 133 | $unicodeNullByteToggle = 0; |
| 134 | } else { |
| 135 | $unicodeNullByteToggle = 1; |
| 136 | } |
| 137 | $t = ($B64Values[$c] & 15); |
| 138 | $p = 2; |
| 139 | } elseif ($p == 2) { |
| 140 | if ($unicodeNullByteToggle) { |
| 141 | $d = $d . chr(($t << 4) + (($B64Values[$c] & 60) >> 2)); |
| 142 | $unicodeNullByteToggle = 0; |
| 143 | } else { |
| 144 | $unicodeNullByteToggle = 1; |
| 145 | } |
| 146 | $t = ($B64Values[$c] & 3); |
| 147 | $p = 3; |
| 148 | } elseif ($p == 3) { |
| 149 | if ($unicodeNullByteToggle) { |
| 150 | $d = $d . chr(($t << 6) + $B64Values[$c]); |
| 151 | $unicodeNullByteToggle = 0; |
| 152 | } else { |
| 153 | $unicodeNullByteToggle = 1; |
| 154 | } |
| 155 | $t = ($B64Values[$c] & 3); |
| 156 | $p = 0; |
| 157 | } |
| 158 | } |
| 159 | return $d; |
| 160 | } |
| 161 | |
| 162 | ?> |