From b0f6cc8aa170d9c8a9f209aa78231aafe57fd9e5 Mon Sep 17 00:00:00 2001 From: Deltik Date: Sun, 4 Feb 2018 05:57:16 -0600 Subject: [PATCH] Sanity check in xmlClass::xml2array() for PHP 7.2 Since PHP 7.2, running get_object_vars() on a SimpleXMLElement object may return sequential arrays rather than nothing if there were no children. This commit introduces a sanity check in xmlClass::xml2array() that ensures child tags are processed rather than sequential array keys. This sanity check does not affect past major versions of PHP. Fixes: #3018 Supersedes: #3019 --- e107_handlers/xml_class.php | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/e107_handlers/xml_class.php b/e107_handlers/xml_class.php index 4002a1bd4..fc64e5add 100644 --- a/e107_handlers/xml_class.php +++ b/e107_handlers/xml_class.php @@ -553,7 +553,7 @@ class xmlClass } //Recursive calls start here - if($tags) + if(self::is_assoc($tags)) { $tags = array_keys($tags); $count_tags = count($tags); @@ -749,6 +749,25 @@ class xmlClass return $vars; } + /** + * Determine if the provided variable is an associative array + * + * This method is necessary because since PHP 7.2, get_object_vars() on + * a SimpleXMLElement object began returning sequential arrays, and + * xmlClass::xml2array() interpreted the sequence as XML tags. + * + * See https://github.com/e107inc/e107/issues/3018 for details. + * + * @param array $array The variable to check + * @return boolean true if the provided variable is an associative array, + * false if it's a sequential array or anything else + */ + private static function is_assoc($array) + { + if (!is_array($array) || array() === $array) return false; + return array_keys($array) !== range(0, count($array) - 1); + } + /** * Load XML file and parse it (optional) *