Subversion Repositories php-qbpwcf

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 liveuser 1
<?php
2
 
3
/*
4
 * This file is part of the Symfony package.
5
 *
6
 * (c) Fabien Potencier <fabien@symfony.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
 
12
namespace Symfony\Polyfill\Mbstring;
13
 
14
/**
15
 * Partial mbstring implementation in PHP, iconv based, UTF-8 centric.
16
 *
17
 * Implemented:
18
 * - mb_chr                  - Returns a specific character from its Unicode code point
19
 * - mb_convert_encoding     - Convert character encoding
20
 * - mb_convert_variables    - Convert character code in variable(s)
21
 * - mb_decode_mimeheader    - Decode string in MIME header field
22
 * - mb_encode_mimeheader    - Encode string for MIME header XXX NATIVE IMPLEMENTATION IS REALLY BUGGED
23
 * - mb_decode_numericentity - Decode HTML numeric string reference to character
24
 * - mb_encode_numericentity - Encode character to HTML numeric string reference
25
 * - mb_convert_case         - Perform case folding on a string
26
 * - mb_detect_encoding      - Detect character encoding
27
 * - mb_get_info             - Get internal settings of mbstring
28
 * - mb_http_input           - Detect HTTP input character encoding
29
 * - mb_http_output          - Set/Get HTTP output character encoding
30
 * - mb_internal_encoding    - Set/Get internal character encoding
31
 * - mb_list_encodings       - Returns an array of all supported encodings
32
 * - mb_ord                  - Returns the Unicode code point of a character
33
 * - mb_output_handler       - Callback function converts character encoding in output buffer
34
 * - mb_scrub                - Replaces ill-formed byte sequences with substitute characters
35
 * - mb_strlen               - Get string length
36
 * - mb_strpos               - Find position of first occurrence of string in a string
37
 * - mb_strrpos              - Find position of last occurrence of a string in a string
38
 * - mb_str_split            - Convert a string to an array
39
 * - mb_strtolower           - Make a string lowercase
40
 * - mb_strtoupper           - Make a string uppercase
41
 * - mb_substitute_character - Set/Get substitution character
42
 * - mb_substr               - Get part of string
43
 * - mb_stripos              - Finds position of first occurrence of a string within another, case insensitive
44
 * - mb_stristr              - Finds first occurrence of a string within another, case insensitive
45
 * - mb_strrchr              - Finds the last occurrence of a character in a string within another
46
 * - mb_strrichr             - Finds the last occurrence of a character in a string within another, case insensitive
47
 * - mb_strripos             - Finds position of last occurrence of a string within another, case insensitive
48
 * - mb_strstr               - Finds first occurrence of a string within another
49
 * - mb_strwidth             - Return width of string
50
 * - mb_substr_count         - Count the number of substring occurrences
51
 *
52
 * Not implemented:
53
 * - mb_convert_kana         - Convert "kana" one from another ("zen-kaku", "han-kaku" and more)
54
 * - mb_ereg_*               - Regular expression with multibyte support
55
 * - mb_parse_str            - Parse GET/POST/COOKIE data and set global variable
56
 * - mb_preferred_mime_name  - Get MIME charset string
57
 * - mb_regex_encoding       - Returns current encoding for multibyte regex as string
58
 * - mb_regex_set_options    - Set/Get the default options for mbregex functions
59
 * - mb_send_mail            - Send encoded mail
60
 * - mb_split                - Split multibyte string using regular expression
61
 * - mb_strcut               - Get part of string
62
 * - mb_strimwidth           - Get truncated string with specified width
63
 *
64
 * @author Nicolas Grekas <p@tchwork.com>
65
 *
66
 * @internal
67
 */
