*/ public array $meta = []; /** @var array */ private array $data = []; private function __construct() {} public static function open(string $file): self { $raw = @file_get_contents($file); if ($raw === false) { throw new \RuntimeException("Cannot open $file"); } return self::fromString($raw, basename($file)); } public static function fromString(string $raw, string $name = 'data'): self { if (strlen($raw) < 12 || substr($raw, 0, 4) !== 'CRYF') { throw new \RuntimeException("$name is not a .cryf file"); } $f = new self(); $h = unpack('vversion/vflags', $raw, 4); $f->version = $h['version']; $f->flags = $h['flags']; if ($f->version !== 2) { throw new \RuntimeException("$name: unsupported .cryf version {$f->version}"); } if ($f->flags & self::ENCRYPTED) { throw new \RuntimeException("$name is encrypted: it cannot be read without its key"); } $end = strlen($raw); if (($f->flags & self::SIGNED) && $end >= 48 && substr($raw, $end - 36, 4) === 'SIGN') { $end -= 36; } $body = substr($raw, 12, $end - 12); if ($f->flags & self::ZLIB) { if (!function_exists('gzuncompress')) { throw new \RuntimeException("$name is compressed and this PHP has no zlib"); } $body = @gzuncompress(substr($body, 4)); if ($body === false) { throw new \RuntimeException("$name: corrupted zlib stream"); } } $outer = @unserialize($body, ['allowed_classes' => false]); if (!is_array($outer) || !is_array($outer[0] ?? null) || !is_string($outer[1] ?? null)) { throw new \RuntimeException("$name: corrupted file"); } $f->meta = $outer[0]; $data = @unserialize($outer[1], ['allowed_classes' => false]); $f->data = is_array($data) ? $data : []; return $f; } /** The header, the same keys as cryo_info(): php, script, time, pid, host, thaw_count, resume, format, frames, sources, signed, compressed, encrypted. */ public function info(): array { $out = []; foreach (['php', 'script', 'time', 'pid', 'host', 'thaw_count', 'resume', 'mode'] as $k) { if (array_key_exists($k, $this->meta)) { $out[$k] = $this->meta[$k]; } } return $out + [ 'format' => $this->version, 'frames' => count((array)($this->meta['frames'] ?? [])), 'sources' => count((array)($this->meta['sources'] ?? [])), 'signed' => (bool)($this->flags & self::SIGNED), 'compressed' => (bool)($this->flags & self::ZLIB), 'encrypted' => (bool)($this->flags & self::ENCRYPTED), ]; } /** @return list> the frames, innermost first: func, class, file, line, num_args, opline */ public function frames(): array { return array_reverse(array_values(array_filter((array)($this->meta['frames'] ?? []), 'is_array'))); } /** * The variables of a frame (index of frames(), innermost first): name => [type, preview], $this first. * @return array */ public function variables(int $frame): array { $all = array_values((array)($this->data['frames'] ?? [])); $fd = $all[count($all) - 1 - $frame] ?? null; if (!is_array($fd)) { return []; } $out = []; if (array_key_exists('this', $fd)) { $out['$this'] = self::describe($fd['this']); } foreach ((array)($fd['cvs'] ?? []) as $name => $value) { $out['$' . $name] = self::describe($value); } return $out; } /** Globals of the frozen script (without the superglobals): name => [type, preview]. */ public function globals(): array { $out = []; foreach ((array)($this->data['globals'] ?? []) as $name => $value) { if (!in_array($name, ['GLOBALS', '_SERVER', '_ENV', '_GET', '_POST', '_COOKIE', '_FILES', '_REQUEST', '_SESSION', 'argv', 'argc'], true)) { $out['$' . $name] = self::describe($value); } } return $out; } /** A value as [type, short preview]: no method of it is called. @return array{0: string, 1: string} */ public static function describe(mixed $v, int $depth = 0): array { if ($v instanceof \__PHP_Incomplete_Class) { $props = (array)$v; $class = (string)($props['__PHP_Incomplete_Class_Name'] ?? 'object'); unset($props['__PHP_Incomplete_Class_Name']); $parts = []; foreach (array_slice($props, 0, 4, true) as $k => $p) { $parts[] = self::propName((string)$k) . ': ' . ($depth < 1 ? self::describe($p, $depth + 1)[1] : '…'); } return [$class, $class . ' {' . implode(', ', $parts) . (count($props) > 4 ? ', …' : '') . '}']; } return match (true) { is_array($v) => ['array', self::arrayPreview($v, $depth)], is_string($v) => ['string', '"' . self::cut($v, 40) . '"' . (strlen($v) > 40 ? ' (' . strlen($v) . ')' : '')], is_int($v) => ['int', (string)$v], is_float($v) => ['float', (string)$v], is_bool($v) => ['bool', $v ? 'true' : 'false'], $v === null => ['null', 'null'], is_object($v) => [get_class($v), get_class($v)], default => [gettype($v), gettype($v)], }; } private static function arrayPreview(array $a, int $depth): string { if ($depth >= 1) { return '[' . count($a) . ']'; } $parts = []; $list = array_is_list($a); foreach (array_slice($a, 0, 4, true) as $k => $x) { $parts[] = ($list ? '' : var_export($k, true) . ' => ') . self::describe($x, $depth + 1)[1]; } return '[' . implode(', ', $parts) . (count($a) > 4 ? ', … ' . count($a) . ' items' : '') . ']'; } /** "\0Class\0prop" (private) / "\0*\0prop" (protected) => "prop" */ private static function propName(string $k): string { $p = strrpos($k, "\0"); return $p === false ? $k : substr($k, $p + 1); } private static function cut(string $s, int $n): string { $s = (string)preg_replace('/[\x00-\x1f]/', ' ', $s); return strlen($s) > $n ? mb_strcut($s, 0, $n) . '…' : $s; } }