This commit is contained in:
Marcin Kurczewski 2013-10-13 22:58:02 +02:00
parent f96d2a96db
commit b5c3ddca1c
2 changed files with 54 additions and 1 deletions

View file

@ -76,4 +76,57 @@ class TextHelper
{
return self::useUnits($number, 1000, ['', 'K', 'M']);
}
public static function removeUnsafeKeys(&$input, $regex)
{
if (is_array($input))
{
foreach ($input as $key => $val)
{
if (preg_match($regex, $key))
unset($input[$key]);
else
self::removeUnsafeKeys($input[$key], $regex);
}
}
elseif (is_object($input))
{
foreach ($input as $key => $val)
{
if (preg_match($regex, $key))
unset($input->$key);
else
self::removeUnsafeKeys($input->$key, $regex);
}
}
}
public static function jsonEncode($obj, $illegalKeysRegex = '')
{
if (is_array($obj))
{
foreach ($obj as $key => $val)
{
if ($val instanceof RedBean_OODBBean)
{
$obj[$key] = R::exportAll($val);
}
}
}
elseif (is_object($obj))
{
foreach ($obj as $key => $val)
{
if ($val instanceof RedBean_OODBBean)
{
$obj->$key = R::exportAll($val);
}
}
}
if (!empty($illegalKeysRegex))
self::removeUnsafeKeys($obj, $illegalKeysRegex);
return json_encode($obj, true);
}
}

View file

@ -1,2 +1,2 @@
<?php \Chibi\HeadersHelper::set('Content-Type', 'application/json') ?>
<?php echo json_encode($this->context->transport, true) ?>
<?php echo TextHelper::jsonEncode($this->context->transport, '/.*(email|confirm|pass|salt)/i') ?>