You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Php72.php 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Polyfill\Php72;
  11. /**
  12. * @author Nicolas Grekas <p@tchwork.com>
  13. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  14. *
  15. * @internal
  16. */
  17. final class Php72
  18. {
  19. private static $hashMask;
  20. public static function utf8_encode($s)
  21. {
  22. $s .= $s;
  23. $len = \strlen($s);
  24. for ($i = $len >> 1, $j = 0; $i < $len; ++$i, ++$j) {
  25. switch (true) {
  26. case $s[$i] < "\x80": $s[$j] = $s[$i]; break;
  27. case $s[$i] < "\xC0": $s[$j] = "\xC2"; $s[++$j] = $s[$i]; break;
  28. default: $s[$j] = "\xC3"; $s[++$j] = \chr(\ord($s[$i]) - 64); break;
  29. }
  30. }
  31. return substr($s, 0, $j);
  32. }
  33. public static function utf8_decode($s)
  34. {
  35. $s = (string) $s;
  36. $len = \strlen($s);
  37. for ($i = 0, $j = 0; $i < $len; ++$i, ++$j) {
  38. switch ($s[$i] & "\xF0") {
  39. case "\xC0":
  40. case "\xD0":
  41. $c = (\ord($s[$i] & "\x1F") << 6) | \ord($s[++$i] & "\x3F");
  42. $s[$j] = $c < 256 ? \chr($c) : '?';
  43. break;
  44. case "\xF0":
  45. ++$i;
  46. // no break
  47. case "\xE0":
  48. $s[$j] = '?';
  49. $i += 2;
  50. break;
  51. default:
  52. $s[$j] = $s[$i];
  53. }
  54. }
  55. return substr($s, 0, $j);
  56. }
  57. public static function php_os_family()
  58. {
  59. if ('\\' === \DIRECTORY_SEPARATOR) {
  60. return 'Windows';
  61. }
  62. $map = array(
  63. 'Darwin' => 'Darwin',
  64. 'DragonFly' => 'BSD',
  65. 'FreeBSD' => 'BSD',
  66. 'NetBSD' => 'BSD',
  67. 'OpenBSD' => 'BSD',
  68. 'Linux' => 'Linux',
  69. 'SunOS' => 'Solaris',
  70. );
  71. return isset($map[PHP_OS]) ? $map[PHP_OS] : 'Unknown';
  72. }
  73. public static function spl_object_id($object)
  74. {
  75. if (null === self::$hashMask) {
  76. self::initHashMask();
  77. }
  78. if (null === $hash = spl_object_hash($object)) {
  79. return;
  80. }
  81. return self::$hashMask ^ hexdec(substr($hash, 16 - \PHP_INT_SIZE, \PHP_INT_SIZE));
  82. }
  83. public static function sapi_windows_vt100_support($stream, $enable = null)
  84. {
  85. if (!\is_resource($stream)) {
  86. trigger_error('sapi_windows_vt100_support() expects parameter 1 to be resource, '.\gettype($stream).' given', E_USER_WARNING);
  87. return false;
  88. }
  89. $meta = stream_get_meta_data($stream);
  90. if ('STDIO' !== $meta['stream_type']) {
  91. trigger_error('sapi_windows_vt100_support() was not able to analyze the specified stream', E_USER_WARNING);
  92. return false;
  93. }
  94. // We cannot actually disable vt100 support if it is set
  95. if (false === $enable || !self::stream_isatty($stream)) {
  96. return false;
  97. }
  98. // The native function does not apply to stdin
  99. $meta = array_map('strtolower', $meta);
  100. $stdin = 'php://stdin' === $meta['uri'] || 'php://fd/0' === $meta['uri'];
  101. return !$stdin
  102. && (false !== getenv('ANSICON')
  103. || 'ON' === getenv('ConEmuANSI')
  104. || 'xterm' === getenv('TERM')
  105. || 'Hyper' === getenv('TERM_PROGRAM'));
  106. }
  107. public static function stream_isatty($stream)
  108. {
  109. if (!\is_resource($stream)) {
  110. trigger_error('stream_isatty() expects parameter 1 to be resource, '.\gettype($stream).' given', E_USER_WARNING);
  111. return false;
  112. }
  113. if ('\\' === \DIRECTORY_SEPARATOR) {
  114. $stat = @fstat($stream);
  115. // Check if formatted mode is S_IFCHR
  116. return $stat ? 0020000 === ($stat['mode'] & 0170000) : false;
  117. }
  118. return \function_exists('posix_isatty') && @posix_isatty($stream);
  119. }
  120. private static function initHashMask()
  121. {
  122. $obj = (object) array();
  123. self::$hashMask = -1;
  124. // check if we are nested in an output buffering handler to prevent a fatal error with ob_start() below
  125. $obFuncs = array('ob_clean', 'ob_end_clean', 'ob_flush', 'ob_end_flush', 'ob_get_contents', 'ob_get_flush');
  126. foreach (debug_backtrace(\PHP_VERSION_ID >= 50400 ? DEBUG_BACKTRACE_IGNORE_ARGS : false) as $frame) {
  127. if (isset($frame['function'][0]) && !isset($frame['class']) && 'o' === $frame['function'][0] && \in_array($frame['function'], $obFuncs)) {
  128. $frame['line'] = 0;
  129. break;
  130. }
  131. }
  132. if (!empty($frame['line'])) {
  133. ob_start();
  134. debug_zval_dump($obj);
  135. self::$hashMask = (int) substr(ob_get_clean(), 17);
  136. }
  137. self::$hashMask ^= hexdec(substr(spl_object_hash($obj), 16 - \PHP_INT_SIZE, \PHP_INT_SIZE));
  138. }
  139. public static function mb_chr($code, $encoding = null)
  140. {
  141. if (0x80 > $code %= 0x200000) {
  142. $s = \chr($code);
  143. } elseif (0x800 > $code) {
  144. $s = \chr(0xC0 | $code >> 6).\chr(0x80 | $code & 0x3F);
  145. } elseif (0x10000 > $code) {
  146. $s = \chr(0xE0 | $code >> 12).\chr(0x80 | $code >> 6 & 0x3F).\chr(0x80 | $code & 0x3F);
  147. } else {
  148. $s = \chr(0xF0 | $code >> 18).\chr(0x80 | $code >> 12 & 0x3F).\chr(0x80 | $code >> 6 & 0x3F).\chr(0x80 | $code & 0x3F);
  149. }
  150. if ('UTF-8' !== $encoding) {
  151. $s = mb_convert_encoding($s, $encoding, 'UTF-8');
  152. }
  153. return $s;
  154. }
  155. public static function mb_ord($s, $encoding = null)
  156. {
  157. if (null == $encoding) {
  158. $s = mb_convert_encoding($s, 'UTF-8');
  159. } elseif ('UTF-8' !== $encoding) {
  160. $s = mb_convert_encoding($s, 'UTF-8', $encoding);
  161. }
  162. if (1 === \strlen($s)) {
  163. return \ord($s);
  164. }
  165. $code = ($s = unpack('C*', substr($s, 0, 4))) ? $s[1] : 0;
  166. if (0xF0 <= $code) {
  167. return (($code - 0xF0) << 18) + (($s[2] - 0x80) << 12) + (($s[3] - 0x80) << 6) + $s[4] - 0x80;
  168. }
  169. if (0xE0 <= $code) {
  170. return (($code - 0xE0) << 12) + (($s[2] - 0x80) << 6) + $s[3] - 0x80;
  171. }
  172. if (0xC0 <= $code) {
  173. return (($code - 0xC0) << 6) + $s[2] - 0x80;
  174. }
  175. return $code;
  176. }
  177. }