2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Charset conversion functions.
11 * Loads the recode or iconv extensions if any of it is not loaded yet
13 if (isset($cfg['AllowAnywhereRecoding'])
14 && $cfg['AllowAnywhereRecoding']
17 if ($cfg['RecodingEngine'] == 'recode') {
18 if (!@extension_loaded
('recode')) {
20 if (!@extension_loaded
('recode')) {
21 echo $strCantLoadRecodeIconv;
25 $PMA_recoding_engine = 'recode';
26 } elseif ($cfg['RecodingEngine'] == 'iconv') {
27 if (!@extension_loaded
('iconv')) {
29 if (!@extension_loaded
('iconv')) {
30 echo $strCantLoadRecodeIconv;
34 $PMA_recoding_engine = 'iconv';
36 if (@extension_loaded
('iconv')) {
37 $PMA_recoding_engine = 'iconv';
38 } elseif (@extension_loaded
('recode')) {
39 $PMA_recoding_engine = 'recode';
42 if (!@extension_loaded
('iconv')) {
44 if (!@extension_loaded
('recode')) {
45 echo $strCantLoadRecodeIconv;
48 $PMA_recoding_engine = 'recode';
51 $PMA_recoding_engine = 'iconv';
55 } // end load recode/iconv extension
57 define('PMA_CHARSET_NONE', 0);
58 define('PMA_CHARSET_ICONV', 1);
59 define('PMA_CHARSET_LIBICONV', 2);
60 define('PMA_CHARSET_RECODE', 3);
61 define('PMA_CHARSET_ICONV_AIX', 4);
63 if (!isset($cfg['IconvExtraParams'])) {
64 $cfg['IconvExtraParams'] = '';
67 // Finally detects which function will we use:
68 if (isset($cfg['AllowAnywhereRecoding'])
69 && $cfg['AllowAnywhereRecoding']
72 if (!isset($PMA_recoding_engine)) {
73 $PMA_recoding_engine = $cfg['RecodingEngine'];
75 if ($PMA_recoding_engine == 'iconv') {
76 if (@function_exists
('iconv')) {
77 if ((@stristr
(PHP_OS
, 'AIX')) && (@strcasecmp
(ICONV_IMPL
, 'unknown') == 0) && (@strcasecmp
(ICONV_VERSION
, 'unknown') == 0)) {
78 $PMA_recoding_engine = PMA_CHARSET_ICONV_AIX
;
80 $PMA_recoding_engine = PMA_CHARSET_ICONV
;
82 } elseif (@function_exists
('libiconv')) {
83 $PMA_recoding_engine = PMA_CHARSET_LIBICONV
;
85 $PMA_recoding_engine = PMA_CHARSET_NONE
;
87 if (!isset($GLOBALS['is_header_sent'])) {
88 include './libraries/header.inc.php';
90 echo $strCantUseRecodeIconv;
91 require_once './libraries/footer.inc.php';
94 } elseif ($PMA_recoding_engine == 'recode') {
95 if (@function_exists
('recode_string')) {
96 $PMA_recoding_engine = PMA_CHARSET_RECODE
;
98 $PMA_recoding_engine = PMA_CHARSET_NONE
;
100 require_once './libraries/header.inc.php';
101 echo $strCantUseRecodeIconv;
102 require_once './libraries/footer.inc.php';
106 if (@function_exists
('iconv')) {
107 if ((@stristr
(PHP_OS
, 'AIX')) && (@strcasecmp
(ICONV_IMPL
, 'unknown') == 0) && (@strcasecmp
(ICONV_VERSION
, 'unknown') == 0)) {
108 $PMA_recoding_engine = PMA_CHARSET_ICONV_AIX
;
110 $PMA_recoding_engine = PMA_CHARSET_ICONV
;
112 } elseif (@function_exists
('libiconv')) {
113 $PMA_recoding_engine = PMA_CHARSET_LIBICONV
;
114 } elseif (@function_exists
('recode_string')) {
115 $PMA_recoding_engine = PMA_CHARSET_RECODE
;
117 $PMA_recoding_engine = PMA_CHARSET_NONE
;
119 require_once './libraries/header.inc.php';
120 echo $strCantUseRecodeIconv;
121 require_once './libraries/footer.inc.php';
126 $PMA_recoding_engine = PMA_CHARSET_NONE
;
129 /* Load AIX iconv wrapper if needed */
130 if ($PMA_recoding_engine == PMA_CHARSET_ICONV_AIX
) {
131 require_once './libraries/iconv_wrapper.lib.php';
135 * Converts encoding according to current settings.
137 * @param mixed what to convert (string or array of strings or object returned by mysql_fetch_field)
139 * @return string converted string or array of strings
141 * @global array the configuration array
142 * @global boolean whether recoding is allowed or not
143 * @global string the current charset
144 * @global array the charset to convert to
150 function PMA_convert_display_charset($what) {
151 global $cfg, $allow_recoding, $charset, $convcharset;
153 if (!(isset($cfg['AllowAnywhereRecoding']) && $cfg['AllowAnywhereRecoding'] && $allow_recoding)
154 ||
$convcharset == $charset // rabus: if input and output charset are the same, we don't have to do anything...
155 // this constant is not defined before the login:
156 ||
(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
158 } elseif (is_array($what)) {
160 foreach ($what AS $key => $val) {
161 if (is_string($val) ||
is_array($val)) {
162 if (is_string($key)) {
163 $result[PMA_convert_display_charset($key)] = PMA_convert_display_charset($val);
165 $result[$key] = PMA_convert_display_charset($val);
168 $result[$key] = $val;
172 } elseif (is_string($what)) {
174 switch ($GLOBALS['PMA_recoding_engine']) {
175 case PMA_CHARSET_RECODE
:
176 return recode_string($convcharset . '..' . $charset, $what);
177 case PMA_CHARSET_ICONV
:
178 return iconv($convcharset, $charset . $cfg['IconvExtraParams'], $what);
179 case PMA_CHARSET_ICONV_AIX
:
180 return PMA_aix_iconv_wrapper($convcharset, $charset . $cfg['IconvExtraParams'], $what);
181 case PMA_CHARSET_LIBICONV
:
182 return libiconv($convcharset, $charset . $GLOBALS['cfg']['IconvExtraParams'], $what);
186 } elseif (is_object($what)) {
187 // isn't it object returned from mysql_fetch_field ?
188 if (@is_string
($what->name
)) {
189 $what->name
= PMA_convert_display_charset($what->name
);
191 if (@is_string
($what->table
)) {
192 $what->table
= PMA_convert_display_charset($what->table
);
194 if (@is_string
($what->Database
)) {
195 $what->Database
= PMA_convert_display_charset($what->Database
);
199 // when we don't know what it is we don't touch it...
202 } // end of the "PMA_convert_display_charset()" function
206 * Converts encoding of text according to current settings.
208 * @param string what to convert
210 * @return string converted text
212 * @global array the configuration array
213 * @global boolean whether recoding is allowed or not
214 * @global string the current charset
215 * @global array the charset to convert to
221 function PMA_convert_charset($what) {
222 global $cfg, $allow_recoding, $charset, $convcharset;
224 if (!(isset($cfg['AllowAnywhereRecoding']) && $cfg['AllowAnywhereRecoding'] && $allow_recoding)
225 ||
$convcharset == $charset) { // rabus: if input and output charset are the same, we don't have to do anything...
228 switch ($GLOBALS['PMA_recoding_engine']) {
229 case PMA_CHARSET_RECODE
:
230 return recode_string($charset . '..' . $convcharset, $what);
231 case PMA_CHARSET_ICONV
:
232 return iconv($charset, $convcharset . $cfg['IconvExtraParams'], $what);
233 case PMA_CHARSET_ICONV_AIX
:
234 return PMA_aix_iconv_wrapper($charset, $convcharset . $cfg['IconvExtraParams'], $what);
235 case PMA_CHARSET_LIBICONV
:
236 return libiconv($charset, $convcharset . $GLOBALS['cfg']['IconvExtraParams'], $what);
241 } // end of the "PMA_convert_charset()" function
244 * Converts encoding of text according to parameters with detected
245 * conversion function.
247 * @param string source charset
248 * @param string target charset
249 * @param string what to convert
251 * @return string converted text
257 function PMA_convert_string($src_charset, $dest_charset, $what) {
258 if ($src_charset == $dest_charset) {
261 switch ($GLOBALS['PMA_recoding_engine']) {
262 case PMA_CHARSET_RECODE
:
263 return recode_string($src_charset . '..' . $dest_charset, $what);
264 case PMA_CHARSET_ICONV
:
265 return iconv($src_charset, $dest_charset . $GLOBALS['cfg']['IconvExtraParams'], $what);
266 case PMA_CHARSET_ICONV_AIX
:
267 return PMA_aix_iconv_wrapper($src_charset, $dest_charset . $GLOBALS['cfg']['IconvExtraParams'], $what);
268 case PMA_CHARSET_LIBICONV
:
269 return libiconv($src_charset, $dest_charset, $what);
273 } // end of the "PMA_convert_string()" function
277 * Converts encoding of file according to parameters with detected
278 * conversion function. The old file will be unlinked and new created and
279 * its file name is returned.
281 * @param string source charset
282 * @param string target charset
283 * @param string file to convert
285 * @return string new temporay file
291 function PMA_convert_file($src_charset, $dest_charset, $file) {
292 switch ($GLOBALS['PMA_recoding_engine']) {
293 case PMA_CHARSET_RECODE
:
294 case PMA_CHARSET_ICONV
:
295 case PMA_CHARSET_LIBICONV
:
296 $tmpfname = tempnam('', 'PMA_convert_file');
297 $fin = fopen($file, 'r');
298 $fout = fopen($tmpfname, 'w');
299 if ($GLOBALS['PMA_recoding_engine'] == PMA_CHARSET_RECODE
) {
300 recode_file($src_charset . '..' . $dest_charset, $fin, $fout);
302 while (!feof($fin)) {
303 $line = fgets($fin, 4096);
304 if ($GLOBALS['PMA_recoding_engine'] == PMA_CHARSET_ICONV
) {
305 $dist = iconv($src_charset, $dest_charset . $GLOBALS['cfg']['IconvExtraParams'], $line);
306 } elseif ($GLOBALS['PMA_recoding_engine'] == PMA_CHARSET_ICONV_AIX
) {
307 $dist = PMA_aix_iconv_wrapper($src_charset, $dest_charset . $GLOBALS['cfg']['IconvExtraParams'], $line);
309 $dist = libiconv($src_charset, $dest_charset . $GLOBALS['cfg']['IconvExtraParams'], $line);
322 } // end of the "PMA_convert_file()" function