2
/* $Id: charset_conversion.lib.php 8629 2006-02-22 08:26:32Z nijel $ */
3
// vim: expandtab sw=4 ts=4 sts=4:
7
* Charset conversion functions.
12
* Loads the recode or iconv extensions if any of it is not loaded yet
14
if (isset($cfg['AllowAnywhereRecoding'])
15
&& $cfg['AllowAnywhereRecoding']
18
if ($cfg['RecodingEngine'] == 'recode') {
19
if (!@extension_loaded('recode')) {
21
if (!@extension_loaded('recode')) {
22
echo $strCantLoadRecodeIconv;
26
$PMA_recoding_engine = 'recode';
27
} elseif ($cfg['RecodingEngine'] == 'iconv') {
28
if (!@extension_loaded('iconv')) {
30
if (!@extension_loaded('iconv')) {
31
echo $strCantLoadRecodeIconv;
35
$PMA_recoding_engine = 'iconv';
37
if (@extension_loaded('iconv')) {
38
$PMA_recoding_engine = 'iconv';
39
} elseif (@extension_loaded('recode')) {
40
$PMA_recoding_engine = 'recode';
43
if (!@extension_loaded('iconv')) {
45
if (!@extension_loaded('recode')) {
46
echo $strCantLoadRecodeIconv;
49
$PMA_recoding_engine = 'recode';
52
$PMA_recoding_engine = 'iconv';
56
} // end load recode/iconv extension
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
define('PMA_CHARSET_ICONV_AIX', 4);
64
if (!isset($cfg['IconvExtraParams'])) {
65
$cfg['IconvExtraParams'] = '';
68
// Finally detects which function will we use:
69
if (isset($cfg['AllowAnywhereRecoding'])
70
&& $cfg['AllowAnywhereRecoding']
73
if (!isset($PMA_recoding_engine)) {
74
$PMA_recoding_engine = $cfg['RecodingEngine'];
76
if ($PMA_recoding_engine == 'iconv') {
77
if (@function_exists('iconv')) {
78
if ((@stristr(PHP_OS, 'AIX')) && (@strcasecmp(ICONV_IMPL, 'unknown') == 0) && (@strcasecmp(ICONV_VERSION, 'unknown') == 0)) {
79
$PMA_recoding_engine = PMA_CHARSET_ICONV_AIX;
81
$PMA_recoding_engine = PMA_CHARSET_ICONV;
83
} elseif (@function_exists('libiconv')) {
84
$PMA_recoding_engine = PMA_CHARSET_LIBICONV;
86
$PMA_recoding_engine = PMA_CHARSET_NONE;
88
if (!isset($GLOBALS['is_header_sent'])) {
89
include('./libraries/header.inc.php');
91
echo $strCantUseRecodeIconv;
92
require_once('./libraries/footer.inc.php');
95
} elseif ($PMA_recoding_engine == 'recode') {
96
if (@function_exists('recode_string')) {
97
$PMA_recoding_engine = PMA_CHARSET_RECODE;
99
$PMA_recoding_engine = PMA_CHARSET_NONE;
101
require_once('./libraries/header.inc.php');
102
echo $strCantUseRecodeIconv;
103
require_once('./libraries/footer.inc.php');
107
if (@function_exists('iconv')) {
108
if ((@stristr(PHP_OS, 'AIX')) && (@strcasecmp(ICONV_IMPL, 'unknown') == 0) && (@strcasecmp(ICONV_VERSION, 'unknown') == 0)) {
109
$PMA_recoding_engine = PMA_CHARSET_ICONV_AIX;
111
$PMA_recoding_engine = PMA_CHARSET_ICONV;
113
} elseif (@function_exists('libiconv')) {
114
$PMA_recoding_engine = PMA_CHARSET_LIBICONV;
115
} elseif (@function_exists('recode_string')) {
116
$PMA_recoding_engine = PMA_CHARSET_RECODE;
118
$PMA_recoding_engine = PMA_CHARSET_NONE;
120
require_once('./libraries/header.inc.php');
121
echo $strCantUseRecodeIconv;
122
require_once('./libraries/footer.inc.php');
127
$PMA_recoding_engine = PMA_CHARSET_NONE;
130
/* Load AIX iconv wrapper if needed */
131
if ($PMA_recoding_engine == PMA_CHARSET_ICONV_AIX) {
132
require_once('./libraries/iconv_wrapper.lib.php');
136
* Converts encoding according to current settings.
138
* @param mixed what to convert (string or array of strings or object returned by mysql_fetch_field)
140
* @return string converted string or array of strings
142
* @global array the configuration array
143
* @global boolean whether recoding is allowed or not
144
* @global string the current charset
145
* @global array the charset to convert to
151
function PMA_convert_display_charset($what) {
152
global $cfg, $allow_recoding, $charset, $convcharset;
154
if (!(isset($cfg['AllowAnywhereRecoding']) && $cfg['AllowAnywhereRecoding'] && $allow_recoding)
155
|| $convcharset == $charset // rabus: if input and output charset are the same, we don't have to do anything...
156
// this constant is not defined before the login:
157
|| (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
159
} elseif (is_array($what)) {
161
foreach ($what AS $key => $val) {
162
if (is_string($val) || is_array($val)) {
163
if (is_string($key)) {
164
$result[PMA_convert_display_charset($key)] = PMA_convert_display_charset($val);
166
$result[$key] = PMA_convert_display_charset($val);
169
$result[$key] = $val;
173
} elseif (is_string($what)) {
175
switch ($GLOBALS['PMA_recoding_engine']) {
176
case PMA_CHARSET_RECODE:
177
return recode_string($convcharset . '..' . $charset, $what);
178
case PMA_CHARSET_ICONV:
179
return iconv($convcharset, $charset . $cfg['IconvExtraParams'], $what);
180
case PMA_CHARSET_ICONV_AIX:
181
return PMA_aix_iconv_wrapper($convcharset, $charset . $cfg['IconvExtraParams'], $what);
182
case PMA_CHARSET_LIBICONV:
183
return libiconv($convcharset, $charset . $GLOBALS['cfg']['IconvExtraParams'], $what);
187
} elseif (is_object($what)) {
188
// isn't it object returned from mysql_fetch_field ?
189
if (@is_string($what->name)) {
190
$what->name = PMA_convert_display_charset($what->name);
192
if (@is_string($what->table)) {
193
$what->table = PMA_convert_display_charset($what->table);
195
if (@is_string($what->Database)) {
196
$what->Database = PMA_convert_display_charset($what->Database);
200
// when we don't know what it is we don't touch it...
203
} // end of the "PMA_convert_display_charset()" function
207
* Converts encoding of text according to current settings.
209
* @param string what to convert
211
* @return string converted text
213
* @global array the configuration array
214
* @global boolean whether recoding is allowed or not
215
* @global string the current charset
216
* @global array the charset to convert to
222
function PMA_convert_charset($what) {
223
global $cfg, $allow_recoding, $charset, $convcharset;
225
if (!(isset($cfg['AllowAnywhereRecoding']) && $cfg['AllowAnywhereRecoding'] && $allow_recoding)
226
|| $convcharset == $charset) { // rabus: if input and output charset are the same, we don't have to do anything...
229
switch ($GLOBALS['PMA_recoding_engine']) {
230
case PMA_CHARSET_RECODE:
231
return recode_string($charset . '..' . $convcharset, $what);
232
case PMA_CHARSET_ICONV:
233
return iconv($charset, $convcharset . $cfg['IconvExtraParams'], $what);
234
case PMA_CHARSET_ICONV_AIX:
235
return PMA_aix_iconv_wrapper($charset, $convcharset . $cfg['IconvExtraParams'], $what);
236
case PMA_CHARSET_LIBICONV:
237
return libiconv($charset, $convcharset . $GLOBALS['cfg']['IconvExtraParams'], $what);
242
} // end of the "PMA_convert_charset()" function
245
* Converts encoding of text according to parameters with detected
246
* conversion function.
248
* @param string source charset
249
* @param string target charset
250
* @param string what to convert
252
* @return string converted text
258
function PMA_convert_string($src_charset, $dest_charset, $what) {
259
if ($src_charset == $dest_charset) {
262
switch ($GLOBALS['PMA_recoding_engine']) {
263
case PMA_CHARSET_RECODE:
264
return recode_string($src_charset . '..' . $dest_charset, $what);
265
case PMA_CHARSET_ICONV:
266
return iconv($src_charset, $dest_charset . $GLOBALS['cfg']['IconvExtraParams'], $what);
267
case PMA_CHARSET_ICONV_AIX:
268
return PMA_aix_iconv_wrapper($src_charset, $dest_charset . $GLOBALS['cfg']['IconvExtraParams'], $what);
269
case PMA_CHARSET_LIBICONV:
270
return libiconv($src_charset, $dest_charset, $what);
274
} // end of the "PMA_convert_string()" function
278
* Converts encoding of file according to parameters with detected
279
* conversion function. The old file will be unlinked and new created and
280
* its file name is returned.
282
* @param string source charset
283
* @param string target charset
284
* @param string file to convert
286
* @return string new temporay file
292
function PMA_convert_file($src_charset, $dest_charset, $file) {
293
switch ($GLOBALS['PMA_recoding_engine']) {
294
case PMA_CHARSET_RECODE:
295
case PMA_CHARSET_ICONV:
296
case PMA_CHARSET_LIBICONV:
297
$tmpfname = tempnam('', 'PMA_convert_file');
298
$fin = fopen($file, 'r');
299
$fout = fopen($tmpfname, 'w');
300
if ($GLOBALS['PMA_recoding_engine'] == PMA_CHARSET_RECODE) {
301
recode_file($src_charset . '..' . $dest_charset, $fin, $fout);
303
while (!feof($fin)) {
304
$line = fgets($fin, 4096);
305
if ($GLOBALS['PMA_recoding_engine'] == PMA_CHARSET_ICONV) {
306
$dist = iconv($src_charset, $dest_charset . $GLOBALS['cfg']['IconvExtraParams'], $line);
307
} elseif ($GLOBALS['PMA_recoding_engine'] == PMA_CHARSET_ICONV_AIX) {
308
$dist = PMA_aix_iconv_wrapper($src_charset, $dest_charset . $GLOBALS['cfg']['IconvExtraParams'], $line);
310
$dist = libiconv($src_charset, $dest_charset . $GLOBALS['cfg']['IconvExtraParams'], $line);
323
} // end of the "PMA_convert_file()" function