68
final class Mbstring
69
{
70
    const MB_CASE_FOLD = PHP_INT_MAX;
71
 
72
    private static $encodingList = array('ASCII', 'UTF-8');
73
    private static $language = 'neutral';
74
    private static $internalEncoding = 'UTF-8';
75
    private static $caseFold = array(
76
        array('µ', 'ſ', "\xCD\x85", 'ς', "\xCF\x90", "\xCF\x91", "\xCF\x95", "\xCF\x96", "\xCF\xB0", "\xCF\xB1", "\xCF\xB5", "\xE1\xBA\x9B", "\xE1\xBE\xBE"),
77
        array('μ', 's', 'ι',        'σ', 'β',        'θ',        'φ',        'π',        'κ',        'ρ',        'ε',        "\xE1\xB9\xA1", 'ι'),
78
    );
79
 
80
    public static function mb_convert_encoding($s, $toEncoding, $fromEncoding = null)
81
    {
82
        if (\is_array($fromEncoding) || false !== strpos($fromEncoding, ',')) {
83
            $fromEncoding = self::mb_detect_encoding($s, $fromEncoding);
84
        } else {
85
            $fromEncoding = self::getEncoding($fromEncoding);
86
        }
87
 
88
        $toEncoding = self::getEncoding($toEncoding);
89
 
90
        if ('BASE64' === $fromEncoding) {
91
            $s = base64_decode($s);
92
            $fromEncoding = $toEncoding;
93
        }
94
 
95
        if ('BASE64' === $toEncoding) {
96
            return base64_encode($s);
97
        }
98
 
99
        if ('HTML-ENTITIES' === $toEncoding || 'HTML' === $toEncoding) {
100
            if ('HTML-ENTITIES' === $fromEncoding || 'HTML' === $fromEncoding) {
101
                $fromEncoding = 'Windows-1252';
102
            }
103
            if ('UTF-8' !== $fromEncoding) {
104
                $s = iconv($fromEncoding, 'UTF-8//IGNORE', $s);
105
            }
106
 
107
            return preg_replace_callback('/[\x80-\xFF]+/', array(__CLASS__, 'html_encoding_callback'), $s);
108
        }
109
 
110
        if ('HTML-ENTITIES' === $fromEncoding) {
111
            $s = html_entity_decode($s, ENT_COMPAT, 'UTF-8');
112
            $fromEncoding = 'UTF-8';
113
        }
114
 
115
        return iconv($fromEncoding, $toEncoding.'//IGNORE', $s);
116
    }
117
 
118
    public static function mb_convert_variables($toEncoding, $fromEncoding, &...$vars)
119
    {
120
        $ok = true;
121
        array_walk_recursive($vars, function (&$v) use (&$ok, $toEncoding, $fromEncoding) {
122
            if (false === $v = Mbstring::mb_convert_encoding($v, $toEncoding, $fromEncoding)) {
123
                $ok = false;
124
            }
125
        });
126
 
127
        return $ok ? $fromEncoding : false;
128
    }
129
 
130
    public static function mb_decode_mimeheader($s)
131
    {
132
        return iconv_mime_decode($s, 2, self::$internalEncoding);
133
    }
134
 
135
    public static function mb_encode_mimeheader($s, $charset = null, $transferEncoding = null, $linefeed = null, $indent = null)
136
    {
137
        trigger_error('mb_encode_mimeheader() is bugged. Please use iconv_mime_encode() instead', E_USER_WARNING);
138
    }
139
 
140
    public static function mb_decode_numericentity($s, $convmap, $encoding = null)
141
    {
142
        if (null !== $s && !\is_scalar($s) && !(\is_object($s) && \method_exists($s, '__toString'))) {
143
            trigger_error('mb_decode_numericentity() expects parameter 1 to be string, '.\gettype($s).' given', E_USER_WARNING);
144
 
145
            return null;
146
        }
147
 
148
        if (!\is_array($convmap) || !$convmap) {
149
            return false;
150
        }
151
 
152
        if (null !== $encoding && !\is_scalar($encoding)) {
153
            trigger_error('mb_decode_numericentity() expects parameter 3 to be string, '.\gettype($s).' given', E_USER_WARNING);
154
 
155
            return '';  // Instead of null (cf. mb_encode_numericentity).
156
        }
157
 
158
        $s = (string) $s;
159
        if ('' === $s) {
160
            return '';
161
        }
162
 
163
        $encoding = self::getEncoding($encoding);
164
 
165
        if ('UTF-8' === $encoding) {
166
            $encoding = null;
167
            if (!preg_match('//u', $s)) {
168
                $s = @iconv('UTF-8', 'UTF-8//IGNORE', $s);
169
            }
170
        } else {
171
            $s = iconv($encoding, 'UTF-8//IGNORE', $s);
172
        }
173
 
174
        $cnt = floor(\count($convmap) / 4) * 4;
175
 
176
        for ($i = 0; $i < $cnt; $i += 4) {
177
            // collector_decode_htmlnumericentity ignores $convmap[$i + 3]
178
            $convmap[$i] += $convmap[$i + 2];
179
            $convmap[$i + 1] += $convmap[$i + 2];
180
        }
181
 
182
        $s = preg_replace_callback('/&#(?:0*([0-9]+)|x0*([0-9a-fA-F]+))(?!&);?/', function (array $m) use ($cnt, $convmap) {
183
            $c = isset($m[2]) ? (int) hexdec($m[2]) : $m[1];
184
            for ($i = 0; $i < $cnt; $i += 4) {
185
                if ($c >= $convmap[$i] && $c <= $convmap[$i + 1]) {
186
                    return Mbstring::mb_chr($c - $convmap[$i + 2]);
187
                }
188
            }
189
 
190
            return $m[0];
191
        }, $s);
192
 
193
        if (null === $encoding) {
194
            return $s;
195
        }
196
 
197
        return iconv('UTF-8', $encoding.'//IGNORE', $s);
198
    }
199
 
200
    public static function mb_encode_numericentity($s, $convmap, $encoding = null, $is_hex = false)
201
    {
202
        if (null !== $s && !\is_scalar($s) && !(\is_object($s) && \method_exists($s, '__toString'))) {
203
            trigger_error('mb_encode_numericentity() expects parameter 1 to be string, '.\gettype($s).' given', E_USER_WARNING);
204
 
205
            return null;
206
        }
207
 
208
        if (!\is_array($convmap) || !$convmap) {
209
            return false;
210
        }
211
 
212
        if (null !== $encoding && !\is_scalar($encoding)) {
213
            trigger_error('mb_encode_numericentity() expects parameter 3 to be string, '.\gettype($s).' given', E_USER_WARNING);
214
 
215
            return null;  // Instead of '' (cf. mb_decode_numericentity).
216
        }
217
 
218
        if (null !== $is_hex && !\is_scalar($is_hex)) {
219
            trigger_error('mb_encode_numericentity() expects parameter 4 to be boolean, '.\gettype($s).' given', E_USER_WARNING);
220
 
221
            return null;
222
        }
223
 
224
        $s = (string) $s;
225
        if ('' === $s) {
226
            return '';
227
        }
228
 
229
        $encoding = self::getEncoding($encoding);
230
 
231
        if ('UTF-8' === $encoding) {
232
            $encoding = null;
233
            if (!preg_match('//u', $s)) {
234
                $s = @iconv('UTF-8', 'UTF-8//IGNORE', $s);
235
            }
236
        } else {
237
            $s = iconv($encoding, 'UTF-8//IGNORE', $s);
238
        }
239
 
240
        static $ulenMask = array("\xC0" => 2, "\xD0" => 2, "\xE0" => 3, "\xF0" => 4);
241
 
242
        $cnt = floor(\count($convmap) / 4) * 4;
243
        $i = 0;
244
        $len = \strlen($s);
245
        $result = '';
246
 
247
        while ($i < $len) {
248
            $ulen = $s[$i] < "\x80" ? 1 : $ulenMask[$s[$i] & "\xF0"];
249
            $uchr = substr($s, $i, $ulen);
250
            $i += $ulen;
251
            $c = self::mb_ord($uchr);
252
 
253
            for ($j = 0; $j < $cnt; $j += 4) {
254
                if ($c >= $convmap[$j] && $c <= $convmap[$j + 1]) {
255
                    $cOffset = ($c + $convmap[$j + 2]) & $convmap[$j + 3];
256
                    $result .= $is_hex ? sprintf('&#x%X;', $cOffset) : '&#'.$cOffset.';';
257
                    continue 2;
258
                }
259
            }
260
            $result .= $uchr;
261
        }
262
 
263
        if (null === $encoding) {
264
            return $result;
265
        }
266
 
267
        return iconv('UTF-8', $encoding.'//IGNORE', $result);
268
    }
269
 
270
    public static function mb_convert_case($s, $mode, $encoding = null)
271
    {
272
        $s = (string) $s;
273
        if ('' === $s) {
274
            return '';
275
        }
276
 
277
        $encoding = self::getEncoding($encoding);
278
 
279
        if ('UTF-8' === $encoding) {
280
            $encoding = null;
281
            if (!preg_match('//u', $s)) {
282
                $s = @iconv('UTF-8', 'UTF-8//IGNORE', $s);
283
            }
284
        } else {
285
            $s = iconv($encoding, 'UTF-8//IGNORE', $s);
286
        }
287
 
288
        if (MB_CASE_TITLE == $mode) {
289
            static $titleRegexp = null;
290
            if (null === $titleRegexp) {
291
                $titleRegexp = self::getData('titleCaseRegexp');
292
            }
293
            $s = preg_replace_callback($titleRegexp, array(__CLASS__, 'title_case'), $s);
294
        } else {
295
            if (MB_CASE_UPPER == $mode) {
296
                static $upper = null;
297
                if (null === $upper) {
298
                    $upper = self::getData('upperCase');
299
                }
300
                $map = $upper;
301
            } else {
302
                if (self::MB_CASE_FOLD === $mode) {
303
                    $s = str_replace(self::$caseFold[0], self::$caseFold[1], $s);
304
                }
305
 
306
                static $lower = null;
307
                if (null === $lower) {
308
                    $lower = self::getData('lowerCase');
309
                }
310
                $map = $lower;
311
            }
312
 
313
            static $ulenMask = array("\xC0" => 2, "\xD0" => 2, "\xE0" => 3, "\xF0" => 4);
314
 
315
            $i = 0;
316
            $len = \strlen($s);
317
 
318
            while ($i < $len) {
319
                $ulen = $s[$i] < "\x80" ? 1 : $ulenMask[$s[$i] & "\xF0"];
320
                $uchr = substr($s, $i, $ulen);
321
                $i += $ulen;
322
 
323
                if (isset($map[$uchr])) {
324
                    $uchr = $map[$uchr];
325
                    $nlen = \strlen($uchr);
326
 
327
                    if ($nlen == $ulen) {
328
                        $nlen = $i;
329
                        do {
330
                            $s[--$nlen] = $uchr[--$ulen];
331
                        } while ($ulen);
332
                    } else {
333
                        $s = substr_replace($s, $uchr, $i - $ulen, $ulen);
334
                        $len += $nlen - $ulen;
335
                        $i += $nlen - $ulen;
336
                    }
337
                }
338
            }
339
        }
340
 
341
        if (null === $encoding) {
342
            return $s;
343
        }
344
 
345
        return iconv('UTF-8', $encoding.'//IGNORE', $s);
346
    }
347
 
348
    public static function mb_internal_encoding($encoding = null)
349
    {
350
        if (null === $encoding) {
351
            return self::$internalEncoding;
352
        }
353
 
354
        $encoding = self::getEncoding($encoding);
355
 
356
        if ('UTF-8' === $encoding || false !== @iconv($encoding, $encoding, ' ')) {
357
            self::$internalEncoding = $encoding;
358
 
359
            return true;
360
        }
361
 
362
        return false;
363
    }
364
 
365
    public static function mb_language($lang = null)
366
    {
367
        if (null === $lang) {
368
            return self::$language;
369
        }
370
 
371
        switch ($lang = strtolower($lang)) {
372
            case 'uni':
373
            case 'neutral':
374
                self::$language = $lang;
375
 
376
                return true;
377
        }
378
 
379
        return false;
380
    }
381
 
382
    public static function mb_list_encodings()
383
    {
384
        return array('UTF-8');
385
    }
386
 
387
    public static function mb_encoding_aliases($encoding)
388
    {
389
        switch (strtoupper($encoding)) {
390
            case 'UTF8':
391
            case 'UTF-8':
392
                return array('utf8');
393
        }
394
 
395
        return false;
396
    }
397
 
398
    public static function mb_check_encoding($var = null, $encoding = null)
399
    {
400
        if (null === $encoding) {
401
            if (null === $var) {
402
                return false;
403
            }
404
            $encoding = self::$internalEncoding;
405
        }
406
 
407
        return self::mb_detect_encoding($var, array($encoding)) || false !== @iconv($encoding, $encoding, $var);
408
    }
409
 
410
    public static function mb_detect_encoding($str, $encodingList = null, $strict = false)
411
    {
412
        if (null === $encodingList) {
413
            $encodingList = self::$encodingList;
414
        } else {
415
            if (!\is_array($encodingList)) {
416
                $encodingList = array_map('trim', explode(',', $encodingList));
417
            }
418
            $encodingList = array_map('strtoupper', $encodingList);
419
        }
420
 
421
        foreach ($encodingList as $enc) {
422
            switch ($enc) {
423
                case 'ASCII':
424
                    if (!preg_match('/[\x80-\xFF]/', $str)) {
425
                        return $enc;
426
                    }
427
                    break;
428
 
429
                case 'UTF8':
430
                case 'UTF-8':
431
                    if (preg_match('//u', $str)) {
432
                        return 'UTF-8';
433
                    }
434
                    break;
435
 
436
                default:
437
                    if (0 === strncmp($enc, 'ISO-8859-', 9)) {
438
                        return $enc;
439
                    }
440
            }
441
        }
442
 
443
        return false;
444
    }
445
 
446
    public static function mb_detect_order($encodingList = null)
447
    {
448
        if (null === $encodingList) {
449
            return self::$encodingList;
450
        }
451
 
452
        if (!\is_array($encodingList)) {
453
            $encodingList = array_map('trim', explode(',', $encodingList));
454
        }
455
        $encodingList = array_map('strtoupper', $encodingList);
456
 
457
        foreach ($encodingList as $enc) {
458
            switch ($enc) {
459
                default:
460
                    if (strncmp($enc, 'ISO-8859-', 9)) {
461
                        return false;
462
                    }
463
                    // no break
464
                case 'ASCII':
465
                case 'UTF8':
466
                case 'UTF-8':
467
            }
468
        }
469
 
470
        self::$encodingList = $encodingList;
471
 
472
        return true;
473
    }
474
 
475
    public static function mb_strlen($s, $encoding = null)
476
    {
477
        $encoding = self::getEncoding($encoding);
478
        if ('CP850' === $encoding || 'ASCII' === $encoding) {
479
            return \strlen($s);
480
        }
481
 
482
        return @iconv_strlen($s, $encoding);
483
    }
484
 
485
    public static function mb_strpos($haystack, $needle, $offset = 0, $encoding = null)
486
    {
487
        $encoding = self::getEncoding($encoding);
488
        if ('CP850' === $encoding || 'ASCII' === $encoding) {
489
            return strpos($haystack, $needle, $offset);
490
        }
491
 
492
        $needle = (string) $needle;
493
        if ('' === $needle) {
494
            trigger_error(__METHOD__.': Empty delimiter', E_USER_WARNING);
495
 
496
            return false;
497
        }
498
 
499
        return iconv_strpos($haystack, $needle, $offset, $encoding);
500
    }
501
 
502
    public static function mb_strrpos($haystack, $needle, $offset = 0, $encoding = null)
503
    {
504
        $encoding = self::getEncoding($encoding);
505
        if ('CP850' === $encoding || 'ASCII' === $encoding) {
506
            return strrpos($haystack, $needle, $offset);
507
        }
508
 
509
        if ($offset != (int) $offset) {
510
            $offset = 0;
511
        } elseif ($offset = (int) $offset) {
512
            if ($offset < 0) {
513
                if (0 > $offset += self::mb_strlen($needle)) {
514
                    $haystack = self::mb_substr($haystack, 0, $offset, $encoding);
515
                }
516
                $offset = 0;
517
            } else {
518
                $haystack = self::mb_substr($haystack, $offset, 2147483647, $encoding);
519
            }
520
        }
521
 
522
        $pos = iconv_strrpos($haystack, $needle, $encoding);
523
 
524
        return false !== $pos ? $offset + $pos : false;
525
    }
526
 
527
    public static function mb_str_split($string, $split_length = 1, $encoding = null)
528
    {
529
        if (null !== $string && !\is_scalar($string) && !(\is_object($string) && \method_exists($string, '__toString'))) {
530
            trigger_error('mb_str_split() expects parameter 1 to be string, '.\gettype($string).' given', E_USER_WARNING);
531
 
532
            return null;
533
        }
534
 
535
        if (1 > $split_length = (int) $split_length) {
536
            trigger_error('The length of each segment must be greater than zero', E_USER_WARNING);
537
 
538
            return false;
539
        }
540
 
541
        if (null === $encoding) {
542
            $encoding = mb_internal_encoding();
543
        }
544
 
545
        if ('UTF-8' === $encoding = self::getEncoding($encoding)) {
546
            $rx = '/(';
547
            while (65535 < $split_length) {
548
                $rx .= '.{65535}';
549
                $split_length -= 65535;
550
            }
551
            $rx .= '.{'.$split_length.'})/us';
552
 
553
            return preg_split($rx, $string, null, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
554
        }
555
 
556
        $result = array();
557
        $length = mb_strlen($string, $encoding);
558
 
559
        for ($i = 0; $i < $length; $i += $split_length) {
560
            $result[] = mb_substr($string, $i, $split_length, $encoding);
561
        }
562
 
563
        return $result;
564
    }
565
 
566
    public static function mb_strtolower($s, $encoding = null)
567
    {
568
        return self::mb_convert_case($s, MB_CASE_LOWER, $encoding);
569
    }
570
 
571
    public static function mb_strtoupper($s, $encoding = null)
572
    {
573
        return self::mb_convert_case($s, MB_CASE_UPPER, $encoding);
574
    }
575
 
576
    public static function mb_substitute_character($c = null)
577
    {
578
        if (0 === strcasecmp($c, 'none')) {
579
            return true;
580
        }
581
 
582
        return null !== $c ? false : 'none';
583
    }
584
 
585
    public static function mb_substr($s, $start, $length = null, $encoding = null)
586
    {
587
        $encoding = self::getEncoding($encoding);
588
        if ('CP850' === $encoding || 'ASCII' === $encoding) {
589
            return (string) substr($s, $start, null === $length ? 2147483647 : $length);
590
        }
591
 
592
        if ($start < 0) {
593
            $start = iconv_strlen($s, $encoding) + $start;
594
            if ($start < 0) {
595
                $start = 0;
596
            }
597
        }
598
 
599
        if (null === $length) {
600
            $length = 2147483647;
601
        } elseif ($length < 0) {
602
            $length = iconv_strlen($s, $encoding) + $length - $start;
603
            if ($length < 0) {
604
                return '';
605
            }
606
        }
607
 
608
        return (string) iconv_substr($s, $start, $length, $encoding);
609
    }
610
 
611
    public static function mb_stripos($haystack, $needle, $offset = 0, $encoding = null)
612
    {
613
        $haystack = self::mb_convert_case($haystack, self::MB_CASE_FOLD, $encoding);
614
        $needle = self::mb_convert_case($needle, self::MB_CASE_FOLD, $encoding);
615
 
616
        return self::mb_strpos($haystack, $needle, $offset, $encoding);
617
    }
618
 
619
    public static function mb_stristr($haystack, $needle, $part = false, $encoding = null)
620
    {
621
        $pos = self::mb_stripos($haystack, $needle, 0, $encoding);
622
 
623
        return self::getSubpart($pos, $part, $haystack, $encoding);
624
    }
625
 
626
    public static function mb_strrchr($haystack, $needle, $part = false, $encoding = null)
627
    {
628
        $encoding = self::getEncoding($encoding);
629
        if ('CP850' === $encoding || 'ASCII' === $encoding) {
630
            $pos = strrpos($haystack, $needle);
631
        } else {
632
            $needle = self::mb_substr($needle, 0, 1, $encoding);
633
            $pos = iconv_strrpos($haystack, $needle, $encoding);
634
        }
635
 
636
        return self::getSubpart($pos, $part, $haystack, $encoding);
637
    }
638
 
639
    public static function mb_strrichr($haystack, $needle, $part = false, $encoding = null)
640
    {
641
        $needle = self::mb_substr($needle, 0, 1, $encoding);
642
        $pos = self::mb_strripos($haystack, $needle, $encoding);
643
 
644
        return self::getSubpart($pos, $part, $haystack, $encoding);
645
    }
646
 
647
    public static function mb_strripos($haystack, $needle, $offset = 0, $encoding = null)
648
    {
649
        $haystack = self::mb_convert_case($haystack, self::MB_CASE_FOLD, $encoding);
650
        $needle = self::mb_convert_case($needle, self::MB_CASE_FOLD, $encoding);
651
 
652
        return self::mb_strrpos($haystack, $needle, $offset, $encoding);
653
    }
654
 
655
    public static function mb_strstr($haystack, $needle, $part = false, $encoding = null)
656
    {
657
        $pos = strpos($haystack, $needle);
658
        if (false === $pos) {
659
            return false;
660
        }
661
        if ($part) {
662
            return substr($haystack, 0, $pos);
663
        }
664
 
665
        return substr($haystack, $pos);
666
    }
667
 
668
    public static function mb_get_info($type = 'all')
669
    {
670
        $info = array(
671
            'internal_encoding' => self::$internalEncoding,
672
            'http_output' => 'pass',
673
            'http_output_conv_mimetypes' => '^(text/|application/xhtml\+xml)',
674
            'func_overload' => 0,
675
            'func_overload_list' => 'no overload',
676
            'mail_charset' => 'UTF-8',
677
            'mail_header_encoding' => 'BASE64',
678
            'mail_body_encoding' => 'BASE64',
679
            'illegal_chars' => 0,
680
            'encoding_translation' => 'Off',
681
            'language' => self::$language,
682
            'detect_order' => self::$encodingList,
683
            'substitute_character' => 'none',
684
            'strict_detection' => 'Off',
685
        );
686
 
687
        if ('all' === $type) {
688
            return $info;
689
        }
690
        if (isset($info[$type])) {
691
            return $info[$type];
692
        }
693
 
694
        return false;
695
    }
696
 
697
    public static function mb_http_input($type = '')
698
    {
699
        return false;
700
    }
701
 
702
    public static function mb_http_output($encoding = null)
703
    {
704
        return null !== $encoding ? 'pass' === $encoding : 'pass';
705
    }
706
 
707
    public static function mb_strwidth($s, $encoding = null)
708
    {
709
        $encoding = self::getEncoding($encoding);
710
 
711
        if ('UTF-8' !== $encoding) {
712
            $s = iconv($encoding, 'UTF-8//IGNORE', $s);
713
        }
714
 
715
        $s = preg_replace('/[\x{1100}-\x{115F}\x{2329}\x{232A}\x{2E80}-\x{303E}\x{3040}-\x{A4CF}\x{AC00}-\x{D7A3}\x{F900}-\x{FAFF}\x{FE10}-\x{FE19}\x{FE30}-\x{FE6F}\x{FF00}-\x{FF60}\x{FFE0}-\x{FFE6}\x{20000}-\x{2FFFD}\x{30000}-\x{3FFFD}]/u', '', $s, -1, $wide);
716
 
717
        return ($wide << 1) + iconv_strlen($s, 'UTF-8');
718
    }
719
 
720
    public static function mb_substr_count($haystack, $needle, $encoding = null)
721
    {
722
        return substr_count($haystack, $needle);
723
    }
724
 
725
    public static function mb_output_handler($contents, $status)
726
    {
727
        return $contents;
728
    }
729
 
730
    public static function mb_chr($code, $encoding = null)
731
    {
732
        if (0x80 > $code %= 0x200000) {
733
            $s = \chr($code);
734
        } elseif (0x800 > $code) {
735
            $s = \chr(0xC0 | $code >> 6).\chr(0x80 | $code & 0x3F);
736
        } elseif (0x10000 > $code) {
737
            $s = \chr(0xE0 | $code >> 12).\chr(0x80 | $code >> 6 & 0x3F).\chr(0x80 | $code & 0x3F);
738
        } else {
739
            $s = \chr(0xF0 | $code >> 18).\chr(0x80 | $code >> 12 & 0x3F).\chr(0x80 | $code >> 6 & 0x3F).\chr(0x80 | $code & 0x3F);
740
        }
741
 
742
        if ('UTF-8' !== $encoding = self::getEncoding($encoding)) {
743
            $s = mb_convert_encoding($s, $encoding, 'UTF-8');
744
        }
745
 
746
        return $s;
747
    }
748
 
749
    public static function mb_ord($s, $encoding = null)
750
    {
751
        if ('UTF-8' !== $encoding = self::getEncoding($encoding)) {
752
            $s = mb_convert_encoding($s, 'UTF-8', $encoding);
753
        }
754
 
755
        if (1 === \strlen($s)) {
756
            return \ord($s);
757
        }
758
 
759
        $code = ($s = unpack('C*', substr($s, 0, 4))) ? $s[1] : 0;
760
        if (0xF0 <= $code) {
761
            return (($code - 0xF0) << 18) + (($s[2] - 0x80) << 12) + (($s[3] - 0x80) << 6) + $s[4] - 0x80;
762
        }
763
        if (0xE0 <= $code) {
764
            return (($code - 0xE0) << 12) + (($s[2] - 0x80) << 6) + $s[3] - 0x80;
765
        }
766
        if (0xC0 <= $code) {
767
            return (($code - 0xC0) << 6) + $s[2] - 0x80;
768
        }
769
 
770
        return $code;
771
    }
772
 
773
    private static function getSubpart($pos, $part, $haystack, $encoding)
774
    {
775
        if (false === $pos) {
776
            return false;
777
        }
778
        if ($part) {
779
            return self::mb_substr($haystack, 0, $pos, $encoding);
780
        }
781
 
782
        return self::mb_substr($haystack, $pos, null, $encoding);
783
    }
784
 
785
    private static function html_encoding_callback(array $m)
786
    {
787
        $i = 1;
788
        $entities = '';
789
        $m = unpack('C*', htmlentities($m[0], ENT_COMPAT, 'UTF-8'));
790
 
791
        while (isset($m[$i])) {
792
            if (0x80 > $m[$i]) {
793
                $entities .= \chr($m[$i++]);
794
                continue;
795
            }
796
            if (0xF0 <= $m[$i]) {
797
                $c = (($m[$i++] - 0xF0) << 18) + (($m[$i++] - 0x80) << 12) + (($m[$i++] - 0x80) << 6) + $m[$i++] - 0x80;
798
            } elseif (0xE0 <= $m[$i]) {
799
                $c = (($m[$i++] - 0xE0) << 12) + (($m[$i++] - 0x80) << 6) + $m[$i++] - 0x80;
800
            } else {
801
                $c = (($m[$i++] - 0xC0) << 6) + $m[$i++] - 0x80;
802
            }
803
 
804
            $entities .= '&#'.$c.';';
805
        }
806
 
807
        return $entities;
808
    }
809
 
810
    private static function title_case(array $s)
811
    {
812
        return self::mb_convert_case($s[1], MB_CASE_UPPER, 'UTF-8').self::mb_convert_case($s[2], MB_CASE_LOWER, 'UTF-8');
813
    }
814
 
815
    private static function getData($file)
816
    {
817
        if (file_exists($file = __DIR__.'/Resources/unidata/'.$file.'.php')) {
818
            return require $file;
819
        }
820
 
821
        return false;
822
    }
823
 
824
    private static function getEncoding($encoding)
825
    {
826
        if (null === $encoding) {
827
            return self::$internalEncoding;
828
        }
829
 
830
        if ('UTF-8' === $encoding) {
831
            return 'UTF-8';
832
        }
833
 
834
        $encoding = strtoupper($encoding);
835
 
836
        if ('8BIT' === $encoding || 'BINARY' === $encoding) {
837
            return 'CP850';
838
        }
839
 
840
        if ('UTF8' === $encoding) {
841
            return 'UTF-8';
842
        }
843
 
844
        return $encoding;
845
    }
846
}