869ece5d |
1 | <?php |
2 | |
3 | function encodeBASE64($s) { |
4 | $B64Chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+,'; |
5 | $p = 0; // phase: 1 / 2 / 3 / 1 / 2 / 3... |
6 | $e = ''; // base64-encoded string |
7 | //foreach($s as $c) { |
8 | for ($i = 0; $i < strlen($s); $i++) { |
9 | $c = $s[$i]; |
10 | if ($p == 0) { |
11 | $e = $e . substr($B64Chars, ((ord($c) & 252) >> 2), 1); |
12 | $t = (ord($c) & 3); |
13 | $p = 1; |
14 | } elseif ($p == 1) { |
15 | $e = $e . $B64Chars[($t << 4) + ((ord($c) & 240) >> 4)]; |
16 | $t = (ord($c) & 15); |
17 | $p = 2; |
18 | } elseif ($p == 2) { |
19 | $e = $e . $B64Chars[($t << 2) + ((ord($c) & 192) >> 6)]; |
20 | $e = $e . $B64Chars[ord($c) & 63]; |
21 | $p = 0; |
22 | } |
23 | } |
24 | // |
25 | // flush buffer |
26 | // |
27 | if ($p == 1) { |
28 | $e = $e . $B64Chars[$t << 4]; |
29 | } elseif ($p == 2) { |
30 | $e = $e . $B64Chars[$t << 2]; |
31 | } |
32 | return $e; |
33 | } |
34 | |
35 | function imap_utf7_encode_local($s) { |
36 | $b64_s = ''; // buffer for substring to be base64-encoded |
37 | $utf7_s = ''; // imap-utf7-encoded string |
38 | for ($i = 0; $i < strlen($s); $i++) { |
39 | $c = $s[$i]; |
40 | $ord_c = ord($c); |
41 | if ((($ord_c >= 0x20) and ($ord_c <= 0x25)) or |
42 | (($ord_c >= 0x27) and ($ord_c <= 0x7e))) { |
43 | if ($b64_s) { |
44 | $utf7_s = $utf7_s . '&' . encodeBASE64($b64_s) .'-'; |
45 | $b64_s = ''; |
46 | } |
47 | $utf7_s = $utf7_s . $c; |
48 | } elseif ($ord_c == 0x26) { |
49 | if ($b64_s) { |
50 | $utf7_s = $utf7_s . '&' . encodeBASE64($b64_s) . '-'; |
51 | $b64_s = ''; |
52 | } |
53 | $utf7_s = $utf7_s . '&-'; |
54 | } else { |
55 | $b64_s = $b64_s . chr(0) . $c; |
56 | } |
57 | } |
58 | // |
59 | // flush buffer |
60 | // |
61 | if ($b64_s) { |
62 | $utf7_s = $utf7_s . '&' . encodeBASE64($b64_s) . '-'; |
63 | $b64_s = ''; |
64 | } |
65 | return $utf7_s; |
66 | } |
67 | |
68 | ?> |