Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | * This file is part of the Symfony package. |
| 5 | * |
| 6 | * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | * |
| 8 | * For the full copyright and license information, please view the LICENSE |
| 9 | * file that was distributed with this source code. |
| 10 | */ |
| 11 | |
| 12 | namespace Symfony\Component\VarDumper\Caster; |
| 13 | |
| 14 | use Symfony\Component\VarDumper\Cloner\Stub; |
| 15 | |
| 16 | /** |
| 17 | * Casts common resource types to array representation. |
| 18 | * |
| 19 | * @author Nicolas Grekas <p@tchwork.com> |
| 20 | * |
| 21 | * @final |
| 22 | */ |
| 23 | class ResourceCaster |
| 24 | { |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 25 | public static function castCurl(\CurlHandle $h, array $a, Stub $stub, bool $isNested): array |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 26 | { |
| 27 | return curl_getinfo($h); |
| 28 | } |
| 29 | |
| 30 | public static function castDba($dba, array $a, Stub $stub, bool $isNested) |
| 31 | { |
| 32 | $list = dba_list(); |
| 33 | $a['file'] = $list[(int) $dba]; |
| 34 | |
| 35 | return $a; |
| 36 | } |
| 37 | |
| 38 | public static function castProcess($process, array $a, Stub $stub, bool $isNested) |
| 39 | { |
| 40 | return proc_get_status($process); |
| 41 | } |
| 42 | |
| 43 | public static function castStream($stream, array $a, Stub $stub, bool $isNested) |
| 44 | { |
| 45 | $a = stream_get_meta_data($stream) + static::castStreamContext($stream, $a, $stub, $isNested); |
| 46 | if ($a['uri'] ?? false) { |
| 47 | $a['uri'] = new LinkStub($a['uri']); |
| 48 | } |
| 49 | |
| 50 | return $a; |
| 51 | } |
| 52 | |
| 53 | public static function castStreamContext($stream, array $a, Stub $stub, bool $isNested) |
| 54 | { |
| 55 | return @stream_context_get_params($stream) ?: $a; |
| 56 | } |
| 57 | |
| 58 | public static function castGd($gd, array $a, Stub $stub, bool $isNested) |
| 59 | { |
| 60 | $a['size'] = imagesx($gd).'x'.imagesy($gd); |
| 61 | $a['trueColor'] = imageistruecolor($gd); |
| 62 | |
| 63 | return $a; |
| 64 | } |
| 65 | |
| 66 | public static function castMysqlLink($h, array $a, Stub $stub, bool $isNested) |
| 67 | { |
| 68 | $a['host'] = mysql_get_host_info($h); |
| 69 | $a['protocol'] = mysql_get_proto_info($h); |
| 70 | $a['server'] = mysql_get_server_info($h); |
| 71 | |
| 72 | return $a; |
| 73 | } |
| 74 | |
| 75 | public static function castOpensslX509($h, array $a, Stub $stub, bool $isNested) |
| 76 | { |
| 77 | $stub->cut = -1; |
| 78 | $info = openssl_x509_parse($h, false); |
| 79 | |
| 80 | $pin = openssl_pkey_get_public($h); |
| 81 | $pin = openssl_pkey_get_details($pin)['key']; |
| 82 | $pin = \array_slice(explode("\n", $pin), 1, -2); |
| 83 | $pin = base64_decode(implode('', $pin)); |
| 84 | $pin = base64_encode(hash('sha256', $pin, true)); |
| 85 | |
| 86 | $a += [ |
| 87 | 'subject' => new EnumStub(array_intersect_key($info['subject'], ['organizationName' => true, 'commonName' => true])), |
| 88 | 'issuer' => new EnumStub(array_intersect_key($info['issuer'], ['organizationName' => true, 'commonName' => true])), |
| 89 | 'expiry' => new ConstStub(date(\DateTime::ISO8601, $info['validTo_time_t']), $info['validTo_time_t']), |
| 90 | 'fingerprint' => new EnumStub([ |
| 91 | 'md5' => new ConstStub(wordwrap(strtoupper(openssl_x509_fingerprint($h, 'md5')), 2, ':', true)), |
| 92 | 'sha1' => new ConstStub(wordwrap(strtoupper(openssl_x509_fingerprint($h, 'sha1')), 2, ':', true)), |
| 93 | 'sha256' => new ConstStub(wordwrap(strtoupper(openssl_x509_fingerprint($h, 'sha256')), 2, ':', true)), |
| 94 | 'pin-sha256' => new ConstStub($pin), |
| 95 | ]), |
| 96 | ]; |
| 97 | |
| 98 | return $a; |
| 99 | } |
| 100 | } |