6 * Copyright (c) 1999-2003 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.
14 function sqimap_mb_convert_encoding($str, $to_encoding, $from_encoding, $default_charset)
16 // Allows mbstring functions only with iso-8859-*, utf-8 and
17 // iso-2022-jp (Japanese)
18 // koi8-r and gb2312 can be added only in php 4.3+
19 if ( stristr('iso-8859-',$default_charset) ||
20 stristr('utf-8',$default_charset) ||
21 stristr('iso-2022-jp',$default_charset) ) {
22 if (function_exists('mb_convert_encoding')) {
23 return mb_convert_encoding($str, $to_encoding, $from_encoding);
29 function imap_utf7_encode_local($s) {
30 global $languages, $squirrelmail_language;
32 if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
33 function_exists($languages[$squirrelmail_language]['XTRA_CODE'])) {
34 return $languages[$squirrelmail_language]['XTRA_CODE']('utf7-imap_encode', $s);
37 if ($s == '') //If empty, don't bother
40 global $default_charset;
42 if (strtolower($default_charset) != 'iso-8859-1') {
43 $utf7_s = sqimap_mb_convert_encoding($s, 'UTF7-IMAP', $default_charset, $default_charset);
48 // Later code works only for ISO-8859-1
50 $b64_s = ''; // buffer for substring to be base64-encoded
51 $utf7_s = ''; // imap-utf7-encoded string
52 for ($i = 0; $i < strlen($s); $i++
) {
55 if ((($ord_c >= 0x20) and ($ord_c <= 0x25)) or
56 (($ord_c >= 0x27) and ($ord_c <= 0x7e))) {
58 $utf7_s = $utf7_s . '&' . encodeBASE64($b64_s) .'-';
61 $utf7_s = $utf7_s . $c;
62 } elseif ($ord_c == 0x26) {
64 $utf7_s = $utf7_s . '&' . encodeBASE64($b64_s) . '-';
67 $utf7_s = $utf7_s . '&-';
69 $b64_s = $b64_s . chr(0) . $c;
76 $utf7_s = $utf7_s . '&' . encodeBASE64($b64_s) . '-';
82 function imap_utf7_decode_local($s) {
83 global $languages, $squirrelmail_language;
85 if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
86 function_exists($languages[$squirrelmail_language]['XTRA_CODE'])) {
87 return $languages[$squirrelmail_language]['XTRA_CODE']('utf7-imap_decode', $s);
90 if ($s == '') //If empty, don't bother
93 global $default_charset;
95 if (strtolower($default_charset) != 'iso-8859-1') {
96 $utf7_s = sqimap_mb_convert_encoding($s, $default_charset, 'UTF7-IMAP', $default_charset);
101 // Later code works only for ISO-8859-1
105 for ($i = 0, $len = strlen($s); $i < $len; $i++
) {
107 if (strlen($b64_s) > 0) {
110 $iso_8859_1_s = $iso_8859_1_s . '&';
112 $iso_8859_1_s = $iso_8859_1_s .
113 decodeBASE64(substr($b64_s, 1));
117 $b64_s = $b64_s . $c;
123 $iso_8859_1_s = $iso_8859_1_s . $c;
127 return $iso_8859_1_s;
130 function encodeBASE64($s) {
131 $B64Chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+,';
132 $p = 0; // phase: 1 / 2 / 3 / 1 / 2 / 3...
133 $e = ''; // base64-encoded string
134 //foreach($s as $c) {
135 for ($i = 0; $i < strlen($s); $i++
) {
138 $e = $e . substr($B64Chars, ((ord($c) & 252) >> 2), 1);
142 $e = $e . $B64Chars[($t << 4) +
((ord($c) & 240) >> 4)];
146 $e = $e . $B64Chars[($t << 2) +
((ord($c) & 192) >> 6)];
147 $e = $e . $B64Chars[ord($c) & 63];
155 $e = $e . $B64Chars[$t << 4];
157 $e = $e . $B64Chars[$t << 2];
162 function decodeBASE64($s) {
164 'A' => 0, 'B' => 1, 'C' => 2, 'D' => 3, 'E' => 4, 'F' => 5,
165 'G' => 6, 'H' => 7, 'I' => 8, 'J' => 9, 'K' => 10, 'L' => 11,
166 'M' => 12, 'N' => 13, 'O' => 14, 'P' => 15, 'Q' => 16, 'R' => 17,
167 'S' => 18, 'T' => 19, 'U' => 20, 'V' => 21, 'W' => 22, 'X' => 23,
168 'Y' => 24, 'Z' => 25,
169 'a' => 26, 'b' => 27, 'c' => 28, 'd' => 29, 'e' => 30, 'f' => 31,
170 'g' => 32, 'h' => 33, 'i' => 34, 'j' => 35, 'k' => 36, 'l' => 37,
171 'm' => 38, 'n' => 39, 'o' => 40, 'p' => 41, 'q' => 42, 'r' => 43,
172 's' => 44, 't' => 45, 'u' => 46, 'v' => 47, 'w' => 48, 'x' => 49,
173 'y' => 50, 'z' => 51,
174 '0' => 52, '1' => 53, '2' => 54, '3' => 55, '4' => 56, '5' => 57,
175 '6' => 58, '7' => 59, '8' => 60, '9' => 61, '+' => 62, ',' => 63
179 $unicodeNullByteToggle = 0;
180 for ($i = 0, $len = strlen($s); $i < $len; $i++
) {
186 if ($unicodeNullByteToggle) {
187 $d = $d . chr(($t << 2) +
(($B64Values[$c] & 48) >> 4));
188 $unicodeNullByteToggle = 0;
190 $unicodeNullByteToggle = 1;
192 $t = ($B64Values[$c] & 15);
195 if ($unicodeNullByteToggle) {
196 $d = $d . chr(($t << 4) +
(($B64Values[$c] & 60) >> 2));
197 $unicodeNullByteToggle = 0;
199 $unicodeNullByteToggle = 1;
201 $t = ($B64Values[$c] & 3);
204 if ($unicodeNullByteToggle) {
205 $d = $d . chr(($t << 6) +
$B64Values[$c]);
206 $unicodeNullByteToggle = 0;
208 $unicodeNullByteToggle = 1;
210 $t = ($B64Values[$c] & 3);