File: charset_conversion.lib.php

package info (click to toggle)
phpmyadmin 4%3A2.9.1.1-3
  • links: PTS
  • area: main
  • in suites: etch-m68k
  • size: 13,224 kB
  • ctags: 119,177
  • sloc: php: 148,860; sh: 636; sql: 224; perl: 142
file content (325 lines) | stat: -rw-r--r-- 11,697 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
<?php
/* $Id: charset_conversion.lib.php 8629 2006-02-22 08:26:32Z nijel $ */
// vim: expandtab sw=4 ts=4 sts=4:


/**
 * Charset conversion functions.
 */


/**
 * Loads the recode or iconv extensions if any of it is not loaded yet
 */
if (isset($cfg['AllowAnywhereRecoding'])
    && $cfg['AllowAnywhereRecoding']
    && $allow_recoding) {

    if ($cfg['RecodingEngine'] == 'recode') {
        if (!@extension_loaded('recode')) {
            PMA_dl('recode');
            if (!@extension_loaded('recode')) {
                echo $strCantLoadRecodeIconv;
                exit;
            }
        }
        $PMA_recoding_engine             = 'recode';
    } elseif ($cfg['RecodingEngine'] == 'iconv') {
        if (!@extension_loaded('iconv')) {
            PMA_dl('iconv');
            if (!@extension_loaded('iconv')) {
                echo $strCantLoadRecodeIconv;
                exit;
            }
        }
        $PMA_recoding_engine             = 'iconv';
    } else {
        if (@extension_loaded('iconv')) {
            $PMA_recoding_engine         = 'iconv';
        } elseif (@extension_loaded('recode')) {
            $PMA_recoding_engine         = 'recode';
        } else {
            PMA_dl('iconv');
            if (!@extension_loaded('iconv')) {
                PMA_dl('recode');
                if (!@extension_loaded('recode')) {
                    echo $strCantLoadRecodeIconv;
                    exit;
                } else {
                    $PMA_recoding_engine = 'recode';
                }
            } else {
                $PMA_recoding_engine     = 'iconv';
            }
        }
    }
} // end load recode/iconv extension

define('PMA_CHARSET_NONE', 0);
define('PMA_CHARSET_ICONV', 1);
define('PMA_CHARSET_LIBICONV', 2);
define('PMA_CHARSET_RECODE', 3);
define('PMA_CHARSET_ICONV_AIX', 4);

if (!isset($cfg['IconvExtraParams'])) {
    $cfg['IconvExtraParams'] = '';
}

// Finally detects which function will we use:
if (isset($cfg['AllowAnywhereRecoding'])
    && $cfg['AllowAnywhereRecoding']
    && $allow_recoding) {

    if (!isset($PMA_recoding_engine)) {
        $PMA_recoding_engine = $cfg['RecodingEngine'];
    }
    if ($PMA_recoding_engine == 'iconv') {
        if (@function_exists('iconv')) {
            if ((@stristr(PHP_OS, 'AIX')) && (@strcasecmp(ICONV_IMPL, 'unknown') == 0) && (@strcasecmp(ICONV_VERSION, 'unknown') == 0)) {
                $PMA_recoding_engine = PMA_CHARSET_ICONV_AIX;
            } else {
                $PMA_recoding_engine = PMA_CHARSET_ICONV;
            }
        } elseif (@function_exists('libiconv')) {
            $PMA_recoding_engine = PMA_CHARSET_LIBICONV;
        } else {
            $PMA_recoding_engine = PMA_CHARSET_NONE;

            if (!isset($GLOBALS['is_header_sent'])) {
                include('./libraries/header.inc.php');
            }
            echo $strCantUseRecodeIconv;
            require_once('./libraries/footer.inc.php');
            exit();
        }
    } elseif ($PMA_recoding_engine == 'recode') {
        if (@function_exists('recode_string')) {
            $PMA_recoding_engine = PMA_CHARSET_RECODE;
        } else {
            $PMA_recoding_engine = PMA_CHARSET_NONE;

            require_once('./libraries/header.inc.php');
            echo $strCantUseRecodeIconv;
            require_once('./libraries/footer.inc.php');
            exit;
        }
    } else {
        if (@function_exists('iconv')) {
            if ((@stristr(PHP_OS, 'AIX')) && (@strcasecmp(ICONV_IMPL, 'unknown') == 0) && (@strcasecmp(ICONV_VERSION, 'unknown') == 0)) {
                $PMA_recoding_engine = PMA_CHARSET_ICONV_AIX;
            } else {
                $PMA_recoding_engine = PMA_CHARSET_ICONV;
            }
        } elseif (@function_exists('libiconv')) {
            $PMA_recoding_engine = PMA_CHARSET_LIBICONV;
        } elseif (@function_exists('recode_string')) {
            $PMA_recoding_engine = PMA_CHARSET_RECODE;
        } else {
            $PMA_recoding_engine = PMA_CHARSET_NONE;

            require_once('./libraries/header.inc.php');
            echo $strCantUseRecodeIconv;
            require_once('./libraries/footer.inc.php');
            exit;
        }
    }
} else {
    $PMA_recoding_engine         = PMA_CHARSET_NONE;
}

