2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Charset conversion functions.
9 if (! defined('PHPMYADMIN')) {
14 * Loads the recode or iconv extensions if any of it is not loaded yet
16 if (isset($cfg['AllowAnywhereRecoding'])
17 && $cfg['AllowAnywhereRecoding']) {
19 if ($cfg['RecodingEngine'] == 'recode') {
20 if (!@extension_loaded
('recode')) {
21 echo $strCantLoadRecodeIconv;
24 $PMA_recoding_engine = 'recode';
25 } elseif ($cfg['RecodingEngine'] == 'iconv') {
26 if (!@extension_loaded
('iconv')) {
27 echo $strCantLoadRecodeIconv;
30 $PMA_recoding_engine = 'iconv';
32 if (@extension_loaded
('iconv')) {
33 $PMA_recoding_engine = 'iconv';
34 } elseif (@extension_loaded
('recode')) {
35 $PMA_recoding_engine = 'recode';
37 echo $strCantLoadRecodeIconv;
41 } // end load recode/iconv extension
43 define('PMA_CHARSET_NONE', 0);
44 define('PMA_CHARSET_ICONV', 1);
45 define('PMA_CHARSET_LIBICONV', 2);
46 define('PMA_CHARSET_RECODE', 3);
47 define('PMA_CHARSET_ICONV_AIX', 4);
49 if (!isset($cfg['IconvExtraParams'])) {
50 $cfg['IconvExtraParams'] = '';
53 // Finally detect which function we will use:
54 if (isset($cfg['AllowAnywhereRecoding'])
55 && $cfg['AllowAnywhereRecoding']) {
57 if (!isset($PMA_recoding_engine)) {
58 $PMA_recoding_engine = $cfg['RecodingEngine'];
60 if ($PMA_recoding_engine == 'iconv') {
61 if (@function_exists
('iconv')) {
62 if ((@stristr
(PHP_OS
, 'AIX')) && (@strcasecmp
(ICONV_IMPL
, 'unknown') == 0) && (@strcasecmp
(ICONV_VERSION
, 'unknown') == 0)) {
63 $PMA_recoding_engine = PMA_CHARSET_ICONV_AIX
;
65 $PMA_recoding_engine = PMA_CHARSET_ICONV
;
67 } elseif (@function_exists
('libiconv')) {
68 $PMA_recoding_engine = PMA_CHARSET_LIBICONV
;
70 $PMA_recoding_engine = PMA_CHARSET_NONE
;
72 if (!isset($GLOBALS['is_header_sent'])) {
73 include './libraries/header.inc.php';
75 echo $strCantUseRecodeIconv;
76 require_once './libraries/footer.inc.php';
79 } elseif ($PMA_recoding_engine == 'recode') {
80 if (@function_exists
('recode_string')) {
81 $PMA_recoding_engine = PMA_CHARSET_RECODE
;
83 $PMA_recoding_engine = PMA_CHARSET_NONE
;
85 require_once './libraries/header.inc.php';
86 echo $strCantUseRecodeIconv;
87 require_once './libraries/footer.inc.php';
91 if (@function_exists
('iconv')) {
92 if ((@stristr
(PHP_OS
, 'AIX')) && (@strcasecmp
(ICONV_IMPL
, 'unknown') == 0) && (@strcasecmp
(ICONV_VERSION
, 'unknown') == 0)) {
93 $PMA_recoding_engine = PMA_CHARSET_ICONV_AIX
;
95 $PMA_recoding_engine = PMA_CHARSET_ICONV
;
97 } elseif (@function_exists
('libiconv')) {
98 $PMA_recoding_engine = PMA_CHARSET_LIBICONV
;
99 } elseif (@function_exists
('recode_string')) {
100 $PMA_recoding_engine = PMA_CHARSET_RECODE
;
102 $PMA_recoding_engine = PMA_CHARSET_NONE
;
104 require_once './libraries/header.inc.php';
105 echo $strCantUseRecodeIconv;
106 require_once './libraries/footer.inc.php';
111 $PMA_recoding_engine = PMA_CHARSET_NONE
;
114 /* Load AIX iconv wrapper if needed */
115 if ($PMA_recoding_engine == PMA_CHARSET_ICONV_AIX
) {
116 require_once './libraries/iconv_wrapper.lib.php';
120 * Converts encoding of text according to current settings.
122 * @param string what to convert
124 * @return string converted text
126 * @global array the configuration array
127 * @global boolean whether recoding is allowed or not
128 * @global string the current charset
129 * @global array the charset to convert to
135 function PMA_convert_charset($what) {
136 global $cfg, $charset, $convcharset;
138 if (!(isset($cfg['AllowAnywhereRecoding']) && $cfg['AllowAnywhereRecoding'] )
139 ||
$convcharset == $charset) { // rabus: if input and output charset are the same, we don't have to do anything...
142 switch ($GLOBALS['PMA_recoding_engine']) {
143 case PMA_CHARSET_RECODE
:
144 return recode_string($charset . '..' . $convcharset, $what);
145 case PMA_CHARSET_ICONV
:
146 return iconv($charset, $convcharset . $cfg['IconvExtraParams'], $what);
147 case PMA_CHARSET_ICONV_AIX
:
148 return PMA_aix_iconv_wrapper($charset, $convcharset . $cfg['IconvExtraParams'], $what);
149 case PMA_CHARSET_LIBICONV
:
150 return libiconv($charset, $convcharset . $GLOBALS['cfg']['IconvExtraParams'], $what);
155 } // end of the "PMA_convert_charset()" function
158 * Converts encoding of text according to parameters with detected
159 * conversion function.
161 * @param string source charset
162 * @param string target charset
163 * @param string what to convert
165 * @return string converted text
171 function PMA_convert_string($src_charset, $dest_charset, $what) {
172 if ($src_charset == $dest_charset) {
175 switch ($GLOBALS['PMA_recoding_engine']) {
176 case PMA_CHARSET_RECODE
:
177 return recode_string($src_charset . '..' . $dest_charset, $what);
178 case PMA_CHARSET_ICONV
:
179 return iconv($src_charset, $dest_charset . $GLOBALS['cfg']['IconvExtraParams'], $what);
180 case PMA_CHARSET_ICONV_AIX
:
181 return PMA_aix_iconv_wrapper($src_charset, $dest_charset . $GLOBALS['cfg']['IconvExtraParams'], $what);
182 case PMA_CHARSET_LIBICONV
:
183 return libiconv($src_charset, $dest_charset, $what);
187 } // end of the "PMA_convert_string()" function
191 * Converts encoding of file according to parameters with detected
192 * conversion function. The old file will be unlinked and new created and
193 * its file name is returned.
195 * @param string source charset
196 * @param string target charset
197 * @param string file to convert
199 * @return string new temporay file
205 function PMA_convert_file($src_charset, $dest_charset, $file) {
206 switch ($GLOBALS['PMA_recoding_engine']) {
207 case PMA_CHARSET_RECODE
:
208 case PMA_CHARSET_ICONV
:
209 case PMA_CHARSET_LIBICONV
:
210 $tmpfname = tempnam('', 'PMA_convert_file');
211 $fin = fopen($file, 'r');
212 $fout = fopen($tmpfname, 'w');
213 if ($GLOBALS['PMA_recoding_engine'] == PMA_CHARSET_RECODE
) {
214 recode_file($src_charset . '..' . $dest_charset, $fin, $fout);
216 while (!feof($fin)) {
217 $line = fgets($fin, 4096);
218 if ($GLOBALS['PMA_recoding_engine'] == PMA_CHARSET_ICONV
) {
219 $dist = iconv($src_charset, $dest_charset . $GLOBALS['cfg']['IconvExtraParams'], $line);
220 } elseif ($GLOBALS['PMA_recoding_engine'] == PMA_CHARSET_ICONV_AIX
) {
221 $dist = PMA_aix_iconv_wrapper($src_charset, $dest_charset . $GLOBALS['cfg']['IconvExtraParams'], $line);
223 $dist = libiconv($src_charset, $dest_charset . $GLOBALS['cfg']['IconvExtraParams'], $line);
236 } // end of the "PMA_convert_file()" function