| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * imap_utf7_local.php |
| 5 | * |
| 6 | * Copyright (c) 1999-2004 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 imap UTF7 conversions. |
| 10 | * |
| 11 | * @version $Id$ |
| 12 | * @package squirrelmail |
| 13 | * @subpackage imap |
| 14 | */ |
| 15 | |
| 16 | /** |
| 17 | * @param string $str |
| 18 | * @param string $to_encoding |
| 19 | * @param string $from_encoding |
| 20 | * @param string $default_charset |
| 21 | * @return string |
| 22 | */ |
| 23 | function sqimap_mb_convert_encoding($str, $to_encoding, $from_encoding, $default_charset) |
| 24 | { |
| 25 | // Allows mbstring functions only with iso-8859-*, utf-8 and |
| 26 | // iso-2022-jp (Japanese) |
| 27 | // koi8-r and gb2312 can be added only in php 4.3+ |
| 28 | if ( stristr($default_charset, 'iso-8859-') || |
| 29 | stristr($default_charset, 'utf-8') || |
| 30 | stristr($default_charset, 'iso-2022-jp') ) { |
| 31 | if (function_exists('mb_convert_encoding')) { |
| 32 | return mb_convert_encoding($str, $to_encoding, $from_encoding); |
| 33 | } |
| 34 | } |
| 35 | return ''; |
| 36 | } |
| 37 | |
| 38 | function imap_utf7_encode_local($s) { |
| 39 | global $languages, $squirrelmail_language; |
| 40 | |
| 41 | if (isset($languages[$squirrelmail_language]['XTRA_CODE']) && |
| 42 | function_exists($languages[$squirrelmail_language]['XTRA_CODE'].'_utf7_imap_encode')) { |
| 43 | return call_user_func($languages[$squirrelmail_language]['XTRA_CODE'] . '_utf7_imap_encode', $s); |
| 44 | } |
| 45 | |
| 46 | if ($s == '') //If empty, don't bother |
| 47 | return ''; |
| 48 | |
| 49 | global $default_charset; |
| 50 | set_my_charset(); //must be called before using $default_charset |
| 51 | if ((strtolower($default_charset) != 'iso-8859-1') && ($default_charset != '')) { |
| 52 | $utf7_s = sqimap_mb_convert_encoding($s, 'UTF7-IMAP', $default_charset, $default_charset); |
| 53 | if ($utf7_s != '') |
| 54 | return $utf7_s; |
| 55 | } |
| 56 | |
| 57 | // Later code works only for ISO-8859-1 |
| 58 | |
| 59 | $b64_s = ''; // buffer for substring to be base64-encoded |
| 60 | $utf7_s = ''; // imap-utf7-encoded string |
| 61 | for ($i = 0; $i < strlen($s); $i++) { |
| 62 | $c = $s[$i]; |
| 63 | $ord_c = ord($c); |
| 64 | if ((($ord_c >= 0x20) and ($ord_c <= 0x25)) or |
| 65 | (($ord_c >= 0x27) and ($ord_c <= 0x7e))) { |
| 66 | if ($b64_s) { |
| 67 | $utf7_s = $utf7_s . '&' . encodeBASE64($b64_s) .'-'; |
| 68 | $b64_s = ''; |
| 69 | } |
| 70 | $utf7_s = $utf7_s . $c; |
| 71 | } elseif ($ord_c == 0x26) { |
| 72 | if ($b64_s) { |
| 73 | $utf7_s = $utf7_s . '&' . encodeBASE64($b64_s) . '-'; |
| 74 | $b64_s = ''; |
| 75 | } |
| 76 | $utf7_s = $utf7_s . '&-'; |
| 77 | } else { |
| 78 | $b64_s = $b64_s . chr(0) . $c; |
| 79 | } |
| 80 | } |
| 81 | // |
| 82 | // flush buffer |
| 83 | // |
| 84 | if ($b64_s) { |
| 85 | $utf7_s = $utf7_s . '&' . encodeBASE64($b64_s) . '-'; |
| 86 | $b64_s = ''; |
| 87 | } |
| 88 | return $utf7_s; |
| 89 | } |
| 90 | |
| 91 | function imap_utf7_decode_local($s) { |
| 92 | global $languages, $squirrelmail_language; |
| 93 | |
| 94 | if (isset($languages[$squirrelmail_language]['XTRA_CODE']) && |
| 95 | function_exists($languages[$squirrelmail_language]['XTRA_CODE'] . '_utf7_imap_decode')) { |
| 96 | return call_user_func($languages[$squirrelmail_language]['XTRA_CODE'] . '_utf7_imap_decode', $s); |
| 97 | } |
| 98 | |
| 99 | if ($s == '') //If empty, don't bother |
| 100 | return ''; |
| 101 | |
| 102 | global $default_charset; |
| 103 | set_my_charset(); //must be called before using $default_charset |
| 104 | if ((strtolower($default_charset) != 'iso-8859-1') && ($default_charset != '')) { |
| 105 | $utf7_s = sqimap_mb_convert_encoding($s, $default_charset, 'UTF7-IMAP', $default_charset); |
| 106 | if ($utf7_s != '') |
| 107 | return $utf7_s; |
| 108 | } |
| 109 | |
| 110 | // Later code works only for ISO-8859-1 |
| 111 | |
| 112 | $b64_s = ''; |
| 113 | $iso_8859_1_s = ''; |
| 114 | for ($i = 0, $len = strlen($s); $i < $len; $i++) { |
| 115 | $c = $s[$i]; |
| 116 | if (strlen($b64_s) > 0) { |
| 117 | if ($c == '-') { |
| 118 | if ($b64_s == '&') { |
| 119 | $iso_8859_1_s = $iso_8859_1_s . '&'; |
| 120 | } else { |
| 121 | $iso_8859_1_s = $iso_8859_1_s . |
| 122 | decodeBASE64(substr($b64_s, 1)); |
| 123 | } |
| 124 | $b64_s = ''; |
| 125 | } else { |
| 126 | $b64_s = $b64_s . $c; |
| 127 | } |
| 128 | } else { |
| 129 | if ($c == '&') { |
| 130 | $b64_s = '&'; |
| 131 | } else { |
| 132 | $iso_8859_1_s = $iso_8859_1_s . $c; |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | return $iso_8859_1_s; |
| 137 | } |
| 138 | |
| 139 | function encodeBASE64($s) { |
| 140 | $B64Chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+,'; |
| 141 | $p = 0; // phase: 1 / 2 / 3 / 1 / 2 / 3... |
| 142 | $e = ''; // base64-encoded string |
| 143 | //foreach($s as $c) { |
| 144 | for ($i = 0; $i < strlen($s); $i++) { |
| 145 | $c = $s[$i]; |
| 146 | if ($p == 0) { |
| 147 | $e = $e . substr($B64Chars, ((ord($c) & 252) >> 2), 1); |
| 148 | $t = (ord($c) & 3); |
| 149 | $p = 1; |
| 150 | } elseif ($p == 1) { |
| 151 | $e = $e . $B64Chars[($t << 4) + ((ord($c) & 240) >> 4)]; |
| 152 | $t = (ord($c) & 15); |
| 153 | $p = 2; |
| 154 | } elseif ($p == 2) { |
| 155 | $e = $e . $B64Chars[($t << 2) + ((ord($c) & 192) >> 6)]; |
| 156 | $e = $e . $B64Chars[ord($c) & 63]; |
| 157 | $p = 0; |
| 158 | } |
| 159 | } |
| 160 | // |
| 161 | // flush buffer |
| 162 | // |
| 163 | if ($p == 1) { |
| 164 | $e = $e . $B64Chars[$t << 4]; |
| 165 | } elseif ($p == 2) { |
| 166 | $e = $e . $B64Chars[$t << 2]; |
| 167 | } |
| 168 | return $e; |
| 169 | } |
| 170 | |
| 171 | function decodeBASE64($s) { |
| 172 | $B64Values = array( |
| 173 | 'A' => 0, 'B' => 1, 'C' => 2, 'D' => 3, 'E' => 4, 'F' => 5, |
| 174 | 'G' => 6, 'H' => 7, 'I' => 8, 'J' => 9, 'K' => 10, 'L' => 11, |
| 175 | 'M' => 12, 'N' => 13, 'O' => 14, 'P' => 15, 'Q' => 16, 'R' => 17, |
| 176 | 'S' => 18, 'T' => 19, 'U' => 20, 'V' => 21, 'W' => 22, 'X' => 23, |
| 177 | 'Y' => 24, 'Z' => 25, |
| 178 | 'a' => 26, 'b' => 27, 'c' => 28, 'd' => 29, 'e' => 30, 'f' => 31, |
| 179 | 'g' => 32, 'h' => 33, 'i' => 34, 'j' => 35, 'k' => 36, 'l' => 37, |
| 180 | 'm' => 38, 'n' => 39, 'o' => 40, 'p' => 41, 'q' => 42, 'r' => 43, |
| 181 | 's' => 44, 't' => 45, 'u' => 46, 'v' => 47, 'w' => 48, 'x' => 49, |
| 182 | 'y' => 50, 'z' => 51, |
| 183 | '0' => 52, '1' => 53, '2' => 54, '3' => 55, '4' => 56, '5' => 57, |
| 184 | '6' => 58, '7' => 59, '8' => 60, '9' => 61, '+' => 62, ',' => 63 |
| 185 | ); |
| 186 | $p = 0; |
| 187 | $d = ''; |
| 188 | $unicodeNullByteToggle = 0; |
| 189 | for ($i = 0, $len = strlen($s); $i < $len; $i++) { |
| 190 | $c = $s[$i]; |
| 191 | if ($p == 0) { |
| 192 | $t = $B64Values[$c]; |
| 193 | $p = 1; |
| 194 | } elseif ($p == 1) { |
| 195 | if ($unicodeNullByteToggle) { |
| 196 | $d = $d . chr(($t << 2) + (($B64Values[$c] & 48) >> 4)); |
| 197 | $unicodeNullByteToggle = 0; |
| 198 | } else { |
| 199 | $unicodeNullByteToggle = 1; |
| 200 | } |
| 201 | $t = ($B64Values[$c] & 15); |
| 202 | $p = 2; |
| 203 | } elseif ($p == 2) { |
| 204 | if ($unicodeNullByteToggle) { |
| 205 | $d = $d . chr(($t << 4) + (($B64Values[$c] & 60) >> 2)); |
| 206 | $unicodeNullByteToggle = 0; |
| 207 | } else { |
| 208 | $unicodeNullByteToggle = 1; |
| 209 | } |
| 210 | $t = ($B64Values[$c] & 3); |
| 211 | $p = 3; |
| 212 | } elseif ($p == 3) { |
| 213 | if ($unicodeNullByteToggle) { |
| 214 | $d = $d . chr(($t << 6) + $B64Values[$c]); |
| 215 | $unicodeNullByteToggle = 0; |
| 216 | } else { |
| 217 | $unicodeNullByteToggle = 1; |
| 218 | } |
| 219 | $t = ($B64Values[$c] & 3); |
| 220 | $p = 0; |
| 221 | } |
| 222 | } |
| 223 | return $d; |
| 224 | } |
| 225 | |
| 226 | ?> |