/* Load AIX iconv wrapper if needed */
if ($PMA_recoding_engine == PMA_CHARSET_ICONV_AIX) {
    require_once('./libraries/iconv_wrapper.lib.php');
}

/**
 * Converts encoding according to current settings.
 *
 * @param   mixed    what to convert (string or array of strings or object returned by mysql_fetch_field)
 *
 * @return  string   converted string or array of strings
 *
 * @global  array    the configuration array
 * @global  boolean  whether recoding is allowed or not
 * @global  string   the current charset
 * @global  array    the charset to convert to
 *
 * @access  public
 *
 * @author  nijel
 */
function PMA_convert_display_charset($what) {
    global $cfg, $allow_recoding, $charset, $convcharset;

    if (!(isset($cfg['AllowAnywhereRecoding']) && $cfg['AllowAnywhereRecoding'] && $allow_recoding)
        || $convcharset == $charset // rabus: if input and output charset are the same, we don't have to do anything...
        // this constant is not defined before the login:
        || (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
        return $what;
    } elseif (is_array($what)) {
        $result = array();
        foreach ($what AS $key => $val) {
            if (is_string($val) || is_array($val)) {
                if (is_string($key)) {
                    $result[PMA_convert_display_charset($key)] = PMA_convert_display_charset($val);
                } else {
                    $result[$key] = PMA_convert_display_charset($val);
                }
            } else {
                $result[$key]     = $val;
            }
        } // end while
        return $result;
    } elseif (is_string($what)) {

        switch ($GLOBALS['PMA_recoding_engine']) {
            case PMA_CHARSET_RECODE:
                return recode_string($convcharset . '..'  . $charset, $what);
            case PMA_CHARSET_ICONV:
                return iconv($convcharset, $charset . $cfg['IconvExtraParams'], $what);
            case PMA_CHARSET_ICONV_AIX:
                return PMA_aix_iconv_wrapper($convcharset, $charset . $cfg['IconvExtraParams'], $what);
            case PMA_CHARSET_LIBICONV:
                return libiconv($convcharset, $charset . $GLOBALS['cfg']['IconvExtraParams'], $what);
            default:
                return $what;
        }
    } elseif (is_object($what)) {
        // isn't it object returned from mysql_fetch_field ?
        if (@is_string($what->name)) {
            $what->name = PMA_convert_display_charset($what->name);
        }
        if (@is_string($what->table)) {
            $what->table = PMA_convert_display_charset($what->table);
        }
        if (@is_string($what->Database)) {
            $what->Database = PMA_convert_display_charset($what->Database);
        }
        return $what;
    } else {
        // when we don't know what it is we don't touch it...
        return $what;
    }
} //  end of the "PMA_convert_display_charset()" function


/**
 * Converts encoding of text according to current settings.
 *
 * @param   string   what to convert
 *
 * @return  string   converted text
 *
 * @global  array    the configuration array
 * @global  boolean  whether recoding is allowed or not
 * @global  string   the current charset
 * @global  array    the charset to convert to
 *
 * @access  public
 *
 * @author  nijel
 */
function PMA_convert_charset($what) {
    global $cfg, $allow_recoding, $charset, $convcharset;

    if (!(isset($cfg['AllowAnywhereRecoding']) && $cfg['AllowAnywhereRecoding'] && $allow_recoding)
        || $convcharset == $charset) { // rabus: if input and output charset are the same, we don't have to do anything...
        return $what;
    } else {
        switch ($GLOBALS['PMA_recoding_engine']) {
            case PMA_CHARSET_RECODE:
                return recode_string($charset . '..'  . $convcharset, $what);
            case PMA_CHARSET_ICONV:
                return iconv($charset, $convcharset . $cfg['IconvExtraParams'], $what);
            case PMA_CHARSET_ICONV_AIX:
                return PMA_aix_iconv_wrapper($charset, $convcharset . $cfg['IconvExtraParams'], $what);
            case PMA_CHARSET_LIBICONV:
                return libiconv($charset, $convcharset . $GLOBALS['cfg']['IconvExtraParams'], $what);
            default:
                return $what;
        }
    }
} //  end of the "PMA_convert_charset()" function

/**
 * Converts encoding of text according to parameters with detected
 * conversion function.
 *
 * @param   string   source charset
 * @param   string   target charset
 * @param   string   what to convert
 *
 * @return  string   converted text
 *
 * @access  public
 *
 * @author  nijel
 */
function PMA_convert_string($src_charset, $dest_charset, $what) {
    if ($src_charset == $dest_charset) {
        return $what;
    }
    switch ($GLOBALS['PMA_recoding_engine']) {
        case PMA_CHARSET_RECODE:
            return recode_string($src_charset . '..'  . $dest_charset, $what);
        case PMA_CHARSET_ICONV:
            return iconv($src_charset, $dest_charset . $GLOBALS['cfg']['IconvExtraParams'], $what);
        case PMA_CHARSET_ICONV_AIX:
            return PMA_aix_iconv_wrapper($src_charset, $dest_charset . $GLOBALS['cfg']['IconvExtraParams'], $what);
        case PMA_CHARSET_LIBICONV:
            return libiconv($src_charset, $dest_charset, $what);
        default:
            return $what;
    }
} //  end of the "PMA_convert_string()" function


/**
 * Converts encoding of file according to parameters with detected
 * conversion function. The old file will be unlinked and new created and
 * its file name is returned.
 *
 * @param   string   source charset
 * @param   string   target charset
 * @param   string   file to convert
 *
 * @return  string   new temporay file
 *
 * @access  public
 *
 * @author  nijel
 */
function PMA_convert_file($src_charset, $dest_charset, $file) {
    switch ($GLOBALS['PMA_recoding_engine']) {
        case PMA_CHARSET_RECODE:
        case PMA_CHARSET_ICONV:
        case PMA_CHARSET_LIBICONV:
            $tmpfname = tempnam('', 'PMA_convert_file');
            $fin      = fopen($file, 'r');
            $fout     = fopen($tmpfname, 'w');
            if ($GLOBALS['PMA_recoding_engine'] == PMA_CHARSET_RECODE) {
                recode_file($src_charset . '..'  . $dest_charset, $fin, $fout);
            } else {
                while (!feof($fin)) {
                    $line = fgets($fin, 4096);
                    if ($GLOBALS['PMA_recoding_engine'] == PMA_CHARSET_ICONV) {
                        $dist = iconv($src_charset, $dest_charset . $GLOBALS['cfg']['IconvExtraParams'], $line);
                    } elseif ($GLOBALS['PMA_recoding_engine'] == PMA_CHARSET_ICONV_AIX) {
                        $dist = PMA_aix_iconv_wrapper($src_charset, $dest_charset . $GLOBALS['cfg']['IconvExtraParams'], $line);
                    } else {
                        $dist = libiconv($src_charset, $dest_charset . $GLOBALS['cfg']['IconvExtraParams'], $line);
                    }
                    fputs($fout, $dist);
                } // end while
            }
            fclose($fin);
            fclose($fout);
            unlink($file);

            return $tmpfname;
        default:
            return $file;
    }
} //  end of the "PMA_convert_file()" function

?>