c2c9ae538d97f1239848477a1e4c28cea8775b26
[squirrelmail.git] / functions / imap_utf7_local.php
1 <?php
2
3 /**
4 * imap_general.php
5 *
6 * Copyright (c) 1999-2003 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 general imap functions.
10 *
11 * $Id $
12 */
13
14 function imap_utf7_encode_local($s) {
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 }
21
22 $b64_s = ''; // buffer for substring to be base64-encoded
23 $utf7_s = ''; // imap-utf7-encoded string
24 for ($i = 0; $i < strlen($s); $i++) {
25 $c = $s[$i];
26 $ord_c = ord($c);
27 if ((($ord_c >= 0x20) and ($ord_c <= 0x25)) or
28 (($ord_c >= 0x27) and ($ord_c <= 0x7e))) {
29 if ($b64_s) {
30 $utf7_s = $utf7_s . '&' . encodeBASE64($b64_s) .'-';
31 $b64_s = '';
32 }
33 $utf7_s = $utf7_s . $c;
34 } elseif ($ord_c == 0x26) {
35 if ($b64_s) {
36 $utf7_s = $utf7_s . '&' . encodeBASE64($b64_s) . '-';
37 $b64_s = '';
38 }
39 $utf7_s = $utf7_s . '&-';
40 } else {
41 $b64_s = $b64_s . chr(0) . $c;
42 }
43 }
44 //
45 // flush buffer
46 //
47 if ($b64_s) {
48 $utf7_s = $utf7_s . '&' . encodeBASE64($b64_s) . '-';
49 $b64_s = '';
50 }
51 return $utf7_s;
52 }
53
54 function imap_utf7_decode_local($s) {
55 global $languages, $squirrelmail_language;
56
57 if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
58 function_exists($languages[$squirrelmail_language]['XTRA_CODE'])) {
59 return $languages[$squirrelmail_language]['XTRA_CODE']('utf7-imap_decode', $s);
60 }
61
62 $b64_s = '';
63 $iso_8859_1_s = '';
64 for ($i = 0, $len = strlen($s); $i < $len; $i++) {
65 $c = $s[$i];
66 if (strlen($b64_s) > 0) {
67 if ($c == '-') {
68 if ($b64_s == '&') {
69 $iso_8859_1_s = $iso_8859_1_s . '&';
70 } else {
71 $iso_8859_1_s = $iso_8859_1_s .
72 decodeBASE64(substr($b64_s, 1));
73 }
74 $b64_s = '';
75 } else {
76 $b64_s = $b64_s . $c;
77 }
78 } else {
79 if ($c == '&') {
80 $b64_s = '&';
81 } else {
82 $iso_8859_1_s = $iso_8859_1_s . $c;
83 }
84 }
85 }
86 return $iso_8859_1_s;
87 }
88
89 function encodeBASE64($s) {
90 $B64Chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+,';
91 $p = 0; // phase: 1 / 2 / 3 / 1 / 2 / 3...
92 $e = ''; // base64-encoded string
93 //foreach($s as $c) {
94 for ($i = 0; $i < strlen($s); $i++) {
95 $c = $s[$i];
96 if ($p == 0) {
97 $e = $e . substr($B64Chars, ((ord($c) & 252) >> 2), 1);
98 $t = (ord($c) & 3);
99 $p = 1;
100 } elseif ($p == 1) {
101 $e = $e . $B64Chars[($t << 4) + ((ord($c) & 240) >> 4)];
102 $t = (ord($c) & 15);
103 $p = 2;
104 } elseif ($p == 2) {
105 $e = $e . $B64Chars[($t << 2) + ((ord($c) & 192) >> 6)];
106 $e = $e . $B64Chars[ord($c) & 63];
107 $p = 0;
108 }
109 }
110 //
111 // flush buffer
112 //
113 if ($p == 1) {
114 $e = $e . $B64Chars[$t << 4];
115 } elseif ($p == 2) {
116 $e = $e . $B64Chars[$t << 2];
117 }
118 return $e;
119 }
120
121 function decodeBASE64($s) {
122 $B64Values = array(
123 'A' => 0, 'B' => 1, 'C' => 2, 'D' => 3, 'E' => 4, 'F' => 5,
124 'G' => 6, 'H' => 7, 'I' => 8, 'J' => 9, 'K' => 10, 'L' => 11,
125 'M' => 12, 'N' => 13, 'O' => 14, 'P' => 15, 'Q' => 16, 'R' => 17,
126 'S' => 18, 'T' => 19, 'U' => 20, 'V' => 21, 'W' => 22, 'X' => 23,
127 'Y' => 24, 'Z' => 25,
128 'a' => 26, 'b' => 27, 'c' => 28, 'd' => 29, 'e' => 30, 'f' => 31,
129 'g' => 32, 'h' => 33, 'i' => 34, 'j' => 35, 'k' => 36, 'l' => 37,
130 'm' => 38, 'n' => 39, 'o' => 40, 'p' => 41, 'q' => 42, 'r' => 43,
131 's' => 44, 't' => 45, 'u' => 46, 'v' => 47, 'w' => 48, 'x' => 49,
132 'y' => 50, 'z' => 51,
133 '0' => 52, '1' => 53, '2' => 54, '3' => 55, '4' => 56, '5' => 57,
134 '6' => 58, '7' => 59, '8' => 60, '9' => 61, '+' => 62, ',' => 63
135 );
136 $p = 0;
137 $d = '';
138 $unicodeNullByteToggle = 0;
139 for ($i = 0, $len = strlen($s); $i < $len; $i++) {
140 $c = $s[$i];
141 if ($p == 0) {
142 $t = $B64Values[$c];
143 $p = 1;
144 } elseif ($p == 1) {
145 if ($unicodeNullByteToggle) {
146 $d = $d . chr(($t << 2) + (($B64Values[$c] & 48) >> 4));
147 $unicodeNullByteToggle = 0;
148 } else {
149 $unicodeNullByteToggle = 1;
150 }
151 $t = ($B64Values[$c] & 15);
152 $p = 2;
153 } elseif ($p == 2) {
154 if ($unicodeNullByteToggle) {
155 $d = $d . chr(($t << 4) + (($B64Values[$c] & 60) >> 2));
156 $unicodeNullByteToggle = 0;
157 } else {
158 $unicodeNullByteToggle = 1;
159 }
160 $t = ($B64Values[$c] & 3);
161 $p = 3;
162 } elseif ($p == 3) {
163 if ($unicodeNullByteToggle) {
164 $d = $d . chr(($t << 6) + $B64Values[$c]);
165 $unicodeNullByteToggle = 0;
166 } else {
167 $unicodeNullByteToggle = 1;
168 }
169 $t = ($B64Values[$c] & 3);
170 $p = 0;
171 }
172 }
173 return $d;
174 }
175
176 ?>