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