审查视图

vendor/symfony/polyfill-intl-idn/Idn.php 8.4 KB
郭盛 authored
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
<?php

/*
 * Copyright (c) 2014 TrueServer B.V.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is furnished
 * to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 * Originally forked from
 * https://github.com/true/php-punycode/blob/v2.1.1/src/Punycode.php
 */

namespace Symfony\Polyfill\Intl\Idn;

/**
 * Partial intl implementation in pure PHP.
 *
 * Implemented:
 * - idn_to_ascii - Convert domain name to IDNA ASCII form
 * - idn_to_utf8  - Convert domain name from IDNA ASCII to Unicode
 *
 * @author Renan Gonçalves <renan.saddam@gmail.com>
 * @author Sebastian Kroczek <sk@xbug.de>
 * @author Dmitry Lukashin <dmitry@lukashin.ru>
 * @author Laurent Bassin <laurent@bassin.info>
 *
 * @internal
 */
final class Idn
{
    const INTL_IDNA_VARIANT_2003 = 0;
    const INTL_IDNA_VARIANT_UTS46 = 1;

    private static $encodeTable = array(
        'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
        'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
        'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
    );

    private static $decodeTable = array(
        'a' => 0, 'b' => 1, 'c' => 2, 'd' => 3, 'e' => 4, 'f' => 5,
        'g' => 6, 'h' => 7, 'i' => 8, 'j' => 9, 'k' => 10, 'l' => 11,
        'm' => 12, 'n' => 13, 'o' => 14, 'p' => 15, 'q' => 16, 'r' => 17,
        's' => 18, 't' => 19, 'u' => 20, 'v' => 21, 'w' => 22, 'x' => 23,
        'y' => 24, 'z' => 25, '0' => 26, '1' => 27, '2' => 28, '3' => 29,
        '4' => 30, '5' => 31, '6' => 32, '7' => 33, '8' => 34, '9' => 35,
    );

    public static function idn_to_ascii($domain, $options, $variant, &$idna_info = array())
    {
        if (\PHP_VERSION_ID >= 70200 && self::INTL_IDNA_VARIANT_2003 === $variant) {
            @trigger_error('idn_to_ascii(): INTL_IDNA_VARIANT_2003 is deprecated', E_USER_DEPRECATED);
        }

        if (self::INTL_IDNA_VARIANT_UTS46 === $variant) {
            $domain = mb_strtolower($domain, 'utf-8');
        }

        $parts = explode('.', $domain);

        foreach ($parts as $i => &$part) {
            if ('' === $part && \count($parts) > 1 + $i) {
                return false;
            }
            if (false === $part = self::encodePart($part)) {
                return false;
            }
        }

        $output = implode('.', $parts);

        $idna_info = array(
            'result' => \strlen($output) > 255 ? false : $output,
            'isTransitionalDifferent' => false,
            'errors' => 0,
        );

        return $idna_info['result'];
    }

    public static function idn_to_utf8($domain, $options, $variant, &$idna_info = array())
    {
        if (\PHP_VERSION_ID >= 70200 && self::INTL_IDNA_VARIANT_2003 === $variant) {
            @trigger_error('idn_to_utf8(): INTL_IDNA_VARIANT_2003 is deprecated', E_USER_DEPRECATED);
        }

        $parts = explode('.', $domain);

        foreach ($parts as &$part) {
            $length = \strlen($part);
            if ($length < 1 || 63 < $length) {
                continue;
            }
            if (0 !== strpos($part, 'xn--')) {
                continue;
            }

            $part = substr($part, 4);
            $part = self::decodePart($part);
        }

        $output = implode('.', $parts);

        $idna_info = array(
            'result' => \strlen($output) > 255 ? false : $output,
            'isTransitionalDifferent' => false,
            'errors' => 0,
        );

        return $idna_info['result'];
    }

