2.5.3-rc2
[phpmyadmin/crack.git] / libraries / charset_conversion.lib.php3
blob959cfb87fec31b4cdc3842555be76007519be42d
1 <?php
2 /* $Id$ */
3 // vim: expandtab sw=4 ts=4 sts=4:
6 /**
7 * Charset conversion functions.
8 */
11 if (!defined('PMA_CHARSET_CONVERSION_LIB_INCLUDED')){
12 define('PMA_CHARSET_CONVERSION_LIB_INCLUDED', 1);
14 /**
15 * Loads the recode or iconv extensions if any of it is not loaded yet
17 if (isset($cfg['AllowAnywhereRecoding'])
18 && $cfg['AllowAnywhereRecoding']
19 && $allow_recoding) {
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')) {
28 PMA_dl('recode');
29 if (!@extension_loaded('recode')) {
30 echo $strCantLoadRecodeIconv;
31 exit();
34 $PMA_recoding_engine = 'recode';
35 } else if ($cfg['RecodingEngine'] == 'iconv') {
36 if (!@extension_loaded('iconv')) {
37 PMA_dl('iconv');
38 if (!@extension_loaded('iconv')) {
39 echo $strCantLoadRecodeIconv;
40 exit();
43 $PMA_recoding_engine = 'iconv';
44 } else {
45 if (@extension_loaded('iconv')) {
46 $PMA_recoding_engine = 'iconv';
47 } else if (@extension_loaded('recode')) {
48 $PMA_recoding_engine = 'recode';
49 } else {
50 PMA_dl('iconv');
51 if (!@extension_loaded('iconv')) {
52 PMA_dl('recode');
53 if (!@extension_loaded('recode')) {
54 echo $strCantLoadRecodeIconv;
55 exit();
56 } else {
57 $PMA_recoding_engine = 'recode';
59 } else {
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']
78 && $allow_recoding) {
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;
88 } else {
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');
96 exit();
98 } else if ($PMA_recoding_engine == 'recode') {
99 if (@function_exists('recode_string')) {
100 $PMA_recoding_engine = PMA_CHARSET_RECODE;
101 } else {
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');
109 exit();
111 } else {
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;
118 } else {
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');
126 exit();
129 } else {
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
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 return $what;
156 else if (is_array($what)) {
157 $result = array();
158 reset($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);
163 } else {
164 $result[$key] = PMA_convert_display_charset($val);
166 } else {
167 $result[$key] = $val;
169 } // end while
170 return $result;
172 else if (is_string($what)) {
173 switch ($GLOBALS['PMA_recoding_engine']) {
174 case PMA_CHARSET_RECODE:
175 return recode_string($convcharset . '..' . $charset, $what);
176 break;
177 case PMA_CHARSET_ICONV:
178 return iconv($convcharset, $charset . $cfg['IconvExtraParams'], $what);
179 break;
180 case PMA_CHARSET_LIBICONV:
181 return libiconv($convcharset, $charset, $what);
182 break;
183 default:
184 return $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);
198 return $what;
200 else {
201 // when we don't know what it is we don't touch it...
202 return $what;
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
219 * @access public
221 * @author nijel
223 function PMA_convert_charset($what) {
224 global $cfg, $allow_recoding, $charset, $convcharset;
226 if (!(isset($cfg['AllowAnywhereRecoding']) && $cfg['AllowAnywhereRecoding'] && $allow_recoding)) {
227 return $what;
228 } else {
229 switch ($GLOBALS['PMA_recoding_engine']) {
230 case PMA_CHARSET_RECODE:
231 return recode_string($charset . '..' . $convcharset, $what);
232 break;
233 case PMA_CHARSET_ICONV:
234 return iconv($charset, $convcharset . $cfg['IconvExtraParams'], $what);
235 break;
236 case PMA_CHARSET_LIBICONV:
237 return libiconv($charset, $convcharset, $what);
238 break;
239 default:
240 return $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
255 * @access public
257 * @author nijel
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);
263 break;
264 case PMA_CHARSET_ICONV:
265 return iconv($src_charset, $dest_charset . $GLOBALS['cfg']['IconvExtraParams'], $what);
266 break;
267 case PMA_CHARSET_LIBICONV:
268 return libiconv($src_charset, $dest_charset, $what);
269 break;
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 } else {
307 $dist = libiconv($src_charset, $dest_charset, $line);
309 fputs($fout, $dist);
310 } // end while
312 fclose($fin);
313 fclose($fout);
314 unlink($file);
316 return $tmpfname;
317 break;
318 default:
319 return $file;
321 } // end of the "PMA_convert_file()" function
323 } // $__PMA_CHARSET_CONVERSION_LIB__