mirror of
https://github.com/fusionpbx/fusionpbx.git
synced 2026-01-26 18:49:16 +00:00
Add a static xml valid class
This commit is contained in:
@@ -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];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user