diff options
author | Jaime Perez Crespo <jaime.perez@uninett.no> | 2015-04-20 14:39:56 +0200 |
---|---|---|
committer | Jaime Perez Crespo <jaime.perez@uninett.no> | 2015-04-20 14:39:56 +0200 |
commit | 20a0b6c93923b83a401d7f98af0305162acd0a35 (patch) | |
tree | 353bf71c0b7aaeda7b8fd326baf24ccfed52f3c9 /lib/SimpleSAML/Utils/XML.php | |
parent | 7204e20c725f5d9945596d14f721c5f3191c1283 (diff) | |
download | simplesamlphp-20a0b6c93923b83a401d7f98af0305162acd0a35.zip simplesamlphp-20a0b6c93923b83a401d7f98af0305162acd0a35.tar.gz simplesamlphp-20a0b6c93923b83a401d7f98af0305162acd0a35.tar.bz2 |
Move SimpleSAML_Utilities::isDOMElementOfType() to SimpleSAML\Utils\XML::isDOMElementOfType(). Deprecate the former.
Diffstat (limited to 'lib/SimpleSAML/Utils/XML.php')
-rw-r--r-- | lib/SimpleSAML/Utils/XML.php | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/lib/SimpleSAML/Utils/XML.php b/lib/SimpleSAML/Utils/XML.php index 0b47ec9..5a2b3da 100644 --- a/lib/SimpleSAML/Utils/XML.php +++ b/lib/SimpleSAML/Utils/XML.php @@ -97,6 +97,7 @@ class XML $root->appendChild(new \DOMText("\n".$indentBase)); } + /** * Format an XML string. * @@ -158,4 +159,64 @@ class XML $txt = trim($txt); return $txt; } + + + /** + * This function checks if the DOMElement has the correct localName and namespaceURI. + * + * We also define the following shortcuts for namespaces: + * - '@ds': 'http://www.w3.org/2000/09/xmldsig#' + * - '@md': 'urn:oasis:names:tc:SAML:2.0:metadata' + * - '@saml1': 'urn:oasis:names:tc:SAML:1.0:assertion' + * - '@saml1md': 'urn:oasis:names:tc:SAML:profiles:v1metadata' + * - '@saml1p': 'urn:oasis:names:tc:SAML:1.0:protocol' + * - '@saml2': 'urn:oasis:names:tc:SAML:2.0:assertion' + * - '@saml2p': 'urn:oasis:names:tc:SAML:2.0:protocol' + * + * @param \DOMNode $element The element we should check. + * @param string $name The local name the element should have. + * @param string $nsURI The namespaceURI the element should have. + * + * @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> + */ + public static function isDOMElementOfType(\DOMNode $element, $name, $nsURI) + { + if (!($element instanceof \DOMElement) || !is_string($name) || !is_string($nsURI) || strlen($nsURI) === 0) { + // most likely a comment-node + return false; + } + + // check if the namespace is a shortcut, and expand it if it is + if ($nsURI[0] === '@') { + // the defined shortcuts + $shortcuts = array( + '@ds' => 'http://www.w3.org/2000/09/xmldsig#', + '@md' => 'urn:oasis:names:tc:SAML:2.0:metadata', + '@saml1' => 'urn:oasis:names:tc:SAML:1.0:assertion', + '@saml1md' => 'urn:oasis:names:tc:SAML:profiles:v1metadata', + '@saml1p' => 'urn:oasis:names:tc:SAML:1.0:protocol', + '@saml2' => 'urn:oasis:names:tc:SAML:2.0:assertion', + '@saml2p' => 'urn:oasis:names:tc:SAML:2.0:protocol', + '@shibmd' => 'urn:mace:shibboleth:metadata:1.0', + ); + + // check if it is a valid shortcut + if (!array_key_exists($nsURI, $shortcuts)) { + throw new \SimpleSAML_Error_Exception('Unknown namespace shortcut: '.$nsURI); + } + + // expand the shortcut + $nsURI = $shortcuts[$nsURI]; + } + if ($element->localName !== $name) { + return false; + } + if ($element->namespaceURI !== $nsURI) { + return false; + } + return true; + } } |