diff --git a/src/Helpers/TextHelper.php b/src/Helpers/TextHelper.php index c3e61120..523ddd95 100644 --- a/src/Helpers/TextHelper.php +++ b/src/Helpers/TextHelper.php @@ -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); + } } diff --git a/src/Views/layout-json.phtml b/src/Views/layout-json.phtml index 95dd6d28..9086c6f9 100644 --- a/src/Views/layout-json.phtml +++ b/src/Views/layout-json.phtml @@ -1,2 +1,2 @@ -context->transport, true) ?> +context->transport, '/.*(email|confirm|pass|salt)/i') ?>