[ Index ]

PHP Cross Reference of phpBB-3.3.2-deutsch

title

Body

[close]

/vendor/symfony/polyfill-intl-idn/ -> Idn.php (source)

   1  <?php
   2  
   3  /*
   4   * Copyright (c) 2014 TrueServer B.V.
   5   *
   6   * Permission is hereby granted, free of charge, to any person obtaining a copy
   7   * of this software and associated documentation files (the "Software"), to deal
   8   * in the Software without restriction, including without limitation the rights
   9   * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10   * copies of the Software, and to permit persons to whom the Software is furnished
  11   * to do so, subject to the following conditions:
  12   *
  13   * The above copyright notice and this permission notice shall be included in all
  14   * copies or substantial portions of the Software.
  15   *
  16   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22   * THE SOFTWARE.
  23   *
  24   * Originally forked from
  25   * https://github.com/true/php-punycode/blob/v2.1.1/src/Punycode.php
  26   */
  27  
  28  namespace Symfony\Polyfill\Intl\Idn;
  29  
  30  /**
  31   * Partial intl implementation in pure PHP.
  32   *
  33   * Implemented:
  34   * - idn_to_ascii - Convert domain name to IDNA ASCII form
  35   * - idn_to_utf8  - Convert domain name from IDNA ASCII to Unicode
  36   *
  37   * @author Renan Gonçalves <renan.saddam@gmail.com>
  38   * @author Sebastian Kroczek <sk@xbug.de>
  39   * @author Dmitry Lukashin <dmitry@lukashin.ru>
  40   * @author Laurent Bassin <laurent@bassin.info>
  41   *
  42   * @internal
  43   */
  44  final class Idn
  45  {
  46      const INTL_IDNA_VARIANT_2003 = 0;
  47      const INTL_IDNA_VARIANT_UTS46 = 1;
  48  
  49      private static $encodeTable = array(
  50          'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
  51          'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
  52          'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  53      );
  54  
  55      private static $decodeTable = array(
  56          'a' => 0, 'b' => 1, 'c' => 2, 'd' => 3, 'e' => 4, 'f' => 5,
  57          'g' => 6, 'h' => 7, 'i' => 8, 'j' => 9, 'k' => 10, 'l' => 11,
  58          'm' => 12, 'n' => 13, 'o' => 14, 'p' => 15, 'q' => 16, 'r' => 17,
  59          's' => 18, 't' => 19, 'u' => 20, 'v' => 21, 'w' => 22, 'x' => 23,
  60          'y' => 24, 'z' => 25, '0' => 26, '1' => 27, '2' => 28, '3' => 29,
  61          '4' => 30, '5' => 31, '6' => 32, '7' => 33, '8' => 34, '9' => 35,
  62      );
  63  
  64      public static function idn_to_ascii($domain, $options, $variant, &$idna_info = array())
  65      {
  66          if (\PHP_VERSION_ID >= 70200 && self::INTL_IDNA_VARIANT_2003 === $variant) {
  67              @trigger_error('idn_to_ascii(): INTL_IDNA_VARIANT_2003 is deprecated', E_USER_DEPRECATED);
  68          }
  69  
  70          if (self::INTL_IDNA_VARIANT_UTS46 === $variant) {
  71              $domain = mb_strtolower($domain, 'utf-8');
  72          }
  73  
  74          $parts = explode('.', $domain);
  75  
  76          foreach ($parts as $i => &$part) {
  77              if ('' === $part && \count($parts) > 1 + $i) {
  78                  return false;
  79              }
  80              if (false === $part = self::encodePart($part)) {
  81                  return false;
  82              }
  83          }
  84  
  85          $output = implode('.', $parts);
  86  
  87          $idna_info = array(
  88              'result' => \strlen($output) > 255 ? false : $output,
  89              'isTransitionalDifferent' => false,
  90              'errors' => 0,
  91          );
  92  
  93          return $idna_info['result'];
  94      }
  95  
  96      public static function idn_to_utf8($domain, $options, $variant, &$idna_info = array())
  97      {
  98          if (\PHP_VERSION_ID >= 70200 && self::INTL_IDNA_VARIANT_2003 === $variant) {
  99              @trigger_error('idn_to_utf8(): INTL_IDNA_VARIANT_2003 is deprecated', E_USER_DEPRECATED);
 100          }
 101  
 102          $parts = explode('.', $domain);
 103  
 104          foreach ($parts as &$part) {
 105              $length = \strlen($part);
 106              if ($length < 1 || 63 < $length) {
 107                  continue;
 108              }
 109              if (0 !== strpos($part, 'xn--')) {
 110                  continue;
 111              }
 112  
 113              $part = substr($part, 4);
 114              $part = self::decodePart($part);
 115          }
 116  
 117          $output = implode('.', $parts);
 118  
 119          $idna_info = array(
 120              'result' => \strlen($output) > 255 ? false : $output,
 121              'isTransitionalDifferent' => false,
 122              'errors' => 0,
 123          );
 124  
 125          return $idna_info['result'];
 126      }
 127  
 128      private static function encodePart($input)
 129      {
 130          if (\substr($input, 0, 1) === '-' || \substr($input, -1) === '-') {
 131              return false;
 132          }
 133  
 134          $codePoints = self::listCodePoints($input);
 135  
 136          $n = 128;
 137          $bias = 72;
 138          $delta = 0;
 139          $h = $b = \count($codePoints['basic']);
 140  
 141          $output = '';
 142          foreach ($codePoints['basic'] as $code) {
 143              $output .= mb_chr($code, 'utf-8');
 144          }
 145          if ($input === $output) {
 146              return $output;
 147          }
 148          if ($b > 0) {
 149              $output .= '-';
 150          }
 151  
 152          $codePoints['nonBasic'] = array_unique($codePoints['nonBasic']);
 153          sort($codePoints['nonBasic']);
 154  
 155          $i = 0;
 156          $length = mb_strlen($input, 'utf-8');
 157          while ($h < $length) {
 158              $m = $codePoints['nonBasic'][$i++];
 159              $delta += ($m - $n) * ($h + 1);
 160              $n = $m;
 161  
 162              foreach ($codePoints['all'] as $c) {
 163                  if ($c < $n || $c < 128) {
 164                      ++$delta;
 165                  }
 166                  if ($c === $n) {
 167                      $q = $delta;
 168                      for ($k = 36;; $k += 36) {
 169                          $t = self::calculateThreshold($k, $bias);
 170                          if ($q < $t) {
 171                              break;
 172                          }
 173  
 174                          $code = $t + (($q - $t) % (36 - $t));
 175                          $output .= self::$encodeTable[$code];
 176  
 177                          $q = ($q - $t) / (36 - $t);
 178                      }
 179  
 180                      $output .= self::$encodeTable[$q];
 181                      $bias = self::adapt($delta, $h + 1, ($h === $b));
 182                      $delta = 0;
 183                      ++$h;
 184                  }
 185              }
 186  
 187              ++$delta;
 188              ++$n;
 189          }
 190  
 191          $output = 'xn--'.$output;
 192  
 193          return \strlen($output) < 1 || 63 < \strlen($output) ? false : strtolower($output);
 194      }
 195  
 196      private static function listCodePoints($input)
 197      {
 198          $codePoints = array(
 199              'all' => array(),
 200              'basic' => array(),
 201              'nonBasic' => array(),
 202          );
 203  
 204          $length = mb_strlen($input, 'utf-8');
 205          for ($i = 0; $i < $length; ++$i) {
 206              $char = mb_substr($input, $i, 1, 'utf-8');
 207              $code = mb_ord($char, 'utf-8');
 208              if ($code < 128) {
 209                  $codePoints['all'][] = $codePoints['basic'][] = $code;
 210              } else {
 211                  $codePoints['all'][] = $codePoints['nonBasic'][] = $code;
 212              }
 213          }
 214  
 215          return $codePoints;
 216      }
 217  
 218      private static function calculateThreshold($k, $bias)
 219      {
 220          if ($k <= $bias + 1) {
 221              return 1;
 222          }
 223          if ($k >= $bias + 26) {
 224              return 26;
 225          }
 226  
 227          return $k - $bias;
 228      }
 229  
 230      private static function adapt($delta, $numPoints, $firstTime)
 231      {
 232          $delta = (int) ($firstTime ? $delta / 700 : $delta / 2);
 233          $delta += (int) ($delta / $numPoints);
 234  
 235          $k = 0;
 236          while ($delta > 35 * 13) {
 237              $delta = (int) ($delta / 35);
 238              $k = $k + 36;
 239          }
 240  
 241          return $k + (int) (36 * $delta / ($delta + 38));
 242      }
 243  
 244      private static function decodePart($input)
 245      {
 246          $n = 128;
 247          $i = 0;
 248          $bias = 72;
 249          $output = '';
 250  
 251          $pos = strrpos($input, '-');
 252          if (false !== $pos) {
 253              $output = substr($input, 0, $pos++);
 254          } else {
 255              $pos = 0;
 256          }
 257  
 258          $outputLength = \strlen($output);
 259          $inputLength = \strlen($input);
 260  
 261          while ($pos < $inputLength) {
 262              $oldi = $i;
 263              $w = 1;
 264  
 265              for ($k = 36;; $k += 36) {
 266                  $digit = self::$decodeTable[$input[$pos++]];
 267                  $i += $digit * $w;
 268                  $t = self::calculateThreshold($k, $bias);
 269  
 270                  if ($digit < $t) {
 271                      break;
 272                  }
 273  
 274                  $w *= 36 - $t;
 275              }
 276  
 277              $bias = self::adapt($i - $oldi, ++$outputLength, 0 === $oldi);
 278              $n = $n + (int) ($i / $outputLength);
 279              $i = $i % $outputLength;
 280              $output = mb_substr($output, 0, $i, 'utf-8').mb_chr($n, 'utf-8').mb_substr($output, $i, $outputLength - 1, 'utf-8');
 281  
 282              ++$i;
 283          }
 284  
 285          return $output;
 286      }
 287  }


Generated: Wed Nov 11 20:28:18 2020 Cross-referenced by PHPXref 0.7.1