2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Charset conversion functions.
8 if (! defined('PHPMYADMIN')) {
13 * Loads the recode or iconv extensions if any of it is not loaded yet
15 if (isset($cfg['AllowAnywhereRecoding'])
16 && $cfg['AllowAnywhereRecoding']
19 if ($cfg['RecodingEngine'] == 'recode') {
20 if (!@extension_loaded
('recode')) {
22 if (!@extension_loaded
('recode')) {
23 echo $strCantLoadRecodeIconv;
27 $PMA_recoding_engine = 'recode';
28 } elseif ($cfg['RecodingEngine'] == 'iconv') {
29 if (!@extension_loaded
('iconv')) {
31 if (!@extension_loaded
('iconv')) {
32 echo $strCantLoadRecodeIconv;
36 $PMA_recoding_engine = 'iconv';
38 if (@extension_loaded
('iconv')) {
39 $PMA_recoding_engine = 'iconv';
40 } elseif (@extension_loaded
('recode')) {
41 $PMA_recoding_engine = 'recode';
44 if (!@extension_loaded
('iconv')) {
46 if (!@extension_loaded
('recode')) {
47 echo $strCantLoadRecodeIconv;
50 $PMA_recoding_engine = 'recode';
53 $PMA_recoding_engine = 'iconv';
57 } // end load recode/iconv extension
59 define('PMA_CHARSET_NONE', 0);
60 define('PMA_CHARSET_ICONV', 1);
61 define('PMA_CHARSET_LIBICONV', 2);
62 define('PMA_CHARSET_RECODE', 3);
63 define('PMA_CHARSET_ICONV_AIX', 4);
65 if (!isset($cfg['IconvExtraParams'])) {
66 $cfg['IconvExtraParams'] = '';
69 // Finally detects which function will we use:
70 if (isset($cfg['AllowAnywhereRecoding'])
71 && $cfg['AllowAnywhereRecoding']
74 if (!isset($PMA_recoding_engine)) {
75 $PMA_recoding_engine = $cfg['RecodingEngine'];
77 if ($PMA_recoding_engine == 'iconv') {
78 if (@function_exists
('iconv')) {
79 if ((@stristr
(PHP_OS
, 'AIX')) && (@strcasecmp
(ICONV_IMPL
, 'unknown') == 0) && (@strcasecmp
(ICONV_VERSION
, 'unknown') == 0)) {
80 $PMA_recoding_engine = PMA_CHARSET_ICONV_AIX
;
82 $PMA_recoding_engine = PMA_CHARSET_ICONV
;
84 } elseif (@function_exists
('libiconv')) {
85 $PMA_recoding_engine = PMA_CHARSET_LIBICONV
;
87 $PMA_recoding_engine = PMA_CHARSET_NONE
;
89 if (!isset($GLOBALS['is_header_sent'])) {
90 include './libraries/header.inc.php';
92 echo $strCantUseRecodeIconv;
93 require_once './libraries/footer.inc.php';
96 } elseif ($PMA_recoding_engine == 'recode') {
97 if (@function_exists
('recode_string')) {
98 $PMA_recoding_engine = PMA_CHARSET_RECODE
;
100 $PMA_recoding_engine = PMA_CHARSET_NONE
;
102 require_once './libraries/header.inc.php';
103 echo $strCantUseRecodeIconv;
104 require_once './libraries/footer.inc.php';
108 if (@function_exists
('iconv')) {
109 if ((@stristr
(PHP_OS
, 'AIX')) && (@strcasecmp
(ICONV_IMPL
, 'unknown') == 0) && (@strcasecmp
(ICONV_VERSION
, 'unknown') == 0)) {
110 $PMA_recoding_engine = PMA_CHARSET_ICONV_AIX
;
112 $PMA_recoding_engine = PMA_CHARSET_ICONV
;
114 } elseif (@function_exists
('libiconv')) {
115 $PMA_recoding_engine = PMA_CHARSET_LIBICONV
;
116 } elseif (@function_exists
('recode_string')) {
117 $PMA_recoding_engine = PMA_CHARSET_RECODE
;
119 $PMA_recoding_engine = PMA_CHARSET_NONE
;
121 require_once './libraries/header.inc.php';
122 echo $strCantUseRecodeIconv;
123 require_once './libraries/footer.inc.php';
128 $PMA_recoding_engine = PMA_CHARSET_NONE
;
131 /* Load AIX iconv wrapper if needed */
132 if ($PMA_recoding_engine == PMA_CHARSET_ICONV_AIX
) {
133 require_once './libraries/iconv_wrapper.lib.php';
137 * Converts encoding according to current settings.
139 * @param mixed what to convert (string or array of strings or object returned by mysql_fetch_field)
141 * @return string converted string or array of strings
143 * @global array the configuration array
144 * @global boolean whether recoding is allowed or not
145 * @global string the current charset
146 * @global array the charset to convert to
152 function PMA_convert_display_charset($what) {
153 global $cfg, $allow_recoding, $charset, $convcharset;
155 if (!(isset($cfg['AllowAnywhereRecoding']) && $cfg['AllowAnywhereRecoding'] && $allow_recoding)
156 ||
$convcharset == $charset // rabus: if input and output charset are the same, we don't have to do anything...
157 // this constant is not defined before the login:
158 ||
(defined('PMA_MYSQL_INT_VERSION') && PMA_MYSQL_INT_VERSION
>= 40100)) { // lem9: even if AllowAnywhereRecoding is TRUE, do not recode for MySQL >= 4.1.x since MySQL does the job
160 } elseif (is_array($what)) {
162 foreach ($what AS $key => $val) {
163 if (is_string($val) ||
is_array($val)) {
164 if (is_string($key)) {
165 $result[PMA_convert_display_charset($key)] = PMA_convert_display_charset($val);
167 $result[$key] = PMA_convert_display_charset($val);
170 $result[$key] = $val;
174 } elseif (is_string($what)) {
176 switch ($GLOBALS['PMA_recoding_engine']) {
177 case PMA_CHARSET_RECODE
:
178 return recode_string($convcharset . '..' . $charset, $what);
179 case PMA_CHARSET_ICONV
:
180 return iconv($convcharset, $charset . $cfg['IconvExtraParams'], $what);
181 case PMA_CHARSET_ICONV_AIX
:
182 return PMA_aix_iconv_wrapper($convcharset, $charset . $cfg['IconvExtraParams'], $what);
183 case PMA_CHARSET_LIBICONV
:
184 return libiconv($convcharset, $charset . $GLOBALS['cfg']['IconvExtraParams'], $what);
188 } elseif (is_object($what)) {
189 // isn't it object returned from mysql_fetch_field ?
190 if (@is_string
($what->name
)) {
191 $what->name
= PMA_convert_display_charset($what->name
);
193 if (@is_string
($what->table
)) {
194 $what->table
= PMA_convert_display_charset($what->table
);
196 if (@is_string
($what->Database
)) {
197 $what->Database
= PMA_convert_display_charset($what->Database
);
201 // when we don't know what it is we don't touch it...
204 } // end of the "PMA_convert_display_charset()" function
208 * Converts encoding of text according to current settings.
210 * @param string what to convert
212 * @return string converted text
214 * @global array the configuration array
215 * @global boolean whether recoding is allowed or not
216 * @global string the current charset
217 * @global array the charset to convert to
223 function PMA_convert_charset($what) {
224 global $cfg, $allow_recoding, $charset, $convcharset;
226 if (!(isset($cfg['AllowAnywhereRecoding']) && $cfg['AllowAnywhereRecoding'] && $allow_recoding)
227 ||
$convcharset == $charset) { // rabus: if input and output charset are the same, we don't have to do anything...
230 switch ($GLOBALS['PMA_recoding_engine']) {
231 case PMA_CHARSET_RECODE
:
232 return recode_string($charset . '..' . $convcharset, $what);
233 case PMA_CHARSET_ICONV
:
234 return iconv($charset, $convcharset . $cfg['IconvExtraParams'], $what);
235 case PMA_CHARSET_ICONV_AIX
:
236 return PMA_aix_iconv_wrapper($charset, $convcharset . $cfg['IconvExtraParams'], $what);
237 case PMA_CHARSET_LIBICONV
:
238 return libiconv($charset, $convcharset . $GLOBALS['cfg']['IconvExtraParams'], $what);
243 } // end of the "PMA_convert_charset()" function
246 * Converts encoding of text according to parameters with detected
247 * conversion function.
249 * @param string source charset
250 * @param string target charset
251 * @param string what to convert
253 * @return string converted text
259 function PMA_convert_string($src_charset, $dest_charset, $what) {
260 if ($src_charset == $dest_charset) {
263 switch ($GLOBALS['PMA_recoding_engine']) {
264 case PMA_CHARSET_RECODE
:
265 return recode_string($src_charset . '..' . $dest_charset, $what);
266 case PMA_CHARSET_ICONV
:
267 return iconv($src_charset, $dest_charset . $GLOBALS['cfg']['IconvExtraParams'], $what);
268 case PMA_CHARSET_ICONV_AIX
:
269 return PMA_aix_iconv_wrapper($src_charset, $dest_charset . $GLOBALS['cfg']['IconvExtraParams'], $what);
270 case PMA_CHARSET_LIBICONV
:
271 return libiconv($src_charset, $dest_charset, $what);
275 } // end of the "PMA_convert_string()" function
279 * Converts encoding of file according to parameters with detected
280 * conversion function. The old file will be unlinked and new created and
281 * its file name is returned.
283 * @param string source charset
284 * @param string target charset
285 * @param string file to convert
287 * @return string new temporay file
293 function PMA_convert_file($src_charset, $dest_charset, $file) {
294 switch ($GLOBALS['PMA_recoding_engine']) {
295 case PMA_CHARSET_RECODE
:
296 case PMA_CHARSET_ICONV
:
297 case PMA_CHARSET_LIBICONV
:
298 $tmpfname = tempnam('', 'PMA_convert_file');
299 $fin = fopen($file, 'r');
300 $fout = fopen($tmpfname, 'w');
301 if ($GLOBALS['PMA_recoding_engine'] == PMA_CHARSET_RECODE
) {
302 recode_file($src_charset . '..' . $dest_charset, $fin, $fout);
304 while (!feof($fin)) {
305 $line = fgets($fin, 4096);
306 if ($GLOBALS['PMA_recoding_engine'] == PMA_CHARSET_ICONV
) {
307 $dist = iconv($src_charset, $dest_charset . $GLOBALS['cfg']['IconvExtraParams'], $line);
308 } elseif ($GLOBALS['PMA_recoding_engine'] == PMA_CHARSET_ICONV_AIX
) {
309 $dist = PMA_aix_iconv_wrapper($src_charset, $dest_charset . $GLOBALS['cfg']['IconvExtraParams'], $line);
311 $dist = libiconv($src_charset, $dest_charset . $GLOBALS['cfg']['IconvExtraParams'], $line);
324 } // end of the "PMA_convert_file()" function