Revert initial commit
[phpmyadmin/blinky.git] / libraries / charset_conversion.lib.php
blobdbcb4fdc791841b2939633e78be0b65b063a1b91
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 * Failure on loading recode/iconv extensions.
16 function PMA_failRecoding() {
17 PMA_fatalError(__('Couldn\'t load the iconv or recode extension needed for charset conversion. Either configure PHP to enable these extensions or disable charset conversion in phpMyAdmin.'));
20 /**
21 * Loads the recode or iconv extensions if any of it is not loaded yet
23 if (isset($cfg['AllowAnywhereRecoding'])
24 && $cfg['AllowAnywhereRecoding']) {
26 if ($cfg['RecodingEngine'] == 'recode') {
27 if (!@extension_loaded('recode')) {
28 PMA_failRecoding();
30 $PMA_recoding_engine = 'recode';
31 } elseif ($cfg['RecodingEngine'] == 'iconv') {
32 if (!@extension_loaded('iconv')) {
33 PMA_failRecoding();
35 $PMA_recoding_engine = 'iconv';
36 } else {
37 if (@extension_loaded('iconv')) {
38 $PMA_recoding_engine = 'iconv';
39 } elseif (@extension_loaded('recode')) {
40 $PMA_recoding_engine = 'recode';
41 } else {
42 PMA_failRecoding();
45 } // end load recode/iconv extension
47 define('PMA_CHARSET_NONE', 0);
48 define('PMA_CHARSET_ICONV', 1);
49 define('PMA_CHARSET_LIBICONV', 2);
50 define('PMA_CHARSET_RECODE', 3);
51 define('PMA_CHARSET_ICONV_AIX', 4);
53 if (!isset($cfg['IconvExtraParams'])) {
54 $cfg['IconvExtraParams'] = '';
57 // Finally detect which function we will use:
58 if (isset($cfg['AllowAnywhereRecoding'])
59 && $cfg['AllowAnywhereRecoding']) {
61 if (!isset($PMA_recoding_engine)) {
62 $PMA_recoding_engine = $cfg['RecodingEngine'];
64 if ($PMA_recoding_engine == 'iconv') {
65 if (@function_exists('iconv')) {
66 if ((@stristr(PHP_OS, 'AIX')) && (@strcasecmp(ICONV_IMPL, 'unknown') == 0) && (@strcasecmp(ICONV_VERSION, 'unknown') == 0)) {
67 $PMA_recoding_engine = PMA_CHARSET_ICONV_AIX;
68 } else {
69 $PMA_recoding_engine = PMA_CHARSET_ICONV;
71 } elseif (@function_exists('libiconv')) {
72 $PMA_recoding_engine = PMA_CHARSET_LIBICONV;
73 } else {
74 $PMA_recoding_engine = PMA_CHARSET_NONE;
76 if (!isset($GLOBALS['is_header_sent'])) {
77 include './libraries/header.inc.php';
79 echo __('Couldn\'t use the iconv, libiconv, or recode_string functions, although the necessary extensions appear to be loaded. Check your PHP configuration.');
80 require_once './libraries/footer.inc.php';
81 exit();
83 } elseif ($PMA_recoding_engine == 'recode') {
84 if (@function_exists('recode_string')) {
85 $PMA_recoding_engine = PMA_CHARSET_RECODE;
86 } else {
87 $PMA_recoding_engine = PMA_CHARSET_NONE;
89 require_once './libraries/header.inc.php';
90 echo __('Couldn\'t use the iconv, libiconv, or recode_string functions, although the necessary extensions appear to be loaded. Check your PHP configuration.');
91 require_once './libraries/footer.inc.php';
92 exit;
94 } else {
95 if (@function_exists('iconv')) {
96 if ((@stristr(PHP_OS, 'AIX')) && (@strcasecmp(ICONV_IMPL, 'unknown') == 0) && (@strcasecmp(ICONV_VERSION, 'unknown') == 0)) {
97 $PMA_recoding_engine = PMA_CHARSET_ICONV_AIX;
98 } else {
99 $PMA_recoding_engine = PMA_CHARSET_ICONV;
101 } elseif (@function_exists('libiconv')) {
102 $PMA_recoding_engine = PMA_CHARSET_LIBICONV;
103 } elseif (@function_exists('recode_string')) {
104 $PMA_recoding_engine = PMA_CHARSET_RECODE;
105 } else {
106 $PMA_recoding_engine = PMA_CHARSET_NONE;
108 require_once './libraries/header.inc.php';
109 echo __('Couldn\'t use the iconv, libiconv, or recode_string functions, although the necessary extensions appear to be loaded. Check your PHP configuration.');
110 require_once './libraries/footer.inc.php';
111 exit;
114 } else {
115 $PMA_recoding_engine = PMA_CHARSET_NONE;
118 /* Load AIX iconv wrapper if needed */
119 if ($PMA_recoding_engine == PMA_CHARSET_ICONV_AIX) {
120 require_once './libraries/iconv_wrapper.lib.php';
124 * Converts encoding of text according to current settings.
126 * @param string what to convert
128 * @return string converted text
130 * @global array the configuration array
131 * @global boolean whether recoding is allowed or not
132 * @global string the current charset
133 * @global array the charset to convert to
135 * @access public
138 function PMA_convert_charset($what) {
139 global $cfg, $charset, $convcharset;
141 if (!(isset($cfg['AllowAnywhereRecoding']) && $cfg['AllowAnywhereRecoding'] )
142 || $convcharset == $charset) { // if input and output charset are the same, we don't have to do anything...
143 return $what;
144 } else {
145 switch ($GLOBALS['PMA_recoding_engine']) {
146 case PMA_CHARSET_RECODE:
147 return recode_string($charset . '..' . $convcharset, $what);
148 case PMA_CHARSET_ICONV:
149 return iconv($charset, $convcharset . $cfg['IconvExtraParams'], $what);
150 case PMA_CHARSET_ICONV_AIX:
151 return PMA_aix_iconv_wrapper($charset, $convcharset . $cfg['IconvExtraParams'], $what);
152 case PMA_CHARSET_LIBICONV:
153 return libiconv($charset, $convcharset . $GLOBALS['cfg']['IconvExtraParams'], $what);
154 default:
155 return $what;
158 } // end of the "PMA_convert_charset()" function
161 * Converts encoding of text according to parameters with detected
162 * conversion function.
164 * @param string source charset
165 * @param string target charset
166 * @param string what to convert
168 * @return string converted text
170 * @access public
173 function PMA_convert_string($src_charset, $dest_charset, $what) {
174 if ($src_charset == $dest_charset) {
175 return $what;
177 switch ($GLOBALS['PMA_recoding_engine']) {
178 case PMA_CHARSET_RECODE:
179 return recode_string($src_charset . '..' . $dest_charset, $what);
180 case PMA_CHARSET_ICONV:
181 return iconv($src_charset, $dest_charset . $GLOBALS['cfg']['IconvExtraParams'], $what);
182 case PMA_CHARSET_ICONV_AIX:
183 return PMA_aix_iconv_wrapper($src_charset, $dest_charset . $GLOBALS['cfg']['IconvExtraParams'], $what);
184 case PMA_CHARSET_LIBICONV:
185 return libiconv($src_charset, $dest_charset, $what);
186 default:
187 return $what;
189 } // end of the "PMA_convert_string()" function
193 * Converts encoding of file according to parameters with detected
194 * conversion function. The old file will be unlinked and new created and
195 * its file name is returned.
197 * @param string source charset
198 * @param string target charset
199 * @param string file to convert
201 * @return string new temporay file
203 * @access public
206 function PMA_convert_file($src_charset, $dest_charset, $file) {
207 switch ($GLOBALS['PMA_recoding_engine']) {
208 case PMA_CHARSET_RECODE:
209 case PMA_CHARSET_ICONV:
210 case PMA_CHARSET_LIBICONV:
211 $tmpfname = tempnam('', 'PMA_convert_file');
212 $fin = fopen($file, 'r');
213 $fout = fopen($tmpfname, 'w');
214 if ($GLOBALS['PMA_recoding_engine'] == PMA_CHARSET_RECODE) {
215 recode_file($src_charset . '..' . $dest_charset, $fin, $fout);
216 } else {
217 while (!feof($fin)) {
218 $line = fgets($fin, 4096);
219 if ($GLOBALS['PMA_recoding_engine'] == PMA_CHARSET_ICONV) {
220 $dist = iconv($src_charset, $dest_charset . $GLOBALS['cfg']['IconvExtraParams'], $line);
221 } elseif ($GLOBALS['PMA_recoding_engine'] == PMA_CHARSET_ICONV_AIX) {
222 $dist = PMA_aix_iconv_wrapper($src_charset, $dest_charset . $GLOBALS['cfg']['IconvExtraParams'], $line);
223 } else {
224 $dist = libiconv($src_charset, $dest_charset . $GLOBALS['cfg']['IconvExtraParams'], $line);
226 fputs($fout, $dist);
227 } // end while
229 fclose($fin);
230 fclose($fout);
231 unlink($file);
233 return $tmpfname;
234 default:
235 return $file;
237 } // end of the "PMA_convert_file()" function