Add base url to output of configtest.php.
[squirrelmail.git] / functions / imap_utf7_local.php
CommitLineData
334a77f8 1<?php
334a77f8 2/**
d5ddd62f 3 * functions/imap_utf7_local.php - utf7-imap functions
334a77f8 4 *
6c84ba1e 5 * Copyright (c) 1999-2005 The SquirrelMail Project Team
334a77f8 6 * Licensed under the GNU GPL. For full terms see the file COPYING.
7 *
abaa3d33 8 * This implements all functions that do imap UTF7 conversions.
334a77f8 9 *
eb19bc67 10 * @version $Id$
d6c32258 11 * @package squirrelmail
eb19bc67 12 * @subpackage imap
334a77f8 13 */
14
d6c32258 15/**
d5ddd62f 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 ''
d6c32258 22 */
d5ddd62f 23function 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);
abaa3d33 28 }
91e0dccc 29 return '';
abaa3d33 30}
31
d5ddd62f 32/**
33 * encode folder name to utf7-imap
34 *
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
38 */
334a77f8 39function imap_utf7_encode_local($s) {
e842b215 40 global $languages, $squirrelmail_language;
62f7daa5 41
e842b215 42 if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
91e0dccc 43 function_exists($languages[$squirrelmail_language]['XTRA_CODE'].'_utf7_imap_encode')) {
adc95714 44 return call_user_func($languages[$squirrelmail_language]['XTRA_CODE'] . '_utf7_imap_encode', $s);
e842b215 45 }
8b35c78e 46
91e0dccc 47 if ($s == '') //If empty, don't bother
48 return '';
8b35c78e 49
abaa3d33 50 global $default_charset;
91e0dccc 51 set_my_charset(); //must be called before using $default_charset
02d0931e 52 if ((strtolower($default_charset) != 'iso-8859-1') && ($default_charset != '')) {
91e0dccc 53 $utf7_s = sqimap_mb_convert_encoding($s, 'UTF7-IMAP', $default_charset, $default_charset);
54 if ($utf7_s != '')
55 return $utf7_s;
8b35c78e 56 }
57
62f7daa5 58 // Later code works only for ISO-8859-1
59
91e0dccc 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++) {
63 $c = $s[$i];
64 $ord_c = ord($c);
65 if ((($ord_c >= 0x20) and ($ord_c <= 0x25)) or
66 (($ord_c >= 0x27) and ($ord_c <= 0x7e))) {
67 if ($b64_s) {
68 $utf7_s = $utf7_s . '&' . encodeBASE64($b64_s) .'-';
69 $b64_s = '';
70 }
71 $utf7_s = $utf7_s . $c;
72 } elseif ($ord_c == 0x26) {
73 if ($b64_s) {
74 $utf7_s = $utf7_s . '&' . encodeBASE64($b64_s) . '-';
75 $b64_s = '';
76 }
77 $utf7_s = $utf7_s . '&-';
78 } else {
79 $b64_s = $b64_s . chr(0) . $c;
80 }
81 }
82 //
83 // flush buffer
84 //
85 if ($b64_s) {
86 $utf7_s = $utf7_s . '&' . encodeBASE64($b64_s) . '-';
87 $b64_s = '';
88 }
89 return $utf7_s;
334a77f8 90}
91
d5ddd62f 92/**
93 * converts folder name from utf7-imap to charset used by translation
94 *
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
98 */
334a77f8 99function imap_utf7_decode_local($s) {
e842b215 100 global $languages, $squirrelmail_language;
62f7daa5 101
e842b215 102 if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
91e0dccc 103 function_exists($languages[$squirrelmail_language]['XTRA_CODE'] . '_utf7_imap_decode')) {
adc95714 104 return call_user_func($languages[$squirrelmail_language]['XTRA_CODE'] . '_utf7_imap_decode', $s);
e842b215 105 }
8b35c78e 106
91e0dccc 107 if ($s == '') //If empty, don't bother
108 return '';
8b35c78e 109
abaa3d33 110 global $default_charset;
91e0dccc 111 set_my_charset(); //must be called before using $default_charset
dacf28e3 112 if ((strtolower($default_charset) != 'iso-8859-1') && ($default_charset != '')) {
91e0dccc 113 $utf7_s = sqimap_mb_convert_encoding($s, $default_charset, 'UTF7-IMAP', $default_charset);
114 if ($utf7_s != '')
115 return $utf7_s;
abaa3d33 116 }
8b35c78e 117
118 // Later code works only for ISO-8859-1
62f7daa5 119
91e0dccc 120 $b64_s = '';
121 $iso_8859_1_s = '';
122 for ($i = 0, $len = strlen($s); $i < $len; $i++) {
123 $c = $s[$i];
124 if (strlen($b64_s) > 0) {
125 if ($c == '-') {
126 if ($b64_s == '&') {
127 $iso_8859_1_s = $iso_8859_1_s . '&';
128 } else {
129 $iso_8859_1_s = $iso_8859_1_s .
130 decodeBASE64(substr($b64_s, 1));
131 }
132 $b64_s = '';
133 } else {
134 $b64_s = $b64_s . $c;
135 }
136 } else {
137 if ($c == '&') {
138 $b64_s = '&';
139 } else {
140 $iso_8859_1_s = $iso_8859_1_s . $c;
141 }
142 }
143 }
144 return $iso_8859_1_s;
334a77f8 145}
d5ddd62f 146/**
147 * Converts string to base64
148 * @param string $s string
149 * @return string base64 encoded string
150 */
334a77f8 151function encodeBASE64($s) {
91e0dccc 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++) {
157 $c = $s[$i];
158 if ($p == 0) {
159 $e = $e . substr($B64Chars, ((ord($c) & 252) >> 2), 1);
160 $t = (ord($c) & 3);
161 $p = 1;
162 } elseif ($p == 1) {
163 $e = $e . $B64Chars[($t << 4) + ((ord($c) & 240) >> 4)];
164 $t = (ord($c) & 15);
165 $p = 2;
166 } elseif ($p == 2) {
167 $e = $e . $B64Chars[($t << 2) + ((ord($c) & 192) >> 6)];
168 $e = $e . $B64Chars[ord($c) & 63];
169 $p = 0;
170 }
171 }
172 //
173 // flush buffer
174 //
175 if ($p == 1) {
176 $e = $e . $B64Chars[$t << 4];
177 } elseif ($p == 2) {
178 $e = $e . $B64Chars[$t << 2];
179 }
180 return $e;
334a77f8 181}
182
d5ddd62f 183/**
184 * Converts string from base64
185 * @param string $s base64 encoded string
186 * @return string decoded string
187 */
334a77f8 188function decodeBASE64($s) {
91e0dccc 189 $B64Values = array(
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
202 );
203 $p = 0;
204 $d = '';
205 $unicodeNullByteToggle = 0;
206 for ($i = 0, $len = strlen($s); $i < $len; $i++) {
207 $c = $s[$i];
208 if ($p == 0) {
209 $t = $B64Values[$c];
210 $p = 1;
211 } elseif ($p == 1) {
212 if ($unicodeNullByteToggle) {
213 $d = $d . chr(($t << 2) + (($B64Values[$c] & 48) >> 4));
214 $unicodeNullByteToggle = 0;
215 } else {
216 $unicodeNullByteToggle = 1;
217 }
218 $t = ($B64Values[$c] & 15);
219 $p = 2;
220 } elseif ($p == 2) {
221 if ($unicodeNullByteToggle) {
222 $d = $d . chr(($t << 4) + (($B64Values[$c] & 60) >> 2));
223 $unicodeNullByteToggle = 0;
224 } else {
225 $unicodeNullByteToggle = 1;
226 }
227 $t = ($B64Values[$c] & 3);
228 $p = 3;
229 } elseif ($p == 3) {
230 if ($unicodeNullByteToggle) {
231 $d = $d . chr(($t << 6) + $B64Values[$c]);
232 $unicodeNullByteToggle = 0;
233 } else {
234 $unicodeNullByteToggle = 1;
235 }
236 $t = ($B64Values[$c] & 3);
237 $p = 0;
238 }
239 }
240 return $d;
334a77f8 241}
242
62f7daa5 243?>