Php72.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 = [
  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. // On 32-bit systems, PHP_INT_SIZE is 4,
  82. return self::$hashMask ^ hexdec(substr($hash, 16 - (\PHP_INT_SIZE * 2 - 1), (\PHP_INT_SIZE * 2 - 1)));
  83. }
  84. public static function sapi_windows_vt100_support($stream, $enable = null)
  85. {
  86. if (!\is_resource($stream)) {
  87. trigger_error('sapi_windows_vt100_support() expects parameter 1 to be resource, '.\gettype($stream).' given', \E_USER_WARNING);
  88. return false;
  89. }
  90. $meta = stream_get_meta_data($stream);
  91. if ('STDIO' !== $meta['stream_type']) {
  92. trigger_error('sapi_windows_vt100_support() was not able to analyze the specified stream', \E_USER_WARNING);
  93. return false;
  94. }
  95. // We cannot actually disable vt100 support if it is set
  96. if (false === $enable || !self::stream_isatty($stream)) {
  97. return false;
  98. }
  99. // The native function does not apply to stdin
  100. $meta = array_map('strtolower', $meta);
  101. $stdin = 'php://stdin' === $meta['uri'] || 'php://fd/0' === $meta['uri'];
  102. return !$stdin
  103. && (false !== getenv('ANSICON')
  104. || 'ON' === getenv('ConEmuANSI')
  105. || 'xterm' === getenv('TERM')
  106. || 'Hyper' === getenv('TERM_PROGRAM'));
  107. }
  108. public static function stream_isatty($stream)
  109. {
  110. if (!\is_resource($stream)) {
  111. trigger_error('stream_isatty() expects parameter 1 to be resource, '.\gettype($stream).' given', \E_USER_WARNING);
  112. return false;
  113. }
  114. if ('\\' === \DIRECTORY_SEPARATOR) {
  115. $stat = @fstat($stream);
  116. // Check if formatted mode is S_IFCHR
  117. return $stat ? 0020000 === ($stat['mode'] & 0170000) : false;
  118. }
  119. return \function_exists('posix_isatty') && @posix_isatty($stream);
  120. }
  121. private static function initHashMask()
  122. {
  123. $obj = (object) [];
  124. self::$hashMask = -1;
  125. // check if we are nested in an output buffering handler to prevent a fatal error with ob_start() below
  126. $obFuncs = ['ob_clean', 'ob_end_clean', 'ob_flush', 'ob_end_flush', 'ob_get_contents', 'ob_get_flush'];
  127. foreach (debug_backtrace(\PHP_VERSION_ID >= 50400 ? \DEBUG_BACKTRACE_IGNORE_ARGS : false) as $frame) {
  128. if (isset($frame['function'][0]) && !isset($frame['class']) && 'o' === $frame['function'][0] && \in_array($frame['function'], $obFuncs)) {
  129. $frame['line'] = 0;
  130. break;
  131. }
  132. }
  133. if (!empty($frame['line'])) {
  134. ob_start();
  135. debug_zval_dump($obj);
  136. self::$hashMask = (int) substr(ob_get_clean(), 17);
  137. }
  138. self::$hashMask ^= hexdec(substr(spl_object_hash($obj), 16 - (\PHP_INT_SIZE * 2 - 1), (\PHP_INT_SIZE * 2 - 1)));
  139. }
  140. public static function mb_chr($code, $encoding = null)
  141. {
  142. if (0x80 > $code %= 0x200000) {
  143. $s = \chr($code);
  144. } elseif (0x800 > $code) {
  145. $s = \chr(0xC0 | $code >> 6).\chr(0x80 | $code & 0x3F);
  146. } elseif (0x10000 > $code) {
  147. $s = \chr(0xE0 | $code >> 12).\chr(0x80 | $code >> 6 & 0x3F).\chr(0x80 | $code & 0x3F);
  148. } else {
  149. $s = \chr(0xF0 | $code >> 18).\chr(0x80 | $code >> 12 & 0x3F).\chr(0x80 | $code >> 6 & 0x3F).\chr(0x80 | $code & 0x3F);
  150. }
  151. if ('UTF-8' !== $encoding) {
  152. $s = mb_convert_encoding($s, $encoding, 'UTF-8');
  153. }
  154. return $s;
  155. }
  156. public static function mb_ord($s, $encoding = null)
  157. {
  158. if (null === $encoding) {
  159. $s = mb_convert_encoding($s, 'UTF-8');
  160. } elseif ('UTF-8' !== $encoding) {
  161. $s = mb_convert_encoding($s, 'UTF-8', $encoding);
  162. }
  163. if (1 === \strlen($s)) {
  164. return \ord($s);
  165. }
  166. $code = ($s = unpack('C*', substr($s, 0, 4))) ? $s[1] : 0;
  167. if (0xF0 <= $code) {
  168. return (($code - 0xF0) << 18) + (($s[2] - 0x80) << 12) + (($s[3] - 0x80) << 6) + $s[4] - 0x80;
  169. }
  170. if (0xE0 <= $code) {
  171. return (($code - 0xE0) << 12) + (($s[2] - 0x80) << 6) + $s[3] - 0x80;
  172. }
  173. if (0xC0 <= $code) {
  174. return (($code - 0xC0) << 6) + $s[2] - 0x80;
  175. }
  176. return $code;
  177. }
  178. }