31721903dfa8b998f8190a2f02b5503b95020eb7
[squirrelmail.git] / functions / i18n.php
1 <?php
2
3 /**
4 * i18n.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 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
18 /* Decodes a string to the internal encoding from the given charset */
19 function charset_decode ($charset, $string) {
20 global $languages, $squirrelmail_language;
21
22 if (isset($languages[$squirrelmail_language]['XTRA_CODE']) &&
23 function_exists($languages[$squirrelmail_language]['XTRA_CODE'])) {
24 $string = $languages[$squirrelmail_language]['XTRA_CODE']('decode', $string);
25 }
26
27 /* All HTML special characters are 7 bit and can be replaced first */
28 $string = htmlspecialchars ($string);
29
30 $charset = strtolower($charset);
31
32 set_my_charset() ;
33
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);
39 } else if ($res[1] == '4') {
40 $ret = charset_decode_iso_8859_4 ($string);
41 } else if ($res[1] == '5') {
42 $ret = charset_decode_iso_8859_5 ($string);
43 } else if ($res[1] == '7') {
44 $ret = charset_decode_iso_8859_7 ($string);
45 } else if ($res[1] == '9') {
46 $ret = charset_decode_iso_8859_9 ($string);
47 } else if ($res[1] == '13') {
48 $ret = charset_decode_iso_8859_13 ($string);
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') {
59 $ret = charset_decode_windows_1251 ($string);
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);
64 } else if ($charset == 'windows-1257') {
65 $ret = charset_decode_windows_1257 ($string);
66 } else if ($charset == 'utf-8') {
67 $ret = charset_decode_utf8 ($string);
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 */
78 function charset_decode_iso_8859_1 ($string) {
79 global $default_charset;
80
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)) {
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);
211 }
212 }
213
214 return ($string);
215 }
216
217 /* iso-8859-2 is used for some eastern European languages */
218 function 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
424 /*
425 ISO/IEC 8859-4:1998 Latin Alphabet No. 4
426 */
427
428 function charset_decode_iso_8859_4 ($string) {
429 global $default_charset;
430
431 if (strtolower($default_charset) == 'iso-8859-4')
432 return $string;
433
434 /* Only do the slow convert if there are 8-bit characters */
435 if (! ereg("[\200-\377]", $string))
436 return $string;
437
438 $string = str_replace ("\241", '&#260;', $string);
439 $string = str_replace ("\242", '&#312;', $string);
440 $string = str_replace ("\243", '&#342;', $string);
441 $string = str_replace ("\245", '&#296;', $string);
442 $string = str_replace ("\246", '&#315;', $string);
443 $string = str_replace ("\251", '&#352;', $string);
444 $string = str_replace ("\252", '&#274;', $string);
445 $string = str_replace ("\253", '&#290;', $string);
446 $string = str_replace ("\254", '&#358;', $string);
447 $string = str_replace ("\256", '&#381;', $string);
448 $string = str_replace ("\261", '&#261;', $string);
449 $string = str_replace ("\262", '&#731;', $string);
450 $string = str_replace ("\263", '&#343;', $string);
451 $string = str_replace ("\265", '&#297;', $string);
452 $string = str_replace ("\266", '&#316;', $string);
453 $string = str_replace ("\267", '&#711;', $string);
454 $string = str_replace ("\271", '&#353;', $string);
455 $string = str_replace ("\272", '&#275;', $string);
456 $string = str_replace ("\273", '&#291;', $string);
457 $string = str_replace ("\274", '&#359;', $string);
458 $string = str_replace ("\275", '&#330;', $string);
459 $string = str_replace ("\276", '&#382;', $string);
460 $string = str_replace ("\277", '&#331;', $string);
461 $string = str_replace ("\300", '&#256;', $string);
462 $string = str_replace ("\307", '&#302;', $string);
463 $string = str_replace ("\310", '&#268;', $string);
464 $string = str_replace ("\312", '&#280;', $string);
465 $string = str_replace ("\314", '&#278;', $string);
466 $string = str_replace ("\317", '&#298;', $string);
467 $string = str_replace ("\320", '&#272;', $string);
468 $string = str_replace ("\321", '&#325;', $string);
469 $string = str_replace ("\322", '&#332;', $string);
470 $string = str_replace ("\323", '&#310;', $string);
471 $string = str_replace ("\331", '&#370;', $string);
472 $string = str_replace ("\335", '&#360;', $string);
473 $string = str_replace ("\336", '&#362;', $string);
474 $string = str_replace ("\340", '&#257;', $string);
475 $string = str_replace ("\347", '&#303;', $string);
476 $string = str_replace ("\350", '&#269;', $string);
477 $string = str_replace ("\352", '&#281;', $string);
478 $string = str_replace ("\354", '&#279;', $string);
479 $string = str_replace ("\357", '&#299;', $string);
480 $string = str_replace ("\360", '&#273;', $string);
481 $string = str_replace ("\361", '&#326;', $string);
482 $string = str_replace ("\362", '&#333;', $string);
483 $string = str_replace ("\363", '&#311;', $string);
484 $string = str_replace ("\371", '&#371;', $string);
485 $string = str_replace ("\375", '&#361;', $string);
486 $string = str_replace ("\376", '&#363;', $string);
487 $string = str_replace ("\377", '&#729;', $string);
488
489 // rest of charset is the same as ISO-8859-1
490 return (charset_decode_iso_8859_1($string));
491 }
492
493 /* iso-8859-7 is Greek. */
494 function 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 */
531 $string = preg_replace("/([\264-\266])/e","'&#' . (ord('\\1')+720);",$string);
532
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 */
540 $string = preg_replace("/([\270-\272])/e","'&#' . (ord('\\1')+720);",$string);
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 */
549 $string = preg_replace("/([\274-\376])/e","'&#'.(ord('\\1')+720);",$string);
550
551 return $string;
552 }
553
554 /*
555 ISOIEC 8859-9:1999 Latin Alphabet No. 5
556 */
557 function charset_decode_iso_8859_9 ($string) {
558 global $default_charset;
559
560 if (strtolower($default_charset) == 'iso-8859-9')
561 return $string;
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 /*
586 ISO/IEC 8859-13:1998 Latin Alphabet No. 7 (Baltic Rim)
587 */
588 function charset_decode_iso_8859_13 ($string) {
589 global $default_charset;
590
591 if (strtolower($default_charset) == 'iso-8859-13')
592 return $string;
593
594 /* Only do the slow convert if there are 8-bit characters */
595 if (! ereg("[\200-\377]", $string))
596 return $string;
597
598 $string = str_replace ("\241", '&#8221;', $string);
599 $string = str_replace ("\245", '&#8222;', $string);
600 $string = str_replace ("\250", '&#216;', $string);
601 $string = str_replace ("\252", '&#342;', $string);
602 $string = str_replace ("\257", '&#198;', $string);
603 $string = str_replace ("\264", '&#8220;', $string);
604 $string = str_replace ("\270", '&#248;', $string);
605 $string = str_replace ("\272", '&#343;', $string);
606 $string = str_replace ("\277", '&#230;', $string);
607 $string = str_replace ("\300", '&#260;', $string);
608 $string = str_replace ("\301", '&#302;', $string);
609 $string = str_replace ("\302", '&#256;', $string);
610 $string = str_replace ("\303", '&#262;', $string);
611 $string = str_replace ("\306", '&#280;', $string);
612 $string = str_replace ("\307", '&#274;', $string);
613 $string = str_replace ("\310", '&#268;', $string);
614 $string = str_replace ("\312", '&#377;', $string);
615 $string = str_replace ("\313", '&#278;', $string);
616 $string = str_replace ("\314", '&#290;', $string);
617 $string = str_replace ("\315", '&#310;', $string);
618 $string = str_replace ("\316", '&#298;', $string);
619 $string = str_replace ("\317", '&#315;', $string);
620 $string = str_replace ("\320", '&#352;', $string);
621 $string = str_replace ("\321", '&#323;', $string);
622 $string = str_replace ("\322", '&#325;', $string);
623 $string = str_replace ("\324", '&#332;', $string);
624 $string = str_replace ("\330", '&#370;', $string);
625 $string = str_replace ("\331", '&#321;', $string);
626 $string = str_replace ("\332", '&#346;', $string);
627 $string = str_replace ("\333", '&#362;', $string);
628 $string = str_replace ("\335", '&#379;', $string);
629 $string = str_replace ("\336", '&#381;', $string);
630 $string = str_replace ("\340", '&#261;', $string);
631 $string = str_replace ("\341", '&#303;', $string);
632 $string = str_replace ("\342", '&#257;', $string);
633 $string = str_replace ("\343", '&#263;', $string);
634 $string = str_replace ("\346", '&#281;', $string);
635 $string = str_replace ("\347", '&#275;', $string);
636 $string = str_replace ("\350", '&#269;', $string);
637 $string = str_replace ("\352", '&#378;', $string);
638 $string = str_replace ("\353", '&#279;', $string);
639 $string = str_replace ("\354", '&#291;', $string);
640 $string = str_replace ("\355", '&#311;', $string);
641 $string = str_replace ("\356", '&#299;', $string);
642 $string = str_replace ("\357", '&#316;', $string);
643 $string = str_replace ("\360", '&#353;', $string);
644 $string = str_replace ("\361", '&#324;', $string);
645 $string = str_replace ("\362", '&#326;', $string);
646 $string = str_replace ("\364", '&#333;', $string);
647 $string = str_replace ("\370", '&#371;', $string);
648 $string = str_replace ("\371", '&#322;', $string);
649 $string = str_replace ("\372", '&#347;', $string);
650 $string = str_replace ("\373", '&#363;', $string);
651 $string = str_replace ("\375", '&#380;', $string);
652 $string = str_replace ("\376", '&#382;', $string);
653 $string = str_replace ("\377", '&#8217;', $string);
654
655 // rest of charset is the same as ISO-8859-1
656 return (charset_decode_iso_8859_1($string));
657 }
658
659 /*
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 */
663 function charset_decode_iso_8859_15 ($string) {
664 // Euro sign
665 $string = str_replace ("\244", '&#8364;', $string);
666 // Latin capital letter S with caron
667 $string = str_replace ("\246", '&#352;', $string);
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
684 /* ISO-8859-5 is Cyrillic */
685 function 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 */
692 function charset_decode_iso_8859_default ($string) {
693 return (strtr($string, "\240\241\242\243\244\245\246\247".
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".
704 "\370\371\372\373\374\375\376\377",
705 "????????????????????????????????????????".
706 "????????????????????????????????????????".
707 "????????????????????????????????????????".
708 "????????"));
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 */
716 function 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 */
731 function 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
874 /*
875 windows-1254 (Turks)
876 */
877 function charset_decode_windows_1254 ($string) {
878 global $default_charset;
879
880 if (strtolower($default_charset) == 'windows-1254')
881 return $string;
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);
891 // latin small letter f with hook 131 -> 402
892 $string = str_replace("\203", '&#402;', $string);
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);
901 // modifier letter circumflex accent 136->710
902 $string = str_replace("\210", '&#710;', $string);
903 // per mille sign 137 -> 8240
904 $string = str_replace("\211", '&#8240;', $string);
905 // latin capital letter s with caron 138 -> 352
906 $string = str_replace("\212", '&#352;', $string);
907 // single left-pointing angle quotation mark 139 -> 8249
908 $string = str_replace("\213", '&#8249;', $string);
909 // latin capital ligature oe 140 -> 338
910 $string = str_replace("\214", '&#338;', $string);
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);
925 // small tilde 152 -> 732
926 $string = str_replace("\230", '&#732;', $string);
927 // trade mark sign 153 -> 8482
928 $string = str_replace("\231", '&#8482;', $string);
929 // latin small letter s with caron 154 -> 353
930 $string = str_replace("\232", '&#353;', $string);
931 // single right-pointing angle quotation mark 155 -> 8250
932 $string = str_replace("\233", '&#8250;', $string);
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 */
957 function charset_decode_windows_1253 ($string) {
958 global $default_charset;
959
960 if (strtolower($default_charset) == 'windows-1253')
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 */
1004 function charset_decode_windows_1257 ($string) {
1005 global $default_charset;
1006
1007 if (strtolower($default_charset) == 'windows-1257')
1008 return $string;
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);
1034 $string = str_replace("\235", '&#175;', $string);
1035 $string = str_replace("\236", '&#731;', $string);
1036 $string = str_replace ("\250", '&#216;', $string);
1037 $string = str_replace ("\252", '&#342;', $string);
1038 $string = str_replace ("\257", '&#198;', $string);
1039 $string = str_replace ("\270", '&#248;', $string);
1040 $string = str_replace ("\272", '&#343;', $string);
1041 $string = str_replace ("\277", '&#230;', $string);
1042 $string = str_replace ("\300", '&#260;', $string);
1043 $string = str_replace ("\301", '&#302;', $string);
1044 $string = str_replace ("\302", '&#256;', $string);
1045 $string = str_replace ("\303", '&#262;', $string);
1046 $string = str_replace ("\306", '&#280;', $string);
1047 $string = str_replace ("\307", '&#274;', $string);
1048 $string = str_replace ("\310", '&#268;', $string);
1049 $string = str_replace ("\312", '&#377;', $string);
1050 $string = str_replace ("\313", '&#278;', $string);
1051 $string = str_replace ("\314", '&#290;', $string);
1052 $string = str_replace ("\315", '&#310;', $string);
1053 $string = str_replace ("\316", '&#298;', $string);
1054 $string = str_replace ("\317", '&#315;', $string);
1055 $string = str_replace ("\320", '&#352;', $string);
1056 $string = str_replace ("\321", '&#323;', $string);
1057 $string = str_replace ("\322", '&#325;', $string);
1058 $string = str_replace ("\324", '&#332;', $string);
1059 $string = str_replace ("\330", '&#370;', $string);
1060 $string = str_replace ("\331", '&#321;', $string);
1061 $string = str_replace ("\332", '&#340;', $string);
1062 $string = str_replace ("\333", '&#362;', $string);
1063 $string = str_replace ("\335", '&#379;', $string);
1064 $string = str_replace ("\336", '&#381;', $string);
1065 $string = str_replace ("\340", '&#261;', $string);
1066 $string = str_replace ("\341", '&#303;', $string);
1067 $string = str_replace ("\342", '&#257;', $string);
1068 $string = str_replace ("\343", '&#263;', $string);
1069 $string = str_replace ("\346", '&#281;', $string);
1070 $string = str_replace ("\347", '&#275;', $string);
1071 $string = str_replace ("\350", '&#269;', $string);
1072 $string = str_replace ("\352", '&#378;', $string);
1073 $string = str_replace ("\353", '&#279;', $string);
1074 $string = str_replace ("\354", '&#291;', $string);
1075 $string = str_replace ("\355", '&#311;', $string);
1076 $string = str_replace ("\356", '&#299;', $string);
1077 $string = str_replace ("\357", '&#316;', $string);
1078 $string = str_replace ("\360", '&#353;', $string);
1079 $string = str_replace ("\361", '&#324;', $string);
1080 $string = str_replace ("\362", '&#326;', $string);
1081 $string = str_replace ("\364", '&#333;', $string);
1082 $string = str_replace ("\370", '&#371;', $string);
1083 $string = str_replace ("\371", '&#322;', $string);
1084 $string = str_replace ("\372", '&#347;', $string);
1085 $string = str_replace ("\373", '&#363;', $string);
1086 $string = str_replace ("\375", '&#380;', $string);
1087 $string = str_replace ("\376", '&#382;', $string);
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
1094 /* windows-1251 is Microsoft Cyrillic encoding */
1095 function 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
1102
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 */
1108 function set_up_language($sm_language, $do_search = false) {
1109
1110 static $SetupAlready = 0;
1111 global $use_gettext, $languages,
1112 $squirrelmail_language, $squirrelmail_default_language,
1113 $sm_notAlias;
1114
1115 if ($SetupAlready) {
1116 return;
1117 }
1118
1119 $SetupAlready = TRUE;
1120
1121 if ($do_search && ! $sm_language && isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
1122 $sm_language = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
1123 }
1124
1125 if (!$sm_language && isset($squirrelmail_default_language)) {
1126 $squirrelmail_language = $squirrelmail_default_language;
1127 $sm_language = $squirrelmail_default_language;
1128 }
1129 $sm_notAlias = $sm_language;
1130 while (isset($languages[$sm_notAlias]['ALIAS'])) {
1131 $sm_notAlias = $languages[$sm_notAlias]['ALIAS'];
1132 }
1133
1134 if ( isset($sm_language) &&
1135 $use_gettext &&
1136 $sm_language != '' &&
1137 isset($languages[$sm_notAlias]['CHARSET']) ) {
1138 bindtextdomain( 'squirrelmail', SM_PATH . 'locale/' );
1139 textdomain( 'squirrelmail' );
1140 if ( !ini_get('safe_mode') &&
1141 getenv( 'LC_ALL' ) != $sm_notAlias ) {
1142 putenv( "LC_ALL=$sm_notAlias" );
1143 putenv( "LANG=$sm_notAlias" );
1144 putenv( "LANGUAGE=$sm_notAlias" );
1145 }
1146 setlocale(LC_ALL, $sm_notAlias);
1147 $squirrelmail_language = $sm_notAlias;
1148 if ($squirrelmail_language == 'ja_JP' && function_exists('mb_detect_encoding') ) {
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 {
1156 header( 'Content-Type: text/html; charset=' . $languages[$sm_notAlias]['CHARSET'] );
1157 }
1158 }
1159 }
1160
1161 function 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 */
1172 global $data_dir, $username, $default_charset, $languages, $squirrelmail_default_language;
1173
1174 $my_language = getPref($data_dir, $username, 'language');
1175 if (!$my_language) {
1176 $my_language = $squirrelmail_default_language ;
1177 }
1178 while (isset($languages[$my_language]['ALIAS'])) {
1179 $my_language = $languages[$my_language]['ALIAS'];
1180 }
1181 $my_charset = $languages[$my_language]['CHARSET'];
1182 if ($my_charset) {
1183 $default_charset = $my_charset;
1184 }
1185 }
1186
1187 /* ------------------------------ main --------------------------- */
1188
1189 global $squirrelmail_language, $languages, $use_gettext;
1190
1191 if (! isset($squirrelmail_language)) {
1192 $squirrelmail_language = '';
1193 }
1194
1195 /* This array specifies the available languages. */
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
1203 $languages['cs_CZ']['NAME'] = 'Czech';
1204 $languages['cs_CZ']['CHARSET'] = 'iso-8859-2';
1205 $languages['cs']['ALIAS'] = 'cs_CZ';
1206
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';
1216
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? :)
1219
1220 $languages['el_GR']['NAME'] = 'Greek';
1221 $languages['el_GR']['CHARSET'] = 'iso-8859-7';
1222 $languages['el']['ALIAS'] = 'el_GR';
1223
1224 $languages['en_US']['NAME'] = 'English';
1225 $languages['en_US']['CHARSET'] = 'iso-8859-1';
1226 $languages['en']['ALIAS'] = 'en_US';
1227
1228 $languages['es_ES']['NAME'] = 'Spanish';
1229 $languages['es_ES']['CHARSET'] = 'iso-8859-1';
1230 $languages['es']['ALIAS'] = 'es_ES';
1231
1232 $languages['et_EE']['NAME'] = 'Estonian';
1233 $languages['et_EE']['CHARSET'] = 'iso-8859-15';
1234 $languages['et']['ALIAS'] = 'et_EE';
1235
1236 $languages['fi_FI']['NAME'] = 'Finnish';
1237 $languages['fi_FI']['CHARSET'] = 'iso-8859-1';
1238 $languages['fi']['ALIAS'] = 'fi_FI';
1239
1240 $languages['fr_FR']['NAME'] = 'French';
1241 $languages['fr_FR']['CHARSET'] = 'iso-8859-1';
1242 $languages['fr']['ALIAS'] = 'fr_FR';
1243
1244 $languages['hr_HR']['NAME'] = 'Croatian';
1245 $languages['hr_HR']['CHARSET'] = 'iso-8859-2';
1246 $languages['hr']['ALIAS'] = 'hr_HR';
1247
1248 $languages['hu_HU']['NAME'] = 'Hungarian';
1249 $languages['hu_HU']['CHARSET'] = 'iso-8859-2';
1250 $languages['hu']['ALIAS'] = 'hu_HU';
1251
1252 $languages['id_ID']['NAME'] = 'Indonesian';
1253 $languages['id_ID']['CHARSET'] = 'iso-8859-1';
1254 $languages['id']['ALIAS'] = 'id_ID';
1255
1256 $languages['is_IS']['NAME'] = 'Icelandic';
1257 $languages['is_IS']['CHARSET'] = 'iso-8859-1';
1258 $languages['is']['ALIAS'] = 'is_IS';
1259
1260 $languages['it_IT']['NAME'] = 'Italian';
1261 $languages['it_IT']['CHARSET'] = 'iso-8859-1';
1262 $languages['it']['ALIAS'] = 'it_IT';
1263
1264 $languages['ja_JP']['NAME'] = 'Japanese';
1265 $languages['ja_JP']['CHARSET'] = 'iso-2022-jp';
1266 $languages['ja_JP']['XTRA_CODE'] = 'japanese_charset_xtra';
1267 $languages['ja']['ALIAS'] = 'ja_JP';
1268
1269 $languages['ko_KR']['NAME'] = 'Korean';
1270 $languages['ko_KR']['CHARSET'] = 'euc-KR';
1271 $languages['ko_KR']['XTRA_CODE'] = 'korean_charset_xtra';
1272 $languages['ko']['ALIAS'] = 'ko_KR';
1273
1274 $languages['nl_NL']['NAME'] = 'Dutch';
1275 $languages['nl_NL']['CHARSET'] = 'iso-8859-1';
1276 $languages['nl']['ALIAS'] = 'nl_NL';
1277
1278 $languages['no_NO']['NAME'] = 'Norwegian (Bokm&aring;l)';
1279 $languages['no_NO']['CHARSET'] = 'iso-8859-1';
1280 $languages['no']['ALIAS'] = 'no_NO';
1281 $languages['nn_NO']['NAME'] = 'Norwegian (Nynorsk)';
1282 $languages['nn_NO']['CHARSET'] = 'iso-8859-1';
1283
1284 $languages['pl_PL']['NAME'] = 'Polish';
1285 $languages['pl_PL']['CHARSET'] = 'iso-8859-2';
1286 $languages['pl']['ALIAS'] = 'pl_PL';
1287
1288 $languages['pt_PT']['NAME'] = 'Portuguese (Portugal)';
1289 $languages['pt_PT']['CHARSET'] = 'iso-8859-1';
1290 $languages['pt_BR']['NAME'] = 'Portuguese (Brazil)';
1291 $languages['pt_BR']['CHARSET'] = 'iso-8859-1';
1292 $languages['pt']['ALIAS'] = 'pt_PT';
1293
1294 $languages['ru_RU']['NAME'] = 'Russian';
1295 $languages['ru_RU']['CHARSET'] = 'koi8-r';
1296 $languages['ru']['ALIAS'] = 'ru_RU';
1297
1298 $languages['sr_YU']['NAME'] = 'Serbian';
1299 $languages['sr_YU']['CHARSET'] = 'iso-8859-2';
1300 $languages['sr']['ALIAS'] = 'sr_YU';
1301
1302 $languages['sv_SE']['NAME'] = 'Swedish';
1303 $languages['sv_SE']['CHARSET'] = 'iso-8859-1';
1304 $languages['sv']['ALIAS'] = 'sv_SE';
1305
1306 $languages['tr_TR']['NAME'] = 'Turkish';
1307 $languages['tr_TR']['CHARSET'] = 'iso-8859-9';
1308 $languages['tr']['ALIAS'] = 'tr_TR';
1309
1310 $languages['zh_TW']['NAME'] = 'Chinese Trad';
1311 $languages['zh_TW']['CHARSET'] = 'big5';
1312 $languages['tw']['ALIAS'] = 'zh_TW';
1313
1314 $languages['zh_CN']['NAME'] = 'Chinese Simp';
1315 $languages['zh_CN']['CHARSET'] = 'gb2312';
1316 $languages['cn']['ALIAS'] = 'zh_CN';
1317
1318 $languages['sk_SK']['NAME'] = 'Slovak';
1319 $languages['sk_SK']['CHARSET'] = 'iso-8859-2';
1320 $languages['sk']['ALIAS'] = 'sk_SK';
1321
1322 $languages['ro_RO']['NAME'] = 'Romanian';
1323 $languages['ro_RO']['CHARSET'] = 'iso-8859-2';
1324 $languages['ro']['ALIAS'] = 'ro_RO';
1325
1326 $languages['th_TH']['NAME'] = 'Thai';
1327 $languages['th_TH']['CHARSET'] = 'tis-620';
1328 $languages['th']['ALIAS'] = 'th_TH';
1329
1330 $languages['lt_LT']['NAME'] = 'Lithuanian';
1331 $languages['lt_LT']['CHARSET'] = 'windows-1257';
1332 $languages['lt']['ALIAS'] = 'lt_LT';
1333
1334 $languages['sl_SI']['NAME'] = 'Slovenian';
1335 $languages['sl_SI']['CHARSET'] = 'iso-8859-2';
1336 $languages['sl']['ALIAS'] = 'sl_SI';
1337
1338 $languages['bg_BG']['NAME'] = 'Bulgarian';
1339 $languages['bg_BG']['CHARSET'] = 'windows-1251';
1340 $languages['bg']['ALIAS'] = 'bg_BG';
1341
1342 $languages['uk_UA']['NAME'] = 'Ukrainian';
1343 $languages['uk_UA']['CHARSET'] = 'koi8-u';
1344 $languages['uk']['ALIAS'] = 'uk_UA';
1345
1346 // Right to left languages
1347
1348 $languages['ar']['NAME'] = 'Arabic';
1349 $languages['ar']['CHARSET'] = 'windows-1256';
1350 $languages['ar']['DIR'] = 'rtl';
1351
1352 $languages['he_IL']['NAME'] = 'Hebrew';
1353 $languages['he_IL']['CHARSET'] = 'windows-1255';
1354 $languages['he_IL']['DIR'] = 'rtl';
1355 $languages['he']['ALIAS'] = 'he_IL';
1356
1357
1358 /* Detect whether gettext is installed. */
1359 $gettext_flags = 0;
1360 if (function_exists('_')) {
1361 $gettext_flags += 1;
1362 }
1363 if (function_exists('bindtextdomain')) {
1364 $gettext_flags += 2;
1365 }
1366 if (function_exists('textdomain')) {
1367 $gettext_flags += 4;
1368 }
1369
1370 /* If gettext is fully loaded, cool */
1371 if ($gettext_flags == 7) {
1372 $use_gettext = true;
1373 }
1374 /* If we can fake gettext, try that */
1375 elseif ($gettext_flags == 0) {
1376 $use_gettext = true;
1377 include_once(SM_PATH . 'functions/gettext.php');
1378 } else {
1379 /* Uh-ho. A weird install */
1380 if (! $gettext_flags & 1) {
1381 function _($str) {
1382 return $str;
1383 }
1384 }
1385 if (! $gettext_flags & 2) {
1386 function bindtextdomain() {
1387 return;
1388 }
1389 }
1390 if (! $gettext_flags & 4) {
1391 function textdomain() {
1392 return;
1393 }
1394 }
1395 }
1396
1397 function charset_decode_utf8 ($string) {
1398 /*
1399 Every decoded character consists of n bytes. First byte is octal
1400 300-375, other bytes - always octals 200-277.
1401
1402 \a\b characters are decoded to html code octdec(a-300)*64 + octdec(b-200)
1403 \a\b\c characters are decoded to html code octdec(a-340)*64*64 + octdec(b-200)*64 + octdec(c-200)
1404
1405 decoding cycle is unfinished. please test and report problems to tokul@users.sourceforge.net
1406 */
1407 global $default_charset, $languages, $sm_notAlias;
1408
1409 if (strtolower($default_charset) == 'utf-8')
1410 return $string;
1411 if (strtolower($languages[$sm_notAlias]['CHARSET']) == 'utf-8')
1412 return $string;
1413
1414 /* Only do the slow convert if there are 8-bit characters */
1415 if (! ereg("[\200-\377]", $string))
1416 return $string;
1417
1418 // decode three byte unicode characters
1419 $string = preg_replace("/([\340-\357])([\200-\277])([\200-\277])/e",
1420 "'&#'.((ord('\\1')-224)*4096+(ord('\\2')-128)*64+(ord('\\3')-128)).';'",
1421 $string);
1422
1423 // decode two byte unicode characters
1424 $string = preg_replace("/([\300-\337])([\200-\277])/e",
1425 "'&#'.((ord('\\1')-192)*64+(ord('\\2')-128)).';'",
1426 $string);
1427
1428 return $string;
1429 }
1430
1431 /*
1432 * Japanese charset extra function
1433 *
1434 */
1435 function japanese_charset_xtra() {
1436 $ret = func_get_arg(1); /* default return value */
1437 if (function_exists('mb_detect_encoding')) {
1438 switch (func_get_arg(0)) { /* action */
1439 case 'decode':
1440 $detect_encoding = mb_detect_encoding($ret);
1441 if ($detect_encoding == 'JIS' ||
1442 $detect_encoding == 'EUC-JP' ||
1443 $detect_encoding == 'SJIS') {
1444
1445 $ret = mb_convert_encoding($ret, 'EUC-JP', 'AUTO');
1446 }
1447 break;
1448 case 'encode':
1449 $detect_encoding = mb_detect_encoding($ret);
1450 if ($detect_encoding == 'JIS' ||
1451 $detect_encoding == 'EUC-JP' ||
1452 $detect_encoding == 'SJIS') {
1453
1454 $ret = mb_convert_encoding($ret, 'JIS', 'AUTO');
1455 }
1456 break;
1457 case 'strimwidth':
1458 $width = func_get_arg(2);
1459 $ret = mb_strimwidth($ret, 0, $width, '...');
1460 break;
1461 case 'encodeheader':
1462 $result = '';
1463 if (strlen($ret) > 0) {
1464 $tmpstr = mb_substr($ret, 0, 1);
1465 $prevcsize = strlen($tmpstr);
1466 for ($i = 1; $i < mb_strlen($ret); $i++) {
1467 $tmp = mb_substr($ret, $i, 1);
1468 if (strlen($tmp) == $prevcsize) {
1469 $tmpstr .= $tmp;
1470 } else {
1471 if ($prevcsize == 1) {
1472 $result .= $tmpstr;
1473 } else {
1474 $result .= mb_encode_mimeheader($tmpstr);
1475 }
1476 $tmpstr = $tmp;
1477 $prevcsize = strlen($tmp);
1478 }
1479 }
1480 if (strlen($tmpstr)) {
1481 if (strlen(mb_substr($tmpstr, 0, 1)) == 1)
1482 $result .= $tmpstr;
1483 else
1484 $result .= mb_encode_mimeheader($tmpstr);
1485 }
1486 }
1487 $ret = $result;
1488 //$ret = mb_encode_mimeheader($ret);
1489 break;
1490 case 'decodeheader':
1491 $ret = str_replace("\t", "", $ret);
1492 if (eregi('=\\?([^?]+)\\?(q|b)\\?([^?]+)\\?=', $ret))
1493 $ret = mb_decode_mimeheader($ret);
1494 $ret = mb_convert_encoding($ret, 'EUC-JP', 'AUTO');
1495 break;
1496 case 'downloadfilename':
1497 $useragent = func_get_arg(2);
1498 if (strstr($useragent, 'Windows') !== false ||
1499 strstr($useragent, 'Mac_') !== false) {
1500 $ret = mb_convert_encoding($ret, 'SJIS', 'AUTO');
1501 } else {
1502 $ret = mb_convert_encoding($ret, 'EUC-JP', 'AUTO');
1503 }
1504 break;
1505 }
1506 }
1507 return $ret;
1508 }
1509
1510
1511 /*
1512 * Korean charset extra function
1513 * Hangul(Korean Character) Attached File Name Fix.
1514 */
1515 function korean_charset_xtra() {
1516
1517 $ret = func_get_arg(1); /* default return value */
1518 if (func_get_arg(0) == 'downloadfilename') { /* action */
1519 $ret = str_replace("\x0D\x0A", '', $ret); /* Hanmail's CR/LF Clear */
1520 for ($i=0;$i<strlen($ret);$i++) {
1521 if ($ret[$i] >= "\xA1" && $ret[$i] <= "\xFE") { /* 0xA1 - 0XFE are Valid */
1522 $i++;
1523 continue;
1524 } else if (($ret[$i] >= 'a' && $ret[$i] <= 'z') || /* From Original ereg_replace in download.php */
1525 ($ret[$i] >= 'A' && $ret[$i] <= 'Z') ||
1526 ($ret[$i] == '.') || ($ret[$i] == '-')) {
1527 continue;
1528 } else {
1529 $ret[$i] = '_';
1530 }
1531 }
1532
1533 }
1534
1535 return $ret;
1536 }
1537
1538 ?>