blob: 6ae908524f6c18d7c0a042af7e115878f4967f07 [file] [log] [blame]
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02001<?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
12namespace Symfony\Component\VarDumper\Caster;
13
14use 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 */
23class ResourceCaster
24{
25 /**
26 * @param \CurlHandle|resource $h
27 *
28 * @return array
29 */
30 public static function castCurl($h, array $a, Stub $stub, bool $isNested)
31 {
32 return curl_getinfo($h);
33 }
34
35 public static function castDba($dba, array $a, Stub $stub, bool $isNested)
36 {
37 $list = dba_list();
38 $a['file'] = $list[(int) $dba];
39
40 return $a;
41 }
42
43 public static function castProcess($process, array $a, Stub $stub, bool $isNested)
44 {
45 return proc_get_status($process);
46 }
47
48 public static function castStream($stream, array $a, Stub $stub, bool $isNested)
49 {
50 $a = stream_get_meta_data($stream) + static::castStreamContext($stream, $a, $stub, $isNested);
51 if ($a['uri'] ?? false) {
52 $a['uri'] = new LinkStub($a['uri']);
53 }
54
55 return $a;
56 }
57
58 public static function castStreamContext($stream, array $a, Stub $stub, bool $isNested)
59 {
60 return @stream_context_get_params($stream) ?: $a;
61 }
62
63 public static function castGd($gd, array $a, Stub $stub, bool $isNested)
64 {
65 $a['size'] = imagesx($gd).'x'.imagesy($gd);
66 $a['trueColor'] = imageistruecolor($gd);
67
68 return $a;
69 }
70
71 public static function castMysqlLink($h, array $a, Stub $stub, bool $isNested)
72 {
73 $a['host'] = mysql_get_host_info($h);
74 $a['protocol'] = mysql_get_proto_info($h);
75 $a['server'] = mysql_get_server_info($h);
76
77 return $a;
78 }
79
80 public static function castOpensslX509($h, array $a, Stub $stub, bool $isNested)
81 {
82 $stub->cut = -1;
83 $info = openssl_x509_parse($h, false);
84
85 $pin = openssl_pkey_get_public($h);
86 $pin = openssl_pkey_get_details($pin)['key'];
87 $pin = \array_slice(explode("\n", $pin), 1, -2);
88 $pin = base64_decode(implode('', $pin));
89 $pin = base64_encode(hash('sha256', $pin, true));
90
91 $a += [
92 'subject' => new EnumStub(array_intersect_key($info['subject'], ['organizationName' => true, 'commonName' => true])),
93 'issuer' => new EnumStub(array_intersect_key($info['issuer'], ['organizationName' => true, 'commonName' => true])),
94 'expiry' => new ConstStub(date(\DateTime::ISO8601, $info['validTo_time_t']), $info['validTo_time_t']),
95 'fingerprint' => new EnumStub([
96 'md5' => new ConstStub(wordwrap(strtoupper(openssl_x509_fingerprint($h, 'md5')), 2, ':', true)),
97 'sha1' => new ConstStub(wordwrap(strtoupper(openssl_x509_fingerprint($h, 'sha1')), 2, ':', true)),
98 'sha256' => new ConstStub(wordwrap(strtoupper(openssl_x509_fingerprint($h, 'sha256')), 2, ':', true)),
99 'pin-sha256' => new ConstStub($pin),
100 ]),
101 ];
102
103 return $a;
104 }
105}