Closed #24
This commit is contained in:
parent
f96d2a96db
commit
b5c3ddca1c
2 changed files with 54 additions and 1 deletions
|
@ -76,4 +76,57 @@ class TextHelper
|
||||||
{
|
{
|
||||||
return self::useUnits($number, 1000, ['', 'K', 'M']);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,2 +1,2 @@
|
||||||
<?php \Chibi\HeadersHelper::set('Content-Type', 'application/json') ?>
|
<?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') ?>
|
||||||
|
|
Reference in a new issue