    private static function encodePart($input)
    {
        $codePoints = self::listCodePoints($input);

        $n = 128;
        $bias = 72;
        $delta = 0;
        $h = $b = \count($codePoints['basic']);

        $output = '';
        foreach ($codePoints['basic'] as $code) {
            $output .= mb_chr($code, 'utf-8');
        }
        if ($input === $output) {
            return $output;
        }
        if ($b > 0) {
            $output .= '-';
        }

        $codePoints['nonBasic'] = array_unique($codePoints['nonBasic']);
        sort($codePoints['nonBasic']);

        $i = 0;
        $length = mb_strlen($input, 'utf-8');
        while ($h < $length) {
            $m = $codePoints['nonBasic'][$i++];
            $delta += ($m - $n) * ($h + 1);
            $n = $m;

            foreach ($codePoints['all'] as $c) {
                if ($c < $n || $c < 128) {
                    ++$delta;
                }
                if ($c === $n) {
                    $q = $delta;
                    for ($k = 36;; $k += 36) {
                        $t = self::calculateThreshold($k, $bias);
                        if ($q < $t) {
                            break;
                        }

                        $code = $t + (($q - $t) % (36 - $t));
                        $output .= self::$encodeTable[$code];

                        $q = ($q - $t) / (36 - $t);
                    }

                    $output .= self::$encodeTable[$q];
                    $bias = self::adapt($delta, $h + 1, ($h === $b));
                    $delta = 0;
                    ++$h;
                }
            }

            ++$delta;
            ++$n;
        }

        $output = 'xn--'.$output;

        return \strlen($output) < 1 || 63 < \strlen($output) ? false : strtolower($output);
    }

    private static function listCodePoints($input)
    {
        $codePoints = array(
            'all' => array(),
            'basic' => array(),
            'nonBasic' => array(),
        );

        $length = mb_strlen($input, 'utf-8');
        for ($i = 0; $i < $length; ++$i) {
            $char = mb_substr($input, $i, 1, 'utf-8');
            $code = mb_ord($char, 'utf-8');
            if ($code < 128) {
                $codePoints['all'][] = $codePoints['basic'][] = $code;
            } else {
                $codePoints['all'][] = $codePoints['nonBasic'][] = $code;
            }
        }

        return $codePoints;
    }

    private static function calculateThreshold($k, $bias)
    {
        if ($k <= $bias + 1) {
            return 1;
        }
        if ($k >= $bias + 26) {
            return 26;
        }

        return $k - $bias;
    }

    private static function adapt($delta, $numPoints, $firstTime)
    {
        $delta = (int) ($firstTime ? $delta / 700 : $delta / 2);
        $delta += (int) ($delta / $numPoints);

        $k = 0;
        while ($delta > 35 * 13) {
            $delta = (int) ($delta / 35);
            $k = $k + 36;
        }

        return $k + (int) (36 * $delta / ($delta + 38));
    }

    private static function decodePart($input)
    {
        $n = 128;
        $i = 0;
        $bias = 72;
        $output = '';

        $pos = strrpos($input, '-');
        if (false !== $pos) {
            $output = substr($input, 0, $pos++);
        } else {
            $pos = 0;
        }

        $outputLength = \strlen($output);
        $inputLength = \strlen($input);

        while ($pos < $inputLength) {
            $oldi = $i;
            $w = 1;

            for ($k = 36;; $k += 36) {
                $digit = self::$decodeTable[$input[$pos++]];
                $i += $digit * $w;
                $t = self::calculateThreshold($k, $bias);

                if ($digit < $t) {
                    break;
                }

                $w *= 36 - $t;
            }

            $bias = self::adapt($i - $oldi, ++$outputLength, 0 === $oldi);
            $n = $n + (int) ($i / $outputLength);
            $i = $i % $outputLength;
            $output = mb_substr($output, 0, $i, 'utf-8').mb_chr($n, 'utf-8').mb_substr($output, $i, $outputLength - 1, 'utf-8');

            ++$i;
        }

        return $output;
    }
}