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.

103 lines
3.2 KiB

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