2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Charset conversion functions.
8 if (! defined('PHPMYADMIN')) {
13 * Loads the recode or iconv extensions if any of it is not loaded yet
15 if (isset($cfg['AllowAnywhereRecoding'])
16 && $cfg['AllowAnywhereRecoding']) {
18 if ($cfg['RecodingEngine'] == 'recode') {
19 if (!@extension_loaded
('recode')) {
20 echo $strCantLoadRecodeIconv;
23 $PMA_recoding_engine = 'recode';
24 } elseif ($cfg['RecodingEngine'] == 'iconv') {
25 if (!@extension_loaded
('iconv')) {
26 echo $strCantLoadRecodeIconv;
29 $PMA_recoding_engine = 'iconv';
31 if (@extension_loaded
('iconv')) {
32 $PMA_recoding_engine = 'iconv';
33 } elseif (@extension_loaded
('recode')) {
34 $PMA_recoding_engine = 'recode';
36 echo $strCantLoadRecodeIconv;
40 } // end load recode/iconv extension
42 define('PMA_CHARSET_NONE', 0);
43 define('PMA_CHARSET_ICONV', 1);
44 define('PMA_CHARSET_LIBICONV', 2);
45 define('PMA_CHARSET_RECODE', 3);
46 define('PMA_CHARSET_ICONV_AIX', 4);
48 if (!isset($cfg['IconvExtraParams'])) {
49 $cfg['IconvExtraParams'] = '';
52 // Finally detect which function we will use:
53 if (isset($cfg['AllowAnywhereRecoding'])
54 && $cfg['AllowAnywhereRecoding']) {
56 if (!isset($PMA_recoding_engine)) {
57 $PMA_recoding_engine = $cfg['RecodingEngine'];
59 if ($PMA_recoding_engine == 'iconv') {
60 if (@function_exists
('iconv')) {
61 if ((@stristr
(PHP_OS
, 'AIX')) && (@strcasecmp
(ICONV_IMPL
, 'unknown') == 0) && (@strcasecmp
(ICONV_VERSION
, 'unknown') == 0)) {
62 $PMA_recoding_engine = PMA_CHARSET_ICONV_AIX
;
64 $PMA_recoding_engine = PMA_CHARSET_ICONV
;
66 } elseif (@function_exists
('libiconv')) {
67 $PMA_recoding_engine = PMA_CHARSET_LIBICONV
;
69 $PMA_recoding_engine = PMA_CHARSET_NONE
;
71 if (!isset($GLOBALS['is_header_sent'])) {
72 include './libraries/header.inc.php';
74 echo $strCantUseRecodeIconv;
75 require_once './libraries/footer.inc.php';
78 } elseif ($PMA_recoding_engine == 'recode') {
79 if (@function_exists
('recode_string')) {
80 $PMA_recoding_engine = PMA_CHARSET_RECODE
;
82 $PMA_recoding_engine = PMA_CHARSET_NONE
;
84 require_once './libraries/header.inc.php';
85 echo $strCantUseRecodeIconv;
86 require_once './libraries/footer.inc.php';
90 if (@function_exists
('iconv')) {
91 if ((@stristr
(PHP_OS
, 'AIX')) && (@strcasecmp
(ICONV_IMPL
, 'unknown') == 0) && (@strcasecmp
(ICONV_VERSION
, 'unknown') == 0)) {
92 $PMA_recoding_engine = PMA_CHARSET_ICONV_AIX
;
94 $PMA_recoding_engine = PMA_CHARSET_ICONV
;
96 } elseif (@function_exists
('libiconv')) {
97 $PMA_recoding_engine = PMA_CHARSET_LIBICONV
;
98 } elseif (@function_exists
('recode_string')) {
99 $PMA_recoding_engine = PMA_CHARSET_RECODE
;
101 $PMA_recoding_engine = PMA_CHARSET_NONE
;
103 require_once './libraries/header.inc.php';
104 echo $strCantUseRecodeIconv;
105 require_once './libraries/footer.inc.php';
110 $PMA_recoding_engine = PMA_CHARSET_NONE
;
113 /* Load AIX iconv wrapper if needed */
114 if ($PMA_recoding_engine == PMA_CHARSET_ICONV_AIX
) {
115 require_once './libraries/iconv_wrapper.lib.php';
119 * Converts encoding of text according to current settings.
121 * @param string what to convert
123 * @return string converted text
125 * @global array the configuration array
126 * @global boolean whether recoding is allowed or not
127 * @global string the current charset
128 * @global array the charset to convert to
134 function PMA_convert_charset($what) {
135 global $cfg, $charset, $convcharset;
137 if (!(isset($cfg['AllowAnywhereRecoding']) && $cfg['AllowAnywhereRecoding'] )
138 ||
$convcharset == $charset) { // rabus: if input and output charset are the same, we don't have to do anything...
141 switch ($GLOBALS['PMA_recoding_engine']) {
142 case PMA_CHARSET_RECODE
:
143 return recode_string($charset . '..' . $convcharset, $what);
144 case PMA_CHARSET_ICONV
:
145 return iconv($charset, $convcharset . $cfg['IconvExtraParams'], $what);
146 case PMA_CHARSET_ICONV_AIX
:
147 return PMA_aix_iconv_wrapper($charset, $convcharset . $cfg['IconvExtraParams'], $what);
148 case PMA_CHARSET_LIBICONV
:
149 return libiconv($charset, $convcharset . $GLOBALS['cfg']['IconvExtraParams'], $what);
154 } // end of the "PMA_convert_charset()" function
157 * Converts encoding of text according to parameters with detected
158 * conversion function.
160 * @param string source charset
161 * @param string target charset
162 * @param string what to convert
164 * @return string converted text
170 function PMA_convert_string($src_charset, $dest_charset, $what) {
171 if ($src_charset == $dest_charset) {
174 switch ($GLOBALS['PMA_recoding_engine']) {
175 case PMA_CHARSET_RECODE
:
176 return recode_string($src_charset . '..' . $dest_charset, $what);
177 case PMA_CHARSET_ICONV
:
178 return iconv($src_charset, $dest_charset . $GLOBALS['cfg']['IconvExtraParams'], $what);
179 case PMA_CHARSET_ICONV_AIX
:
180 return PMA_aix_iconv_wrapper($src_charset, $dest_charset . $GLOBALS['cfg']['IconvExtraParams'], $what);
181 case PMA_CHARSET_LIBICONV
:
182 return libiconv($src_charset, $dest_charset, $what);
186 } // end of the "PMA_convert_string()" function
190 * Converts encoding of file according to parameters with detected
191 * conversion function. The old file will be unlinked and new created and
192 * its file name is returned.
194 * @param string source charset
195 * @param string target charset
196 * @param string file to convert
198 * @return string new temporay file
204 function PMA_convert_file($src_charset, $dest_charset, $file) {
205 switch ($GLOBALS['PMA_recoding_engine']) {
206 case PMA_CHARSET_RECODE
:
207 case PMA_CHARSET_ICONV
:
208 case PMA_CHARSET_LIBICONV
:
209 $tmpfname = tempnam('', 'PMA_convert_file');
210 $fin = fopen($file, 'r');
211 $fout = fopen($tmpfname, 'w');
212 if ($GLOBALS['PMA_recoding_engine'] == PMA_CHARSET_RECODE
) {
213 recode_file($src_charset . '..' . $dest_charset, $fin, $fout);
215 while (!feof($fin)) {
216 $line = fgets($fin, 4096);
217 if ($GLOBALS['PMA_recoding_engine'] == PMA_CHARSET_ICONV
) {
218 $dist = iconv($src_charset, $dest_charset . $GLOBALS['cfg']['IconvExtraParams'], $line);
219 } elseif ($GLOBALS['PMA_recoding_engine'] == PMA_CHARSET_ICONV_AIX
) {
220 $dist = PMA_aix_iconv_wrapper($src_charset, $dest_charset . $GLOBALS['cfg']['IconvExtraParams'], $line);
222 $dist = libiconv($src_charset, $dest_charset . $GLOBALS['cfg']['IconvExtraParams'], $line);
235 } // end of the "PMA_convert_file()" function