説明なし

ResourceCaster.php 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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\Component\VarDumper\Caster;
  11. use Symfony\Component\VarDumper\Cloner\Stub;
  12. /**
  13. * Casts common resource types to array representation.
  14. *
  15. * @author Nicolas Grekas <p@tchwork.com>
  16. *
  17. * @final since Symfony 4.4
  18. */
  19. class ResourceCaster
  20. {
  21. /**
  22. * @param \CurlHandle|resource $h
  23. *
  24. * @return array
  25. */
  26. public static function castCurl($h, array $a, Stub $stub, $isNested)
  27. {
  28. return curl_getinfo($h);
  29. }
  30. public static function castDba($dba, array $a, Stub $stub, $isNested)
  31. {
  32. $list = dba_list();
  33. $a['file'] = $list[(int) $dba];
  34. return $a;
  35. }
  36. public static function castProcess($process, array $a, Stub $stub, $isNested)
  37. {
  38. return proc_get_status($process);
  39. }
  40. public static function castStream($stream, array $a, Stub $stub, $isNested)
  41. {
  42. $a = stream_get_meta_data($stream) + static::castStreamContext($stream, $a, $stub, $isNested);
  43. if ($a['uri'] ?? false) {
  44. $a['uri'] = new LinkStub($a['uri']);
  45. }
  46. return $a;
  47. }
  48. public static function castStreamContext($stream, array $a, Stub $stub, $isNested)
  49. {
  50. return @stream_context_get_params($stream) ?: $a;
  51. }
  52. public static function castGd($gd, array $a, Stub $stub, $isNested)
  53. {
  54. $a['size'] = imagesx($gd).'x'.imagesy($gd);
  55. $a['trueColor'] = imageistruecolor($gd);
  56. return $a;
  57. }
  58. public static function castMysqlLink($h, array $a, Stub $stub, $isNested)
  59. {
  60. $a['host'] = mysql_get_host_info($h);
  61. $a['protocol'] = mysql_get_proto_info($h);
  62. $a['server'] = mysql_get_server_info($h);
  63. return $a;
  64. }
  65. public static function castOpensslX509($h, array $a, Stub $stub, $isNested)
  66. {
  67. $stub->cut = -1;
  68. $info = openssl_x509_parse($h, false);
  69. $pin = openssl_pkey_get_public($h);
  70. $pin = openssl_pkey_get_details($pin)['key'];
  71. $pin = \array_slice(explode("\n", $pin), 1, -2);
  72. $pin = base64_decode(implode('', $pin));
  73. $pin = base64_encode(hash('sha256', $pin, true));
  74. $a += [
  75. 'subject' => new EnumStub(array_intersect_key($info['subject'], ['organizationName' => true, 'commonName' => true])),
  76. 'issuer' => new EnumStub(array_intersect_key($info['issuer'], ['organizationName' => true, 'commonName' => true])),
  77. 'expiry' => new ConstStub(date(\DateTime::ISO8601, $info['validTo_time_t']), $info['validTo_time_t']),
  78. 'fingerprint' => new EnumStub([
  79. 'md5' => new ConstStub(wordwrap(strtoupper(openssl_x509_fingerprint($h, 'md5')), 2, ':', true)),
  80. 'sha1' => new ConstStub(wordwrap(strtoupper(openssl_x509_fingerprint($h, 'sha1')), 2, ':', true)),
  81. 'sha256' => new ConstStub(wordwrap(strtoupper(openssl_x509_fingerprint($h, 'sha256')), 2, ':', true)),
  82. 'pin-sha256' => new ConstStub($pin),
  83. ]),
  84. ];
  85. return $a;
  86. }
  87. }