summaryrefslogtreecommitdiffstats
path: root/lib/SimpleSAML/Utils/XML.php
diff options
context:
space:
mode:
authorJaime Perez Crespo <jaime.perez@uninett.no>2015-04-23 15:11:26 +0200
committerJaime Perez Crespo <jaime.perez@uninett.no>2015-04-23 15:11:26 +0200
commitce445821164d3aa48ea8932eedf2c60d5876005c (patch)
tree6ca4137e72d72063dd97a3ffa08c870e41201195 /lib/SimpleSAML/Utils/XML.php
parent78cf59dbb48d054f2c53a5fe45a6a136b9dd977b (diff)
downloadsimplesamlphp-ce445821164d3aa48ea8932eedf2c60d5876005c.zip
simplesamlphp-ce445821164d3aa48ea8932eedf2c60d5876005c.tar.gz
simplesamlphp-ce445821164d3aa48ea8932eedf2c60d5876005c.tar.bz2
Move SimpleSAML_Utilities::validateXML() to SimpleSAML\Utils\XML::isValid().
Diffstat (limited to 'lib/SimpleSAML/Utils/XML.php')
-rw-r--r--lib/SimpleSAML/Utils/XML.php56
1 files changed, 56 insertions, 0 deletions
diff --git a/lib/SimpleSAML/Utils/XML.php b/lib/SimpleSAML/Utils/XML.php
index f594752..72ddbcf 100644
--- a/lib/SimpleSAML/Utils/XML.php
+++ b/lib/SimpleSAML/Utils/XML.php
@@ -271,6 +271,7 @@ class XML
*
* @return boolean True if both namespace and local name matches, false otherwise.
* @throws \SimpleSAML_Error_Exception If the namespace shortcut is unknown.
+ *
* @author Andreas Solberg, UNINETT AS <andreas.solberg@uninett.no>
* @author Olav Morken, UNINETT AS <olav.morken@uninett.no>
*/
@@ -311,4 +312,59 @@ class XML
}
return true;
}
+
+
+ /**
+ * This function attempts to validate an XML string against the specified schema. It will parse the string into a
+ * DOM document and validate this document against the schema.
+ *
+ * Note that this function returns values that are evaluated as a logical true, both when validation works and when
+ * it doesn't. Please use strict comparisons to check the values returned.
+ *
+ * @param string|\DOMDocument $xml The XML string or document which should be validated.
+ * @param string $schema The filename of the schema that should be used to validate the document.
+ *
+ * @return boolean|string Returns a string with errors found if validation fails. True if validation passes ok.
+ * @throws \InvalidArgumentException If $schema is not a string, or $xml is neither a string nor a \DOMDocument.
+ *
+ * @author Olav Morken, UNINETT AS <olav.morken@uninett.no>
+ */
+ public static function isValid($xml, $schema)
+ {
+ if (!(is_string($schema) && (is_string($xml) || $xml instanceof \DOMDocument))) {
+ throw new \InvalidArgumentException('Invalid input parameters.');
+ }
+
+ \SimpleSAML_XML_Errors::begin();
+
+ if ($xml instanceof \DOMDocument) {
+ $dom = $xml;
+ $res = true;
+ } else {
+ $dom = new \DOMDocument;
+ $res = $dom->loadXML($xml);
+ }
+
+ if ($res) {
+
+ $config = \SimpleSAML_Configuration::getInstance();
+ $schemaPath = $config->resolvePath('schemas').'/';
+ $schemaFile = $schemaPath.$schema;
+
+ $res = $dom->schemaValidate($schemaFile);
+ if ($res) {
+ \SimpleSAML_XML_Errors::end();
+ return true;
+ }
+
+ $errorText = "Schema validation failed on XML string:\n";
+ } else {
+ $errorText = "Failed to parse XML string for schema validation:\n";
+ }
+
+ $errors = \SimpleSAML_XML_Errors::end();
+ $errorText .= \SimpleSAML_XML_Errors::formatErrors($errors);
+
+ return $errorText;
+ }
}