3 // vim: expandtab sw=4 ts=4 sts=4:
7 * Charset conversion functions.
12 * Loads the recode or iconv extensions if any of it is not loaded yet
14 if (isset($cfg['AllowAnywhereRecoding'])
15 && $cfg['AllowAnywhereRecoding']
18 if ($cfg['RecodingEngine'] == 'recode') {
19 if (!@extension_loaded
('recode')) {
21 if (!@extension_loaded
('recode')) {
22 echo $strCantLoadRecodeIconv;
26 $PMA_recoding_engine = 'recode';
27 } else if ($cfg['RecodingEngine'] == 'iconv') {
28 if (!@extension_loaded
('iconv')) {
30 if (!@extension_loaded
('iconv')) {
31 echo $strCantLoadRecodeIconv;
35 $PMA_recoding_engine = 'iconv';
37 if (@extension_loaded
('iconv')) {
38 $PMA_recoding_engine = 'iconv';
39 } else if (@extension_loaded
('recode')) {
40 $PMA_recoding_engine = 'recode';
43 if (!@extension_loaded
('iconv')) {
45 if (!@extension_loaded
('recode')) {
46 echo $strCantLoadRecodeIconv;
49 $PMA_recoding_engine = 'recode';
52 $PMA_recoding_engine = 'iconv';
56 } // end load recode/iconv extension
58 define('PMA_CHARSET_NONE', 0);
59 define('PMA_CHARSET_ICONV', 1);
60 define('PMA_CHARSET_LIBICONV', 2);
61 define('PMA_CHARSET_RECODE', 3);
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 $PMA_recoding_engine = PMA_CHARSET_ICONV
;
78 } else if (@function_exists
('libiconv')) {
79 $PMA_recoding_engine = PMA_CHARSET_LIBICONV
;
81 $PMA_recoding_engine = PMA_CHARSET_NONE
;
83 if (!isset($GLOBALS['is_header_sent'])) {
84 include('./header.inc.php');
86 echo $strCantUseRecodeIconv;
87 require_once('./footer.inc.php');
90 } else if ($PMA_recoding_engine == 'recode') {
91 if (@function_exists
('recode_string')) {
92 $PMA_recoding_engine = PMA_CHARSET_RECODE
;
94 $PMA_recoding_engine = PMA_CHARSET_NONE
;
96 require_once('./header.inc.php');
97 echo $strCantUseRecodeIconv;
98 require_once('./footer.inc.php');
102 if (@function_exists
('iconv')) {
103 $PMA_recoding_engine = PMA_CHARSET_ICONV
;
104 } else if (@function_exists
('libiconv')) {
105 $PMA_recoding_engine = PMA_CHARSET_LIBICONV
;
106 } elseif (@function_exists
('recode_string')) {
107 $PMA_recoding_engine = PMA_CHARSET_RECODE
;
109 $PMA_recoding_engine = PMA_CHARSET_NONE
;
111 require_once('./header.inc.php');
112 echo $strCantUseRecodeIconv;
113 require_once('./footer.inc.php');
118 $PMA_recoding_engine = PMA_CHARSET_NONE
;
123 * Converts encoding according to current settings.
125 * @param mixed what to convert (string or array of strings or object returned by mysql_fetch_field)
127 * @return string converted string or array of strings
129 * @global array the configuration array
130 * @global boolean whether recoding is allowed or not
131 * @global string the current charset
132 * @global array the charset to convert to
138 function PMA_convert_display_charset($what) {
139 global $cfg, $allow_recoding, $charset, $convcharset;
141 if (!(isset($cfg['AllowAnywhereRecoding']) && $cfg['AllowAnywhereRecoding'] && $allow_recoding)
142 ||
$convcharset == $charset // rabus: if input and output charset are the same, we don't have to do anything...
143 // this constant is not defined before the login:
144 ||
(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
147 else if (is_array($what)) {
149 foreach ($what AS $key => $val) {
150 if (is_string($val) ||
is_array($val)) {
151 if (is_string($key)) {
152 $result[PMA_convert_display_charset($key)] = PMA_convert_display_charset($val);
154 $result[$key] = PMA_convert_display_charset($val);
157 $result[$key] = $val;
162 else if (is_string($what)) {
164 switch ($GLOBALS['PMA_recoding_engine']) {
165 case PMA_CHARSET_RECODE
:
166 return recode_string($convcharset . '..' . $charset, $what);
167 case PMA_CHARSET_ICONV
:
168 return iconv($convcharset, $charset . $cfg['IconvExtraParams'], $what);
169 case PMA_CHARSET_LIBICONV
:
170 return libiconv($convcharset, $charset, $what);
175 else if (is_object($what)) {
176 // isn't it object returned from mysql_fetch_field ?
177 if (@is_string
($what->name
)) {
178 $what->name
= PMA_convert_display_charset($what->name
);
180 if (@is_string
($what->table
)) {
181 $what->table
= PMA_convert_display_charset($what->table
);
183 if (@is_string
($what->Database
)) {
184 $what->Database
= PMA_convert_display_charset($what->Database
);
189 // when we don't know what it is we don't touch it...
192 } // end of the "PMA_convert_display_charset()" function
196 * Converts encoding of text according to current settings.
198 * @param string what to convert
200 * @return string converted text
202 * @global array the configuration array
203 * @global boolean whether recoding is allowed or not
204 * @global string the current charset
205 * @global array the charset to convert to
211 function PMA_convert_charset($what) {
212 global $cfg, $allow_recoding, $charset, $convcharset;
214 if (!(isset($cfg['AllowAnywhereRecoding']) && $cfg['AllowAnywhereRecoding'] && $allow_recoding)
215 ||
$convcharset == $charset) { // rabus: if input and output charset are the same, we don't have to do anything...
218 switch ($GLOBALS['PMA_recoding_engine']) {
219 case PMA_CHARSET_RECODE
:
220 return recode_string($charset . '..' . $convcharset, $what);
221 case PMA_CHARSET_ICONV
:
222 return iconv($charset, $convcharset . $cfg['IconvExtraParams'], $what);
223 case PMA_CHARSET_LIBICONV
:
224 return libiconv($charset, $convcharset, $what);
229 } // end of the "PMA_convert_charset()" function
232 * Converts encoding of text according to parameters with detected
233 * conversion function.
235 * @param string source charset
236 * @param string target charset
237 * @param string what to convert
239 * @return string converted text
245 function PMA_convert_string($src_charset, $dest_charset, $what) {
246 if ($src_charset == $dest_charset) return $what;
247 switch ($GLOBALS['PMA_recoding_engine']) {
248 case PMA_CHARSET_RECODE
:
249 return recode_string($src_charset . '..' . $dest_charset, $what);
250 case PMA_CHARSET_ICONV
:
251 return iconv($src_charset, $dest_charset . $GLOBALS['cfg']['IconvExtraParams'], $what);
252 case PMA_CHARSET_LIBICONV
:
253 return libiconv($src_charset, $dest_charset, $what);
257 } // end of the "PMA_convert_string()" function
261 * Converts encoding of file according to parameters with detected
262 * conversion function. The old file will be unlinked and new created and
263 * its file name is returned.
265 * @param string source charset
266 * @param string target charset
267 * @param string file to convert
269 * @return string new temporay file
275 function PMA_convert_file($src_charset, $dest_charset, $file) {
276 switch ($GLOBALS['PMA_recoding_engine']) {
277 case PMA_CHARSET_RECODE
:
278 case PMA_CHARSET_ICONV
:
279 case PMA_CHARSET_LIBICONV
:
280 $tmpfname = tempnam('', 'PMA_convert_file');
281 $fin = fopen($file, 'r');
282 $fout = fopen($tmpfname, 'w');
283 if ($GLOBALS['PMA_recoding_engine'] == PMA_CHARSET_RECODE
) {
284 recode_file($src_charset . '..' . $dest_charset, $fin, $fout);
286 while (!feof($fin)) {
287 $line = fgets($fin, 4096);
288 if ($GLOBALS['PMA_recoding_engine'] == PMA_CHARSET_ICONV
) {
289 $dist = iconv($src_charset, $dest_charset . $GLOBALS['cfg']['IconvExtraParams'], $line);
291 $dist = libiconv($src_charset, $dest_charset, $line);
304 } // end of the "PMA_convert_file()" function