3 // vim: expandtab sw=4 ts=4 sts=4:
7 * Charset conversion functions.
11 if (!defined('PMA_CHARSET_CONVERSION_LIB_INCLUDED')){
12 define('PMA_CHARSET_CONVERSION_LIB_INCLUDED', 1);
15 * Loads the recode or iconv extensions if any of it is not loaded yet
17 if (isset($cfg['AllowAnywhereRecoding'])
18 && $cfg['AllowAnywhereRecoding']
21 // Initializes configuration for default, if not set:
22 if (!isset($cfg['RecodingEngine'])) {
23 $cfg['RecodingEngine'] = 'auto';
26 if ($cfg['RecodingEngine'] == 'recode') {
27 if (!@extension_loaded
('recode')) {
29 if (!@extension_loaded
('recode')) {
30 echo $strCantLoadRecodeIconv;
34 $PMA_recoding_engine = 'recode';
35 } else if ($cfg['RecodingEngine'] == 'iconv') {
36 if (!@extension_loaded
('iconv')) {
38 if (!@extension_loaded
('iconv')) {
39 echo $strCantLoadRecodeIconv;
43 $PMA_recoding_engine = 'iconv';
45 if (@extension_loaded
('iconv')) {
46 $PMA_recoding_engine = 'iconv';
47 } else if (@extension_loaded
('recode')) {
48 $PMA_recoding_engine = 'recode';
51 if (!@extension_loaded
('iconv')) {
53 if (!@extension_loaded
('recode')) {
54 echo $strCantLoadRecodeIconv;
57 $PMA_recoding_engine = 'recode';
60 $PMA_recoding_engine = 'iconv';
64 } // end load recode/iconv extension
66 define('PMA_CHARSET_NONE', 0);
67 define('PMA_CHARSET_ICONV', 1);
68 define('PMA_CHARSET_LIBICONV', 2);
69 define('PMA_CHARSET_RECODE', 3);
71 if (!isset($cfg['IconvExtraParams'])) {
72 $cfg['IconvExtraParams'] = '';
75 // Finally detects which function will we use:
76 if (isset($cfg['AllowAnywhereRecoding'])
77 && $cfg['AllowAnywhereRecoding']
80 if (!isset($PMA_recoding_engine)) {
81 $PMA_recoding_engine = $cfg['RecodingEngine'];
83 if ($PMA_recoding_engine == 'iconv') {
84 if (@function_exists
('iconv')) {
85 $PMA_recoding_engine = PMA_CHARSET_ICONV
;
86 } else if (@function_exists
('libiconv')) {
87 $PMA_recoding_engine = PMA_CHARSET_LIBICONV
;
89 $PMA_recoding_engine = PMA_CHARSET_NONE
;
91 if (!isset($GLOBALS['is_header_sent'])) {
92 include('./header.inc.php3');
94 echo $strCantUseRecodeIconv;
95 include('./footer.inc.php3');
98 } else if ($PMA_recoding_engine == 'recode') {
99 if (@function_exists
('recode_string')) {
100 $PMA_recoding_engine = PMA_CHARSET_RECODE
;
102 $PMA_recoding_engine = PMA_CHARSET_NONE
;
104 if (!isset($GLOBALS['is_header_sent'])) {
105 include('./header.inc.php3');
107 echo $strCantUseRecodeIconv;
108 include('./footer.inc.php3');
112 if (@function_exists
('iconv')) {
113 $PMA_recoding_engine = PMA_CHARSET_ICONV
;
114 } else if (@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 if (!isset($GLOBALS['is_header_sent'])) {
122 include('./header.inc.php3');
124 echo $strCantUseRecodeIconv;
125 include('./footer.inc.php3');
130 $PMA_recoding_engine = PMA_CHARSET_NONE
;
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)) {
156 else if (is_array($what)) {
159 while (list($key, $val) = each($what)) {
160 if (is_string($val) ||
is_array($val)) {
161 if (is_string($key)) {
162 $result[PMA_convert_display_charset($key)] = PMA_convert_display_charset($val);
164 $result[$key] = PMA_convert_display_charset($val);
167 $result[$key] = $val;
172 else if (is_string($what)) {
173 switch ($GLOBALS['PMA_recoding_engine']) {
174 case PMA_CHARSET_RECODE
:
175 return recode_string($convcharset . '..' . $charset, $what);
177 case PMA_CHARSET_ICONV
:
178 return iconv($convcharset, $charset . $cfg['IconvExtraParams'], $what);
180 case PMA_CHARSET_LIBICONV
:
181 return libiconv($convcharset, $charset, $what);
187 else if (is_object($what)) {
188 // isn't it object returned from mysql_fetch_field ?
189 if (@is_string
($what->name
)) {
190 $what->name
= PMA_convert_display_charset($what->name
);
192 if (@is_string
($what->table
)) {
193 $what->table
= PMA_convert_display_charset($what->table
);
195 if (@is_string
($what->Database
)) {
196 $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)) {
229 switch ($GLOBALS['PMA_recoding_engine']) {
230 case PMA_CHARSET_RECODE
:
231 return recode_string($charset . '..' . $convcharset, $what);
233 case PMA_CHARSET_ICONV
:
234 return iconv($charset, $convcharset . $cfg['IconvExtraParams'], $what);
236 case PMA_CHARSET_LIBICONV
:
237 return libiconv($charset, $convcharset, $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 switch ($GLOBALS['PMA_recoding_engine']) {
261 case PMA_CHARSET_RECODE
:
262 return recode_string($src_charset . '..' . $dest_charset, $what);
264 case PMA_CHARSET_ICONV
:
265 return iconv($src_charset, $dest_charset . $GLOBALS['cfg']['IconvExtraParams'], $what);
267 case PMA_CHARSET_LIBICONV
:
268 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);
307 $dist = libiconv($src_charset, $dest_charset, $line);
321 } // end of the "PMA_convert_file()" function
323 } // $__PMA_CHARSET_CONVERSION_LIB__