Updated SVG handling, gracefully fix broken base64-encoded messages, also close XSS...
[squirrelmail.git] / functions / imap_utf7_local.php
CommitLineData
334a77f8 1<?php
4b4abf93 2
334a77f8 3/**
d5ddd62f 4 * functions/imap_utf7_local.php - utf7-imap functions
334a77f8 5 *
abaa3d33 6 * This implements all functions that do imap UTF7 conversions.
4c12c858 7 * Before 1.3.2 functions were stored in imap_utf7_decode_local.php and
8 * imap_utf7_encode_local.php files.
334a77f8 9 *
8ed19238 10 * @copyright 1999-2019 The SquirrelMail Project Team
4b4abf93 11 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
eb19bc67 12 * @version $Id$
d6c32258 13 * @package squirrelmail
eb19bc67 14 * @subpackage imap
4c12c858 15 * @since 1.3.2
334a77f8 16 */
17
d6c32258 18/**
d5ddd62f 19 * Function that uses php mbstring functions to convert from and to utf7-imap charset
4c12c858 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.
d5ddd62f 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 ''
4c12c858 28 * @since 1.4.2
d6c32258 29 */
d5ddd62f 30function 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);
abaa3d33 35 }
91e0dccc 36 return '';
abaa3d33 37}
38
d5ddd62f 39/**
40 * encode folder name to utf7-imap
e50f5ac2 41 *
d5ddd62f 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
4c12c858 45 * @since 1.2.7
d5ddd62f 46 */
334a77f8 47function imap_utf7_encode_local($s) {
e842b215 48 global $languages, $squirrelmail_language;
62f7daa5 49
e842b215 50 if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
91e0dccc 51 function_exists($languages[$squirrelmail_language]['XTRA_CODE'].'_utf7_imap_encode')) {
adc95714 52 return call_user_func($languages[$squirrelmail_language]['XTRA_CODE'] . '_utf7_imap_encode', $s);
e842b215 53 }
8b35c78e 54
91e0dccc 55 if ($s == '') //If empty, don't bother
56 return '';
8b35c78e 57
abaa3d33 58 global $default_charset;
91e0dccc 59 set_my_charset(); //must be called before using $default_charset
02d0931e 60 if ((strtolower($default_charset) != 'iso-8859-1') && ($default_charset != '')) {
91e0dccc 61 $utf7_s = sqimap_mb_convert_encoding($s, 'UTF7-IMAP', $default_charset, $default_charset);
62 if ($utf7_s != '')
63 return $utf7_s;
8b35c78e 64 }
65
62f7daa5 66 // Later code works only for ISO-8859-1
67
91e0dccc 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;
334a77f8 98}
99
d5ddd62f 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
4c12c858 106 * @since 1.2.7
d5ddd62f 107 */
334a77f8 108function imap_utf7_decode_local($s) {
e842b215 109 global $languages, $squirrelmail_language;
62f7daa5 110
e842b215 111 if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
91e0dccc 112 function_exists($languages[$squirrelmail_language]['XTRA_CODE'] . '_utf7_imap_decode')) {
adc95714 113 return call_user_func($languages[$squirrelmail_language]['XTRA_CODE'] . '_utf7_imap_decode', $s);
e842b215 114 }
8b35c78e 115
91e0dccc 116 if ($s == '') //If empty, don't bother
117 return '';
8b35c78e 118
abaa3d33 119 global $default_charset;
91e0dccc 120 set_my_charset(); //must be called before using $default_charset
dacf28e3 121 if ((strtolower($default_charset) != 'iso-8859-1') && ($default_charset != '')) {
91e0dccc 122 $utf7_s = sqimap_mb_convert_encoding($s, $default_charset, 'UTF7-IMAP', $default_charset);
123 if ($utf7_s != '')
124 return $utf7_s;
abaa3d33 125 }
8b35c78e 126
127 // Later code works only for ISO-8859-1
62f7daa5 128
91e0dccc 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;
334a77f8 154}
d5ddd62f 155/**
156 * Converts string to base64
157 * @param string $s string
158 * @return string base64 encoded string
4c12c858 159 * @since 1.2.7
d5ddd62f 160 */
334a77f8 161function encodeBASE64($s) {
91e0dccc 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;
334a77f8 191}
192
d5ddd62f 193/**
194 * Converts string from base64
195 * @param string $s base64 encoded string
196 * @return string decoded string
4c12c858 197 * @since 1.2.7
d5ddd62f 198 */
334a77f8 199function decodeBASE64($s) {
91e0dccc 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;
334a77f8 252}