6 * Copyright (c) 1999-2004 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
9 * This implements all functions that do imap UTF7 conversions.
12 * @package squirrelmail
17 * @param string $to_encoding
18 * @param string $from_encoding
19 * @param string $default_charset
22 function sqimap_mb_convert_encoding($str, $to_encoding, $from_encoding, $default_charset)
24 // Allows mbstring functions only with iso-8859-*, utf-8 and
25 // iso-2022-jp (Japanese)
26 // koi8-r and gb2312 can be added only in php 4.3+
27 if ( stristr($default_charset, 'iso-8859-') ||
28 stristr($default_charset, 'utf-8') ||
29 stristr($default_charset, 'iso-2022-jp') ) {
30 if (function_exists('mb_convert_encoding')) {
31 return mb_convert_encoding($str, $to_encoding, $from_encoding);
37 function imap_utf7_encode_local($s) {
38 global $languages, $squirrelmail_language;
40 if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
41 function_exists($languages[$squirrelmail_language]['XTRA_CODE'])) {
42 return $languages[$squirrelmail_language]['XTRA_CODE']('utf7-imap_encode', $s);
45 if ($s == '') //If empty, don't bother
48 global $default_charset;
49 set_my_charset(); //must be called before using $default_charset
50 if ((strtolower($default_charset) != 'iso-8859-1') && ($default_charset != '')) {
51 $utf7_s = sqimap_mb_convert_encoding($s, 'UTF7-IMAP', $default_charset, $default_charset);
56 // Later code works only for ISO-8859-1
58 $b64_s = ''; // buffer for substring to be base64-encoded
59 $utf7_s = ''; // imap-utf7-encoded string
60 for ($i = 0; $i < strlen($s); $i++
) {
63 if ((($ord_c >= 0x20) and ($ord_c <= 0x25)) or
64 (($ord_c >= 0x27) and ($ord_c <= 0x7e))) {
66 $utf7_s = $utf7_s . '&' . encodeBASE64($b64_s) .'-';
69 $utf7_s = $utf7_s . $c;
70 } elseif ($ord_c == 0x26) {
72 $utf7_s = $utf7_s . '&' . encodeBASE64($b64_s) . '-';
75 $utf7_s = $utf7_s . '&-';
77 $b64_s = $b64_s . chr(0) . $c;
84 $utf7_s = $utf7_s . '&' . encodeBASE64($b64_s) . '-';
90 function imap_utf7_decode_local($s) {
91 global $languages, $squirrelmail_language;
93 if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
94 function_exists($languages[$squirrelmail_language]['XTRA_CODE'])) {
95 return $languages[$squirrelmail_language]['XTRA_CODE']('utf7-imap_decode', $s);
98 if ($s == '') //If empty, don't bother
101 global $default_charset;
102 set_my_charset(); //must be called before using $default_charset
103 if ((strtolower($default_charset) != 'iso-8859-1') && ($default_charset != '')) {
104 $utf7_s = sqimap_mb_convert_encoding($s, $default_charset, 'UTF7-IMAP', $default_charset);
109 // Later code works only for ISO-8859-1
113 for ($i = 0, $len = strlen($s); $i < $len; $i++
) {
115 if (strlen($b64_s) > 0) {
118 $iso_8859_1_s = $iso_8859_1_s . '&';
120 $iso_8859_1_s = $iso_8859_1_s .
121 decodeBASE64(substr($b64_s, 1));
125 $b64_s = $b64_s . $c;
131 $iso_8859_1_s = $iso_8859_1_s . $c;
135 return $iso_8859_1_s;
138 function encodeBASE64($s) {
139 $B64Chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+,';
140 $p = 0; // phase: 1 / 2 / 3 / 1 / 2 / 3...
141 $e = ''; // base64-encoded string
142 //foreach($s as $c) {
143 for ($i = 0; $i < strlen($s); $i++
) {
146 $e = $e . substr($B64Chars, ((ord($c) & 252) >> 2), 1);
150 $e = $e . $B64Chars[($t << 4) +
((ord($c) & 240) >> 4)];
154 $e = $e . $B64Chars[($t << 2) +
((ord($c) & 192) >> 6)];
155 $e = $e . $B64Chars[ord($c) & 63];
163 $e = $e . $B64Chars[$t << 4];
165 $e = $e . $B64Chars[$t << 2];
170 function decodeBASE64($s) {
172 'A' => 0, 'B' => 1, 'C' => 2, 'D' => 3, 'E' => 4, 'F' => 5,
173 'G' => 6, 'H' => 7, 'I' => 8, 'J' => 9, 'K' => 10, 'L' => 11,
174 'M' => 12, 'N' => 13, 'O' => 14, 'P' => 15, 'Q' => 16, 'R' => 17,
175 'S' => 18, 'T' => 19, 'U' => 20, 'V' => 21, 'W' => 22, 'X' => 23,
176 'Y' => 24, 'Z' => 25,
177 'a' => 26, 'b' => 27, 'c' => 28, 'd' => 29, 'e' => 30, 'f' => 31,
178 'g' => 32, 'h' => 33, 'i' => 34, 'j' => 35, 'k' => 36, 'l' => 37,
179 'm' => 38, 'n' => 39, 'o' => 40, 'p' => 41, 'q' => 42, 'r' => 43,
180 's' => 44, 't' => 45, 'u' => 46, 'v' => 47, 'w' => 48, 'x' => 49,
181 'y' => 50, 'z' => 51,
182 '0' => 52, '1' => 53, '2' => 54, '3' => 55, '4' => 56, '5' => 57,
183 '6' => 58, '7' => 59, '8' => 60, '9' => 61, '+' => 62, ',' => 63
187 $unicodeNullByteToggle = 0;
188 for ($i = 0, $len = strlen($s); $i < $len; $i++
) {
194 if ($unicodeNullByteToggle) {
195 $d = $d . chr(($t << 2) +
(($B64Values[$c] & 48) >> 4));
196 $unicodeNullByteToggle = 0;
198 $unicodeNullByteToggle = 1;
200 $t = ($B64Values[$c] & 15);
203 if ($unicodeNullByteToggle) {
204 $d = $d . chr(($t << 4) +
(($B64Values[$c] & 60) >> 2));
205 $unicodeNullByteToggle = 0;
207 $unicodeNullByteToggle = 1;
209 $t = ($B64Values[$c] & 3);
212 if ($unicodeNullByteToggle) {
213 $d = $d . chr(($t << 6) +
$B64Values[$c]);
214 $unicodeNullByteToggle = 0;
216 $unicodeNullByteToggle = 1;
218 $t = ($B64Values[$c] & 3);