Merge pull request #431 from xmujay/0609_monitor
[phpmyadmin/aamir.git] / libraries / kanji-encoding.lib.php
bloba6fc32034f4cd7059f417e588390a78c32fd2073
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Set of functions for kanji-encoding convert (available only with japanese
5 * language)
7 * PHP4 configure requirements:
8 * --enable-mbstring --enable-mbstr-enc-trans --enable-mbregex
10 * 2002/2/22 - by Yukihiro Kawada <kawada@den.fujifilm.co.jp>
12 * @package PhpMyAdmin
14 if (! defined('PHPMYADMIN')) {
15 exit;
18 /**
19 * Gets the php internal encoding codes and sets the available encoding
20 * codes list
21 * 2002/1/4 by Y.Kawada
23 * @global string the available encoding codes list
25 * @return boolean always true
27 function PMA_Kanji_checkEncoding()
29 global $kanji_encoding_list;
31 $internal_enc = mb_internal_encoding();
32 if ($internal_enc == 'EUC-JP') {
33 $kanji_encoding_list = 'ASCII,EUC-JP,SJIS,JIS';
34 } else {
35 $kanji_encoding_list = 'ASCII,SJIS,EUC-JP,JIS';
38 return true;
39 } // end of the 'PMA_Kanji_checkEncoding' function
42 /**
43 * Reverses SJIS & EUC-JP position in the encoding codes list
44 * 2002/1/4 by Y.Kawada
46 * @global string the available encoding codes list
48 * @return boolean always true
50 function PMA_Kanji_changeOrder()
52 global $kanji_encoding_list;
54 $parts = explode(',', $kanji_encoding_list);
55 if ($parts[1] == 'EUC-JP') {
56 $kanji_encoding_list = 'ASCII,SJIS,EUC-JP,JIS';
57 } else {
58 $kanji_encoding_list = 'ASCII,EUC-JP,SJIS,JIS';
61 return true;
62 } // end of the 'PMA_Kanji_changeOrder' function
65 /**
66 * Kanji string encoding convert
67 * 2002/1/4 by Y.Kawada
69 * @param string $str the string to convert
70 * @param string $enc the destination encoding code
71 * @param string $kana set 'kana' convert to JIS-X208-kana
73 * @global string the available encoding codes list
75 * @return string the converted string
77 function PMA_Kanji_strConv($str, $enc, $kana)
79 global $kanji_encoding_list;
81 if ($enc == '' && $kana == '') {
82 return $str;
84 $string_encoding = mb_detect_encoding($str, $kanji_encoding_list);
86 if ($kana == 'kana') {
87 $dist = mb_convert_kana($str, 'KV', $string_encoding);
88 $str = $dist;
90 if ($string_encoding != $enc && $enc != '') {
91 $dist = mb_convert_encoding($str, $enc, $string_encoding);
92 } else {
93 $dist = $str;
95 return $dist;
96 } // end of the 'PMA_Kanji_strConv' function
99 /**
100 * Kanji file encoding convert
101 * 2002/1/4 by Y.Kawada
103 * @param string $file the name of the file to convert
104 * @param string $enc the destination encoding code
105 * @param string $kana set 'kana' convert to JIS-X208-kana
107 * @return string the name of the converted file
109 function PMA_Kanji_fileConv($file, $enc, $kana)
111 if ($enc == '' && $kana == '') {
112 return $file;
115 $tmpfname = tempnam('', $enc);
116 $fpd = fopen($tmpfname, 'wb');
117 $fps = fopen($file, 'r');
118 PMA_Kanji_changeOrder();
119 while (!feof($fps)) {
120 $line = fgets($fps, 4096);
121 $dist = PMA_Kanji_strConv($line, $enc, $kana);
122 fputs($fpd, $dist);
123 } // end while
124 PMA_Kanji_changeOrder();
125 fclose($fps);
126 fclose($fpd);
127 unlink($file);
129 return $tmpfname;
130 } // end of the 'PMA_Kanji_fileConv' function
134 * Defines radio form fields to switch between encoding modes
135 * 2002/1/4 by Y.Kawada
137 * @return string xhtml code for the radio controls
139 function PMA_Kanji_encodingForm()
141 return "\n"
142 . '<ul>' . "\n" . '<li>'
143 . '<input type="radio" name="knjenc" value="" checked="checked" '
144 . 'id="kj-none" />'
145 . '<label for="kj-none">'
146 /* l10n: This is currently used only in Japanese locales */
147 . _pgettext('None encoding conversion', 'None')
148 . "</label>\n"
149 . '<input type="radio" name="knjenc" value="EUC-JP" id="kj-euc" />'
150 . '<label for="kj-euc">EUC</label>' . "\n"
151 . '<input type="radio" name="knjenc" value="SJIS" id="kj-sjis" />'
152 . '<label for="kj-sjis">SJIS</label>' . "\n"
153 . '</li>' . "\n" . '<li>'
154 . '<input type="checkbox" name="xkana" value="kana" id="kj-kana" />'
155 . "\n"
156 . '<label for="kj-kana">'
157 /* l10n: This is currently used only in Japanese locales */
158 . __('Convert to Kana')
159 . '</label><br />'
160 . "\n"
161 . '</li>' . "\n" . '</ul>'
163 } // end of the 'PMA_Kanji_encodingForm' function
166 PMA_Kanji_checkEncoding();