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 * (do not put a "@" before the dl() because we want to see the error
18 * message: multithreaded web servers don't support dl() but we cannot
19 * detect if the server is multithreaded, and under PHP 4.2.1 at least,
20 * it reports that the function dl exists...)
22 if (isset($cfg['AllowAnywhereRecoding'])
23 && $cfg['AllowAnywhereRecoding']
25 && ((PMA_PHP_INT_VERSION
>= 40000 && !@ini_get
('safe_mode') && @ini_get
('enable_dl'))
26 ||
(PMA_PHP_INT_VERSION
< 40000 && PMA_PHP_INT_VERSION
> 30009 && !@get_cfg_var
('safe_mode')))
27 && @function_exists
('dl')) {
35 // Initializes configuration for default, if not set:
36 if (!isset($cfg['RecodingEngine'])) {
37 $cfg['RecodingEngine'] = 'auto';
40 if ($cfg['RecodingEngine'] == 'recode') {
41 if (!@extension_loaded
('recode')) {
42 @dl
('recode' . $suffix);
43 if (!@extension_loaded
('recode')) {
44 echo $strCantLoadRecodeIconv;
48 $PMA_recoding_engine = 'recode';
49 } else if ($cfg['RecodingEngine'] == 'iconv') {
50 if (!@extension_loaded
('iconv')) {
51 @dl
('iconv' . $suffix);
52 if (!@extension_loaded
('iconv')) {
53 echo $strCantLoadRecodeIconv;
57 $PMA_recoding_engine = 'iconv';
59 if (@extension_loaded
('iconv')) {
60 $PMA_recoding_engine = 'iconv';
61 } else if (@extension_loaded
('recode')) {
62 $PMA_recoding_engine = 'recode';
64 @dl
('iconv' . $suffix);
65 if (!@extension_loaded
('iconv')) {
66 @dl
('recode' . $suffix);
67 if (!@extension_loaded
('recode')) {
68 echo $strCantLoadRecodeIconv;
71 $PMA_recoding_engine = 'recode';
74 $PMA_recoding_engine = 'iconv';
78 } // end load recode/iconv extension
80 define('PMA_CHARSET_NONE', 0);
81 define('PMA_CHARSET_ICONV', 1);
82 define('PMA_CHARSET_LIBICONV', 2);
83 define('PMA_CHARSET_RECODE', 3);
85 // Finally detects which function will we use:
86 if (isset($cfg['AllowAnywhereRecoding'])
87 && $cfg['AllowAnywhereRecoding']
90 if (!isset($PMA_recoding_engine)) {
91 $PMA_recoding_engine = $cfg['RecodingEngine'];
93 if ($PMA_recoding_engine == 'iconv') {
94 if (@function_exists
('iconv')) {
95 $PMA_recoding_engine = PMA_CHARSET_ICONV
;
96 } else if (@function_exists
('libiconv')) {
97 $PMA_recoding_engine = PMA_CHARSET_LIBICONV
;
99 $PMA_recoding_engine = PMA_CHARSET_NONE
;
101 if (!isset($GLOBALS['is_header_sent'])) {
102 include('./header.inc.php3');
104 echo $strCantUseRecodeIconv;
105 include('./footer.inc.php3');
108 } else if ($PMA_recoding_engine == 'recode') {
109 if (@function_exists
('recode_string')) {
110 $PMA_recoding_engine = PMA_CHARSET_RECODE
;
112 $PMA_recoding_engine = PMA_CHARSET_NONE
;
114 if (!isset($GLOBALS['is_header_sent'])) {
115 include('./header.inc.php3');
117 echo $strCantUseRecodeIconv;
118 include('./footer.inc.php3');
122 if (@function_exists
('iconv')) {
123 $PMA_recoding_engine = PMA_CHARSET_ICONV
;
124 } else if (@function_exists
('libiconv')) {
125 $PMA_recoding_engine = PMA_CHARSET_LIBICONV
;
126 } elseif (@function_exists
('recode_string')) {
127 $PMA_recoding_engine = PMA_CHARSET_RECODE
;
129 $PMA_recoding_engine = PMA_CHARSET_NONE
;
131 if (!isset($GLOBALS['is_header_sent'])) {
132 include('./header.inc.php3');
134 echo $strCantUseRecodeIconv;
135 include('./footer.inc.php3');
140 $PMA_recoding_engine = PMA_CHARSET_NONE
;
145 * Converts encoding according to current settings.
147 * @param mixed what to convert (string or array of strings or object returned by mysql_fetch_field)
149 * @return string converted string or array of strings
151 * @global array the configuration array
152 * @global boolean whether recoding is allowed or not
153 * @global string the current charset
154 * @global array the charset to convert to
160 function PMA_convert_display_charset($what) {
161 global $cfg, $allow_recoding, $charset, $convcharset;
163 if (!(isset($cfg['AllowAnywhereRecoding']) && $cfg['AllowAnywhereRecoding'] && $allow_recoding)) {
166 else if (is_array($what)) {
169 while (list($key, $val) = each($what)) {
170 if (is_string($val) ||
is_array($val)) {
171 if (is_string($key)) {
172 $result[PMA_convert_display_charset($key)] = PMA_convert_display_charset($val);
174 $result[$key] = PMA_convert_display_charset($val);
177 $result[$key] = $val;
182 else if (is_string($what)) {
183 switch ($GLOBALS['PMA_recoding_engine']) {
184 case PMA_CHARSET_RECODE
:
185 return recode_string($convcharset . '..' . $charset, $what);
187 case PMA_CHARSET_ICONV
:
188 return iconv($convcharset, $charset, $what);
190 case PMA_CHARSET_LIBICONV
:
191 return libiconv($convcharset, $charset, $what);
197 else if (is_object($what)) {
198 // isn't it object returned from mysql_fetch_field ?
199 if (@is_string
($what->name
)) {
200 $what->name
= PMA_convert_display_charset($what->name
);
202 if (@is_string
($what->table
)) {
203 $what->table
= PMA_convert_display_charset($what->table
);
205 if (@is_string
($what->Database
)) {
206 $what->Database
= PMA_convert_display_charset($what->Database
);
211 // when we don't know what it is we don't touch it...
214 } // end of the "PMA_convert_display_charset()" function
218 * Converts encoding of text according to current settings.
220 * @param string what to convert
222 * @return string converted text
224 * @global array the configuration array
225 * @global boolean whether recoding is allowed or not
226 * @global string the current charset
227 * @global array the charset to convert to
233 function PMA_convert_charset($what) {
234 global $cfg, $allow_recoding, $charset, $convcharset;
236 if (!(isset($cfg['AllowAnywhereRecoding']) && $cfg['AllowAnywhereRecoding'] && $allow_recoding)) {
239 switch ($GLOBALS['PMA_recoding_engine']) {
240 case PMA_CHARSET_RECODE
:
241 return recode_string($charset . '..' . $convcharset, $what);
243 case PMA_CHARSET_ICONV
:
244 return iconv($charset, $convcharset, $what);
246 case PMA_CHARSET_LIBICONV
:
247 return libiconv($charset, $convcharset, $what);
253 } // end of the "PMA_convert_charset()" function
256 * Converts encoding of text according to parameters with detected
257 * conversion function.
259 * @param string source charset
260 * @param string target charset
261 * @param string what to convert
263 * @return string converted text
269 function PMA_convert_string($src_charset, $dest_charset, $what) {
270 switch ($GLOBALS['PMA_recoding_engine']) {
271 case PMA_CHARSET_RECODE
:
272 return recode_string($src_charset . '..' . $dest_charset, $what);
274 case PMA_CHARSET_ICONV
:
275 return iconv($src_charset, $dest_charset, $what);
277 case PMA_CHARSET_LIBICONV
:
278 return libiconv($src_charset, $dest_charset, $what);
283 } // end of the "PMA_convert_string()" function
287 * Converts encoding of file according to parameters with detected
288 * conversion function. The old file will be unlinked and new created and
289 * its file name is returned.
291 * @param string source charset
292 * @param string target charset
293 * @param string file to convert
295 * @return string new temporay file
301 function PMA_convert_file($src_charset, $dest_charset, $file) {
302 switch ($GLOBALS['PMA_recoding_engine']) {
303 case PMA_CHARSET_RECODE
:
304 case PMA_CHARSET_ICONV
:
305 case PMA_CHARSET_LIBICONV
:
306 $tmpfname = tempnam('', 'PMA_convert_file');
307 $fin = fopen($file, 'r');
308 $fout = fopen($tmpfname, 'w');
309 if ($GLOBALS['PMA_recoding_engine'] == PMA_CHARSET_RECODE
) {
310 recode_file($src_charset . '..' . $dest_charset, $fin, $fout);
312 while (!feof($fin)) {
313 $line = fgets($fin, 4096);
314 if ($GLOBALS['PMA_recoding_engine'] == PMA_CHARSET_ICONV
) {
315 $dist = iconv($src_charset, $dest_charset, $line);
317 $dist = libiconv($src_charset, $dest_charset, $line);
331 } // end of the "PMA_convert_file()" function
333 } // $__PMA_CHARSET_CONVERSION_LIB__