$default_charset variable equal to user's locale when decoding messages.
[squirrelmail.git] / functions / i18n.php
CommitLineData
59177427 1<?php
1fd97780 2
35586184 3/**
4 * i18n.php
5 *
76911253 6 * Copyright (c) 1999-2003 The SquirrelMail Project Team
35586184 7 * Licensed under the GNU GPL. For full terms see the file COPYING.
8 *
9 * This file contains variuos functions that are needed to do
10 * internationalization of SquirrelMail.
11 *
12 * Internally the output character set is used. Other characters are
13 * encoded using Unicode entities according to HTML 4.0.
14 *
15 * $Id$
16 */
17
a2a7852b 18/* Decodes a string to the internal encoding from the given charset */
19function charset_decode ($charset, $string) {
6fbd125b 20 global $languages, $squirrelmail_language;
a2a7852b 21
3714db45 22 if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
23 function_exists($languages[$squirrelmail_language]['XTRA_CODE'])) {
6fbd125b 24 $string = $languages[$squirrelmail_language]['XTRA_CODE']('decode', $string);
25 }
b05c8961 26
a2a7852b 27 /* All HTML special characters are 7 bit and can be replaced first */
28 $string = htmlspecialchars ($string);
29
30 $charset = strtolower($charset);
31
94965562 32 set_my_charset() ;
33
a2a7852b 34 if (ereg('iso-8859-([[:digit:]]+)', $charset, $res)) {
35 if ($res[1] == '1') {
36 $ret = charset_decode_iso_8859_1 ($string);
37 } else if ($res[1] == '2') {
38 $ret = charset_decode_iso_8859_2 ($string);
9be313d5 39 } else if ($res[1] == '4') {
40 $ret = charset_decode_iso_8859_4 ($string);
94965562 41 } else if ($res[1] == '5') {
42 $ret = charset_decode_iso_8859_5 ($string);
a2a7852b 43 } else if ($res[1] == '7') {
44 $ret = charset_decode_iso_8859_7 ($string);
3ab35042 45 } else if ($res[1] == '9') {
46 $ret = charset_decode_iso_8859_9 ($string);
9be313d5 47 } else if ($res[1] == '13') {
48 $ret = charset_decode_iso_8859_13 ($string);
a2a7852b 49 } else if ($res[1] == '15') {
50 $ret = charset_decode_iso_8859_15 ($string);
51 } else {
52 $ret = charset_decode_iso_8859_default ($string);
53 }
54 } else if ($charset == 'ns_4551-1') {
55 $ret = charset_decode_ns_4551_1 ($string);
56 } else if ($charset == 'koi8-r') {
57 $ret = charset_decode_koi8r ($string);
58 } else if ($charset == 'windows-1251') {
ecd877a8 59 $ret = charset_decode_windows_1251 ($string);
3ab35042 60 } else if ($charset == 'windows-1253') {
61 $ret = charset_decode_windows_1253 ($string);
62 } else if ($charset == 'windows-1254') {
63 $ret = charset_decode_windows_1254 ($string);
c37a12f8 64 } else if ($charset == 'windows-1257') {
65 $ret = charset_decode_windows_1257 ($string);
3ab35042 66 } else if ($charset == 'utf-8') {
67 $ret = charset_decode_utf8 ($string);
a2a7852b 68 } else {
69 $ret = $string;
70 }
71 return( $ret );
72}
73
74/*
75 iso-8859-1 is the same as Latin 1 and is normally used
76 in western europe.
77 */
78function charset_decode_iso_8859_1 ($string) {
79 global $default_charset;
13e0c649 80
a2a7852b 81 if (strtolower($default_charset) <> 'iso-8859-1') {
82 /* Only do the slow convert if there are 8-bit characters */
83 if (ereg("[\200-\377]", $string)) {
066c374f 84 $string = str_replace("\201", '&#129;', $string);
85 $string = str_replace("\202", '&#130;', $string);
86 $string = str_replace("\203", '&#131;', $string);
87 $string = str_replace("\204", '&#132;', $string);
88 $string = str_replace("\205", '&#133;', $string);
89 $string = str_replace("\206", '&#134;', $string);
90 $string = str_replace("\207", '&#135;', $string);
91 $string = str_replace("\210", '&#136;', $string);
92 $string = str_replace("\211", '&#137;', $string);
93 $string = str_replace("\212", '&#138;', $string);
94 $string = str_replace("\213", '&#139;', $string);
95 $string = str_replace("\214", '&#140;', $string);
96 $string = str_replace("\215", '&#141;', $string);
97 $string = str_replace("\216", '&#142;', $string);
98 $string = str_replace("\217", '&#143;', $string);
99 $string = str_replace("\220", '&#144;', $string);
100 $string = str_replace("\221", '&#145;', $string);
101 $string = str_replace("\222", '&#146;', $string);
102 $string = str_replace("\223", '&#147;', $string);
103 $string = str_replace("\224", '&#148;', $string);
104 $string = str_replace("\225", '&#149;', $string);
105 $string = str_replace("\226", '&#150;', $string);
106 $string = str_replace("\227", '&#151;', $string);
107 $string = str_replace("\230", '&#152;', $string);
108 $string = str_replace("\231", '&#153;', $string);
109 $string = str_replace("\232", '&#154;', $string);
110 $string = str_replace("\233", '&#155;', $string);
111 $string = str_replace("\234", '&#156;', $string);
112 $string = str_replace("\235", '&#157;', $string);
113 $string = str_replace("\236", '&#158;', $string);
114 $string = str_replace("\237", '&#159;', $string);
115 $string = str_replace("\240", '&#160;', $string);
116 $string = str_replace("\241", '&#161;', $string);
117 $string = str_replace("\242", '&#162;', $string);
118 $string = str_replace("\243", '&#163;', $string);
119 $string = str_replace("\244", '&#164;', $string);
120 $string = str_replace("\245", '&#165;', $string);
121 $string = str_replace("\246", '&#166;', $string);
122 $string = str_replace("\247", '&#167;', $string);
123 $string = str_replace("\250", '&#168;', $string);
124 $string = str_replace("\251", '&#169;', $string);
125 $string = str_replace("\252", '&#170;', $string);
126 $string = str_replace("\253", '&#171;', $string);
127 $string = str_replace("\254", '&#172;', $string);
128 $string = str_replace("\255", '&#173;', $string);
129 $string = str_replace("\256", '&#174;', $string);
130 $string = str_replace("\257", '&#175;', $string);
131 $string = str_replace("\260", '&#176;', $string);
132 $string = str_replace("\261", '&#177;', $string);
133 $string = str_replace("\262", '&#178;', $string);
134 $string = str_replace("\263", '&#179;', $string);
135 $string = str_replace("\264", '&#180;', $string);
136 $string = str_replace("\265", '&#181;', $string);
137 $string = str_replace("\266", '&#182;', $string);
138 $string = str_replace("\267", '&#183;', $string);
139 $string = str_replace("\270", '&#184;', $string);
140 $string = str_replace("\271", '&#185;', $string);
141 $string = str_replace("\272", '&#186;', $string);
142 $string = str_replace("\273", '&#187;', $string);
143 $string = str_replace("\274", '&#188;', $string);
144 $string = str_replace("\275", '&#189;', $string);
145 $string = str_replace("\276", '&#190;', $string);
146 $string = str_replace("\277", '&#191;', $string);
147 $string = str_replace("\300", '&#192;', $string);
148 $string = str_replace("\301", '&#193;', $string);
149 $string = str_replace("\302", '&#194;', $string);
150 $string = str_replace("\303", '&#195;', $string);
151 $string = str_replace("\304", '&#196;', $string);
152 $string = str_replace("\305", '&#197;', $string);
153 $string = str_replace("\306", '&#198;', $string);
154 $string = str_replace("\307", '&#199;', $string);
155 $string = str_replace("\310", '&#200;', $string);
156 $string = str_replace("\311", '&#201;', $string);
157 $string = str_replace("\312", '&#202;', $string);
158 $string = str_replace("\313", '&#203;', $string);
159 $string = str_replace("\314", '&#204;', $string);
160 $string = str_replace("\315", '&#205;', $string);
161 $string = str_replace("\316", '&#206;', $string);
162 $string = str_replace("\317", '&#207;', $string);
163 $string = str_replace("\320", '&#208;', $string);
164 $string = str_replace("\321", '&#209;', $string);
165 $string = str_replace("\322", '&#210;', $string);
166 $string = str_replace("\323", '&#211;', $string);
167 $string = str_replace("\324", '&#212;', $string);
168 $string = str_replace("\325", '&#213;', $string);
169 $string = str_replace("\326", '&#214;', $string);
170 $string = str_replace("\327", '&#215;', $string);
171 $string = str_replace("\330", '&#216;', $string);
172 $string = str_replace("\331", '&#217;', $string);
173 $string = str_replace("\332", '&#218;', $string);
174 $string = str_replace("\333", '&#219;', $string);
175 $string = str_replace("\334", '&#220;', $string);
176 $string = str_replace("\335", '&#221;', $string);
177 $string = str_replace("\336", '&#222;', $string);
178 $string = str_replace("\337", '&#223;', $string);
179 $string = str_replace("\340", '&#224;', $string);
180 $string = str_replace("\341", '&#225;', $string);
181 $string = str_replace("\342", '&#226;', $string);
182 $string = str_replace("\343", '&#227;', $string);
183 $string = str_replace("\344", '&#228;', $string);
184 $string = str_replace("\345", '&#229;', $string);
185 $string = str_replace("\346", '&#230;', $string);
186 $string = str_replace("\347", '&#231;', $string);
187 $string = str_replace("\350", '&#232;', $string);
188 $string = str_replace("\351", '&#233;', $string);
189 $string = str_replace("\352", '&#234;', $string);
190 $string = str_replace("\353", '&#235;', $string);
191 $string = str_replace("\354", '&#236;', $string);
192 $string = str_replace("\355", '&#237;', $string);
193 $string = str_replace("\356", '&#238;', $string);
194 $string = str_replace("\357", '&#239;', $string);
195 $string = str_replace("\360", '&#240;', $string);
196 $string = str_replace("\361", '&#241;', $string);
197 $string = str_replace("\362", '&#242;', $string);
198 $string = str_replace("\363", '&#243;', $string);
199 $string = str_replace("\364", '&#244;', $string);
200 $string = str_replace("\365", '&#245;', $string);
201 $string = str_replace("\366", '&#246;', $string);
202 $string = str_replace("\367", '&#247;', $string);
203 $string = str_replace("\370", '&#248;', $string);
204 $string = str_replace("\371", '&#249;', $string);
205 $string = str_replace("\372", '&#250;', $string);
206 $string = str_replace("\373", '&#251;', $string);
207 $string = str_replace("\374", '&#252;', $string);
208 $string = str_replace("\375", '&#253;', $string);
209 $string = str_replace("\376", '&#254;', $string);
210 $string = str_replace("\377", '&#255;', $string);
a2a7852b 211 }
212 }
213
214 return ($string);
215}
216
217/* iso-8859-2 is used for some eastern European languages */
218function charset_decode_iso_8859_2 ($string) {
219 global $default_charset;
220
221 if (strtolower($default_charset) == 'iso-8859-2')
222 return $string;
223
224 /* Only do the slow convert if there are 8-bit characters */
225 if (! ereg("[\200-\377]", $string))
226 return $string;
227
228 /* NO-BREAK SPACE */
229 $string = str_replace("\240", '&#160;', $string);
230 /* LATIN CAPITAL LETTER A WITH OGONEK */
231 $string = str_replace("\241", '&#260;', $string);
232 /* BREVE */
233 $string = str_replace("\242", '&#728;', $string);
234 // LATIN CAPITAL LETTER L WITH STROKE
235 $string = str_replace("\243", '&#321;', $string);
236 // CURRENCY SIGN
237 $string = str_replace("\244", '&#164;', $string);
238 // LATIN CAPITAL LETTER L WITH CARON
239 $string = str_replace("\245", '&#317;', $string);
240 // LATIN CAPITAL LETTER S WITH ACUTE
241 $string = str_replace("\246", '&#346;', $string);
242 // SECTION SIGN
243 $string = str_replace("\247", '&#167;', $string);
244 // DIAERESIS
245 $string = str_replace("\250", '&#168;', $string);
246 // LATIN CAPITAL LETTER S WITH CARON
247 $string = str_replace("\251", '&#352;', $string);
248 // LATIN CAPITAL LETTER S WITH CEDILLA
249 $string = str_replace("\252", '&#350;', $string);
250 // LATIN CAPITAL LETTER T WITH CARON
251 $string = str_replace("\253", '&#356;', $string);
252 // LATIN CAPITAL LETTER Z WITH ACUTE
253 $string = str_replace("\254", '&#377;', $string);
254 // SOFT HYPHEN
255 $string = str_replace("\255", '&#173;', $string);
256 // LATIN CAPITAL LETTER Z WITH CARON
257 $string = str_replace("\256", '&#381;', $string);
258 // LATIN CAPITAL LETTER Z WITH DOT ABOVE
259 $string = str_replace("\257", '&#379;', $string);
260 // DEGREE SIGN
261 $string = str_replace("\260", '&#176;', $string);
262 // LATIN SMALL LETTER A WITH OGONEK
263 $string = str_replace("\261", '&#261;', $string);
264 // OGONEK
265 $string = str_replace("\262", '&#731;', $string);
266 // LATIN SMALL LETTER L WITH STROKE
267 $string = str_replace("\263", '&#322;', $string);
268 // ACUTE ACCENT
269 $string = str_replace("\264", '&#180;', $string);
270 // LATIN SMALL LETTER L WITH CARON
271 $string = str_replace("\265", '&#318;', $string);
272 // LATIN SMALL LETTER S WITH ACUTE
273 $string = str_replace("\266", '&#347;', $string);
274 // CARON
275 $string = str_replace("\267", '&#711;', $string);
276 // CEDILLA
277 $string = str_replace("\270", '&#184;', $string);
278 // LATIN SMALL LETTER S WITH CARON
279 $string = str_replace("\271", '&#353;', $string);
280 // LATIN SMALL LETTER S WITH CEDILLA
281 $string = str_replace("\272", '&#351;', $string);
282 // LATIN SMALL LETTER T WITH CARON
283 $string = str_replace("\273", '&#357;', $string);
284 // LATIN SMALL LETTER Z WITH ACUTE
285 $string = str_replace("\274", '&#378;', $string);
286 // DOUBLE ACUTE ACCENT
287 $string = str_replace("\275", '&#733;', $string);
288 // LATIN SMALL LETTER Z WITH CARON
289 $string = str_replace("\276", '&#382;', $string);
290 // LATIN SMALL LETTER Z WITH DOT ABOVE
291 $string = str_replace("\277", '&#380;', $string);
292 // LATIN CAPITAL LETTER R WITH ACUTE
293 $string = str_replace("\300", '&#340;', $string);
294 // LATIN CAPITAL LETTER A WITH ACUTE
295 $string = str_replace("\301", '&#193;', $string);
296 // LATIN CAPITAL LETTER A WITH CIRCUMFLEX
297 $string = str_replace("\302", '&#194;', $string);
298 // LATIN CAPITAL LETTER A WITH BREVE
299 $string = str_replace("\303", '&#258;', $string);
300 // LATIN CAPITAL LETTER A WITH DIAERESIS
301 $string = str_replace("\304", '&#196;', $string);
302 // LATIN CAPITAL LETTER L WITH ACUTE
303 $string = str_replace("\305", '&#313;', $string);
304 // LATIN CAPITAL LETTER C WITH ACUTE
305 $string = str_replace("\306", '&#262;', $string);
306 // LATIN CAPITAL LETTER C WITH CEDILLA
307 $string = str_replace("\307", '&#199;', $string);
308 // LATIN CAPITAL LETTER C WITH CARON
309 $string = str_replace("\310", '&#268;', $string);
310 // LATIN CAPITAL LETTER E WITH ACUTE
311 $string = str_replace("\311", '&#201;', $string);
312 // LATIN CAPITAL LETTER E WITH OGONEK
313 $string = str_replace("\312", '&#280;', $string);
314 // LATIN CAPITAL LETTER E WITH DIAERESIS
315 $string = str_replace("\313", '&#203;', $string);
316 // LATIN CAPITAL LETTER E WITH CARON
317 $string = str_replace("\314", '&#282;', $string);
318 // LATIN CAPITAL LETTER I WITH ACUTE
319 $string = str_replace("\315", '&#205;', $string);
320 // LATIN CAPITAL LETTER I WITH CIRCUMFLEX
321 $string = str_replace("\316", '&#206;', $string);
322 // LATIN CAPITAL LETTER D WITH CARON
323 $string = str_replace("\317", '&#270;', $string);
324 // LATIN CAPITAL LETTER D WITH STROKE
325 $string = str_replace("\320", '&#272;', $string);
326 // LATIN CAPITAL LETTER N WITH ACUTE
327 $string = str_replace("\321", '&#323;', $string);
328 // LATIN CAPITAL LETTER N WITH CARON
329 $string = str_replace("\322", '&#327;', $string);
330 // LATIN CAPITAL LETTER O WITH ACUTE
331 $string = str_replace("\323", '&#211;', $string);
332 // LATIN CAPITAL LETTER O WITH CIRCUMFLEX
333 $string = str_replace("\324", '&#212;', $string);
334 // LATIN CAPITAL LETTER O WITH DOUBLE ACUTE
335 $string = str_replace("\325", '&#336;', $string);
336 // LATIN CAPITAL LETTER O WITH DIAERESIS
337 $string = str_replace("\326", '&#214;', $string);
338 // MULTIPLICATION SIGN
339 $string = str_replace("\327", '&#215;', $string);
340 // LATIN CAPITAL LETTER R WITH CARON
341 $string = str_replace("\330", '&#344;', $string);
342 // LATIN CAPITAL LETTER U WITH RING ABOVE
343 $string = str_replace("\331", '&#366;', $string);
344 // LATIN CAPITAL LETTER U WITH ACUTE
345 $string = str_replace("\332", '&#218;', $string);
346 // LATIN CAPITAL LETTER U WITH DOUBLE ACUTE
347 $string = str_replace("\333", '&#368;', $string);
348 // LATIN CAPITAL LETTER U WITH DIAERESIS
349 $string = str_replace("\334", '&#220;', $string);
350 // LATIN CAPITAL LETTER Y WITH ACUTE
351 $string = str_replace("\335", '&#221;', $string);
352 // LATIN CAPITAL LETTER T WITH CEDILLA
353 $string = str_replace("\336", '&#354;', $string);
354 // LATIN SMALL LETTER SHARP S
355 $string = str_replace("\337", '&#223;', $string);
356 // LATIN SMALL LETTER R WITH ACUTE
357 $string = str_replace("\340", '&#341;', $string);
358 // LATIN SMALL LETTER A WITH ACUTE
359 $string = str_replace("\341", '&#225;', $string);
360 // LATIN SMALL LETTER A WITH CIRCUMFLEX
361 $string = str_replace("\342", '&#226;', $string);
362 // LATIN SMALL LETTER A WITH BREVE
363 $string = str_replace("\343", '&#259;', $string);
364 // LATIN SMALL LETTER A WITH DIAERESIS
365 $string = str_replace("\344", '&#228;', $string);
366 // LATIN SMALL LETTER L WITH ACUTE
367 $string = str_replace("\345", '&#314;', $string);
368 // LATIN SMALL LETTER C WITH ACUTE
369 $string = str_replace("\346", '&#263;', $string);
370 // LATIN SMALL LETTER C WITH CEDILLA
371 $string = str_replace("\347", '&#231;', $string);
372 // LATIN SMALL LETTER C WITH CARON
373 $string = str_replace("\350", '&#269;', $string);
374 // LATIN SMALL LETTER E WITH ACUTE
375 $string = str_replace("\351", '&#233;', $string);
376 // LATIN SMALL LETTER E WITH OGONEK
377 $string = str_replace("\352", '&#281;', $string);
378 // LATIN SMALL LETTER E WITH DIAERESIS
379 $string = str_replace("\353", '&#235;', $string);
380 // LATIN SMALL LETTER E WITH CARON
381 $string = str_replace("\354", '&#283;', $string);
382 // LATIN SMALL LETTER I WITH ACUTE
383 $string = str_replace("\355", '&#237;', $string);
384 // LATIN SMALL LETTER I WITH CIRCUMFLEX
385 $string = str_replace("\356", '&#238;', $string);
386 // LATIN SMALL LETTER D WITH CARON
387 $string = str_replace("\357", '&#271;', $string);
388 // LATIN SMALL LETTER D WITH STROKE
389 $string = str_replace("\360", '&#273;', $string);
390 // LATIN SMALL LETTER N WITH ACUTE
391 $string = str_replace("\361", '&#324;', $string);
392 // LATIN SMALL LETTER N WITH CARON
393 $string = str_replace("\362", '&#328;', $string);
394 // LATIN SMALL LETTER O WITH ACUTE
395 $string = str_replace("\363", '&#243;', $string);
396 // LATIN SMALL LETTER O WITH CIRCUMFLEX
397 $string = str_replace("\364", '&#244;', $string);
398 // LATIN SMALL LETTER O WITH DOUBLE ACUTE
399 $string = str_replace("\365", '&#337;', $string);
400 // LATIN SMALL LETTER O WITH DIAERESIS
401 $string = str_replace("\366", '&#246;', $string);
402 // DIVISION SIGN
403 $string = str_replace("\367", '&#247;', $string);
404 // LATIN SMALL LETTER R WITH CARON
405 $string = str_replace("\370", '&#345;', $string);
406 // LATIN SMALL LETTER U WITH RING ABOVE
407 $string = str_replace("\371", '&#367;', $string);
408 // LATIN SMALL LETTER U WITH ACUTE
409 $string = str_replace("\372", '&#250;', $string);
410 // LATIN SMALL LETTER U WITH DOUBLE ACUTE
411 $string = str_replace("\373", '&#369;', $string);
412 // LATIN SMALL LETTER U WITH DIAERESIS
413 $string = str_replace("\374", '&#252;', $string);
414 // LATIN SMALL LETTER Y WITH ACUTE
415 $string = str_replace("\375", '&#253;', $string);
416 // LATIN SMALL LETTER T WITH CEDILLA
417 $string = str_replace("\376", '&#355;', $string);
418 // DOT ABOVE
419 $string = str_replace("\377", '&#729;', $string);
420
421 return $string;
422}
423
9be313d5 424/*
c37a12f8 425 ISO/IEC 8859-4:1998 Latin Alphabet No. 4
9be313d5 426*/
427
428function charset_decode_iso_8859_4 ($string) {
94965562 429 global $default_charset;
9be313d5 430
c37a12f8 431 if (strtolower($default_charset) == 'iso-8859-4')
432 return $string;
c37a12f8 433
434 /* Only do the slow convert if there are 8-bit characters */
435 if (! ereg("[\200-\377]", $string))
436 return $string;
437
c37a12f8 438 $string = str_replace ("\241", '&#260;', $string);
c37a12f8 439 $string = str_replace ("\242", '&#312;', $string);
c37a12f8 440 $string = str_replace ("\243", '&#342;', $string);
c37a12f8 441 $string = str_replace ("\245", '&#296;', $string);
c37a12f8 442 $string = str_replace ("\246", '&#315;', $string);
c37a12f8 443 $string = str_replace ("\251", '&#352;', $string);
c37a12f8 444 $string = str_replace ("\252", '&#274;', $string);
c37a12f8 445 $string = str_replace ("\253", '&#290;', $string);
c37a12f8 446 $string = str_replace ("\254", '&#358;', $string);
c37a12f8 447 $string = str_replace ("\256", '&#381;', $string);
c37a12f8 448 $string = str_replace ("\261", '&#261;', $string);
c37a12f8 449 $string = str_replace ("\262", '&#731;', $string);
c37a12f8 450 $string = str_replace ("\263", '&#343;', $string);
c37a12f8 451 $string = str_replace ("\265", '&#297;', $string);
c37a12f8 452 $string = str_replace ("\266", '&#316;', $string);
c37a12f8 453 $string = str_replace ("\267", '&#711;', $string);
c37a12f8 454 $string = str_replace ("\271", '&#353;', $string);
c37a12f8 455 $string = str_replace ("\272", '&#275;', $string);
c37a12f8 456 $string = str_replace ("\273", '&#291;', $string);
c37a12f8 457 $string = str_replace ("\274", '&#359;', $string);
c37a12f8 458 $string = str_replace ("\275", '&#330;', $string);
c37a12f8 459 $string = str_replace ("\276", '&#382;', $string);
c37a12f8 460 $string = str_replace ("\277", '&#331;', $string);
c37a12f8 461 $string = str_replace ("\300", '&#256;', $string);
c37a12f8 462 $string = str_replace ("\307", '&#302;', $string);
c37a12f8 463 $string = str_replace ("\310", '&#268;', $string);
c37a12f8 464 $string = str_replace ("\312", '&#280;', $string);
c37a12f8 465 $string = str_replace ("\314", '&#278;', $string);
c37a12f8 466 $string = str_replace ("\317", '&#298;', $string);
c37a12f8 467 $string = str_replace ("\320", '&#272;', $string);
c37a12f8 468 $string = str_replace ("\321", '&#325;', $string);
c37a12f8 469 $string = str_replace ("\322", '&#332;', $string);
c37a12f8 470 $string = str_replace ("\323", '&#310;', $string);
c37a12f8 471 $string = str_replace ("\331", '&#370;', $string);
c37a12f8 472 $string = str_replace ("\335", '&#360;', $string);
c37a12f8 473 $string = str_replace ("\336", '&#362;', $string);
c37a12f8 474 $string = str_replace ("\340", '&#257;', $string);
c37a12f8 475 $string = str_replace ("\347", '&#303;', $string);
c37a12f8 476 $string = str_replace ("\350", '&#269;', $string);
c37a12f8 477 $string = str_replace ("\352", '&#281;', $string);
c37a12f8 478 $string = str_replace ("\354", '&#279;', $string);
c37a12f8 479 $string = str_replace ("\357", '&#299;', $string);
c37a12f8 480 $string = str_replace ("\360", '&#273;', $string);
c37a12f8 481 $string = str_replace ("\361", '&#326;', $string);
c37a12f8 482 $string = str_replace ("\362", '&#333;', $string);
c37a12f8 483 $string = str_replace ("\363", '&#311;', $string);
c37a12f8 484 $string = str_replace ("\371", '&#371;', $string);
c37a12f8 485 $string = str_replace ("\375", '&#361;', $string);
c37a12f8 486 $string = str_replace ("\376", '&#363;', $string);
c37a12f8 487 $string = str_replace ("\377", '&#729;', $string);
488
489 // rest of charset is the same as ISO-8859-1
9be313d5 490 return (charset_decode_iso_8859_1($string));
491}
492
a2a7852b 493/* iso-8859-7 is Greek. */
494function charset_decode_iso_8859_7 ($string) {
495 global $default_charset;
496
497 if (strtolower($default_charset) == 'iso-8859-7') {
498 return $string;
499 }
500
501 /* Only do the slow convert if there are 8-bit characters */
502 if (!ereg("[\200-\377]", $string)) {
503 return $string;
504 }
505
506 /* Some diverse characters in the beginning */
507 $string = str_replace("\240", '&#160;', $string);
508 $string = str_replace("\241", '&#8216;', $string);
509 $string = str_replace("\242", '&#8217;', $string);
510 $string = str_replace("\243", '&#163;', $string);
511 $string = str_replace("\246", '&#166;', $string);
512 $string = str_replace("\247", '&#167;', $string);
513 $string = str_replace("\250", '&#168;', $string);
514 $string = str_replace("\251", '&#169;', $string);
515 $string = str_replace("\253", '&#171;', $string);
516 $string = str_replace("\254", '&#172;', $string);
517 $string = str_replace("\255", '&#173;', $string);
518 $string = str_replace("\257", '&#8213;', $string);
519 $string = str_replace("\260", '&#176;', $string);
520 $string = str_replace("\261", '&#177;', $string);
521 $string = str_replace("\262", '&#178;', $string);
522 $string = str_replace("\263", '&#179;', $string);
523
524 /* Horizontal bar (parentheki pavla) */
525 $string = str_replace ("\257", '&#8213;', $string);
526
527 /*
528 * ISO-8859-7 characters from 11/04 (0xB4) to 11/06 (0xB6)
529 * These are Unicode 900-902
530 */
b85a4575 531 $string = preg_replace("/([\264-\266])/e","'&#' . (ord('\\1')+720);",$string);
49c17806 532
a2a7852b 533 /* 11/07 (0xB7) Middle dot is the same in iso-8859-1 */
534 $string = str_replace("\267", '&#183;', $string);
535
536 /*
537 * ISO-8859-7 characters from 11/08 (0xB8) to 11/10 (0xBA)
538 * These are Unicode 900-902
539 */
b85a4575 540 $string = preg_replace("/([\270-\272])/e","'&#' . (ord('\\1')+720);",$string);
a2a7852b 541
542 /*
543 * 11/11 (0xBB) Right angle quotation mark is the same as in
544 * iso-8859-1
545 */
546 $string = str_replace("\273", '&#187;', $string);
547
548 /* And now the rest of the charset */
b85a4575 549 $string = preg_replace("/([\274-\376])/e","'&#'.(ord('\\1')+720);",$string);
a2a7852b 550
551 return $string;
552}
553
554/*
3ab35042 555 ISOIEC 8859-9:1999 Latin Alphabet No. 5
556*/
557function charset_decode_iso_8859_9 ($string) {
94965562 558 global $default_charset;
3ab35042 559
560 if (strtolower($default_charset) == 'iso-8859-9')
561 return $string;
3ab35042 562
563 /* Only do the slow convert if there are 8-bit characters */
564 if (! ereg("[\200-\377]", $string))
565 return $string;
566
567 // latin capital letter g with breve 208->286
568 $string = str_replace("\320", '&#286;', $string);
569 // latin capital letter i with dot above 221->304
570 $string = str_replace("\335", '&#304;', $string);
571 // latin capital letter s with cedilla 222->350
572 $string = str_replace("\336", '&#350;', $string);
573 // latin small letter g with breve 240->287
574 $string = str_replace("\360", '&#287;', $string);
575 // latin small letter dotless i 253->305
576 $string = str_replace("\375", '&#305;', $string);
577 // latin small letter s with cedilla 254->351
578 $string = str_replace("\376", '&#351;', $string);
579
580 // rest of charset is the same as ISO-8859-1
581 return (charset_decode_iso_8859_1($string));
582}
583
584
585/*
c37a12f8 586 ISO/IEC 8859-13:1998 Latin Alphabet No. 7 (Baltic Rim)
9be313d5 587*/
9be313d5 588function charset_decode_iso_8859_13 ($string) {
94965562 589 global $default_charset;
c37a12f8 590
591 if (strtolower($default_charset) == 'iso-8859-13')
592 return $string;
c37a12f8 593
594 /* Only do the slow convert if there are 8-bit characters */
595 if (! ereg("[\200-\377]", $string))
596 return $string;
9be313d5 597
c37a12f8 598 $string = str_replace ("\241", '&#8221;', $string);
c37a12f8 599 $string = str_replace ("\245", '&#8222;', $string);
c37a12f8 600 $string = str_replace ("\250", '&#216;', $string);
c37a12f8 601 $string = str_replace ("\252", '&#342;', $string);
c37a12f8 602 $string = str_replace ("\257", '&#198;', $string);
c37a12f8 603 $string = str_replace ("\264", '&#8220;', $string);
c37a12f8 604 $string = str_replace ("\270", '&#248;', $string);
c37a12f8 605 $string = str_replace ("\272", '&#343;', $string);
c37a12f8 606 $string = str_replace ("\277", '&#230;', $string);
c37a12f8 607 $string = str_replace ("\300", '&#260;', $string);
c37a12f8 608 $string = str_replace ("\301", '&#302;', $string);
c37a12f8 609 $string = str_replace ("\302", '&#256;', $string);
c37a12f8 610 $string = str_replace ("\303", '&#262;', $string);
c37a12f8 611 $string = str_replace ("\306", '&#280;', $string);
c37a12f8 612 $string = str_replace ("\307", '&#274;', $string);
c37a12f8 613 $string = str_replace ("\310", '&#268;', $string);
c37a12f8 614 $string = str_replace ("\312", '&#377;', $string);
c37a12f8 615 $string = str_replace ("\313", '&#278;', $string);
c37a12f8 616 $string = str_replace ("\314", '&#290;', $string);
c37a12f8 617 $string = str_replace ("\315", '&#310;', $string);
c37a12f8 618 $string = str_replace ("\316", '&#298;', $string);
c37a12f8 619 $string = str_replace ("\317", '&#315;', $string);
c37a12f8 620 $string = str_replace ("\320", '&#352;', $string);
c37a12f8 621 $string = str_replace ("\321", '&#323;', $string);
c37a12f8 622 $string = str_replace ("\322", '&#325;', $string);
c37a12f8 623 $string = str_replace ("\324", '&#332;', $string);
c37a12f8 624 $string = str_replace ("\330", '&#370;', $string);
c37a12f8 625 $string = str_replace ("\331", '&#321;', $string);
c37a12f8 626 $string = str_replace ("\332", '&#346;', $string);
c37a12f8 627 $string = str_replace ("\333", '&#362;', $string);
c37a12f8 628 $string = str_replace ("\335", '&#379;', $string);
c37a12f8 629 $string = str_replace ("\336", '&#381;', $string);
c37a12f8 630 $string = str_replace ("\340", '&#261;', $string);
c37a12f8 631 $string = str_replace ("\341", '&#303;', $string);
c37a12f8 632 $string = str_replace ("\342", '&#257;', $string);
c37a12f8 633 $string = str_replace ("\343", '&#263;', $string);
c37a12f8 634 $string = str_replace ("\346", '&#281;', $string);
c37a12f8 635 $string = str_replace ("\347", '&#275;', $string);
c37a12f8 636 $string = str_replace ("\350", '&#269;', $string);
c37a12f8 637 $string = str_replace ("\352", '&#378;', $string);
c37a12f8 638 $string = str_replace ("\353", '&#279;', $string);
c37a12f8 639 $string = str_replace ("\354", '&#291;', $string);
c37a12f8 640 $string = str_replace ("\355", '&#311;', $string);
c37a12f8 641 $string = str_replace ("\356", '&#299;', $string);
c37a12f8 642 $string = str_replace ("\357", '&#316;', $string);
c37a12f8 643 $string = str_replace ("\360", '&#353;', $string);
c37a12f8 644 $string = str_replace ("\361", '&#324;', $string);
c37a12f8 645 $string = str_replace ("\362", '&#326;', $string);
c37a12f8 646 $string = str_replace ("\364", '&#333;', $string);
c37a12f8 647 $string = str_replace ("\370", '&#371;', $string);
c37a12f8 648 $string = str_replace ("\371", '&#322;', $string);
c37a12f8 649 $string = str_replace ("\372", '&#347;', $string);
c37a12f8 650 $string = str_replace ("\373", '&#363;', $string);
c37a12f8 651 $string = str_replace ("\375", '&#380;', $string);
c37a12f8 652 $string = str_replace ("\376", '&#382;', $string);
c37a12f8 653 $string = str_replace ("\377", '&#8217;', $string);
654
655 // rest of charset is the same as ISO-8859-1
9be313d5 656 return (charset_decode_iso_8859_1($string));
657}
658
659/*
a2a7852b 660 * iso-8859-15 is Latin 9 and has very much the same use as Latin 1
661 * but has the Euro symbol and some characters needed for French.
662 */
663function charset_decode_iso_8859_15 ($string) {
664 // Euro sign
665 $string = str_replace ("\244", '&#8364;', $string);
666 // Latin capital letter S with caron
84556805 667 $string = str_replace ("\246", '&#352;', $string);
a2a7852b 668 // Latin small letter s with caron
669 $string = str_replace ("\250", '&#353;', $string);
670 // Latin capital letter Z with caron
671 $string = str_replace ("\264", '&#381;', $string);
672 // Latin small letter z with caron
673 $string = str_replace ("\270", '&#382;', $string);
674 // Latin capital ligature OE
675 $string = str_replace ("\274", '&#338;', $string);
676 // Latin small ligature oe
677 $string = str_replace ("\275", '&#339;', $string);
678 // Latin capital letter Y with diaeresis
679 $string = str_replace ("\276", '&#376;', $string);
680
681 return (charset_decode_iso_8859_1($string));
682}
683
84556805 684/* ISO-8859-5 is Cyrillic */
a2a7852b 685function charset_decode_iso_8859_5 ($string) {
686 // Convert to KOI8-R, then return this decoded.
687 $string = convert_cyr_string($string, 'i', 'k');
688 return charset_decode_koi8r($string);
689}
690
691/* Remove all 8 bit characters from all other ISO-8859 character sets */
692function charset_decode_iso_8859_default ($string) {
693 return (strtr($string, "\240\241\242\243\244\245\246\247".
1fd97780 694 "\250\251\252\253\254\255\256\257".
695 "\260\261\262\263\264\265\266\267".
696 "\270\271\272\273\274\275\276\277".
697 "\300\301\302\303\304\305\306\307".
698 "\310\311\312\313\314\315\316\317".
699 "\320\321\322\323\324\325\326\327".
700 "\330\331\332\333\334\335\336\337".
701 "\340\341\342\343\344\345\346\347".
702 "\350\351\352\353\354\355\356\357".
703 "\360\361\362\363\364\365\366\367".
a2a7852b 704 "\370\371\372\373\374\375\376\377",
1fd97780 705 "????????????????????????????????????????".
706 "????????????????????????????????????????".
707 "????????????????????????????????????????".
708 "????????"));
a2a7852b 709
710}
711
712/*
713 * This is the same as ISO-646-NO and is used by some
714 * Microsoft programs when sending Norwegian characters
715 */
716function charset_decode_ns_4551_1 ($string) {
717 /*
718 * These characters are:
719 * Latin capital letter AE
720 * Latin capital letter O with stroke
721 * Latin capital letter A with ring above
722 * and the same as small letters
723 */
724 return strtr ($string, "[\\]{|}", "");
725}
726
727/*
728 * KOI8-R is used to encode Russian mail (Cyrrilic). Defined in RFC
729 * 1489.
730 */
731function charset_decode_koi8r ($string) {
732 global $default_charset;
733
734 if ($default_charset == 'koi8-r') {
735 return $string;
736 }
737
738 /*
739 * Convert to Unicode HTML entities.
740 * This code is rather ineffective.
741 */
742 $string = str_replace("\200", '&#9472;', $string);
743 $string = str_replace("\201", '&#9474;', $string);
744 $string = str_replace("\202", '&#9484;', $string);
745 $string = str_replace("\203", '&#9488;', $string);
746 $string = str_replace("\204", '&#9492;', $string);
747 $string = str_replace("\205", '&#9496;', $string);
748 $string = str_replace("\206", '&#9500;', $string);
749 $string = str_replace("\207", '&#9508;', $string);
750 $string = str_replace("\210", '&#9516;', $string);
751 $string = str_replace("\211", '&#9524;', $string);
752 $string = str_replace("\212", '&#9532;', $string);
753 $string = str_replace("\213", '&#9600;', $string);
754 $string = str_replace("\214", '&#9604;', $string);
755 $string = str_replace("\215", '&#9608;', $string);
756 $string = str_replace("\216", '&#9612;', $string);
757 $string = str_replace("\217", '&#9616;', $string);
758 $string = str_replace("\220", '&#9617;', $string);
759 $string = str_replace("\221", '&#9618;', $string);
760 $string = str_replace("\222", '&#9619;', $string);
761 $string = str_replace("\223", '&#8992;', $string);
762 $string = str_replace("\224", '&#9632;', $string);
763 $string = str_replace("\225", '&#8729;', $string);
764 $string = str_replace("\226", '&#8730;', $string);
765 $string = str_replace("\227", '&#8776;', $string);
766 $string = str_replace("\230", '&#8804;', $string);
767 $string = str_replace("\231", '&#8805;', $string);
768 $string = str_replace("\232", '&#160;', $string);
769 $string = str_replace("\233", '&#8993;', $string);
770 $string = str_replace("\234", '&#176;', $string);
771 $string = str_replace("\235", '&#178;', $string);
772 $string = str_replace("\236", '&#183;', $string);
773 $string = str_replace("\237", '&#247;', $string);
774 $string = str_replace("\240", '&#9552;', $string);
775 $string = str_replace("\241", '&#9553;', $string);
776 $string = str_replace("\242", '&#9554;', $string);
777 $string = str_replace("\243", '&#1105;', $string);
778 $string = str_replace("\244", '&#9555;', $string);
779 $string = str_replace("\245", '&#9556;', $string);
780 $string = str_replace("\246", '&#9557;', $string);
781 $string = str_replace("\247", '&#9558;', $string);
782 $string = str_replace("\250", '&#9559;', $string);
783 $string = str_replace("\251", '&#9560;', $string);
784 $string = str_replace("\252", '&#9561;', $string);
785 $string = str_replace("\253", '&#9562;', $string);
786 $string = str_replace("\254", '&#9563;', $string);
787 $string = str_replace("\255", '&#9564;', $string);
788 $string = str_replace("\256", '&#9565;', $string);
789 $string = str_replace("\257", '&#9566;', $string);
790 $string = str_replace("\260", '&#9567;', $string);
791 $string = str_replace("\261", '&#9568;', $string);
792 $string = str_replace("\262", '&#9569;', $string);
793 $string = str_replace("\263", '&#1025;', $string);
794 $string = str_replace("\264", '&#9570;', $string);
795 $string = str_replace("\265", '&#9571;', $string);
796 $string = str_replace("\266", '&#9572;', $string);
797 $string = str_replace("\267", '&#9573;', $string);
798 $string = str_replace("\270", '&#9574;', $string);
799 $string = str_replace("\271", '&#9575;', $string);
800 $string = str_replace("\272", '&#9576;', $string);
801 $string = str_replace("\273", '&#9577;', $string);
802 $string = str_replace("\274", '&#9578;', $string);
803 $string = str_replace("\275", '&#9579;', $string);
804 $string = str_replace("\276", '&#9580;', $string);
805 $string = str_replace("\277", '&#169;', $string);
806 $string = str_replace("\300", '&#1102;', $string);
807 $string = str_replace("\301", '&#1072;', $string);
808 $string = str_replace("\302", '&#1073;', $string);
809 $string = str_replace("\303", '&#1094;', $string);
810 $string = str_replace("\304", '&#1076;', $string);
811 $string = str_replace("\305", '&#1077;', $string);
812 $string = str_replace("\306", '&#1092;', $string);
813 $string = str_replace("\307", '&#1075;', $string);
814 $string = str_replace("\310", '&#1093;', $string);
815 $string = str_replace("\311", '&#1080;', $string);
816 $string = str_replace("\312", '&#1081;', $string);
817 $string = str_replace("\313", '&#1082;', $string);
818 $string = str_replace("\314", '&#1083;', $string);
819 $string = str_replace("\315", '&#1084;', $string);
820 $string = str_replace("\316", '&#1085;', $string);
821 $string = str_replace("\317", '&#1086;', $string);
822 $string = str_replace("\320", '&#1087;', $string);
823 $string = str_replace("\321", '&#1103;', $string);
824 $string = str_replace("\322", '&#1088;', $string);
825 $string = str_replace("\323", '&#1089;', $string);
826 $string = str_replace("\324", '&#1090;', $string);
827 $string = str_replace("\325", '&#1091;', $string);
828 $string = str_replace("\326", '&#1078;', $string);
829 $string = str_replace("\327", '&#1074;', $string);
830 $string = str_replace("\330", '&#1100;', $string);
831 $string = str_replace("\331", '&#1099;', $string);
832 $string = str_replace("\332", '&#1079;', $string);
833 $string = str_replace("\333", '&#1096;', $string);
834 $string = str_replace("\334", '&#1101;', $string);
835 $string = str_replace("\335", '&#1097;', $string);
836 $string = str_replace("\336", '&#1095;', $string);
837 $string = str_replace("\337", '&#1098;', $string);
838 $string = str_replace("\340", '&#1070;', $string);
839 $string = str_replace("\341", '&#1040;', $string);
840 $string = str_replace("\342", '&#1041;', $string);
841 $string = str_replace("\343", '&#1062;', $string);
842 $string = str_replace("\344", '&#1044;', $string);
843 $string = str_replace("\345", '&#1045;', $string);
844 $string = str_replace("\346", '&#1060;', $string);
845 $string = str_replace("\347", '&#1043;', $string);
846 $string = str_replace("\350", '&#1061;', $string);
847 $string = str_replace("\351", '&#1048;', $string);
848 $string = str_replace("\352", '&#1049;', $string);
849 $string = str_replace("\353", '&#1050;', $string);
850 $string = str_replace("\354", '&#1051;', $string);
851 $string = str_replace("\355", '&#1052;', $string);
852 $string = str_replace("\356", '&#1053;', $string);
853 $string = str_replace("\357", '&#1054;', $string);
854 $string = str_replace("\360", '&#1055;', $string);
855 $string = str_replace("\361", '&#1071;', $string);
856 $string = str_replace("\362", '&#1056;', $string);
857 $string = str_replace("\363", '&#1057;', $string);
858 $string = str_replace("\364", '&#1058;', $string);
859 $string = str_replace("\365", '&#1059;', $string);
860 $string = str_replace("\366", '&#1046;', $string);
861 $string = str_replace("\367", '&#1042;', $string);
862 $string = str_replace("\370", '&#1068;', $string);
863 $string = str_replace("\371", '&#1067;', $string);
864 $string = str_replace("\372", '&#1047;', $string);
865 $string = str_replace("\373", '&#1064;', $string);
866 $string = str_replace("\374", '&#1069;', $string);
867 $string = str_replace("\375", '&#1065;', $string);
868 $string = str_replace("\376", '&#1063;', $string);
869 $string = str_replace("\377", '&#1066;', $string);
870
871 return $string;
872}
873
c37a12f8 874/*
3ab35042 875 windows-1254 (Turks)
c37a12f8 876 */
3ab35042 877function charset_decode_windows_1254 ($string) {
94965562 878 global $default_charset;
c37a12f8 879
3ab35042 880 if (strtolower($default_charset) == 'windows-1254')
c37a12f8 881 return $string;
c37a12f8 882
883 /* Only do the slow convert if there are 8-bit characters */
884 if (! ereg("[\200-\377]", $string))
885 return $string;
886
887 // Euro sign 128 -> 8364
888 $string = str_replace("\200", '&#8364;', $string);
889 // Single low-9 quotation mark 130 -> 8218
890 $string = str_replace("\202", '&#8218;', $string);
3ab35042 891 // latin small letter f with hook 131 -> 402
892 $string = str_replace("\203", '&#402;', $string);
c37a12f8 893 // Double low-9 quotation mark 132 -> 8222
894 $string = str_replace("\204", '&#8222;', $string);
895 // horizontal ellipsis 133 -> 8230
896 $string = str_replace("\205", '&#8230;', $string);
897 // dagger 134 -> 8224
898 $string = str_replace("\206", '&#8224;', $string);
899 // double dagger 135 -> 8225
900 $string = str_replace("\207", '&#8225;', $string);
3ab35042 901 // modifier letter circumflex accent 136->710
902 $string = str_replace("\210", '&#710;', $string);
c37a12f8 903 // per mille sign 137 -> 8240
904 $string = str_replace("\211", '&#8240;', $string);
3ab35042 905 // latin capital letter s with caron 138 -> 352
906 $string = str_replace("\212", '&#352;', $string);
c37a12f8 907 // single left-pointing angle quotation mark 139 -> 8249
908 $string = str_replace("\213", '&#8249;', $string);
3ab35042 909 // latin capital ligature oe 140 -> 338
910 $string = str_replace("\214", '&#338;', $string);
c37a12f8 911 // left single quotation mark 145 -> 8216
912 $string = str_replace("\221", '&#8216;', $string);
913 // right single quotation mark 146 -> 8217
914 $string = str_replace("\222", '&#8217;', $string);
915 // left double quotation mark 147 -> 8220
916 $string = str_replace("\223", '&#8220;', $string);
917 // right double quotation mark 148 -> 8221
918 $string = str_replace("\224", '&#8221;', $string);
919 // bullet 149 -> 8226
920 $string = str_replace("\225", '&#8226;', $string);
921 // en dash 150 -> 8211
922 $string = str_replace("\226", '&#8211;', $string);
923 // em dash 151 -> 8212
924 $string = str_replace("\227", '&#8212;', $string);
3ab35042 925 // small tilde 152 -> 732
926 $string = str_replace("\230", '&#732;', $string);
c37a12f8 927 // trade mark sign 153 -> 8482
928 $string = str_replace("\231", '&#8482;', $string);
3ab35042 929 // latin small letter s with caron 154 -> 353
930 $string = str_replace("\232", '&#353;', $string);
c37a12f8 931 // single right-pointing angle quotation mark 155 -> 8250
932 $string = str_replace("\233", '&#8250;', $string);
3ab35042 933 // latin small ligature oe 156 -> 339
934 $string = str_replace("\234", '&#339;', $string);
935 // latin capital letter y with diaresis 159->376
936 $string = str_replace("\237", '&#376;', $string);
937 // latin capital letter g with breve 208->286
938 $string = str_replace("\320", '&#286;', $string);
939 // latin capital letter i with dot above 221->304
940 $string = str_replace("\335", '&#304;', $string);
941 // latin capital letter s with cedilla 222->350
942 $string = str_replace("\336", '&#350;', $string);
943 // latin small letter g with breve 240->287
944 $string = str_replace("\360", '&#287;', $string);
945 // latin small letter dotless i 253->305
946 $string = str_replace("\375", '&#305;', $string);
947 // latin small letter s with cedilla 254->351
948 $string = str_replace("\376", '&#351;', $string);
949
950 // Rest of charset is like iso-8859-1
951 return (charset_decode_iso_8859_1($string));
952}
953
954/*
955 windows-1253 (Greek)
956 */
957function charset_decode_windows_1253 ($string) {
94965562 958 global $default_charset;
3ab35042 959
94965562 960 if (strtolower($default_charset) == 'windows-1253')
3ab35042 961 return $string;
962
963 /* Only do the slow convert if there are 8-bit characters */
964 if (! ereg("[\200-\377]", $string))
965 return $string;
966
967 $string = str_replace("\200", '&#8364;', $string);
968 $string = str_replace("\202", '&#8218;', $string);
969 $string = str_replace("\203", '&#402;', $string);
970 $string = str_replace("\204", '&#8222;', $string);
971 $string = str_replace("\205", '&#8230;', $string);
972 $string = str_replace("\206", '&#8224;', $string);
973 $string = str_replace("\207", '&#8225;', $string);
974 $string = str_replace("\211", '&#8240;', $string);
975 $string = str_replace("\213", '&#8249;', $string);
976 $string = str_replace("\221", '&#8216;', $string);
977 $string = str_replace("\222", '&#8217;', $string);
978 $string = str_replace("\223", '&#8220;', $string);
979 $string = str_replace("\224", '&#8221;', $string);
980 $string = str_replace("\225", '&#8226;', $string);
981 $string = str_replace("\226", '&#8211;', $string);
982 $string = str_replace("\227", '&#8212;', $string);
983 $string = str_replace("\231", '&#8482;', $string);
984 $string = str_replace("\233", '&#8250;', $string);
985 $string = str_replace("\241", '&#901;', $string);
986 $string = str_replace("\242", '&#902;', $string);
987 $string = str_replace ("\257", '&#8213;', $string);
988 $string = str_replace("\264", '&#900;', $string);
989 $string = str_replace("\270", '&#904;', $string);
990 $string = str_replace ("\271", '&#905;', $string);
991 $string = str_replace ("\272", '&#906;', $string);
992 $string = str_replace ("\274", '&#908;', $string);
993 // cycle for 190-254 symbols
994 $string = preg_replace("/([\274-\376])/e","'&#' . (ord('\\1')+720);",$string);
995
996 // Rest of charset is like iso-8859-1
997 return (charset_decode_iso_8859_1($string));
998}
999
1000
1001/*
1002 windows-1257 (BaltRim)
1003 */
1004function charset_decode_windows_1257 ($string) {
94965562 1005 global $default_charset;
3ab35042 1006
1007 if (strtolower($default_charset) == 'windows-1257')
1008 return $string;
3ab35042 1009
1010 /* Only do the slow convert if there are 8-bit characters */
1011 if (! ereg("[\200-\377]", $string))
1012 return $string;
1013
1014 $string = str_replace("\200", '&#8364;', $string);
1015 $string = str_replace("\202", '&#8218;', $string);
1016 $string = str_replace("\204", '&#8222;', $string);
1017 $string = str_replace("\205", '&#8230;', $string);
1018 $string = str_replace("\206", '&#8224;', $string);
1019 $string = str_replace("\207", '&#8225;', $string);
1020 $string = str_replace("\211", '&#8240;', $string);
1021 $string = str_replace("\213", '&#8249;', $string);
1022 $string = str_replace("\215", '&#168;', $string);
1023 $string = str_replace("\216", '&#711;', $string);
1024 $string = str_replace("\217", '&#184;', $string);
1025 $string = str_replace("\221", '&#8216;', $string);
1026 $string = str_replace("\222", '&#8217;', $string);
1027 $string = str_replace("\223", '&#8220;', $string);
1028 $string = str_replace("\224", '&#8221;', $string);
1029 $string = str_replace("\225", '&#8226;', $string);
1030 $string = str_replace("\226", '&#8211;', $string);
1031 $string = str_replace("\227", '&#8212;', $string);
1032 $string = str_replace("\231", '&#8482;', $string);
1033 $string = str_replace("\233", '&#8250;', $string);
c37a12f8 1034 $string = str_replace("\235", '&#175;', $string);
c37a12f8 1035 $string = str_replace("\236", '&#731;', $string);
c37a12f8 1036 $string = str_replace ("\250", '&#216;', $string);
c37a12f8 1037 $string = str_replace ("\252", '&#342;', $string);
c37a12f8 1038 $string = str_replace ("\257", '&#198;', $string);
c37a12f8 1039 $string = str_replace ("\270", '&#248;', $string);
c37a12f8 1040 $string = str_replace ("\272", '&#343;', $string);
c37a12f8 1041 $string = str_replace ("\277", '&#230;', $string);
c37a12f8 1042 $string = str_replace ("\300", '&#260;', $string);
c37a12f8 1043 $string = str_replace ("\301", '&#302;', $string);
c37a12f8 1044 $string = str_replace ("\302", '&#256;', $string);
c37a12f8 1045 $string = str_replace ("\303", '&#262;', $string);
c37a12f8 1046 $string = str_replace ("\306", '&#280;', $string);
c37a12f8 1047 $string = str_replace ("\307", '&#274;', $string);
c37a12f8 1048 $string = str_replace ("\310", '&#268;', $string);
c37a12f8 1049 $string = str_replace ("\312", '&#377;', $string);
c37a12f8 1050 $string = str_replace ("\313", '&#278;', $string);
c37a12f8 1051 $string = str_replace ("\314", '&#290;', $string);
c37a12f8 1052 $string = str_replace ("\315", '&#310;', $string);
c37a12f8 1053 $string = str_replace ("\316", '&#298;', $string);
c37a12f8 1054 $string = str_replace ("\317", '&#315;', $string);
c37a12f8 1055 $string = str_replace ("\320", '&#352;', $string);
c37a12f8 1056 $string = str_replace ("\321", '&#323;', $string);
c37a12f8 1057 $string = str_replace ("\322", '&#325;', $string);
c37a12f8 1058 $string = str_replace ("\324", '&#332;', $string);
c37a12f8 1059 $string = str_replace ("\330", '&#370;', $string);
c37a12f8 1060 $string = str_replace ("\331", '&#321;', $string);
c37a12f8 1061 $string = str_replace ("\332", '&#340;', $string);
c37a12f8 1062 $string = str_replace ("\333", '&#362;', $string);
c37a12f8 1063 $string = str_replace ("\335", '&#379;', $string);
c37a12f8 1064 $string = str_replace ("\336", '&#381;', $string);
c37a12f8 1065 $string = str_replace ("\340", '&#261;', $string);
c37a12f8 1066 $string = str_replace ("\341", '&#303;', $string);
c37a12f8 1067 $string = str_replace ("\342", '&#257;', $string);
c37a12f8 1068 $string = str_replace ("\343", '&#263;', $string);
c37a12f8 1069 $string = str_replace ("\346", '&#281;', $string);
c37a12f8 1070 $string = str_replace ("\347", '&#275;', $string);
c37a12f8 1071 $string = str_replace ("\350", '&#269;', $string);
c37a12f8 1072 $string = str_replace ("\352", '&#378;', $string);
c37a12f8 1073 $string = str_replace ("\353", '&#279;', $string);
c37a12f8 1074 $string = str_replace ("\354", '&#291;', $string);
c37a12f8 1075 $string = str_replace ("\355", '&#311;', $string);
c37a12f8 1076 $string = str_replace ("\356", '&#299;', $string);
c37a12f8 1077 $string = str_replace ("\357", '&#316;', $string);
c37a12f8 1078 $string = str_replace ("\360", '&#353;', $string);
c37a12f8 1079 $string = str_replace ("\361", '&#324;', $string);
c37a12f8 1080 $string = str_replace ("\362", '&#326;', $string);
c37a12f8 1081 $string = str_replace ("\364", '&#333;', $string);
c37a12f8 1082 $string = str_replace ("\370", '&#371;', $string);
c37a12f8 1083 $string = str_replace ("\371", '&#322;', $string);
c37a12f8 1084 $string = str_replace ("\372", '&#347;', $string);
c37a12f8 1085 $string = str_replace ("\373", '&#363;', $string);
c37a12f8 1086 $string = str_replace ("\375", '&#380;', $string);
c37a12f8 1087 $string = str_replace ("\376", '&#382;', $string);
c37a12f8 1088 $string = str_replace ("\377", '&#729;', $string);
1089
1090 // Rest of charset is like iso-8859-1
1091 return (charset_decode_iso_8859_1($string));
1092}
1093
ecd877a8 1094/* windows-1251 is Microsoft Cyrillic encoding */
1095function charset_decode_windows_1251 ($string) {
1096 // Convert to KOI8-R, then return this decoded.
1097 $string = convert_cyr_string($string, 'w', 'k');
1098 return charset_decode_koi8r($string);
1099}
1100
1101
6fbd125b 1102
a2a7852b 1103/*
1104 * Set up the language to be output
1105 * if $do_search is true, then scan the browser information
1106 * for a possible language that we know
1107 */
1108function set_up_language($sm_language, $do_search = false) {
1109
1110 static $SetupAlready = 0;
9eb0fbd4 1111 global $use_gettext, $languages,
a2a7852b 1112 $squirrelmail_language, $squirrelmail_default_language,
1113 $sm_notAlias;
1114
1115 if ($SetupAlready) {
1116 return;
1117 }
a65846a7 1118
5c920668 1119 $SetupAlready = TRUE;
a2a7852b 1120
9eb0fbd4 1121 if ($do_search && ! $sm_language && isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
1122 $sm_language = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
a2a7852b 1123 }
66d7950f 1124
a2a7852b 1125 if (!$sm_language && isset($squirrelmail_default_language)) {
1126 $squirrelmail_language = $squirrelmail_default_language;
66d7950f 1127 $sm_language = $squirrelmail_default_language;
a2a7852b 1128 }
1129 $sm_notAlias = $sm_language;
1130 while (isset($languages[$sm_notAlias]['ALIAS'])) {
1131 $sm_notAlias = $languages[$sm_notAlias]['ALIAS'];
1132 }
1133
88cb1b4d 1134 if ( isset($sm_language) &&
5c920668 1135 $use_gettext &&
1136 $sm_language != '' &&
1137 isset($languages[$sm_notAlias]['CHARSET']) ) {
a65846a7 1138 bindtextdomain( 'squirrelmail', SM_PATH . 'locale/' );
88cb1b4d 1139 textdomain( 'squirrelmail' );
1140 if ( !ini_get('safe_mode') &&
5c920668 1141 getenv( 'LC_ALL' ) != $sm_notAlias ) {
1142 putenv( "LC_ALL=$sm_notAlias" );
1143 putenv( "LANG=$sm_notAlias" );
1144 putenv( "LANGUAGE=$sm_notAlias" );
a2a7852b 1145 }
88cb1b4d 1146 setlocale(LC_ALL, $sm_notAlias);
5c920668 1147 $squirrelmail_language = $sm_notAlias;
538f1ab7 1148 if ($squirrelmail_language == 'ja_JP' && function_exists('mb_detect_encoding') ) {
b05c8961 1149 header ('Content-Type: text/html; charset=EUC-JP');
1150 if (!function_exists('mb_internal_encoding')) {
1151 echo _("You need to have php4 installed with the multibyte string function enabled (using configure option --with-mbstring).");
1152 }
1153 mb_internal_encoding('EUC-JP');
1154 mb_http_output('pass');
1155 } else {
5c920668 1156 header( 'Content-Type: text/html; charset=' . $languages[$sm_notAlias]['CHARSET'] );
a2a7852b 1157 }
1158}
b05c8961 1159}
a2a7852b 1160
1161function set_my_charset(){
1162
1163 /*
1164 * There can be a $default_charset setting in the
1165 * config.php file, but the user may have a different language
1166 * selected for a user interface. This function checks the
1167 * language selected by the user and tags the outgoing messages
1168 * with the appropriate charset corresponding to the language
1169 * selection. This is "more right" (tm), than just stamping the
1170 * message blindly with the system-wide $default_charset.
1171 */
94965562 1172 global $data_dir, $username, $default_charset, $languages, $squirrelmail_default_language;
88cb1b4d 1173
a2a7852b 1174 $my_language = getPref($data_dir, $username, 'language');
5c920668 1175 if (!$my_language) {
94965562 1176 $my_language = $squirrelmail_default_language ;
5c920668 1177 }
a2a7852b 1178 while (isset($languages[$my_language]['ALIAS'])) {
f7e8861e 1179 $my_language = $languages[$my_language]['ALIAS'];
a2a7852b 1180 }
5c920668 1181 $my_charset = $languages[$my_language]['CHARSET'];
a2a7852b 1182 if ($my_charset) {
1183 $default_charset = $my_charset;
1184 }
1185}
1186
a2a7852b 1187/* ------------------------------ main --------------------------- */
1188
5c920668 1189global $squirrelmail_language, $languages, $use_gettext;
1190
a2a7852b 1191if (! isset($squirrelmail_language)) {
1192 $squirrelmail_language = '';
1193}
1194
1195/* This array specifies the available languages. */
5c920668 1196
1197// The glibc locale is ca_ES.
1198
1199$languages['ca_ES']['NAME'] = 'Catalan';
1200$languages['ca_ES']['CHARSET'] = 'iso-8859-1';
1201$languages['ca']['ALIAS'] = 'ca_ES';
1202
a2a7852b 1203$languages['cs_CZ']['NAME'] = 'Czech';
1204$languages['cs_CZ']['CHARSET'] = 'iso-8859-2';
1205$languages['cs']['ALIAS'] = 'cs_CZ';
a2a7852b 1206
5c920668 1207// Danish locale is da_DK.
1208
1209$languages['da_DK']['NAME'] = 'Danish';
1210$languages['da_DK']['CHARSET'] = 'iso-8859-1';
1211$languages['da']['ALIAS'] = 'da_DK';
1212
1213$languages['de_DE']['NAME'] = 'Deutsch';
1214$languages['de_DE']['CHARSET'] = 'iso-8859-1';
1215$languages['de']['ALIAS'] = 'de_DE';
a2a7852b 1216
5c920668 1217// There is no en_EN! There is en_US, en_BR, en_AU, and so forth,
1218// but who cares about !US, right? Right? :)
a2a7852b 1219
3ab35042 1220$languages['el_GR']['NAME'] = 'Greek';
1221$languages['el_GR']['CHARSET'] = 'iso-8859-7';
1222$languages['el']['ALIAS'] = 'el_GR';
1223
5c920668 1224$languages['en_US']['NAME'] = 'English';
1225$languages['en_US']['CHARSET'] = 'iso-8859-1';
1226$languages['en']['ALIAS'] = 'en_US';
a2a7852b 1227
5c920668 1228$languages['es_ES']['NAME'] = 'Spanish';
1229$languages['es_ES']['CHARSET'] = 'iso-8859-1';
1230$languages['es']['ALIAS'] = 'es_ES';
a2a7852b 1231
5c920668 1232$languages['et_EE']['NAME'] = 'Estonian';
1233$languages['et_EE']['CHARSET'] = 'iso-8859-15';
1234$languages['et']['ALIAS'] = 'et_EE';
a2a7852b 1235
5c920668 1236$languages['fi_FI']['NAME'] = 'Finnish';
1237$languages['fi_FI']['CHARSET'] = 'iso-8859-1';
1238$languages['fi']['ALIAS'] = 'fi_FI';
a2a7852b 1239
5c920668 1240$languages['fr_FR']['NAME'] = 'French';
1241$languages['fr_FR']['CHARSET'] = 'iso-8859-1';
1242$languages['fr']['ALIAS'] = 'fr_FR';
a2a7852b 1243
5c920668 1244$languages['hr_HR']['NAME'] = 'Croatian';
1245$languages['hr_HR']['CHARSET'] = 'iso-8859-2';
1246$languages['hr']['ALIAS'] = 'hr_HR';
a2a7852b 1247
5c920668 1248$languages['hu_HU']['NAME'] = 'Hungarian';
1249$languages['hu_HU']['CHARSET'] = 'iso-8859-2';
1250$languages['hu']['ALIAS'] = 'hu_HU';
a2a7852b 1251
5c920668 1252$languages['id_ID']['NAME'] = 'Indonesian';
1253$languages['id_ID']['CHARSET'] = 'iso-8859-1';
1254$languages['id']['ALIAS'] = 'id_ID';
a2a7852b 1255
5c920668 1256$languages['is_IS']['NAME'] = 'Icelandic';
1257$languages['is_IS']['CHARSET'] = 'iso-8859-1';
1258$languages['is']['ALIAS'] = 'is_IS';
a2a7852b 1259
5c920668 1260$languages['it_IT']['NAME'] = 'Italian';
1261$languages['it_IT']['CHARSET'] = 'iso-8859-1';
1262$languages['it']['ALIAS'] = 'it_IT';
a2a7852b 1263
b05c8961 1264$languages['ja_JP']['NAME'] = 'Japanese';
1265$languages['ja_JP']['CHARSET'] = 'iso-2022-jp';
6fbd125b 1266$languages['ja_JP']['XTRA_CODE'] = 'japanese_charset_xtra';
b05c8961 1267$languages['ja']['ALIAS'] = 'ja_JP';
1268
5c920668 1269$languages['ko_KR']['NAME'] = 'Korean';
1270$languages['ko_KR']['CHARSET'] = 'euc-KR';
1d33e35e 1271$languages['ko_KR']['XTRA_CODE'] = 'korean_charset_xtra';
5c920668 1272$languages['ko']['ALIAS'] = 'ko_KR';
a2a7852b 1273
5c920668 1274$languages['nl_NL']['NAME'] = 'Dutch';
1275$languages['nl_NL']['CHARSET'] = 'iso-8859-1';
1276$languages['nl']['ALIAS'] = 'nl_NL';
a2a7852b 1277
5c920668 1278$languages['no_NO']['NAME'] = 'Norwegian (Bokm&aring;l)';
1279$languages['no_NO']['CHARSET'] = 'iso-8859-1';
1280$languages['no']['ALIAS'] = 'no_NO';
b8b4ac4a 1281$languages['nn_NO']['NAME'] = 'Norwegian (Nynorsk)';
1282$languages['nn_NO']['CHARSET'] = 'iso-8859-1';
a2a7852b 1283
5c920668 1284$languages['pl_PL']['NAME'] = 'Polish';
1285$languages['pl_PL']['CHARSET'] = 'iso-8859-2';
1286$languages['pl']['ALIAS'] = 'pl_PL';
a2a7852b 1287
5c920668 1288$languages['pt_PT']['NAME'] = 'Portuguese (Portugal)';
1289$languages['pt_PT']['CHARSET'] = 'iso-8859-1';
a2a7852b 1290$languages['pt_BR']['NAME'] = 'Portuguese (Brazil)';
1291$languages['pt_BR']['CHARSET'] = 'iso-8859-1';
5c920668 1292$languages['pt']['ALIAS'] = 'pt_PT';
a2a7852b 1293
5c920668 1294$languages['ru_RU']['NAME'] = 'Russian';
1295$languages['ru_RU']['CHARSET'] = 'koi8-r';
1296$languages['ru']['ALIAS'] = 'ru_RU';
a2a7852b 1297
dbec0d67 1298$languages['sr_YU']['NAME'] = 'Serbian';
1299$languages['sr_YU']['CHARSET'] = 'iso-8859-2';
1300$languages['sr']['ALIAS'] = 'sr_YU';
a2a7852b 1301
5c920668 1302$languages['sv_SE']['NAME'] = 'Swedish';
1303$languages['sv_SE']['CHARSET'] = 'iso-8859-1';
1304$languages['sv']['ALIAS'] = 'sv_SE';
a2a7852b 1305
5c920668 1306$languages['tr_TR']['NAME'] = 'Turkish';
1307$languages['tr_TR']['CHARSET'] = 'iso-8859-9';
1308$languages['tr']['ALIAS'] = 'tr_TR';
a2a7852b 1309
b62c1863 1310$languages['zh_TW']['NAME'] = 'Chinese Trad';
5c920668 1311$languages['zh_TW']['CHARSET'] = 'big5';
1312$languages['tw']['ALIAS'] = 'zh_TW';
a2a7852b 1313
b62c1863 1314$languages['zh_CN']['NAME'] = 'Chinese Simp';
1315$languages['zh_CN']['CHARSET'] = 'gb2312';
1316$languages['cn']['ALIAS'] = 'zh_CN';
8ba79a3b 1317
a2a7852b 1318$languages['sk_SK']['NAME'] = 'Slovak';
1319$languages['sk_SK']['CHARSET'] = 'iso-8859-2';
1320$languages['sk']['ALIAS'] = 'sk_SK';
1321
5c920668 1322$languages['ro_RO']['NAME'] = 'Romanian';
1323$languages['ro_RO']['CHARSET'] = 'iso-8859-2';
1324$languages['ro']['ALIAS'] = 'ro_RO';
a2a7852b 1325
5c920668 1326$languages['th_TH']['NAME'] = 'Thai';
1327$languages['th_TH']['CHARSET'] = 'tis-620';
1328$languages['th']['ALIAS'] = 'th_TH';
a2a7852b 1329
5c920668 1330$languages['lt_LT']['NAME'] = 'Lithuanian';
923229f2 1331$languages['lt_LT']['CHARSET'] = 'windows-1257';
5c920668 1332$languages['lt']['ALIAS'] = 'lt_LT';
a2a7852b 1333
5c920668 1334$languages['sl_SI']['NAME'] = 'Slovenian';
1335$languages['sl_SI']['CHARSET'] = 'iso-8859-2';
1336$languages['sl']['ALIAS'] = 'sl_SI';
a2a7852b 1337
5c920668 1338$languages['bg_BG']['NAME'] = 'Bulgarian';
1339$languages['bg_BG']['CHARSET'] = 'windows-1251';
1340$languages['bg']['ALIAS'] = 'bg_BG';
a2a7852b 1341
93ea3e70 1342$languages['uk_UA']['NAME'] = 'Ukrainian';
1343$languages['uk_UA']['CHARSET'] = 'koi8-u';
1344$languages['uk']['ALIAS'] = 'uk_UA';
1345
d3b57948 1346// Right to left languages
1347
1348$languages['he_HE']['NAME'] = 'Hebrew';
1349$languages['he_HE']['CHARSET'] = 'windows-1255';
1350$languages['he_HE']['DIR'] = 'rtl';
1351$languages['he']['ALIAS'] = 'he_HE';
1352
1353
5c920668 1354/* Detect whether gettext is installed. */
a2a7852b 1355$gettext_flags = 0;
1356if (function_exists('_')) {
1357 $gettext_flags += 1;
1358}
1359if (function_exists('bindtextdomain')) {
1360 $gettext_flags += 2;
1361}
1362if (function_exists('textdomain')) {
1363 $gettext_flags += 4;
1364}
1365
5c920668 1366/* If gettext is fully loaded, cool */
a2a7852b 1367if ($gettext_flags == 7) {
1368 $use_gettext = true;
1369}
5c920668 1370/* If we can fake gettext, try that */
a2a7852b 1371elseif ($gettext_flags == 0) {
1372 $use_gettext = true;
e7ab8c9d 1373 include_once(SM_PATH . 'functions/gettext.php');
a2a7852b 1374} else {
5c920668 1375 /* Uh-ho. A weird install */
a2a7852b 1376 if (! $gettext_flags & 1) {
1377 function _($str) {
1378 return $str;
1379 }
1380 }
1381 if (! $gettext_flags & 2) {
1382 function bindtextdomain() {
1383 return;
1384 }
1385 }
1386 if (! $gettext_flags & 4) {
1387 function textdomain() {
1388 return;
1389 }
1390 }
1391}
1392
3ab35042 1393function charset_decode_utf8 ($string) {
1394/*
1395 Every decoded character consists of n bytes. First byte is octal
1396 300-375, other bytes - always octals 200-277.
1397
1398 \a\b characters are decoded to html code octdec(a-300)*64 + octdec(b-200)
1399 \a\b\c characters are decoded to html code octdec(a-340)*64*64 + octdec(b-200)*64 + octdec(c-200)
1400
1401 decoding cycle is unfinished. please test and report problems to tokul@users.sourceforge.net
1402*/
1403 global $default_charset, $languages, $sm_notAlias;
1404
1405 if (strtolower($default_charset) == 'utf-8')
1406 return $string;
1407 if (strtolower($languages[$sm_notAlias]['CHARSET']) == 'utf-8')
1408 return $string;
1409
1410 /* Only do the slow convert if there are 8-bit characters */
1411 if (! ereg("[\200-\377]", $string))
1412 return $string;
1413
1414 // decode three byte unicode characters
1415 $string = preg_replace("/([\340-\357])([\200-\277])([\200-\277])/e",
1416 "'&#'.((ord('\\1')-224)*4096+(ord('\\2')-128)*64+(ord('\\3')-128)).';'",
1417 $string);
1418
1419 // decode two byte unicode characters
1420 $string = preg_replace("/([\300-\337])([\200-\277])/e",
1421 "'&#'.((ord('\\1')-192)*64+(ord('\\2')-128)).';'",
1422 $string);
1423
1424 return $string;
1425}
1d33e35e 1426
1427/*
1428 * Japanese charset extra function
1429 *
1430 */
1431function japanese_charset_xtra() {
1432 $ret = func_get_arg(1); /* default return value */
1433 if (function_exists('mb_detect_encoding')) {
1434 switch (func_get_arg(0)) { /* action */
1435 case 'decode':
1436 $detect_encoding = mb_detect_encoding($ret);
1437 if ($detect_encoding == 'JIS' ||
1438 $detect_encoding == 'EUC-JP' ||
1439 $detect_encoding == 'SJIS') {
1440
1441 $ret = mb_convert_encoding($ret, 'EUC-JP', 'AUTO');
1442 }
1443 break;
1444 case 'encode':
1445 $detect_encoding = mb_detect_encoding($ret);
1446 if ($detect_encoding == 'JIS' ||
1447 $detect_encoding == 'EUC-JP' ||
1448 $detect_encoding == 'SJIS') {
1449
1450 $ret = mb_convert_encoding($ret, 'JIS', 'AUTO');
1451 }
1452 break;
1453 case 'strimwidth':
1454 $width = func_get_arg(2);
1455 $ret = mb_strimwidth($ret, 0, $width, '...');
1456 break;
1457 case 'encodeheader':
8ba05cbe 1458 $result = '';
1459 if (strlen($ret) > 0) {
1460 $tmpstr = mb_substr($ret, 0, 1);
1461 $prevcsize = strlen($tmpstr);
1462 for ($i = 1; $i < mb_strlen($ret); $i++) {
1463 $tmp = mb_substr($ret, $i, 1);
1464 if (strlen($tmp) == $prevcsize) {
1465 $tmpstr .= $tmp;
1466 } else {
1467 if ($prevcsize == 1) {
1468 $result .= $tmpstr;
1469 } else {
1470 $result .= mb_encode_mimeheader($tmpstr);
1471 }
1472 $tmpstr = $tmp;
1473 $prevcsize = strlen($tmp);
1474 }
1475 }
1476 if (strlen($tmpstr)) {
1477 if (strlen(mb_substr($tmpstr, 0, 1)) == 1)
1478 $result .= $tmpstr;
1479 else
1480 $result .= mb_encode_mimeheader($tmpstr);
1481 }
1482 }
1483 $ret = $result;
1484 //$ret = mb_encode_mimeheader($ret);
1d33e35e 1485 break;
1486 case 'decodeheader':
1487 $ret = str_replace("\t", "", $ret);
1488 if (eregi('=\\?([^?]+)\\?(q|b)\\?([^?]+)\\?=', $ret))
1489 $ret = mb_decode_mimeheader($ret);
1490 $ret = mb_convert_encoding($ret, 'EUC-JP', 'AUTO');
1491 break;
1492 case 'downloadfilename':
1493 $useragent = func_get_arg(2);
1494 if (strstr($useragent, 'Windows') !== false ||
1495 strstr($useragent, 'Mac_') !== false) {
1496 $ret = mb_convert_encoding($ret, 'SJIS', 'AUTO');
1497 } else {
1498 $ret = mb_convert_encoding($ret, 'EUC-JP', 'AUTO');
1499}
1500 break;
1501 }
1502 }
1503 return $ret;
1504}
1505
1506
1507/*
1508 * Korean charset extra function
1509 * Hangul(Korean Character) Attached File Name Fix.
1510 */
1511function korean_charset_xtra() {
1512
1513 $ret = func_get_arg(1); /* default return value */
1514 if (func_get_arg(0) == 'downloadfilename') { /* action */
1515 $ret = str_replace("\x0D\x0A", '', $ret); /* Hanmail's CR/LF Clear */
1516 for ($i=0;$i<strlen($ret);$i++) {
1517 if ($ret[$i] >= "\xA1" && $ret[$i] <= "\xFE") { /* 0xA1 - 0XFE are Valid */
1518 $i++;
1519 continue;
1520 } else if (($ret[$i] >= 'a' && $ret[$i] <= 'z') || /* From Original ereg_replace in download.php */
1521 ($ret[$i] >= 'A' && $ret[$i] <= 'Z') ||
1522 ($ret[$i] == '.') || ($ret[$i] == '-')) {
1523 continue;
1524 } else {
1525 $ret[$i] = '_';
1526 }
1527 }
1528
1529 }
1530
1531 return $ret;
1532}
1533
e7ab8c9d 1534?>