0eaca2cb65b9cb9b19a1a9019f381c1099322a3c
4 * functions/imap_utf7_local.php - utf7-imap functions
6 * This implements all functions that do imap UTF7 conversions.
7 * Before 1.3.2 functions were stored in imap_utf7_decode_local.php and
8 * imap_utf7_encode_local.php files.
10 * @copyright © 1999-2009 The SquirrelMail Project Team
11 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
13 * @package squirrelmail
19 * Function that uses php mbstring functions to convert from and to utf7-imap charset
21 * Since 1.5.1 list of supported charsets depends sq_mb_list_encoding function.
22 * Before that it was hardcoded to iso-8859-x, utf-8 and iso-2022-jp.
23 * @param string $str folder name
24 * @param string $to_encoding name of resulting charset
25 * @param string $from_encoding name of original charset
26 * @param string $default_charset default charset used by translation.
27 * @return string encoded folder name or ''
30 function sqimap_mb_convert_encoding($str, $to_encoding, $from_encoding, $default_charset) {
31 $supported_encodings=sq_mb_list_encodings();
32 if ( in_array(strtolower($default_charset),$supported_encodings) &&
33 function_exists('mb_convert_encoding')) {
34 return mb_convert_encoding($str, $to_encoding, $from_encoding);
40 * encode folder name to utf7-imap
42 * If mbstring functions do not support charset used by translation, falls back to iso-8859-1
43 * @param string $s folder name
44 * @return string utf7-imap encoded folder name
47 function imap_utf7_encode_local($s) {
48 global $languages, $squirrelmail_language;
50 if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
51 function_exists($languages[$squirrelmail_language]['XTRA_CODE'].'_utf7_imap_encode')) {
52 return call_user_func($languages[$squirrelmail_language]['XTRA_CODE'] . '_utf7_imap_encode', $s);
55 if ($s == '') //If empty, don't bother
58 global $default_charset;
59 set_my_charset(); //must be called before using $default_charset
60 if ((strtolower($default_charset) != 'iso-8859-1') && ($default_charset != '')) {
61 $utf7_s = sqimap_mb_convert_encoding($s, 'UTF7-IMAP', $default_charset, $default_charset);
66 // Later code works only for ISO-8859-1
68 $b64_s = ''; // buffer for substring to be base64-encoded
69 $utf7_s = ''; // imap-utf7-encoded string
70 for ($i = 0; $i < strlen($s); $i++
) {
73 if ((($ord_c >= 0x20) and ($ord_c <= 0x25)) or
74 (($ord_c >= 0x27) and ($ord_c <= 0x7e))) {
76 $utf7_s = $utf7_s . '&' . encodeBASE64($b64_s) .'-';
79 $utf7_s = $utf7_s . $c;
80 } elseif ($ord_c == 0x26) {
82 $utf7_s = $utf7_s . '&' . encodeBASE64($b64_s) . '-';
85 $utf7_s = $utf7_s . '&-';
87 $b64_s = $b64_s . chr(0) . $c;
94 $utf7_s = $utf7_s . '&' . encodeBASE64($b64_s) . '-';
101 * converts folder name from utf7-imap to charset used by translation
103 * If mbstring functions do not support charset used by translation, falls back to iso-8859-1
104 * @param string $s folder name in utf7-imap
105 * @return string folder name in charset used by translation
108 function imap_utf7_decode_local($s) {
109 global $languages, $squirrelmail_language;
111 if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
112 function_exists($languages[$squirrelmail_language]['XTRA_CODE'] . '_utf7_imap_decode')) {
113 return call_user_func($languages[$squirrelmail_language]['XTRA_CODE'] . '_utf7_imap_decode', $s);
116 if ($s == '') //If empty, don't bother
119 global $default_charset;
120 set_my_charset(); //must be called before using $default_charset
121 if ((strtolower($default_charset) != 'iso-8859-1') && ($default_charset != '')) {
122 $utf7_s = sqimap_mb_convert_encoding($s, $default_charset, 'UTF7-IMAP', $default_charset);
127 // Later code works only for ISO-8859-1
131 for ($i = 0, $len = strlen($s); $i < $len; $i++
) {
133 if (strlen($b64_s) > 0) {
136 $iso_8859_1_s = $iso_8859_1_s . '&';
138 $iso_8859_1_s = $iso_8859_1_s .
139 decodeBASE64(substr($b64_s, 1));
143 $b64_s = $b64_s . $c;
149 $iso_8859_1_s = $iso_8859_1_s . $c;
153 return $iso_8859_1_s;
156 * Converts string to base64
157 * @param string $s string
158 * @return string base64 encoded string
161 function encodeBASE64($s) {
162 $B64Chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+,';
163 $p = 0; // phase: 1 / 2 / 3 / 1 / 2 / 3...
164 $e = ''; // base64-encoded string
165 //foreach($s as $c) {
166 for ($i = 0; $i < strlen($s); $i++
) {
169 $e = $e . substr($B64Chars, ((ord($c) & 252) >> 2), 1);
173 $e = $e . $B64Chars[($t << 4) +
((ord($c) & 240) >> 4)];
177 $e = $e . $B64Chars[($t << 2) +
((ord($c) & 192) >> 6)];
178 $e = $e . $B64Chars[ord($c) & 63];
186 $e = $e . $B64Chars[$t << 4];
188 $e = $e . $B64Chars[$t << 2];
194 * Converts string from base64
195 * @param string $s base64 encoded string
196 * @return string decoded string
199 function decodeBASE64($s) {
201 'A' => 0, 'B' => 1, 'C' => 2, 'D' => 3, 'E' => 4, 'F' => 5,
202 'G' => 6, 'H' => 7, 'I' => 8, 'J' => 9, 'K' => 10, 'L' => 11,
203 'M' => 12, 'N' => 13, 'O' => 14, 'P' => 15, 'Q' => 16, 'R' => 17,
204 'S' => 18, 'T' => 19, 'U' => 20, 'V' => 21, 'W' => 22, 'X' => 23,
205 'Y' => 24, 'Z' => 25,
206 'a' => 26, 'b' => 27, 'c' => 28, 'd' => 29, 'e' => 30, 'f' => 31,
207 'g' => 32, 'h' => 33, 'i' => 34, 'j' => 35, 'k' => 36, 'l' => 37,
208 'm' => 38, 'n' => 39, 'o' => 40, 'p' => 41, 'q' => 42, 'r' => 43,
209 's' => 44, 't' => 45, 'u' => 46, 'v' => 47, 'w' => 48, 'x' => 49,
210 'y' => 50, 'z' => 51,
211 '0' => 52, '1' => 53, '2' => 54, '3' => 55, '4' => 56, '5' => 57,
212 '6' => 58, '7' => 59, '8' => 60, '9' => 61, '+' => 62, ',' => 63
216 $unicodeNullByteToggle = 0;
217 for ($i = 0, $len = strlen($s); $i < $len; $i++
) {
223 if ($unicodeNullByteToggle) {
224 $d = $d . chr(($t << 2) +
(($B64Values[$c] & 48) >> 4));
225 $unicodeNullByteToggle = 0;
227 $unicodeNullByteToggle = 1;
229 $t = ($B64Values[$c] & 15);
232 if ($unicodeNullByteToggle) {
233 $d = $d . chr(($t << 4) +
(($B64Values[$c] & 60) >> 2));
234 $unicodeNullByteToggle = 0;
236 $unicodeNullByteToggle = 1;
238 $t = ($B64Values[$c] & 3);
241 if ($unicodeNullByteToggle) {
242 $d = $d . chr(($t << 6) +
$B64Values[$c]);
243 $unicodeNullByteToggle = 0;
245 $unicodeNullByteToggle = 1;
247 $t = ($B64Values[$c] & 3);