0eaca2cb65b9cb9b19a1a9019f381c1099322a3c
[squirrelmail.git] / functions / imap_utf7_local.php
1 <?php
2
3 /**
4 * functions/imap_utf7_local.php - utf7-imap functions
5 *
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.
9 *
10 * @copyright &copy; 1999-2009 The SquirrelMail Project Team
11 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
12 * @version $Id$
13 * @package squirrelmail
14 * @subpackage imap
15 * @since 1.3.2
16 */
17
18 /**
19 * Function that uses php mbstring functions to convert from and to utf7-imap charset
20 *
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 ''
28 * @since 1.4.2
29 */
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);
35 }
36 return '';
37 }
38
39 /**
40 * encode folder name to utf7-imap
41 *
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
45 * @since 1.2.7
46 */
47 function imap_utf7_encode_local($s) {
48 global $languages, $squirrelmail_language;
49
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);
53 }
54
55 if ($s == '') //If empty, don't bother
56 return '';
57
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);
62 if ($utf7_s != '')
63 return $utf7_s;
64 }
65
66 // Later code works only for ISO-8859-1
67
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++) {
71 $c = $s[$i];
72 $ord_c = ord($c);
73 if ((($ord_c >= 0x20) and ($ord_c <= 0x25)) or
74 (($ord_c >= 0x27) and ($ord_c <= 0x7e))) {
75 if ($b64_s) {
76 $utf7_s = $utf7_s . '&' . encodeBASE64($b64_s) .'-';
77 $b64_s = '';
78 }
79 $utf7_s = $utf7_s . $c;
80 } elseif ($ord_c == 0x26) {
81 if ($b64_s) {
82 $utf7_s = $utf7_s . '&' . encodeBASE64($b64_s) . '-';
83 $b64_s = '';
84 }
85 $utf7_s = $utf7_s . '&-';
86 } else {
87 $b64_s = $b64_s . chr(0) . $c;
88 }
89 }
90 //
91 // flush buffer
92 //
93 if ($b64_s) {
94 $utf7_s = $utf7_s . '&' . encodeBASE64($b64_s) . '-';
95 $b64_s = '';
96 }
97 return $utf7_s;
98 }
99
100 /**
101 * converts folder name from utf7-imap to charset used by translation
102 *
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
106 * @since 1.2.7
107 */
108 function imap_utf7_decode_local($s) {
109 global $languages, $squirrelmail_language;
110
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);
114 }
115
116 if ($s == '') //If empty, don't bother
117 return '';
118
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);
123 if ($utf7_s != '')
124 return $utf7_s;
125 }
126
127 // Later code works only for ISO-8859-1
128
129 $b64_s = '';
130 $iso_8859_1_s = '';
131 for ($i = 0, $len = strlen($s); $i < $len; $i++) {
132 $c = $s[$i];
133 if (strlen($b64_s) > 0) {
134 if ($c == '-') {
135 if ($b64_s == '&') {
136 $iso_8859_1_s = $iso_8859_1_s . '&';
137 } else {
138 $iso_8859_1_s = $iso_8859_1_s .
139 decodeBASE64(substr($b64_s, 1));
140 }
141 $b64_s = '';
142 } else {
143 $b64_s = $b64_s . $c;
144 }
145 } else {
146 if ($c == '&') {
147 $b64_s = '&';
148 } else {
149 $iso_8859_1_s = $iso_8859_1_s . $c;
150 }
151 }
152 }
153 return $iso_8859_1_s;
154 }
155 /**
156 * Converts string to base64
157 * @param string $s string
158 * @return string base64 encoded string
159 * @since 1.2.7
160 */
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++) {
167 $c = $s[$i];
168 if ($p == 0) {
169 $e = $e . substr($B64Chars, ((ord($c) & 252) >> 2), 1);
170 $t = (ord($c) & 3);
171 $p = 1;
172 } elseif ($p == 1) {
173 $e = $e . $B64Chars[($t << 4) + ((ord($c) & 240) >> 4)];
174 $t = (ord($c) & 15);
175 $p = 2;
176 } elseif ($p == 2) {
177 $e = $e . $B64Chars[($t << 2) + ((ord($c) & 192) >> 6)];
178 $e = $e . $B64Chars[ord($c) & 63];
179 $p = 0;
180 }
181 }
182 //
183 // flush buffer
184 //
185 if ($p == 1) {
186 $e = $e . $B64Chars[$t << 4];
187 } elseif ($p == 2) {
188 $e = $e . $B64Chars[$t << 2];
189 }
190 return $e;
191 }
192
193 /**
194 * Converts string from base64
195 * @param string $s base64 encoded string
196 * @return string decoded string
197 * @since 1.2.7
198 */
199 function decodeBASE64($s) {
200 $B64Values = array(
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
213 );
214 $p = 0;
215 $d = '';
216 $unicodeNullByteToggle = 0;
217 for ($i = 0, $len = strlen($s); $i < $len; $i++) {
218 $c = $s[$i];
219 if ($p == 0) {
220 $t = $B64Values[$c];
221 $p = 1;
222 } elseif ($p == 1) {
223 if ($unicodeNullByteToggle) {
224 $d = $d . chr(($t << 2) + (($B64Values[$c] & 48) >> 4));
225 $unicodeNullByteToggle = 0;
226 } else {
227 $unicodeNullByteToggle = 1;
228 }
229 $t = ($B64Values[$c] & 15);
230 $p = 2;
231 } elseif ($p == 2) {
232 if ($unicodeNullByteToggle) {
233 $d = $d . chr(($t << 4) + (($B64Values[$c] & 60) >> 2));
234 $unicodeNullByteToggle = 0;
235 } else {
236 $unicodeNullByteToggle = 1;
237 }
238 $t = ($B64Values[$c] & 3);
239 $p = 3;
240 } elseif ($p == 3) {
241 if ($unicodeNullByteToggle) {
242 $d = $d . chr(($t << 6) + $B64Values[$c]);
243 $unicodeNullByteToggle = 0;
244 } else {
245 $unicodeNullByteToggle = 1;
246 }
247 $t = ($B64Values[$c] & 3);
248 $p = 0;
249 }
250 }
251 return $d;
252 }