lang
[phpmyadmin/crack.git] / libraries / charset_conversion.lib.php3
blob9cf4842d0b030a70aa6984a3735a617c4a8858ec
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 * (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']
24 && $allow_recoding
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')) {
29 if (PMA_IS_WINDOWS) {
30 $suffix = '.dll';
31 } else {
32 $suffix = '.so';
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;
45 exit();
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;
54 exit();
57 $PMA_recoding_engine = 'iconv';
58 } else {
59 if (@extension_loaded('iconv')) {
60 $PMA_recoding_engine = 'iconv';
61 } else if (@extension_loaded('recode')) {
62 $PMA_recoding_engine = 'recode';
63 } else {
64 @dl('iconv' . $suffix);
65 if (!@extension_loaded('iconv')) {
66 @dl('recode' . $suffix);
67 if (!@extension_loaded('recode')) {
68 echo $strCantLoadRecodeIconv;
69 exit();
70 } else {
71 $PMA_recoding_engine = 'recode';
73 } else {
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']
88 && $allow_recoding) {
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;
98 } else {
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');
106 exit();
108 } else if ($PMA_recoding_engine == 'recode') {
109 if (@function_exists('recode_string')) {
110 $PMA_recoding_engine = PMA_CHARSET_RECODE;
111 } else {
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');
119 exit();
121 } else {
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;
128 } else {
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');
136 exit();
139 } else {
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
156 * @access public
158 * @author nijel
160 function PMA_convert_display_charset($what) {
161 global $cfg, $allow_recoding, $charset, $convcharset;
163 if (!(isset($cfg['AllowAnywhereRecoding']) && $cfg['AllowAnywhereRecoding'] && $allow_recoding)) {
164 return $what;
166 else if (is_array($what)) {
167 $result = array();
168 reset($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);
173 } else {
174 $result[$key] = PMA_convert_display_charset($val);
176 } else {
177 $result[$key] = $val;
179 } // end while
180 return $result;
182 else if (is_string($what)) {
183 switch ($GLOBALS['PMA_recoding_engine']) {
184 case PMA_CHARSET_RECODE:
185 return recode_string($convcharset . '..' . $charset, $what);
186 break;
187 case PMA_CHARSET_ICONV:
188 return iconv($convcharset, $charset, $what);
189 break;
190 case PMA_CHARSET_LIBICONV:
191 return libiconv($convcharset, $charset, $what);
192 break;
193 default:
194 return $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);
208 return $what;
210 else {
211 // when we don't know what it is we don't touch it...
212 return $what;
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
229 * @access public
231 * @author nijel
233 function PMA_convert_charset($what) {
234 global $cfg, $allow_recoding, $charset, $convcharset;
236 if (!(isset($cfg['AllowAnywhereRecoding']) && $cfg['AllowAnywhereRecoding'] && $allow_recoding)) {
237 return $what;
238 } else {
239 switch ($GLOBALS['PMA_recoding_engine']) {
240 case PMA_CHARSET_RECODE:
241 return recode_string($charset . '..' . $convcharset, $what);
242 break;
243 case PMA_CHARSET_ICONV:
244 return iconv($charset, $convcharset, $what);
245 break;
246 case PMA_CHARSET_LIBICONV:
247 return libiconv($charset, $convcharset, $what);
248 break;
249 default:
250 return $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
265 * @access public
267 * @author nijel
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);
273 break;
274 case PMA_CHARSET_ICONV:
275 return iconv($src_charset, $dest_charset, $what);
276 break;
277 case PMA_CHARSET_LIBICONV:
278 return libiconv($src_charset, $dest_charset, $what);
279 break;
280 default:
281 return $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
297 * @access public
299 * @author nijel
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);
311 } else {
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);
316 } else {
317 $dist = libiconv($src_charset, $dest_charset, $line);
319 fputs($fout, $dist);
320 } // end while
322 fclose($fin);
323 fclose($fout);
324 unlink($file);
326 return $tmpfname;
327 break;
328 default:
329 return $file;
331 } // end of the "PMA_convert_file()" function
333 } // $__PMA_CHARSET_CONVERSION_LIB__