changes links of images in advanced_tree=true to .png
[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 *
11 * $Id $
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+
19 if ( stristr('iso-8859-',$default_charset) ||
20 stristr('utf-8',$default_charset) ||
21 stristr('iso-2022-jp',$default_charset) ) {
22 if (function_exists('mb_convert_encoding')) {
23 set_my_charset();
24 return mb_convert_encoding($s, $to_encoding, $from_encoding);
25 }
26 }
27 return '';
28}
29
334a77f8 30function imap_utf7_encode_local($s) {
e842b215 31 global $languages, $squirrelmail_language;
32
33 if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
34 function_exists($languages[$squirrelmail_language]['XTRA_CODE'])) {
35 return $languages[$squirrelmail_language]['XTRA_CODE']('utf7-imap_encode', $s);
36 }
8b35c78e 37
abaa3d33 38 if ($s == '') //If empty, don't bother
39 return '';
8b35c78e 40
abaa3d33 41 global $default_charset;
0ad15b1a 42 if (strtolower($default_charset) != 'iso-8859-1') {
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;
0ad15b1a 94 if (strtolower($default_charset) != 'iso-8859-1') {
abaa3d33 95 $utf7_s = sqimap_mb_convert_encoding($s, $default_charset, 'UTF7-IMAP', $default_charset);
96 if ($utf7_s != '')
97 return $utf7_s;
98 }
8b35c78e 99
100 // Later code works only for ISO-8859-1
e842b215 101
334a77f8 102 $b64_s = '';
103 $iso_8859_1_s = '';
104 for ($i = 0, $len = strlen($s); $i < $len; $i++) {
105 $c = $s[$i];
106 if (strlen($b64_s) > 0) {
107 if ($c == '-') {
108 if ($b64_s == '&') {
109 $iso_8859_1_s = $iso_8859_1_s . '&';
110 } else {
111 $iso_8859_1_s = $iso_8859_1_s .
112 decodeBASE64(substr($b64_s, 1));
113 }
114 $b64_s = '';
115 } else {
116 $b64_s = $b64_s . $c;
117 }
118 } else {
119 if ($c == '&') {
120 $b64_s = '&';
121 } else {
122 $iso_8859_1_s = $iso_8859_1_s . $c;
123 }
124 }
125 }
126 return $iso_8859_1_s;
127}
128
129function encodeBASE64($s) {
130 $B64Chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+,';
131 $p = 0; // phase: 1 / 2 / 3 / 1 / 2 / 3...
132 $e = ''; // base64-encoded string
133 //foreach($s as $c) {
134 for ($i = 0; $i < strlen($s); $i++) {
135 $c = $s[$i];
136 if ($p == 0) {
137 $e = $e . substr($B64Chars, ((ord($c) & 252) >> 2), 1);
138 $t = (ord($c) & 3);
139 $p = 1;
140 } elseif ($p == 1) {
141 $e = $e . $B64Chars[($t << 4) + ((ord($c) & 240) >> 4)];
142 $t = (ord($c) & 15);
143 $p = 2;
144 } elseif ($p == 2) {
145 $e = $e . $B64Chars[($t << 2) + ((ord($c) & 192) >> 6)];
146 $e = $e . $B64Chars[ord($c) & 63];
147 $p = 0;
148 }
149 }
150 //
151 // flush buffer
152 //
153 if ($p == 1) {
154 $e = $e . $B64Chars[$t << 4];
155 } elseif ($p == 2) {
156 $e = $e . $B64Chars[$t << 2];
157 }
158 return $e;
159}
160
161function decodeBASE64($s) {
162 $B64Values = array(
163 'A' => 0, 'B' => 1, 'C' => 2, 'D' => 3, 'E' => 4, 'F' => 5,
164 'G' => 6, 'H' => 7, 'I' => 8, 'J' => 9, 'K' => 10, 'L' => 11,
165 'M' => 12, 'N' => 13, 'O' => 14, 'P' => 15, 'Q' => 16, 'R' => 17,
166 'S' => 18, 'T' => 19, 'U' => 20, 'V' => 21, 'W' => 22, 'X' => 23,
167 'Y' => 24, 'Z' => 25,
168 'a' => 26, 'b' => 27, 'c' => 28, 'd' => 29, 'e' => 30, 'f' => 31,
169 'g' => 32, 'h' => 33, 'i' => 34, 'j' => 35, 'k' => 36, 'l' => 37,
170 'm' => 38, 'n' => 39, 'o' => 40, 'p' => 41, 'q' => 42, 'r' => 43,
171 's' => 44, 't' => 45, 'u' => 46, 'v' => 47, 'w' => 48, 'x' => 49,
172 'y' => 50, 'z' => 51,
173 '0' => 52, '1' => 53, '2' => 54, '3' => 55, '4' => 56, '5' => 57,
174 '6' => 58, '7' => 59, '8' => 60, '9' => 61, '+' => 62, ',' => 63
175 );
176 $p = 0;
177 $d = '';
178 $unicodeNullByteToggle = 0;
179 for ($i = 0, $len = strlen($s); $i < $len; $i++) {
180 $c = $s[$i];
181 if ($p == 0) {
182 $t = $B64Values[$c];
183 $p = 1;
184 } elseif ($p == 1) {
185 if ($unicodeNullByteToggle) {
186 $d = $d . chr(($t << 2) + (($B64Values[$c] & 48) >> 4));
187 $unicodeNullByteToggle = 0;
188 } else {
189 $unicodeNullByteToggle = 1;
190 }
191 $t = ($B64Values[$c] & 15);
192 $p = 2;
193 } elseif ($p == 2) {
194 if ($unicodeNullByteToggle) {
195 $d = $d . chr(($t << 4) + (($B64Values[$c] & 60) >> 2));
196 $unicodeNullByteToggle = 0;
197 } else {
198 $unicodeNullByteToggle = 1;
199 }
200 $t = ($B64Values[$c] & 3);
201 $p = 3;
202 } elseif ($p == 3) {
203 if ($unicodeNullByteToggle) {
204 $d = $d . chr(($t << 6) + $B64Values[$c]);
205 $unicodeNullByteToggle = 0;
206 } else {
207 $unicodeNullByteToggle = 1;
208 }
209 $t = ($B64Values[$c] & 3);
210 $p = 0;
211 }
212 }
213 return $d;
214}
215
e842b215 216?>