[ Index ]

PHP Cross Reference of phpBB-3.3.0-deutsch

title

Body

[close]

/vendor/patchwork/utf8/src/Patchwork/Utf8/ -> Bootup.php (source)

   1  <?php
   2  
   3  /*
   4   * Copyright (C) 2016 Nicolas Grekas - p@tchwork.com
   5   *
   6   * This library is free software; you can redistribute it and/or modify it
   7   * under the terms of the (at your option):
   8   * Apache License v2.0 (http://apache.org/licenses/LICENSE-2.0.txt), or
   9   * GNU General Public License v2.0 (http://gnu.org/licenses/gpl-2.0.txt).
  10   */
  11  
  12  namespace Patchwork\Utf8;
  13  
  14  use Normalizer as n;
  15  use Patchwork\Utf8 as u;
  16  use Patchwork\PHP\Shim as s;
  17  
  18  class Bootup
  19  {
  20      public static function initAll()
  21      {
  22          ini_set('default_charset', 'UTF-8');
  23  
  24          self::initUtf8Encode();
  25          self::initIconv();
  26          self::initMbstring();
  27          self::initExif();
  28          self::initIntl();
  29          self::initLocale();
  30      }
  31  
  32      public static function initUtf8Encode()
  33      {
  34          function_exists('utf8_encode') or require  __DIR__.'/Bootup/utf8_encode.php';
  35      }
  36  
  37      public static function initMbstring()
  38      {
  39          if (extension_loaded('mbstring')) {
  40              if (((int) ini_get('mbstring.encoding_translation') || in_array(strtolower(ini_get('mbstring.encoding_translation')), array('on', 'yes', 'true')))
  41                  && !in_array(strtolower(ini_get('mbstring.http_input')), array('pass', '8bit', 'utf-8'))) {
  42                  user_error('php.ini settings: Please disable mbstring.encoding_translation or set mbstring.http_input to "pass"',  E_USER_WARNING);
  43              }
  44  
  45              if (defined('MB_OVERLOAD_STRING') && (MB_OVERLOAD_STRING & (int) ini_get('mbstring.func_overload'))) {
  46                  user_error('php.ini settings: Please disable mbstring.func_overload', E_USER_WARNING);
  47              }
  48  
  49              if (function_exists('mb_regex_encoding')) {
  50                  mb_regex_encoding('UTF-8');
  51              }
  52              ini_set('mbstring.script_encoding', 'pass');
  53  
  54              if ('utf-8' !== strtolower(mb_internal_encoding())) {
  55                  mb_internal_encoding('UTF-8');
  56              }
  57  
  58              if ('none' !== strtolower(mb_substitute_character())) {
  59                  mb_substitute_character('none');
  60              }
  61  
  62              if (!in_array(strtolower(mb_http_output()), array('pass', '8bit'))) {
  63                  mb_http_output('pass');
  64              }
  65  
  66              if (!in_array(strtolower(mb_language()), array('uni', 'neutral'))) {
  67                  mb_language('uni');
  68              }
  69          } elseif (!function_exists('mb_strlen')) {
  70              extension_loaded('iconv') or static::initIconv();
  71  
  72              require  __DIR__.'/Bootup/mbstring.php';
  73          }
  74      }
  75  
  76      public static function initIconv()
  77      {
  78          if (extension_loaded('iconv')) {
  79              if ('UTF-8' !== strtoupper(iconv_get_encoding('input_encoding'))) {
  80                  iconv_set_encoding('input_encoding', 'UTF-8');
  81              }
  82  
  83              if ('UTF-8' !== strtoupper(iconv_get_encoding('internal_encoding'))) {
  84                  iconv_set_encoding('internal_encoding', 'UTF-8');
  85              }
  86  
  87              if ('UTF-8' !== strtoupper(iconv_get_encoding('output_encoding'))) {
  88                  iconv_set_encoding('output_encoding', 'UTF-8');
  89              }
  90          } elseif (!function_exists('iconv')) {
  91              require  __DIR__.'/Bootup/iconv.php';
  92          }
  93      }
  94  
  95      public static function initExif()
  96      {
  97          if (extension_loaded('exif')) {
  98              if (ini_get('exif.encode_unicode') && 'UTF-8' !== strtoupper(ini_get('exif.encode_unicode'))) {
  99                  ini_set('exif.encode_unicode', 'UTF-8');
 100              }
 101  
 102              if (ini_get('exif.encode_jis') && 'UTF-8' !== strtoupper(ini_get('exif.encode_jis'))) {
 103                  ini_set('exif.encode_jis', 'UTF-8');
 104              }
 105          }
 106      }
 107  
 108      public static function initIntl()
 109      {
 110          if (defined('GRAPHEME_CLUSTER_RX')) {
 111              return;
 112          }
 113  
 114          define('GRAPHEME_CLUSTER_RX', PCRE_VERSION >= '8.32' ? '\X' : s\Intl::GRAPHEME_CLUSTER_RX);
 115  
 116          if (!function_exists('grapheme_strlen')) {
 117              extension_loaded('iconv') or static::initIconv();
 118              extension_loaded('mbstring') or static::initMbstring();
 119  
 120              require  __DIR__.'/Bootup/intl.php';
 121          }
 122      }
 123  
 124      public static function initLocale()
 125      {
 126          // With non-UTF-8 locale, basename() bugs.
 127          // Be aware that setlocale() can be slow.
 128          // You'd better properly configure your LANG environment variable to an UTF-8 locale.
 129  
 130          if ('' === basename('§')) {
 131              setlocale(LC_ALL, 'C.UTF-8', 'C');
 132              setlocale(LC_CTYPE, 'en_US.UTF-8', 'fr_FR.UTF-8', 'es_ES.UTF-8', 'de_DE.UTF-8', 'ru_RU.UTF-8', 'pt_BR.UTF-8', 'it_IT.UTF-8', 'ja_JP.UTF-8', 'zh_CN.UTF-8', '0');
 133          }
 134      }
 135  
 136      public static function filterRequestUri($uri = null, $exit = true)
 137      {
 138          if (!isset($uri)) {
 139              if (!isset($_SERVER['REQUEST_URI'])) {
 140                  return;
 141              } else {
 142                  $uri = $_SERVER['REQUEST_URI'];
 143              }
 144          }
 145  
 146          // Ensures the URL is well formed UTF-8
 147          // When not, assumes Windows-1252 and redirects to the corresponding UTF-8 encoded URL
 148  
 149          if (!preg_match('//u', urldecode($uri))) {
 150              $uri = preg_replace_callback(
 151                  '/[\x80-\xFF]+/',
 152                  function ($m) {return urlencode($m[0]);},
 153                  $uri
 154              );
 155  
 156              $uri = preg_replace_callback(
 157                  '/(?:%[89A-F][0-9A-F])+/i',
 158                  function ($m) {return urlencode(u::utf8_encode(urldecode($m[0])));},
 159                  $uri
 160              );
 161  
 162              if ($exit) {
 163                  header('HTTP/1.1 301 Moved Permanently');
 164                  header('Location: '.$uri);
 165  
 166                  exit; // TODO: remove this in 1.2 (BC)
 167              }
 168          }
 169  
 170          return $uri;
 171      }
 172  
 173      public static function filterRequestInputs($normalization_form = 4 /* n::NFC */, $leading_combining = '◌')
 174      {
 175          // Ensures inputs are well formed UTF-8
 176          // When not, assumes Windows-1252 and converts to UTF-8
 177          // Tests only values, not keys
 178  
 179          $a = array(&$_FILES, &$_ENV, &$_GET, &$_POST, &$_COOKIE, &$_SERVER, &$_REQUEST);
 180  
 181          foreach ($a[0] as &$r) {
 182              $a[] = array(&$r['name'], &$r['type']);
 183          }
 184          unset($a[0]);
 185  
 186          $len = count($a) + 1;
 187          for ($i = 1; $i < $len; ++$i) {
 188              foreach ($a[$i] as &$r) {
 189                  $s = $r; // $r is a ref, $s a copy
 190                  if (is_array($s)) {
 191                      $a[$len++] = &$r;
 192                  } else {
 193                      $r = static::filterString($s, $normalization_form, $leading_combining);
 194                  }
 195              }
 196  
 197              unset($a[$i]);
 198          }
 199      }
 200  
 201      public static function filterString($s, $normalization_form = 4 /* n::NFC */, $leading_combining = '◌')
 202      {
 203          if (false !== strpos($s, "\r")) {
 204              // Workaround https://bugs.php.net/65732
 205              $s = str_replace("\r\n", "\n", $s);
 206              $s = strtr($s, "\r", "\n");
 207          }
 208  
 209          if (preg_match('/[\x80-\xFF]/', $s)) {
 210              if (n::isNormalized($s, $normalization_form)) {
 211                  $n = '-';
 212              } else {
 213                  $n = n::normalize($s, $normalization_form);
 214                  if (isset($n[0])) {
 215                      $s = $n;
 216                  } else {
 217                      $s = u::utf8_encode($s);
 218                  }
 219              }
 220  
 221              if ($s[0] >= "\x80" && isset($n[0], $leading_combining[0]) && preg_match('/^\p{Mn}/u', $s)) {
 222                  // Prevent leading combining chars
 223                  // for NFC-safe concatenations.
 224                  $s = $leading_combining.$s;
 225              }
 226          }
 227  
 228          return $s;
 229      }
 230  }


Generated: Tue Apr 7 19:44:41 2020 Cross-referenced by PHPXref 0.7.1