3 * functions/imap_utf7_local.php - utf7-imap functions
5 * Copyright (c) 1999-2004 The SquirrelMail Project Team
6 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 * This implements all functions that do imap UTF7 conversions.
11 * @package squirrelmail
16 * Function that uses php mbstring functions to convert from and to utf7-imap charset
17 * @param string $str folder name
18 * @param string $to_encoding name of resulting charset
19 * @param string $from_encoding name of original charset
20 * @param string $default_charset default charset used by translation.
21 * @return string encoded folder name or ''
23 function sqimap_mb_convert_encoding($str, $to_encoding, $from_encoding, $default_charset) {
24 $supported_encodings=sq_mb_list_encodings();
25 if ( in_array(strtolower($default_charset),$supported_encodings) &&
26 function_exists('mb_convert_encoding')) {
27 return mb_convert_encoding($str, $to_encoding, $from_encoding);
33 * encode folder name to utf7-imap
35 * If mbstring functions do not support charset used by translation, falls back to iso-8859-1
36 * @param string $s folder name
37 * @return string utf7-imap encoded folder name
39 function imap_utf7_encode_local($s) {
40 global $languages, $squirrelmail_language;
42 if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
43 function_exists($languages[$squirrelmail_language]['XTRA_CODE'].'_utf7_imap_encode')) {
44 return call_user_func($languages[$squirrelmail_language]['XTRA_CODE'] . '_utf7_imap_encode', $s);
47 if ($s == '') //If empty, don't bother
50 global $default_charset;
51 set_my_charset(); //must be called before using $default_charset
52 if ((strtolower($default_charset) != 'iso-8859-1') && ($default_charset != '')) {
53 $utf7_s = sqimap_mb_convert_encoding($s, 'UTF7-IMAP', $default_charset, $default_charset);
58 // Later code works only for ISO-8859-1
60 $b64_s = ''; // buffer for substring to be base64-encoded
61 $utf7_s = ''; // imap-utf7-encoded string
62 for ($i = 0; $i < strlen($s); $i++
) {
65 if ((($ord_c >= 0x20) and ($ord_c <= 0x25)) or
66 (($ord_c >= 0x27) and ($ord_c <= 0x7e))) {
68 $utf7_s = $utf7_s . '&' . encodeBASE64($b64_s) .'-';
71 $utf7_s = $utf7_s . $c;
72 } elseif ($ord_c == 0x26) {
74 $utf7_s = $utf7_s . '&' . encodeBASE64($b64_s) . '-';
77 $utf7_s = $utf7_s . '&-';
79 $b64_s = $b64_s . chr(0) . $c;
86 $utf7_s = $utf7_s . '&' . encodeBASE64($b64_s) . '-';
93 * converts folder name from utf7-imap to charset used by translation
95 * If mbstring functions do not support charset used by translation, falls back to iso-8859-1
96 * @param string $s folder name in utf7-imap
97 * @return string folder name in charset used by translation
99 function imap_utf7_decode_local($s) {
100 global $languages, $squirrelmail_language;
102 if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
103 function_exists($languages[$squirrelmail_language]['XTRA_CODE'] . '_utf7_imap_decode')) {
104 return call_user_func($languages[$squirrelmail_language]['XTRA_CODE'] . '_utf7_imap_decode', $s);
107 if ($s == '') //If empty, don't bother
110 global $default_charset;
111 set_my_charset(); //must be called before using $default_charset
112 if ((strtolower($default_charset) != 'iso-8859-1') && ($default_charset != '')) {
113 $utf7_s = sqimap_mb_convert_encoding($s, $default_charset, 'UTF7-IMAP', $default_charset);
118 // Later code works only for ISO-8859-1
122 for ($i = 0, $len = strlen($s); $i < $len; $i++
) {
124 if (strlen($b64_s) > 0) {
127 $iso_8859_1_s = $iso_8859_1_s . '&';
129 $iso_8859_1_s = $iso_8859_1_s .
130 decodeBASE64(substr($b64_s, 1));
134 $b64_s = $b64_s . $c;
140 $iso_8859_1_s = $iso_8859_1_s . $c;
144 return $iso_8859_1_s;
147 * Converts string to base64
148 * @param string $s string
149 * @return string base64 encoded string
151 function encodeBASE64($s) {
152 $B64Chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+,';
153 $p = 0; // phase: 1 / 2 / 3 / 1 / 2 / 3...
154 $e = ''; // base64-encoded string
155 //foreach($s as $c) {
156 for ($i = 0; $i < strlen($s); $i++
) {
159 $e = $e . substr($B64Chars, ((ord($c) & 252) >> 2), 1);
163 $e = $e . $B64Chars[($t << 4) +
((ord($c) & 240) >> 4)];
167 $e = $e . $B64Chars[($t << 2) +
((ord($c) & 192) >> 6)];
168 $e = $e . $B64Chars[ord($c) & 63];
176 $e = $e . $B64Chars[$t << 4];
178 $e = $e . $B64Chars[$t << 2];
184 * Converts string from base64
185 * @param string $s base64 encoded string
186 * @return string decoded string
188 function decodeBASE64($s) {
190 'A' => 0, 'B' => 1, 'C' => 2, 'D' => 3, 'E' => 4, 'F' => 5,
191 'G' => 6, 'H' => 7, 'I' => 8, 'J' => 9, 'K' => 10, 'L' => 11,
192 'M' => 12, 'N' => 13, 'O' => 14, 'P' => 15, 'Q' => 16, 'R' => 17,
193 'S' => 18, 'T' => 19, 'U' => 20, 'V' => 21, 'W' => 22, 'X' => 23,
194 'Y' => 24, 'Z' => 25,
195 'a' => 26, 'b' => 27, 'c' => 28, 'd' => 29, 'e' => 30, 'f' => 31,
196 'g' => 32, 'h' => 33, 'i' => 34, 'j' => 35, 'k' => 36, 'l' => 37,
197 'm' => 38, 'n' => 39, 'o' => 40, 'p' => 41, 'q' => 42, 'r' => 43,
198 's' => 44, 't' => 45, 'u' => 46, 'v' => 47, 'w' => 48, 'x' => 49,
199 'y' => 50, 'z' => 51,
200 '0' => 52, '1' => 53, '2' => 54, '3' => 55, '4' => 56, '5' => 57,
201 '6' => 58, '7' => 59, '8' => 60, '9' => 61, '+' => 62, ',' => 63
205 $unicodeNullByteToggle = 0;
206 for ($i = 0, $len = strlen($s); $i < $len; $i++
) {
212 if ($unicodeNullByteToggle) {
213 $d = $d . chr(($t << 2) +
(($B64Values[$c] & 48) >> 4));
214 $unicodeNullByteToggle = 0;
216 $unicodeNullByteToggle = 1;
218 $t = ($B64Values[$c] & 15);
221 if ($unicodeNullByteToggle) {
222 $d = $d . chr(($t << 4) +
(($B64Values[$c] & 60) >> 2));
223 $unicodeNullByteToggle = 0;
225 $unicodeNullByteToggle = 1;
227 $t = ($B64Values[$c] & 3);
230 if ($unicodeNullByteToggle) {
231 $d = $d . chr(($t << 6) +
$B64Values[$c]);
232 $unicodeNullByteToggle = 0;
234 $unicodeNullByteToggle = 1;
236 $t = ($B64Values[$c] & 3);