6bb597cd69b1080d7711f9cfefda086aa6d96d53
[squirrelmail.git] / functions / imap_utf7_local.php
1 <?php
2
3 /**
4 * imap_utf7_local.php
5 *
6 * Copyright (c) 1999-2004 The SquirrelMail Project Team
7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * This implements all functions that do imap UTF7 conversions.
10 *
11 * $Id$
12 * @package squirrelmail
13 */
14
15 /**
16 * @param string $str
17 * @param string $to_encoding
18 * @param string $from_encoding
19 * @param string $default_charset
20 * @return string
21 */
22 function sqimap_mb_convert_encoding($str, $to_encoding, $from_encoding, $default_charset)
23 {
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);
32 }
33 }
34 return '';
35 }
36
37 function imap_utf7_encode_local($s) {
38 global $languages, $squirrelmail_language;
39
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);
43 }
44
45 if ($s == '') //If empty, don't bother
46 return '';
47
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);
52 if ($utf7_s != '')
53 return $utf7_s;
54 }
55
56 // Later code works only for ISO-8859-1
57
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++) {
61 $c = $s[$i];
62 $ord_c = ord($c);
63 if ((($ord_c >= 0x20) and ($ord_c <= 0x25)) or
64 (($ord_c >= 0x27) and ($ord_c <= 0x7e))) {
65 if ($b64_s) {
66 $utf7_s = $utf7_s . '&' . encodeBASE64($b64_s) .'-';
67 $b64_s = '';
68 }
69 $utf7_s = $utf7_s . $c;
70 } elseif ($ord_c == 0x26) {
71 if ($b64_s) {
72 $utf7_s = $utf7_s . '&' . encodeBASE64($b64_s) . '-';
73 $b64_s = '';
74 }
75 $utf7_s = $utf7_s . '&-';
76 } else {
77 $b64_s = $b64_s . chr(0) . $c;
78 }
79 }
80 //
81 // flush buffer
82 //
83 if ($b64_s) {
84 $utf7_s = $utf7_s . '&' . encodeBASE64($b64_s) . '-';
85 $b64_s = '';
86 }
87 return $utf7_s;
88 }
89
90 function imap_utf7_decode_local($s) {
91 global $languages, $squirrelmail_language;
92
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);
96 }
97
98 if ($s == '') //If empty, don't bother
99 return '';
100
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);
105 if ($utf7_s != '')
106 return $utf7_s;
107 }
108
109 // Later code works only for ISO-8859-1
110
111 $b64_s = '';
112 $iso_8859_1_s = '';
113 for ($i = 0, $len = strlen($s); $i < $len; $i++) {
114 $c = $s[$i];
115 if (strlen($b64_s) > 0) {
116 if ($c == '-') {
117 if ($b64_s == '&') {
118 $iso_8859_1_s = $iso_8859_1_s . '&';
119 } else {
120 $iso_8859_1_s = $iso_8859_1_s .
121 decodeBASE64(substr($b64_s, 1));
122 }
123 $b64_s = '';
124 } else {
125 $b64_s = $b64_s . $c;
126 }
127 } else {
128 if ($c == '&') {
129 $b64_s = '&';
130 } else {
131 $iso_8859_1_s = $iso_8859_1_s . $c;
132 }
133 }
134 }
135 return $iso_8859_1_s;
136 }
137
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++) {
144 $c = $s[$i];
145 if ($p == 0) {
146 $e = $e . substr($B64Chars, ((ord($c) & 252) >> 2), 1);
147 $t = (ord($c) & 3);
148 $p = 1;
149 } elseif ($p == 1) {
150 $e = $e . $B64Chars[($t << 4) + ((ord($c) & 240) >> 4)];
151 $t = (ord($c) & 15);
152 $p = 2;
153 } elseif ($p == 2) {
154 $e = $e . $B64Chars[($t << 2) + ((ord($c) & 192) >> 6)];
155 $e = $e . $B64Chars[ord($c) & 63];
156 $p = 0;
157 }
158 }
159 //
160 // flush buffer
161 //
162 if ($p == 1) {
163 $e = $e . $B64Chars[$t << 4];
164 } elseif ($p == 2) {
165 $e = $e . $B64Chars[$t << 2];
166 }
167 return $e;
168 }
169
170 function decodeBASE64($s) {
171 $B64Values = array(
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
184 );
185 $p = 0;
186 $d = '';
187 $unicodeNullByteToggle = 0;
188 for ($i = 0, $len = strlen($s); $i < $len; $i++) {
189 $c = $s[$i];
190 if ($p == 0) {
191 $t = $B64Values[$c];
192 $p = 1;
193 } elseif ($p == 1) {
194 if ($unicodeNullByteToggle) {
195 $d = $d . chr(($t << 2) + (($B64Values[$c] & 48) >> 4));
196 $unicodeNullByteToggle = 0;
197 } else {
198 $unicodeNullByteToggle = 1;
199 }
200 $t = ($B64Values[$c] & 15);
201 $p = 2;
202 } elseif ($p == 2) {
203 if ($unicodeNullByteToggle) {
204 $d = $d . chr(($t << 4) + (($B64Values[$c] & 60) >> 2));
205 $unicodeNullByteToggle = 0;
206 } else {
207 $unicodeNullByteToggle = 1;
208 }
209 $t = ($B64Values[$c] & 3);
210 $p = 3;
211 } elseif ($p == 3) {
212 if ($unicodeNullByteToggle) {
213 $d = $d . chr(($t << 6) + $B64Values[$c]);
214 $unicodeNullByteToggle = 0;
215 } else {
216 $unicodeNullByteToggle = 1;
217 }
218 $t = ($B64Values[$c] & 3);
219 $p = 0;
220 }
221 }
222 return $d;
223 }
224
225 ?>