250 |
kaklik |
1 |
<?php |
|
|
2 |
/* $Id: charset_conversion.lib.php,v 2.9 2006/01/17 17:02:30 cybot_tm Exp $ */ |
|
|
3 |
// vim: expandtab sw=4 ts=4 sts=4: |
|
|
4 |
|
|
|
5 |
|
|
|
6 |
/** |
|
|
7 |
* Charset conversion functions. |
|
|
8 |
*/ |
|
|
9 |
|
|
|
10 |
|
|
|
11 |
/** |
|
|
12 |
* Loads the recode or iconv extensions if any of it is not loaded yet |
|
|
13 |
*/ |
|
|
14 |
if (isset($cfg['AllowAnywhereRecoding']) |
|
|
15 |
&& $cfg['AllowAnywhereRecoding'] |
|
|
16 |
&& $allow_recoding) { |
|
|
17 |
|
|
|
18 |
if ($cfg['RecodingEngine'] == 'recode') { |
|
|
19 |
if (!@extension_loaded('recode')) { |
|
|
20 |
PMA_dl('recode'); |
|
|
21 |
if (!@extension_loaded('recode')) { |
|
|
22 |
echo $strCantLoadRecodeIconv; |
|
|
23 |
exit; |
|
|
24 |
} |
|
|
25 |
} |
|
|
26 |
$PMA_recoding_engine = 'recode'; |
|
|
27 |
} elseif ($cfg['RecodingEngine'] == 'iconv') { |
|
|
28 |
if (!@extension_loaded('iconv')) { |
|
|
29 |
PMA_dl('iconv'); |
|
|
30 |
if (!@extension_loaded('iconv')) { |
|
|
31 |
echo $strCantLoadRecodeIconv; |
|
|
32 |
exit; |
|
|
33 |
} |
|
|
34 |
} |
|
|
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_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'; |
|
|
50 |
} |
|
|
51 |
} else { |
|
|
52 |
$PMA_recoding_engine = 'iconv'; |
|
|
53 |
} |
|
|
54 |
} |
|
|
55 |
} |
|
|
56 |
} // end load recode/iconv extension |
|
|
57 |
|
|
|
58 |
define('PMA_CHARSET_NONE', 0); |
|
|
59 |
define('PMA_CHARSET_ICONV', 1); |
|
|
60 |
define('PMA_CHARSET_LIBICONV', 2); |
|
|
61 |
define('PMA_CHARSET_RECODE', 3); |
|
|
62 |
|
|
|
63 |
if (!isset($cfg['IconvExtraParams'])) { |
|
|
64 |
$cfg['IconvExtraParams'] = ''; |
|
|
65 |
} |
|
|
66 |
|
|
|
67 |
// Finally detects which function will we use: |
|
|
68 |
if (isset($cfg['AllowAnywhereRecoding']) |
|
|
69 |
&& $cfg['AllowAnywhereRecoding'] |
|
|
70 |
&& $allow_recoding) { |
|
|
71 |
|
|
|
72 |
if (!isset($PMA_recoding_engine)) { |
|
|
73 |
$PMA_recoding_engine = $cfg['RecodingEngine']; |
|
|
74 |
} |
|
|
75 |
if ($PMA_recoding_engine == 'iconv') { |
|
|
76 |
if (@function_exists('iconv')) { |
|
|
77 |
$PMA_recoding_engine = PMA_CHARSET_ICONV; |
|
|
78 |
} elseif (@function_exists('libiconv')) { |
|
|
79 |
$PMA_recoding_engine = PMA_CHARSET_LIBICONV; |
|
|
80 |
} else { |
|
|
81 |
$PMA_recoding_engine = PMA_CHARSET_NONE; |
|
|
82 |
|
|
|
83 |
if (!isset($GLOBALS['is_header_sent'])) { |
|
|
84 |
include('./libraries/header.inc.php'); |
|
|
85 |
} |
|
|
86 |
echo $strCantUseRecodeIconv; |
|
|
87 |
require_once('./libraries/footer.inc.php'); |
|
|
88 |
exit(); |
|
|
89 |
} |
|
|
90 |
} elseif ($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; |
|
|
95 |
|
|
|
96 |
require_once('./libraries/header.inc.php'); |
|
|
97 |
echo $strCantUseRecodeIconv; |
|
|
98 |
require_once('./libraries/footer.inc.php'); |
|
|
99 |
exit; |
|
|
100 |
} |
|
|
101 |
} else { |
|
|
102 |
if (@function_exists('iconv')) { |
|
|
103 |
$PMA_recoding_engine = PMA_CHARSET_ICONV; |
|
|
104 |
} elseif (@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; |
|
|
110 |
|
|
|
111 |
require_once('./libraries/header.inc.php'); |
|
|
112 |
echo $strCantUseRecodeIconv; |
|
|
113 |
require_once('./libraries/footer.inc.php'); |
|
|
114 |
exit; |
|
|
115 |
} |
|
|
116 |
} |
|
|
117 |
} else { |
|
|
118 |
$PMA_recoding_engine = PMA_CHARSET_NONE; |
|
|
119 |
} |
|
|
120 |
|
|
|
121 |
|
|
|
122 |
/** |
|
|
123 |
* Converts encoding according to current settings. |
|
|
124 |
* |
|
|
125 |
* @param mixed what to convert (string or array of strings or object returned by mysql_fetch_field) |
|
|
126 |
* |
|
|
127 |
* @return string converted string or array of strings |
|
|
128 |
* |
|
|
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 |
|
|
133 |
* |
|
|
134 |
* @access public |
|
|
135 |
* |
|
|
136 |
* @author nijel |
|
|
137 |
*/ |
|
|
138 |
function PMA_convert_display_charset($what) { |
|
|
139 |
global $cfg, $allow_recoding, $charset, $convcharset; |
|
|
140 |
|
|
|
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; |
|
|
146 |
} elseif (is_array($what)) { |
|
|
147 |
$result = array(); |
|
|
148 |
foreach ($what AS $key => $val) { |
|
|
149 |
if (is_string($val) || is_array($val)) { |
|
|
150 |
if (is_string($key)) { |
|
|
151 |
$result[PMA_convert_display_charset($key)] = PMA_convert_display_charset($val); |
|
|
152 |
} else { |
|
|
153 |
$result[$key] = PMA_convert_display_charset($val); |
|
|
154 |
} |
|
|
155 |
} else { |
|
|
156 |
$result[$key] = $val; |
|
|
157 |
} |
|
|
158 |
} // end while |
|
|
159 |
return $result; |
|
|
160 |
} elseif (is_string($what)) { |
|
|
161 |
|
|
|
162 |
switch ($GLOBALS['PMA_recoding_engine']) { |
|
|
163 |
case PMA_CHARSET_RECODE: |
|
|
164 |
return recode_string($convcharset . '..' . $charset, $what); |
|
|
165 |
case PMA_CHARSET_ICONV: |
|
|
166 |
return iconv($convcharset, $charset . $cfg['IconvExtraParams'], $what); |
|
|
167 |
case PMA_CHARSET_LIBICONV: |
|
|
168 |
return libiconv($convcharset, $charset, $what); |
|
|
169 |
default: |
|
|
170 |
return $what; |
|
|
171 |
} |
|
|
172 |
} elseif (is_object($what)) { |
|
|
173 |
// isn't it object returned from mysql_fetch_field ? |
|
|
174 |
if (@is_string($what->name)) { |
|
|
175 |
$what->name = PMA_convert_display_charset($what->name); |
|
|
176 |
} |
|
|
177 |
if (@is_string($what->table)) { |
|
|
178 |
$what->table = PMA_convert_display_charset($what->table); |
|
|
179 |
} |
|
|
180 |
if (@is_string($what->Database)) { |
|
|
181 |
$what->Database = PMA_convert_display_charset($what->Database); |
|
|
182 |
} |
|
|
183 |
return $what; |
|
|
184 |
} else { |
|
|
185 |
// when we don't know what it is we don't touch it... |
|
|
186 |
return $what; |
|
|
187 |
} |
|
|
188 |
} // end of the "PMA_convert_display_charset()" function |
|
|
189 |
|
|
|
190 |
|
|
|
191 |
/** |
|
|
192 |
* Converts encoding of text according to current settings. |
|
|
193 |
* |
|
|
194 |
* @param string what to convert |
|
|
195 |
* |
|
|
196 |
* @return string converted text |
|
|
197 |
* |
|
|
198 |
* @global array the configuration array |
|
|
199 |
* @global boolean whether recoding is allowed or not |
|
|
200 |
* @global string the current charset |
|
|
201 |
* @global array the charset to convert to |
|
|
202 |
* |
|
|
203 |
* @access public |
|
|
204 |
* |
|
|
205 |
* @author nijel |
|
|
206 |
*/ |
|
|
207 |
function PMA_convert_charset($what) { |
|
|
208 |
global $cfg, $allow_recoding, $charset, $convcharset; |
|
|
209 |
|
|
|
210 |
if (!(isset($cfg['AllowAnywhereRecoding']) && $cfg['AllowAnywhereRecoding'] && $allow_recoding) |
|
|
211 |
|| $convcharset == $charset) { // rabus: if input and output charset are the same, we don't have to do anything... |
|
|
212 |
return $what; |
|
|
213 |
} else { |
|
|
214 |
switch ($GLOBALS['PMA_recoding_engine']) { |
|
|
215 |
case PMA_CHARSET_RECODE: |
|
|
216 |
return recode_string($charset . '..' . $convcharset, $what); |
|
|
217 |
case PMA_CHARSET_ICONV: |
|
|
218 |
return iconv($charset, $convcharset . $cfg['IconvExtraParams'], $what); |
|
|
219 |
case PMA_CHARSET_LIBICONV: |
|
|
220 |
return libiconv($charset, $convcharset, $what); |
|
|
221 |
default: |
|
|
222 |
return $what; |
|
|
223 |
} |
|
|
224 |
} |
|
|
225 |
} // end of the "PMA_convert_charset()" function |
|
|
226 |
|
|
|
227 |
/** |
|
|
228 |
* Converts encoding of text according to parameters with detected |
|
|
229 |
* conversion function. |
|
|
230 |
* |
|
|
231 |
* @param string source charset |
|
|
232 |
* @param string target charset |
|
|
233 |
* @param string what to convert |
|
|
234 |
* |
|
|
235 |
* @return string converted text |
|
|
236 |
* |
|
|
237 |
* @access public |
|
|
238 |
* |
|
|
239 |
* @author nijel |
|
|
240 |
*/ |
|
|
241 |
function PMA_convert_string($src_charset, $dest_charset, $what) { |
|
|
242 |
if ($src_charset == $dest_charset) { |
|
|
243 |
return $what; |
|
|
244 |
} |
|
|
245 |
switch ($GLOBALS['PMA_recoding_engine']) { |
|
|
246 |
case PMA_CHARSET_RECODE: |
|
|
247 |
return recode_string($src_charset . '..' . $dest_charset, $what); |
|
|
248 |
case PMA_CHARSET_ICONV: |
|
|
249 |
return iconv($src_charset, $dest_charset . $GLOBALS['cfg']['IconvExtraParams'], $what); |
|
|
250 |
case PMA_CHARSET_LIBICONV: |
|
|
251 |
return libiconv($src_charset, $dest_charset, $what); |
|
|
252 |
default: |
|
|
253 |
return $what; |
|
|
254 |
} |
|
|
255 |
} // end of the "PMA_convert_string()" function |
|
|
256 |
|
|
|
257 |
|
|
|
258 |
/** |
|
|
259 |
* Converts encoding of file according to parameters with detected |
|
|
260 |
* conversion function. The old file will be unlinked and new created and |
|
|
261 |
* its file name is returned. |
|
|
262 |
* |
|
|
263 |
* @param string source charset |
|
|
264 |
* @param string target charset |
|
|
265 |
* @param string file to convert |
|
|
266 |
* |
|
|
267 |
* @return string new temporay file |
|
|
268 |
* |
|
|
269 |
* @access public |
|
|
270 |
* |
|
|
271 |
* @author nijel |
|
|
272 |
*/ |
|
|
273 |
function PMA_convert_file($src_charset, $dest_charset, $file) { |
|
|
274 |
switch ($GLOBALS['PMA_recoding_engine']) { |
|
|
275 |
case PMA_CHARSET_RECODE: |
|
|
276 |
case PMA_CHARSET_ICONV: |
|
|
277 |
case PMA_CHARSET_LIBICONV: |
|
|
278 |
$tmpfname = tempnam('', 'PMA_convert_file'); |
|
|
279 |
$fin = fopen($file, 'r'); |
|
|
280 |
$fout = fopen($tmpfname, 'w'); |
|
|
281 |
if ($GLOBALS['PMA_recoding_engine'] == PMA_CHARSET_RECODE) { |
|
|
282 |
recode_file($src_charset . '..' . $dest_charset, $fin, $fout); |
|
|
283 |
} else { |
|
|
284 |
while (!feof($fin)) { |
|
|
285 |
$line = fgets($fin, 4096); |
|
|
286 |
if ($GLOBALS['PMA_recoding_engine'] == PMA_CHARSET_ICONV) { |
|
|
287 |
$dist = iconv($src_charset, $dest_charset . $GLOBALS['cfg']['IconvExtraParams'], $line); |
|
|
288 |
} else { |
|
|
289 |
$dist = libiconv($src_charset, $dest_charset, $line); |
|
|
290 |
} |
|
|
291 |
fputs($fout, $dist); |
|
|
292 |
} // end while |
|
|
293 |
} |
|
|
294 |
fclose($fin); |
|
|
295 |
fclose($fout); |
|
|
296 |
unlink($file); |
|
|
297 |
|
|
|
298 |
return $tmpfname; |
|
|
299 |
default: |
|
|
300 |
return $file; |
|
|
301 |
} |
|
|
302 |
} // end of the "PMA_convert_file()" function |
|
|
303 |
|
|
|
304 |
?> |