C:\xampp\phpMyAdmin\libraries\classes\Encoding.php


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
326
327
328
329
330
331
332
333
334
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
 * Hold the PhpMyAdmin\Encoding class
 *
 * @package PhpMyAdmin
 */
namespace PhpMyAdmin;

use 
PhpMyAdmin\Config\ConfigFile;
use 
PhpMyAdmin\Core;
use 
PhpMyAdmin\Template;

/**
 * Encoding conversion helper class
 *
 * @package PhpMyAdmin
 */
class Encoding
{
    
/**
     * None encoding conversion engine
     *
     * @var int
     */

    
const ENGINE_NONE 0;
    
/**
     * iconv encoding conversion engine
     *
     * @var int
     */
    
const ENGINE_ICONV 1;

    
/**
     * recode encoding conversion engine
     *
     * @var int
     */
    
const ENGINE_RECODE 2;

    
/**
     * mbstring encoding conversion engine
     *
     * @var int
     */
    
const ENGINE_MB 3;

    
/**
     * Chosen encoding engine
     *
     * @var int
     */
    
private static $_engine null;

    
/**
     * Map of conversion engine configurations
     *
     * Each entry contains:
     *
     * - function to detect
     * - engine contant
     * - extension name to warn when missing
     *
     * @var array
     */
    
private static $_enginemap = array(
        
'iconv' => array('iconv'self::ENGINE_ICONV'iconv'),
        
'recode' => array('recode_string'self::ENGINE_RECODE'recode'),
        
'mb' => array('mb_convert_encoding'self::ENGINE_MB'mbstring'),
        
'none' => array('isset'self::ENGINE_NONE''),
    );

    
/**
     * Order of automatic detection of engines
     *
     * @var array
     */
    
private static $_engineorder = array(
        
'iconv''mb''recode',
    );

    
/**
     * Kanji encodings list
     *
     * @var string
     */
    
private static $_kanji_encodings 'ASCII,SJIS,EUC-JP,JIS';

    
/**
     * Initializes encoding engine detecting available backends.
     *
     * @return void
     */
    
public static function initEngine()
    {
        
$engine 'auto';
        if (isset(
$GLOBALS['cfg']['RecodingEngine'])) {
            
$engine $GLOBALS['cfg']['RecodingEngine'];
        }

        
/* Use user configuration */
        
if (isset(self::$_enginemap[$engine])) {
            if (
function_exists(self::$_enginemap[$engine][0])) {
                
self::$_engine self::$_enginemap[$engine][1];
                return;
            } else {
                
Core::warnMissingExtension(self::$_enginemap[$engine][2]);
            }
        }

        
/* Autodetection */
        
foreach (self::$_engineorder as $engine) {
            if (
function_exists(self::$_enginemap[$engine][0])) {
                
self::$_engine self::$_enginemap[$engine][1];
                return;
            }
        }

        
/* Fallback to none conversion */
        
self::$_engine self::ENGINE_NONE;
    }

    
/**
     * Setter for engine. Use with caution, mostly useful for testing.
     *
     * @param int $engine Engine enconding
     *
     * @return void
     */
    
public static function setEngine($engine)
    {
        
self::$_engine $engine;
    }

    
/**
     * Checks whether there is any charset conversion supported
     *
     * @return bool
     */
    
public static function isSupported()
    {
        if (
is_null(self::$_engine)) {
            
self::initEngine();
        }
        return 
self::$_engine != self::ENGINE_NONE;
    }

    
/**
     * Converts encoding of text according to parameters with detected
     * conversion function.
     *
     * @param string $src_charset  source charset
     * @param string $dest_charset target charset
     * @param string $what         what to convert
     *
     * @return string   converted text
     *
     * @access  public
     */
    
public static function convertString($src_charset$dest_charset$what)
    {
        if (
$src_charset == $dest_charset) {
            return 
$what;
        }
        if (
is_null(self::$_engine)) {
            
self::initEngine();
        }
        switch (
self::$_engine) {
            case 
self::ENGINE_RECODE:
                return 
recode_string(
                    
$src_charset '..'  $dest_charset,
                    
$what
                
);
            case 
self::ENGINE_ICONV:
                return 
iconv(
                    
$src_charset,
                    
$dest_charset .
                    (isset(
$GLOBALS['cfg']['IconvExtraParams']) ? $GLOBALS['cfg']['IconvExtraParams'] : ''),
                    
$what
                
);
            case 
self::ENGINE_MB:
                return 
mb_convert_encoding(
                    
$what,
                    
$dest_charset,
                    
$src_charset
                
);
            default:
                return 
$what;
        }
    }

    
/**
     * Detects whether Kanji encoding is available
     *
     * @return bool
     */
    
public static function canConvertKanji()
    {
        return 
$GLOBALS['lang'] == 'ja';
    }

    
/**
     * Setter for Kanji encodings. Use with caution, mostly useful for testing.
     *
     * @return string
     */
    
public static function getKanjiEncodings()
    {
        return 
self::$_kanji_encodings;
    }

    
/**
     * Setter for Kanji encodings. Use with caution, mostly useful for testing.
     *
     * @param string $value Kanji encodings list
     *
     * @return void
     */
    
public static function setKanjiEncodings($value)
    {
        
self::$_kanji_encodings $value;
    }

    
/**
     * Reverses SJIS & EUC-JP position in the encoding codes list
     *
     * @return void
     */
    
public static function kanjiChangeOrder()
    {
        
$parts explode(','self::$_kanji_encodings);
        if (
$parts[1] == 'EUC-JP') {
            
self::$_kanji_encodings 'ASCII,SJIS,EUC-JP,JIS';
        } else {
            
self::$_kanji_encodings 'ASCII,EUC-JP,SJIS,JIS';
        }
    }

    
/**
     * Kanji string encoding convert
     *
     * @param string $str  the string to convert
     * @param string $enc  the destination encoding code
     * @param string $kana set 'kana' convert to JIS-X208-kana
     *
     * @return string   the converted string
     */
    
public static function kanjiStrConv($str$enc$kana)
    {
        if (
$enc == '' && $kana == '') {
            return 
$str;
        }

        
$string_encoding mb_detect_encoding($strself::$_kanji_encodings);
        if (
$string_encoding === false) {
            
$string_encoding 'utf-8';
        }

        if (
$kana == 'kana') {
            
$dist mb_convert_kana($str'KV'$string_encoding);
            
$str  $dist;
        }
        if (
$string_encoding != $enc && $enc != '') {
            
$dist mb_convert_encoding($str$enc$string_encoding);
        } else {
            
$dist $str;
        }
        return 
$dist;
    }


    
/**
     * Kanji file encoding convert
     *
     * @param string $file the name of the file to convert
     * @param string $enc  the destination encoding code
     * @param string $kana set 'kana' convert to JIS-X208-kana
     *
     * @return string   the name of the converted file
     */
    
public static function kanjiFileConv($file$enc$kana)
    {
        if (
$enc == '' && $kana == '') {
            return 
$file;
        }
        
$tmpfname tempnam($GLOBALS['PMA_Config']->getUploadTempDir(), $enc);
        
$fpd      fopen($tmpfname'wb');
        
$fps      fopen($file'r');
        
self::kanjiChangeOrder();
        while (!
feof($fps)) {
            
$line fgets($fps4096);
            
$dist self::kanjiStrConv($line$enc$kana);
            
fputs($fpd$dist);
        } 
// end while
        
self::kanjiChangeOrder();
        
fclose($fps);
        
fclose($fpd);
        
unlink($file);

        return 
$tmpfname;
    }

    
/**
     * Defines radio form fields to switch between encoding modes
     *
     * @return string   xhtml code for the radio controls
     */
    
public static function kanjiEncodingForm()
    {
        return 
Template::get('encoding/kanji_encoding_form')->render();
    }

    
/**
     * Lists available encodings.
     *
     * @return array
     */
    
public static function listEncodings()
    {
        if (
is_null(self::$_engine)) {
            
self::initEngine();
        }
        
/* Most engines do not support listing */
        
if (self::$_engine != self::ENGINE_MB) {
            return 
$GLOBALS['cfg']['AvailableCharsets'];
        }

        return 
array_intersect(
            
array_map('strtolower'mb_list_encodings()),
            
$GLOBALS['cfg']['AvailableCharsets']
        );
    }
}
x

Windows NT KPTV 6.2 build 9200 (Windows Server 2012 Datacenter Edition) i586