1<?php
2/**
3 * iso-2022-kr decoding functions
4 *
5 * This script provides iso-2022-kr (rfc1557) decoding functions.
6 *
7 * @copyright (c) 2004-2005 The SquirrelMail Project Team
8 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
9 * @version $Id: iso_2022_kr.php 12467 2007-06-25 15:52:44Z kink $
10 * @package decode
11 * @subpackage eastasia
12 */
13
14/**
15 * Converts iso-2022-kr texts
16 * @param string $string iso-2022-kr encoded string
17 * @param boolean $save_html don't html encode special characters if true
18 * @return string html encoded string
19 */
20function charset_decode_iso_2022_kr ($string,$save_html=false) {
21    global $aggressive_decoding;
22
23    // undo htmlspecial chars (they can break iso-2022-kr)
24    if (! $save_html) {
25        $string=str_replace(array('&quot;','&lt;','&gt;','&amp;'),array('"','<','>','&'),$string);
26    }
27    // recode
28    // this is CPU intensive task. Use recode functions if they are available.
29    if (function_exists('recode_string')) {
30        $string = recode_string("iso-2022-kr..html",$string);
31
32        // if string sanitizing is not needed, undo htmlspecialchars applied by recode.
33        if ($save_html) {
34            $string=str_replace(array('&quot;','&lt;','&gt;','&amp;'),array('"','<','>','&'),$string);
35        }
36        return $string;
37    }
38
39    // iconv does not support html target, but internal utf-8 decoding is faster than iso-2022-kr.
40    if (function_exists('iconv') && file_exists(SM_PATH . 'functions/decode/utf_8.php') ) {
41        include_once(SM_PATH . 'functions/decode/utf_8.php');
42        $string = iconv('iso-2022-kr','utf-8',$string);
43        // redo htmlspecial chars
44        if (! $save_html) $string = htmlspecialchars($string);
45        return charset_decode_utf_8($string);
46    }
47
48    // aggressive decoding disabled. iso-2022-kr is not supported by iso_2022_support.php
49    //    if (! isset($aggressive_decoding) || ! $aggressive_decoding )
50        return htmlspecialchars($string);
51
52    /**
53     * Include common iso-2022-xx functions
54     */
55    include_once(SM_PATH . 'functions/decode/iso_2022_support.php');
56
57    $index=0;
58    $ret='';
59    $enc_table='ascii';
60
61    while ( $index < strlen($string)) {
62        if (isset($string[$index+2]) && $string[$index]=="\x1B") {
63            // table change
64            switch ($string[$index].$string[$index+1].$string[$index+2]) {
65            case "\x1B\x28\x42":
66                $enc_table='ascii';
67                $index=$index+3;
68                break;
69            case "\x1B\x24\x42":
70                $enc_table='jis0208-1983';
71                $index=$index+3;
72                break;
73            case "\x1B\x28\x4A":
74                $enc_table='jis0201-1976';
75                $index=$index+3;
76                break;
77            case "\x1B\x24\x40":
78                $enc_table='jis0208-1978';
79                $index=$index+3;
80                break;
81            default:
82                return _("Unsupported ESC sequence.");
83            }
84        }
85
86        $ret .= get_iso_2022_symbol($string,$index,$enc_table);
87        $index=$index+get_iso_2022_symbolsize($enc_table);
88    }
89    return $ret;
90}
91