3.3.8-dev
[phpmyadmin/last10db.git] / libraries / charset_conversion.lib.php
blob035a0b87e7207187d2bf0c6dc9625fdf935d1a84
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Charset conversion functions.
6 * @version $Id$
7 * @package phpMyAdmin
8 */
9 if (! defined('PHPMYADMIN')) {
10 exit;
13 /**
14 * Loads the recode or iconv extensions if any of it is not loaded yet
16 if (isset($cfg['AllowAnywhereRecoding'])
17 && $cfg['AllowAnywhereRecoding']) {
19 if ($cfg['RecodingEngine'] == 'recode') {
20 if (!@extension_loaded('recode')) {
21 echo $strCantLoadRecodeIconv;
22 exit;
24 $PMA_recoding_engine = 'recode';
25 } elseif ($cfg['RecodingEngine'] == 'iconv') {
26 if (!@extension_loaded('iconv')) {
27 echo $strCantLoadRecodeIconv;
28 exit;
30 $PMA_recoding_engine = 'iconv';
31 } else {
32 if (@extension_loaded('iconv')) {
33 $PMA_recoding_engine = 'iconv';
34 } elseif (@extension_loaded('recode')) {
35 $PMA_recoding_engine = 'recode';
36 } else {
37 echo $strCantLoadRecodeIconv;
38 exit;
41 } // end load recode/iconv extension
43 define('PMA_CHARSET_NONE', 0);
44 define('PMA_CHARSET_ICONV', 1);
45 define('PMA_CHARSET_LIBICONV', 2);
46 define('PMA_CHARSET_RECODE', 3);
47 define('PMA_CHARSET_ICONV_AIX', 4);
49 if (!isset($cfg['IconvExtraParams'])) {
50 $cfg['IconvExtraParams'] = '';
53 // Finally detect which function we will use:
54 if (isset($cfg['AllowAnywhereRecoding'])
55 && $cfg['AllowAnywhereRecoding']) {
57 if (!isset($PMA_recoding_engine)) {
58 $PMA_recoding_engine = $cfg['RecodingEngine'];
60 if ($PMA_recoding_engine == 'iconv') {
61 if (@function_exists('iconv')) {
62 if ((@stristr(PHP_OS, 'AIX')) && (@strcasecmp(ICONV_IMPL, 'unknown') == 0) && (@strcasecmp(ICONV_VERSION, 'unknown') == 0)) {
63 $PMA_recoding_engine = PMA_CHARSET_ICONV_AIX;
64 } else {
65 $PMA_recoding_engine = PMA_CHARSET_ICONV;
67 } elseif (@function_exists('libiconv')) {
68 $PMA_recoding_engine = PMA_CHARSET_LIBICONV;
69 } else {
70 $PMA_recoding_engine = PMA_CHARSET_NONE;
72 if (!isset($GLOBALS['is_header_sent'])) {
73 include './libraries/header.inc.php';
75 echo $strCantUseRecodeIconv;
76 require_once './libraries/footer.inc.php';
77 exit();
79 } elseif ($PMA_recoding_engine == 'recode') {
80 if (@function_exists('recode_string')) {
81 $PMA_recoding_engine = PMA_CHARSET_RECODE;
82 } else {
83 $PMA_recoding_engine = PMA_CHARSET_NONE;
85 require_once './libraries/header.inc.php';
86 echo $strCantUseRecodeIconv;
87 require_once './libraries/footer.inc.php';
88 exit;
90 } else {
91 if (@function_exists('iconv')) {
92 if ((@stristr(PHP_OS, 'AIX')) && (@strcasecmp(ICONV_IMPL, 'unknown') == 0) && (@strcasecmp(ICONV_VERSION, 'unknown') == 0)) {
93 $PMA_recoding_engine = PMA_CHARSET_ICONV_AIX;
94 } else {
95 $PMA_recoding_engine = PMA_CHARSET_ICONV;
97 } elseif (@function_exists('libiconv')) {
98 $PMA_recoding_engine = PMA_CHARSET_LIBICONV;
99 } elseif (@function_exists('recode_string')) {
100 $PMA_recoding_engine = PMA_CHARSET_RECODE;
101 } else {
102 $PMA_recoding_engine = PMA_CHARSET_NONE;
104 require_once './libraries/header.inc.php';
105 echo $strCantUseRecodeIconv;
106 require_once './libraries/footer.inc.php';
107 exit;
110 } else {
111 $PMA_recoding_engine = PMA_CHARSET_NONE;
114 /* Load AIX iconv wrapper if needed */
115 if ($PMA_recoding_engine == PMA_CHARSET_ICONV_AIX) {
116 require_once './libraries/iconv_wrapper.lib.php';
120 * Converts encoding of text according to current settings.
122 * @param string what to convert
124 * @return string converted text
126 * @global array the configuration array
127 * @global boolean whether recoding is allowed or not
128 * @global string the current charset
129 * @global array the charset to convert to
131 * @access public
133 * @author nijel
135 function PMA_convert_charset($what) {
136 global $cfg, $charset, $convcharset;
138 if (!(isset($cfg['AllowAnywhereRecoding']) && $cfg['AllowAnywhereRecoding'] )
139 || $convcharset == $charset) { // rabus: if input and output charset are the same, we don't have to do anything...
140 return $what;
141 } else {
142 switch ($GLOBALS['PMA_recoding_engine']) {
143 case PMA_CHARSET_RECODE:
144 return recode_string($charset . '..' . $convcharset, $what);
145 case PMA_CHARSET_ICONV:
146 return iconv($charset, $convcharset . $cfg['IconvExtraParams'], $what);
147 case PMA_CHARSET_ICONV_AIX:
148 return PMA_aix_iconv_wrapper($charset, $convcharset . $cfg['IconvExtraParams'], $what);
149 case PMA_CHARSET_LIBICONV:
150 return libiconv($charset, $convcharset . $GLOBALS['cfg']['IconvExtraParams'], $what);
151 default:
152 return $what;
155 } // end of the "PMA_convert_charset()" function
158 * Converts encoding of text according to parameters with detected
159 * conversion function.
161 * @param string source charset
162 * @param string target charset
163 * @param string what to convert
165 * @return string converted text
167 * @access public
169 * @author nijel
171 function PMA_convert_string($src_charset, $dest_charset, $what) {
172 if ($src_charset == $dest_charset) {
173 return $what;
175 switch ($GLOBALS['PMA_recoding_engine']) {
176 case PMA_CHARSET_RECODE:
177 return recode_string($src_charset . '..' . $dest_charset, $what);
178 case PMA_CHARSET_ICONV:
179 return iconv($src_charset, $dest_charset . $GLOBALS['cfg']['IconvExtraParams'], $what);
180 case PMA_CHARSET_ICONV_AIX:
181 return PMA_aix_iconv_wrapper($src_charset, $dest_charset . $GLOBALS['cfg']['IconvExtraParams'], $what);
182 case PMA_CHARSET_LIBICONV:
183 return libiconv($src_charset, $dest_charset, $what);
184 default:
185 return $what;
187 } // end of the "PMA_convert_string()" function
191 * Converts encoding of file according to parameters with detected
192 * conversion function. The old file will be unlinked and new created and
193 * its file name is returned.
195 * @param string source charset
196 * @param string target charset
197 * @param string file to convert
199 * @return string new temporay file
201 * @access public
203 * @author nijel
205 function PMA_convert_file($src_charset, $dest_charset, $file) {
206 switch ($GLOBALS['PMA_recoding_engine']) {
207 case PMA_CHARSET_RECODE:
208 case PMA_CHARSET_ICONV:
209 case PMA_CHARSET_LIBICONV:
210 $tmpfname = tempnam('', 'PMA_convert_file');
211 $fin = fopen($file, 'r');
212 $fout = fopen($tmpfname, 'w');
213 if ($GLOBALS['PMA_recoding_engine'] == PMA_CHARSET_RECODE) {
214 recode_file($src_charset . '..' . $dest_charset, $fin, $fout);
215 } else {
216 while (!feof($fin)) {
217 $line = fgets($fin, 4096);
218 if ($GLOBALS['PMA_recoding_engine'] == PMA_CHARSET_ICONV) {
219 $dist = iconv($src_charset, $dest_charset . $GLOBALS['cfg']['IconvExtraParams'], $line);
220 } elseif ($GLOBALS['PMA_recoding_engine'] == PMA_CHARSET_ICONV_AIX) {
221 $dist = PMA_aix_iconv_wrapper($src_charset, $dest_charset . $GLOBALS['cfg']['IconvExtraParams'], $line);
222 } else {
223 $dist = libiconv($src_charset, $dest_charset . $GLOBALS['cfg']['IconvExtraParams'], $line);
225 fputs($fout, $dist);
226 } // end while
228 fclose($fin);
229 fclose($fout);
230 unlink($file);
232 return $tmpfname;
233 default:
234 return $file;
236 } // end of the "PMA_convert_file()" function