Add link to wiki explaining configuration options.
[phpmyadmin/crack.git] / libraries / charset_conversion.lib.php
blobb4b88415ec8e35d64967048a57390031e1a82458
1 <?php
2 /* $Id$ */
3 // vim: expandtab sw=4 ts=4 sts=4:
6 /**
7 * Charset conversion functions.
8 */
11 /**
12 * Loads the recode or iconv extensions if any of it is not loaded yet
14 if (isset($cfg['AllowAnywhereRecoding'])
15 && $cfg['AllowAnywhereRecoding']
16 && $allow_recoding) {
18 if ($cfg['RecodingEngine'] == 'recode') {
19 if (!@extension_loaded('recode')) {
20 PMA_dl('recode');
21 if (!@extension_loaded('recode')) {
22 echo $strCantLoadRecodeIconv;
23 exit;
26 $PMA_recoding_engine = 'recode';
27 } elseif ($cfg['RecodingEngine'] == 'iconv') {
28 if (!@extension_loaded('iconv')) {
29 PMA_dl('iconv');
30 if (!@extension_loaded('iconv')) {
31 echo $strCantLoadRecodeIconv;
32 exit;
35 $PMA_recoding_engine = 'iconv';
36 } else {
37 if (@extension_loaded('iconv')) {
38 $PMA_recoding_engine = 'iconv';
39 } elseif (@extension_loaded('recode')) {
40 $PMA_recoding_engine = 'recode';
41 } else {
42 PMA_dl('iconv');
43 if (!@extension_loaded('iconv')) {
44 PMA_dl('recode');
45 if (!@extension_loaded('recode')) {
46 echo $strCantLoadRecodeIconv;
47 exit;
48 } else {
49 $PMA_recoding_engine = 'recode';
51 } else {
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);
62 define('PMA_CHARSET_ICONV_AIX', 4);
64 if (!isset($cfg['IconvExtraParams'])) {
65 $cfg['IconvExtraParams'] = '';
68 // Finally detects which function will we use:
69 if (isset($cfg['AllowAnywhereRecoding'])
70 && $cfg['AllowAnywhereRecoding']
71 && $allow_recoding) {
73 if (!isset($PMA_recoding_engine)) {
74 $PMA_recoding_engine = $cfg['RecodingEngine'];
76 if ($PMA_recoding_engine == 'iconv') {
77 if (@function_exists('iconv')) {
78 if ((@stristr(PHP_OS, 'AIX')) && (@strcasecmp(ICONV_IMPL, 'unknown') == 0) && (@strcasecmp(ICONV_VERSION, 'unknown') == 0)) {
79 $PMA_recoding_engine = PMA_CHARSET_ICONV_AIX;
80 } else {
81 $PMA_recoding_engine = PMA_CHARSET_ICONV;
83 } elseif (@function_exists('libiconv')) {
84 $PMA_recoding_engine = PMA_CHARSET_LIBICONV;
85 } else {
86 $PMA_recoding_engine = PMA_CHARSET_NONE;
88 if (!isset($GLOBALS['is_header_sent'])) {
89 include('./libraries/header.inc.php');
91 echo $strCantUseRecodeIconv;
92 require_once('./libraries/footer.inc.php');
93 exit();
95 } elseif ($PMA_recoding_engine == 'recode') {
96 if (@function_exists('recode_string')) {
97 $PMA_recoding_engine = PMA_CHARSET_RECODE;
98 } else {
99 $PMA_recoding_engine = PMA_CHARSET_NONE;
101 require_once('./libraries/header.inc.php');
102 echo $strCantUseRecodeIconv;
103 require_once('./libraries/footer.inc.php');
104 exit;
106 } else {
107 if (@function_exists('iconv')) {
108 if ((@stristr(PHP_OS, 'AIX')) && (@strcasecmp(ICONV_IMPL, 'unknown') == 0) && (@strcasecmp(ICONV_VERSION, 'unknown') == 0)) {
109 $PMA_recoding_engine = PMA_CHARSET_ICONV_AIX;
110 } else {
111 $PMA_recoding_engine = PMA_CHARSET_ICONV;
113 } elseif (@function_exists('libiconv')) {
114 $PMA_recoding_engine = PMA_CHARSET_LIBICONV;
115 } elseif (@function_exists('recode_string')) {
116 $PMA_recoding_engine = PMA_CHARSET_RECODE;
117 } else {
118 $PMA_recoding_engine = PMA_CHARSET_NONE;
120 require_once('./libraries/header.inc.php');
121 echo $strCantUseRecodeIconv;
122 require_once('./libraries/footer.inc.php');
123 exit;
126 } else {
127 $PMA_recoding_engine = PMA_CHARSET_NONE;
130 /* Load AIX iconv wrapper if needed */
131 if ($PMA_recoding_engine == PMA_CHARSET_ICONV_AIX) {
132 require_once('./libraries/iconv_wrapper.lib.php');
136 * Converts encoding according to current settings.
138 * @param mixed what to convert (string or array of strings or object returned by mysql_fetch_field)
140 * @return string converted string or array of strings
142 * @global array the configuration array
143 * @global boolean whether recoding is allowed or not
144 * @global string the current charset
145 * @global array the charset to convert to
147 * @access public
149 * @author nijel
151 function PMA_convert_display_charset($what) {
152 global $cfg, $allow_recoding, $charset, $convcharset;
154 if (!(isset($cfg['AllowAnywhereRecoding']) && $cfg['AllowAnywhereRecoding'] && $allow_recoding)
155 || $convcharset == $charset // rabus: if input and output charset are the same, we don't have to do anything...
156 // this constant is not defined before the login:
157 || (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 return $what;
159 } elseif (is_array($what)) {
160 $result = array();
161 foreach ($what AS $key => $val) {
162 if (is_string($val) || is_array($val)) {
163 if (is_string($key)) {
164 $result[PMA_convert_display_charset($key)] = PMA_convert_display_charset($val);
165 } else {
166 $result[$key] = PMA_convert_display_charset($val);
168 } else {
169 $result[$key] = $val;
171 } // end while
172 return $result;
173 } elseif (is_string($what)) {
175 switch ($GLOBALS['PMA_recoding_engine']) {
176 case PMA_CHARSET_RECODE:
177 return recode_string($convcharset . '..' . $charset, $what);
178 case PMA_CHARSET_ICONV:
179 return iconv($convcharset, $charset . $cfg['IconvExtraParams'], $what);
180 case PMA_CHARSET_ICONV_AIX:
181 return PMA_aix_iconv_wrapper($convcharset, $charset . $cfg['IconvExtraParams'], $what);
182 case PMA_CHARSET_LIBICONV:
183 return libiconv($convcharset, $charset . $GLOBALS['cfg']['IconvExtraParams'], $what);
184 default:
185 return $what;
187 } elseif (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);
198 return $what;
199 } else {
200 // when we don't know what it is we don't touch it...
201 return $what;
203 } // end of the "PMA_convert_display_charset()" function
207 * Converts encoding of text according to current settings.
209 * @param string what to convert
211 * @return string converted text
213 * @global array the configuration array
214 * @global boolean whether recoding is allowed or not
215 * @global string the current charset
216 * @global array the charset to convert to
218 * @access public
220 * @author nijel
222 function PMA_convert_charset($what) {
223 global $cfg, $allow_recoding, $charset, $convcharset;
225 if (!(isset($cfg['AllowAnywhereRecoding']) && $cfg['AllowAnywhereRecoding'] && $allow_recoding)
226 || $convcharset == $charset) { // rabus: if input and output charset are the same, we don't have to do anything...
227 return $what;
228 } else {
229 switch ($GLOBALS['PMA_recoding_engine']) {
230 case PMA_CHARSET_RECODE:
231 return recode_string($charset . '..' . $convcharset, $what);
232 case PMA_CHARSET_ICONV:
233 return iconv($charset, $convcharset . $cfg['IconvExtraParams'], $what);
234 case PMA_CHARSET_ICONV_AIX:
235 return PMA_aix_iconv_wrapper($charset, $convcharset . $cfg['IconvExtraParams'], $what);
236 case PMA_CHARSET_LIBICONV:
237 return libiconv($charset, $convcharset . $GLOBALS['cfg']['IconvExtraParams'], $what);
238 default:
239 return $what;
242 } // end of the "PMA_convert_charset()" function
245 * Converts encoding of text according to parameters with detected
246 * conversion function.
248 * @param string source charset
249 * @param string target charset
250 * @param string what to convert
252 * @return string converted text
254 * @access public
256 * @author nijel
258 function PMA_convert_string($src_charset, $dest_charset, $what) {
259 if ($src_charset == $dest_charset) {
260 return $what;
262 switch ($GLOBALS['PMA_recoding_engine']) {
263 case PMA_CHARSET_RECODE:
264 return recode_string($src_charset . '..' . $dest_charset, $what);
265 case PMA_CHARSET_ICONV:
266 return iconv($src_charset, $dest_charset . $GLOBALS['cfg']['IconvExtraParams'], $what);
267 case PMA_CHARSET_ICONV_AIX:
268 return PMA_aix_iconv_wrapper($src_charset, $dest_charset . $GLOBALS['cfg']['IconvExtraParams'], $what);
269 case PMA_CHARSET_LIBICONV:
270 return libiconv($src_charset, $dest_charset, $what);
271 default:
272 return $what;
274 } // end of the "PMA_convert_string()" function
278 * Converts encoding of file according to parameters with detected
279 * conversion function. The old file will be unlinked and new created and
280 * its file name is returned.
282 * @param string source charset
283 * @param string target charset
284 * @param string file to convert
286 * @return string new temporay file
288 * @access public
290 * @author nijel
292 function PMA_convert_file($src_charset, $dest_charset, $file) {
293 switch ($GLOBALS['PMA_recoding_engine']) {
294 case PMA_CHARSET_RECODE:
295 case PMA_CHARSET_ICONV:
296 case PMA_CHARSET_LIBICONV:
297 $tmpfname = tempnam('', 'PMA_convert_file');
298 $fin = fopen($file, 'r');
299 $fout = fopen($tmpfname, 'w');
300 if ($GLOBALS['PMA_recoding_engine'] == PMA_CHARSET_RECODE) {
301 recode_file($src_charset . '..' . $dest_charset, $fin, $fout);
302 } else {
303 while (!feof($fin)) {
304 $line = fgets($fin, 4096);
305 if ($GLOBALS['PMA_recoding_engine'] == PMA_CHARSET_ICONV) {
306 $dist = iconv($src_charset, $dest_charset . $GLOBALS['cfg']['IconvExtraParams'], $line);
307 } elseif ($GLOBALS['PMA_recoding_engine'] == PMA_CHARSET_ICONV_AIX) {
308 $dist = PMA_aix_iconv_wrapper($src_charset, $dest_charset . $GLOBALS['cfg']['IconvExtraParams'], $line);
309 } else {
310 $dist = libiconv($src_charset, $dest_charset . $GLOBALS['cfg']['IconvExtraParams'], $line);
312 fputs($fout, $dist);
313 } // end while
315 fclose($fin);
316 fclose($fout);
317 unlink($file);
319 return $tmpfname;
320 default:
321 return $file;
323 } // end of the "PMA_convert_file()" function