ResourceCaster.php 3.1 KB

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