From 92042593c8392b6fac1212d316e307d3bc82daea Mon Sep 17 00:00:00 2001 From: FusionPBX Date: Fri, 9 Jan 2026 20:21:33 -0700 Subject: [PATCH] Add a static xml valid class --- resources/classes/xml.php | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/resources/classes/xml.php b/resources/classes/xml.php index 7ae16e6969..06ee84cc9b 100644 --- a/resources/classes/xml.php +++ b/resources/classes/xml.php @@ -17,4 +17,38 @@ class xml { return htmlspecialchars($string, ENT_XML1); } + /** + * Tests the XML to determine if it is valid + * + * @param string $xml_string The XML string to be validated. + * + * @return array Return the xml_valid, and xml_errors. Valid is a boolean value, and errors return an array. + */ + static function valid($xml_string) { + //set the default value to true + $xml_valid = true; + $xml_errors = ''; + + //use the XML to check if it's valid + if (PHP_VERSION_ID < 80000) { + libxml_disable_entity_loader(true); + } + + //enable internal error handling + libxml_use_internal_errors(true); + + //load the XML object + $xml_object = simplexml_load_string($xml_string, 'SimpleXMLElement', LIBXML_NOCDATA); + if (!$xml_object) { + //set the xml_errors and the xml_valid boolean value + $xml_errors = libxml_get_errors(); + $xml_valid = false; + + //clear the libxml error buffer. + libxml_clear_errors(); + } + + //send the result + return ['valid' -> $xml_valid, 'errors' -> $xml_errors]; + } }