Italian update
[phpmyadmin/sankalp_k.git] / libraries / charset_conversion.lib.php
blob8ec0fa3b9c6d48a97c88e41235e15e75d2db7fec
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Charset conversion functions.
6 * @version $Id$
7 */
10 /**
11 * Loads the recode or iconv extensions if any of it is not loaded yet
13 if (isset($cfg['AllowAnywhereRecoding'])
14 && $cfg['AllowAnywhereRecoding']
15 && $allow_recoding) {
17 if ($cfg['RecodingEngine'] == 'recode') {
18 if (!@extension_loaded('recode')) {
19 PMA_dl('recode');
20 if (!@extension_loaded('recode')) {
21 echo $strCantLoadRecodeIconv;
22 exit;
25 $PMA_recoding_engine = 'recode';
26 } elseif ($cfg['RecodingEngine'] == 'iconv') {
27 if (!@extension_loaded('iconv')) {
28 PMA_dl('iconv');
29 if (!@extension_loaded('iconv')) {
30 echo $strCantLoadRecodeIconv;
31 exit;
34 $PMA_recoding_engine = 'iconv';
35 } else {
36 if (@extension_loaded('iconv')) {
37 $PMA_recoding_engine = 'iconv';
38 } elseif (@extension_loaded('recode')) {
39 $PMA_recoding_engine = 'recode';
40 } else {
41 PMA_dl('iconv');
42 if (!@extension_loaded('iconv')) {
43 PMA_dl('recode');
44 if (!@extension_loaded('recode')) {
45 echo $strCantLoadRecodeIconv;
46 exit;
47 } else {
48 $PMA_recoding_engine = 'recode';
50 } else {
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']
70 && $allow_recoding) {
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;
79 } else {
80 $PMA_recoding_engine = PMA_CHARSET_ICONV;
82 } elseif (@function_exists('libiconv')) {
83 $PMA_recoding_engine = PMA_CHARSET_LIBICONV;
84 } else {
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';
92 exit();
94 } elseif ($PMA_recoding_engine == 'recode') {
95 if (@function_exists('recode_string')) {
96 $PMA_recoding_engine = PMA_CHARSET_RECODE;
97 } else {
98 $PMA_recoding_engine = PMA_CHARSET_NONE;
100 require_once './libraries/header.inc.php';
101 echo $strCantUseRecodeIconv;
102 require_once './libraries/footer.inc.php';
103 exit;
105 } else {
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;
109 } else {
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;
116 } else {
117 $PMA_recoding_engine = PMA_CHARSET_NONE;
119 require_once './libraries/header.inc.php';
120 echo $strCantUseRecodeIconv;
121 require_once './libraries/footer.inc.php';
122 exit;
125 } else {
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
146 * @access public
148 * @author nijel
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
157 return $what;
158 } elseif (is_array($what)) {
159 $result = array();
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);
164 } else {
165 $result[$key] = PMA_convert_display_charset($val);
167 } else {
168 $result[$key] = $val;
170 } // end while
171 return $result;
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);
183 default:
184 return $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);
197 return $what;
198 } else {
199 // when we don't know what it is we don't touch it...
200 return $what;
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
217 * @access public
219 * @author nijel
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...
226 return $what;
227 } else {
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);
237 default:
238 return $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
253 * @access public
255 * @author nijel
257 function PMA_convert_string($src_charset, $dest_charset, $what) {
258 if ($src_charset == $dest_charset) {
259 return $what;
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);
270 default:
271 return $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
287 * @access public
289 * @author nijel
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);
301 } else {
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);
308 } else {
309 $dist = libiconv($src_charset, $dest_charset . $GLOBALS['cfg']['IconvExtraParams'], $line);
311 fputs($fout, $dist);
312 } // end while
314 fclose($fin);
315 fclose($fout);
316 unlink($file);
318 return $tmpfname;
319 default:
320 return $file;
322 } // end of the "PMA_convert_file()" function