334a77f8 |
1 | <?php |
2 | |
3 | /** |
4 | * imap_general.php |
5 | * |
76911253 |
6 | * Copyright (c) 1999-2003 The SquirrelMail Project Team |
334a77f8 |
7 | * Licensed under the GNU GPL. For full terms see the file COPYING. |
8 | * |
9 | * This implements all functions that do general imap functions. |
10 | * |
11 | * $Id $ |
12 | */ |
13 | |
14 | function imap_utf7_encode_local($s) { |
e842b215 |
15 | global $languages, $squirrelmail_language; |
16 | |
17 | if (isset($languages[$squirrelmail_language]['XTRA_CODE']) && |
18 | function_exists($languages[$squirrelmail_language]['XTRA_CODE'])) { |
19 | return $languages[$squirrelmail_language]['XTRA_CODE']('utf7-imap_encode', $s); |
20 | } |
8b35c78e |
21 | |
22 | global $default_charset; |
23 | set_my_charset(); |
24 | |
25 | if ( ! $default_charset == "iso-8859-1" ) { |
26 | // Allows mbstring functions only with iso-8859-*, utf-8 and |
27 | // iso-2022-jp (Japanese) |
28 | // koi8-r and gb2312 can be added only in php 4.3+ |
29 | if ( preg_match("/iso-8859-/i",$default_charset) || |
30 | preg_match("/utf-8/i",$default_charset) || |
31 | preg_match("/iso-2022-jp/i",$default_charset) ) { |
32 | if(function_exists("mb_convert_encoding")) { |
33 | return mb_convert_encoding($s, "UTF7-IMAP", $default_charset); |
34 | } |
35 | } |
36 | } |
37 | |
38 | // Later code works only for ISO-8859-1 |
e842b215 |
39 | |
334a77f8 |
40 | $b64_s = ''; // buffer for substring to be base64-encoded |
41 | $utf7_s = ''; // imap-utf7-encoded string |
42 | for ($i = 0; $i < strlen($s); $i++) { |
43 | $c = $s[$i]; |
44 | $ord_c = ord($c); |
45 | if ((($ord_c >= 0x20) and ($ord_c <= 0x25)) or |
46 | (($ord_c >= 0x27) and ($ord_c <= 0x7e))) { |
47 | if ($b64_s) { |
48 | $utf7_s = $utf7_s . '&' . encodeBASE64($b64_s) .'-'; |
49 | $b64_s = ''; |
50 | } |
51 | $utf7_s = $utf7_s . $c; |
52 | } elseif ($ord_c == 0x26) { |
53 | if ($b64_s) { |
54 | $utf7_s = $utf7_s . '&' . encodeBASE64($b64_s) . '-'; |
55 | $b64_s = ''; |
56 | } |
57 | $utf7_s = $utf7_s . '&-'; |
58 | } else { |
59 | $b64_s = $b64_s . chr(0) . $c; |
60 | } |
61 | } |
62 | // |
63 | // flush buffer |
64 | // |
65 | if ($b64_s) { |
66 | $utf7_s = $utf7_s . '&' . encodeBASE64($b64_s) . '-'; |
67 | $b64_s = ''; |
68 | } |
69 | return $utf7_s; |
70 | } |
71 | |
72 | function imap_utf7_decode_local($s) { |
e842b215 |
73 | global $languages, $squirrelmail_language; |
74 | |
75 | if (isset($languages[$squirrelmail_language]['XTRA_CODE']) && |
76 | function_exists($languages[$squirrelmail_language]['XTRA_CODE'])) { |
77 | return $languages[$squirrelmail_language]['XTRA_CODE']('utf7-imap_decode', $s); |
78 | } |
8b35c78e |
79 | |
80 | global $default_charset; |
81 | set_my_charset(); |
82 | |
83 | if ( ! $default_charset == "iso-8859-1" ) { |
84 | // Allows mbstring functions only with iso-8859-*, utf-8 and |
85 | // iso-2022-jp (Japanese) |
86 | // koi8-r and gb2312 can be added only in php 4.3+ |
87 | if ( preg_match("/iso-8859-/i",$default_charset) || |
88 | preg_match("/utf-8/i",$default_charset) || |
89 | preg_match("/iso-2022-jp/i",$default_charset) ) { |
90 | if(function_exists("mb_convert_encoding")) { |
91 | return mb_convert_encoding($s, $default_charset, "UTF7-IMAP"); |
92 | } |
93 | } |
94 | } |
95 | |
96 | // Later code works only for ISO-8859-1 |
e842b215 |
97 | |
334a77f8 |
98 | $b64_s = ''; |
99 | $iso_8859_1_s = ''; |
100 | for ($i = 0, $len = strlen($s); $i < $len; $i++) { |
101 | $c = $s[$i]; |
102 | if (strlen($b64_s) > 0) { |
103 | if ($c == '-') { |
104 | if ($b64_s == '&') { |
105 | $iso_8859_1_s = $iso_8859_1_s . '&'; |
106 | } else { |
107 | $iso_8859_1_s = $iso_8859_1_s . |
108 | decodeBASE64(substr($b64_s, 1)); |
109 | } |
110 | $b64_s = ''; |
111 | } else { |
112 | $b64_s = $b64_s . $c; |
113 | } |
114 | } else { |
115 | if ($c == '&') { |
116 | $b64_s = '&'; |
117 | } else { |
118 | $iso_8859_1_s = $iso_8859_1_s . $c; |
119 | } |
120 | } |
121 | } |
122 | return $iso_8859_1_s; |
123 | } |
124 | |
125 | function encodeBASE64($s) { |
126 | $B64Chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+,'; |
127 | $p = 0; // phase: 1 / 2 / 3 / 1 / 2 / 3... |
128 | $e = ''; // base64-encoded string |
129 | //foreach($s as $c) { |
130 | for ($i = 0; $i < strlen($s); $i++) { |
131 | $c = $s[$i]; |
132 | if ($p == 0) { |
133 | $e = $e . substr($B64Chars, ((ord($c) & 252) >> 2), 1); |
134 | $t = (ord($c) & 3); |
135 | $p = 1; |
136 | } elseif ($p == 1) { |
137 | $e = $e . $B64Chars[($t << 4) + ((ord($c) & 240) >> 4)]; |
138 | $t = (ord($c) & 15); |
139 | $p = 2; |
140 | } elseif ($p == 2) { |
141 | $e = $e . $B64Chars[($t << 2) + ((ord($c) & 192) >> 6)]; |
142 | $e = $e . $B64Chars[ord($c) & 63]; |
143 | $p = 0; |
144 | } |
145 | } |
146 | // |
147 | // flush buffer |
148 | // |
149 | if ($p == 1) { |
150 | $e = $e . $B64Chars[$t << 4]; |
151 | } elseif ($p == 2) { |
152 | $e = $e . $B64Chars[$t << 2]; |
153 | } |
154 | return $e; |
155 | } |
156 | |
157 | function decodeBASE64($s) { |
158 | $B64Values = array( |
159 | 'A' => 0, 'B' => 1, 'C' => 2, 'D' => 3, 'E' => 4, 'F' => 5, |
160 | 'G' => 6, 'H' => 7, 'I' => 8, 'J' => 9, 'K' => 10, 'L' => 11, |
161 | 'M' => 12, 'N' => 13, 'O' => 14, 'P' => 15, 'Q' => 16, 'R' => 17, |
162 | 'S' => 18, 'T' => 19, 'U' => 20, 'V' => 21, 'W' => 22, 'X' => 23, |
163 | 'Y' => 24, 'Z' => 25, |
164 | 'a' => 26, 'b' => 27, 'c' => 28, 'd' => 29, 'e' => 30, 'f' => 31, |
165 | 'g' => 32, 'h' => 33, 'i' => 34, 'j' => 35, 'k' => 36, 'l' => 37, |
166 | 'm' => 38, 'n' => 39, 'o' => 40, 'p' => 41, 'q' => 42, 'r' => 43, |
167 | 's' => 44, 't' => 45, 'u' => 46, 'v' => 47, 'w' => 48, 'x' => 49, |
168 | 'y' => 50, 'z' => 51, |
169 | '0' => 52, '1' => 53, '2' => 54, '3' => 55, '4' => 56, '5' => 57, |
170 | '6' => 58, '7' => 59, '8' => 60, '9' => 61, '+' => 62, ',' => 63 |
171 | ); |
172 | $p = 0; |
173 | $d = ''; |
174 | $unicodeNullByteToggle = 0; |
175 | for ($i = 0, $len = strlen($s); $i < $len; $i++) { |
176 | $c = $s[$i]; |
177 | if ($p == 0) { |
178 | $t = $B64Values[$c]; |
179 | $p = 1; |
180 | } elseif ($p == 1) { |
181 | if ($unicodeNullByteToggle) { |
182 | $d = $d . chr(($t << 2) + (($B64Values[$c] & 48) >> 4)); |
183 | $unicodeNullByteToggle = 0; |
184 | } else { |
185 | $unicodeNullByteToggle = 1; |
186 | } |
187 | $t = ($B64Values[$c] & 15); |
188 | $p = 2; |
189 | } elseif ($p == 2) { |
190 | if ($unicodeNullByteToggle) { |
191 | $d = $d . chr(($t << 4) + (($B64Values[$c] & 60) >> 2)); |
192 | $unicodeNullByteToggle = 0; |
193 | } else { |
194 | $unicodeNullByteToggle = 1; |
195 | } |
196 | $t = ($B64Values[$c] & 3); |
197 | $p = 3; |
198 | } elseif ($p == 3) { |
199 | if ($unicodeNullByteToggle) { |
200 | $d = $d . chr(($t << 6) + $B64Values[$c]); |
201 | $unicodeNullByteToggle = 0; |
202 | } else { |
203 | $unicodeNullByteToggle = 1; |
204 | } |
205 | $t = ($B64Values[$c] & 3); |
206 | $p = 0; |
207 | } |
208 | } |
209 | return $d; |
210 | } |
211 | |
e842b215 |
212 | ?> |