rtrim(strtr(base64_encode($frame), '+/', '-_'), '=')]; } /** The frame in an answer's body: out of its JSON, or the body itself when it isn't SiteRelay's JSON. */ public static function unwrap(string $body): string { if ($body === '' || $body[0] !== '{') { return $body; // a raw frame (gzip) or someone else's page } $json = json_decode($body, true); $wrapped = is_array($json) ? ($json[self::KEY] ?? ($json['data'][self::KEY] ?? null)) : null; if (!is_string($wrapped) || !preg_match('/^[A-Za-z0-9_-]*$/', $wrapped)) { return $body; } $frame = base64_decode(strtr($wrapped, '-_', '+/') . str_repeat('=', (4 - strlen($wrapped) % 4) % 4), true); return $frame === false ? $body : $frame; } }