bug #2356433 [display] HeaderFlipType "fake" problems
[phpmyadmin/crack.git] / libraries / charset_conversion.lib.php
blob5dc0118725ab8ccf72a7a3f1844ace74dc2b44ef
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Charset conversion functions.
6 * @version $Id$
7 */
8 if (! defined('PHPMYADMIN')) {
9 exit;
12 /**
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;
21 exit;
23 $PMA_recoding_engine = 'recode';
24 } elseif ($cfg['RecodingEngine'] == 'iconv') {
25 if (!@extension_loaded('iconv')) {
26 echo $strCantLoadRecodeIconv;
27 exit;
29 $PMA_recoding_engine = 'iconv';
30 } else {
31 if (@extension_loaded('iconv')) {
32 $PMA_recoding_engine = 'iconv';
33 } elseif (@extension_loaded('recode')) {
34 $PMA_recoding_engine = 'recode';
35 } else {
36 echo $strCantLoadRecodeIconv;
37 exit;
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;
63 } else {
64 $PMA_recoding_engine = PMA_CHARSET_ICONV;
66 } elseif (@function_exists('libiconv')) {
67 $PMA_recoding_engine = PMA_CHARSET_LIBICONV;
68 } else {
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';
76 exit();
78 } elseif ($PMA_recoding_engine == 'recode') {
79 if (@function_exists('recode_string')) {
80 $PMA_recoding_engine = PMA_CHARSET_RECODE;
81 } else {
82 $PMA_recoding_engine = PMA_CHARSET_NONE;
84 require_once './libraries/header.inc.php';
85 echo $strCantUseRecodeIconv;
86 require_once './libraries/footer.inc.php';
87 exit;
89 } else {
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;
93 } else {
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;
100 } else {
101 $PMA_recoding_engine = PMA_CHARSET_NONE;
103 require_once './libraries/header.inc.php';
104 echo $strCantUseRecodeIconv;
105 require_once './libraries/footer.inc.php';
106 exit;
109 } else {
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
130 * @access public
132 * @author nijel
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...
139 return $what;
140 } else {
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);
150 default:
151 return $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
166 * @access public
168 * @author nijel
170 function PMA_convert_string($src_charset, $dest_charset, $what) {
171 if ($src_charset == $dest_charset) {
172 return $what;
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);
183 default:
184 return $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
200 * @access public
202 * @author nijel
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);
214 } else {
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);
221 } else {
222 $dist = libiconv($src_charset, $dest_charset . $GLOBALS['cfg']['IconvExtraParams'], $line);
224 fputs($fout, $dist);
225 } // end while
227 fclose($fin);
228 fclose($fout);
229 unlink($file);
231 return $tmpfname;
232 default:
233 return $file;
235 } // end of the "PMA_convert_file()" function