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