sorry, wrong version checked in
[phpmyadmin/arisferyanto.git] / libraries / charset_conversion.lib.php
blob000c981015c8c6c66880fe5be7426c7beac0d5ce
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 } else if ($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 } else if (@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);
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 $PMA_recoding_engine = PMA_CHARSET_ICONV;
78 } else if (@function_exists('libiconv')) {
79 $PMA_recoding_engine = PMA_CHARSET_LIBICONV;
80 } else {
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');
88 exit();
90 } else if ($PMA_recoding_engine == 'recode') {
91 if (@function_exists('recode_string')) {
92 $PMA_recoding_engine = PMA_CHARSET_RECODE;
93 } else {
94 $PMA_recoding_engine = PMA_CHARSET_NONE;
96 require_once('./header.inc.php');
97 echo $strCantUseRecodeIconv;
98 require_once('./footer.inc.php');
99 exit;
101 } else {
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;
108 } else {
109 $PMA_recoding_engine = PMA_CHARSET_NONE;
111 require_once('./header.inc.php');
112 echo $strCantUseRecodeIconv;
113 require_once('./footer.inc.php');
114 exit;
117 } else {
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
134 * @access public
136 * @author nijel
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
145 return $what;
147 else if (is_array($what)) {
148 $result = array();
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);
153 } else {
154 $result[$key] = PMA_convert_display_charset($val);
156 } else {
157 $result[$key] = $val;
159 } // end while
160 return $result;
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);
171 default:
172 return $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);
186 return $what;
188 else {
189 // when we don't know what it is we don't touch it...
190 return $what;
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
207 * @access public
209 * @author nijel
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...
216 return $what;
217 } else {
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);
225 default:
226 return $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
241 * @access public
243 * @author nijel
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);
254 default:
255 return $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
271 * @access public
273 * @author nijel
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);
285 } else {
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);
290 } else {
291 $dist = libiconv($src_charset, $dest_charset, $line);
293 fputs($fout, $dist);
294 } // end while
296 fclose($fin);
297 fclose($fout);
298 unlink($file);
300 return $tmpfname;
301 default:
302 return $file;
304 } // end of the "PMA_convert_file()" function