super-powers/src/64a6ff6c-069c-4a11-a76b-db5.../code.power

39 lines
1007 B
Plaintext

/**
* open base64 string if stored as base64 (in JCB)
*
* @param string|null $data The base64 string
* @param string|null $key We store the string with that suffix :)
* @param string|null $default The default switch
*
* @return string|null The opened string
* @since 3.2.0
*/
public static function open(?string $data, ?string $key = '__.o0=base64=Oo.__', ?string $default = 'string'): ?string
{
// check that we have a string
if (StringHelper::check($data))
{
// check if we have a key
if (StringHelper::check($key))
{
if (strpos($data, $key) !== false)
{
return base64_decode(str_replace($key, '', $data));
}
}
// fallback to this, not perfect method
if (base64_encode(base64_decode($data, true)) === $data)
{
return base64_decode($data);
}
}
// check if we should just return the string
if ('string' === $default)
{
return $data;
}
return $default;
}