ResourceCaster.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. public static function castCurl($h, array $a, Stub $stub, $isNested)
  22. {
  23. return curl_getinfo($h);
  24. }
  25. public static function castDba($dba, array $a, Stub $stub, $isNested)
  26. {
  27. $list = dba_list();
  28. $a['file'] = $list[(int) $dba];
  29. return $a;
  30. }
  31. public static function castProcess($process, array $a, Stub $stub, $isNested)
  32. {
  33. return proc_get_status($process);
  34. }
  35. public static function castStream($stream, array $a, Stub $stub, $isNested)
  36. {
  37. $a = stream_get_meta_data($stream) + static::castStreamContext($stream, $a, $stub, $isNested);
  38. if (isset($a['uri'])) {
  39. $a['uri'] = new LinkStub($a['uri']);
  40. }
  41. return $a;
  42. }
  43. public static function castStreamContext($stream, array $a, Stub $stub, $isNested)
  44. {
  45. return @stream_context_get_params($stream) ?: $a;
  46. }
  47. public static function castGd($gd, array $a, Stub $stub, $isNested)
  48. {
  49. $a['size'] = imagesx($gd).'x'.imagesy($gd);
  50. $a['trueColor'] = imageistruecolor($gd);
  51. return $a;
  52. }
  53. public static function castMysqlLink($h, array $a, Stub $stub, $isNested)
  54. {
  55. $a['host'] = mysql_get_host_info($h);
  56. $a['protocol'] = mysql_get_proto_info($h);
  57. $a['server'] = mysql_get_server_info($h);
  58. return $a;
  59. }
  60. public static function castOpensslX509($h, array $a, Stub $stub, $isNested)
  61. {
  62. $stub->cut = -1;
  63. $info = openssl_x509_parse($h, false);
  64. $pin = openssl_pkey_get_public($h);
  65. $pin = openssl_pkey_get_details($pin)['key'];
  66. $pin = \array_slice(explode("\n", $pin), 1, -2);
  67. $pin = base64_decode(implode('', $pin));
  68. $pin = base64_encode(hash('sha256', $pin, true));
  69. $a += [
  70. 'subject' => new EnumStub(array_intersect_key($info['subject'], ['organizationName' => true, 'commonName' => true])),
  71. 'issuer' => new EnumStub(array_intersect_key($info['issuer'], ['organizationName' => true, 'commonName' => true])),
  72. 'expiry' => new ConstStub(date(\DateTime::ISO8601, $info['validTo_time_t']), $info['validTo_time_t']),
  73. 'fingerprint' => new EnumStub([
  74. 'md5' => new ConstStub(wordwrap(strtoupper(openssl_x509_fingerprint($h, 'md5')), 2, ':', true)),
  75. 'sha1' => new ConstStub(wordwrap(strtoupper(openssl_x509_fingerprint($h, 'sha1')), 2, ':', true)),
  76. 'sha256' => new ConstStub(wordwrap(strtoupper(openssl_x509_fingerprint($h, 'sha256')), 2, ':', true)),
  77. 'pin-sha256' => new ConstStub($pin),
  78. ]),
  79. ];
  80. return $a;
  81. }
  82. }