From 626bf25655fc41d15a290702c21bae65dbe909dd Mon Sep 17 00:00:00 2001
From: Olav Morken <olav.morken@uninett.no>
Date: Thu, 21 Feb 2008 12:15:44 +0000
Subject: [PATCH] Utilities: added isDOMElementOfType helper function.

git-svn-id: https://simplesamlphp.googlecode.com/svn/trunk@317 44740490-163a-0410-bde0-09ae8108e29a
---
 lib/SimpleSAML/Utilities.php | 59 ++++++++++++++++++++++++++++++++++++
 1 file changed, 59 insertions(+)

diff --git a/lib/SimpleSAML/Utilities.php b/lib/SimpleSAML/Utilities.php
index f32f4a419..ddf3d7953 100644
--- a/lib/SimpleSAML/Utilities.php
+++ b/lib/SimpleSAML/Utilities.php
@@ -449,6 +449,65 @@ class SimpleSAML_Utilities {
 
 		return $ret;
 	}
+
+
+	/**
+	 * 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 $element The element we should check.
+	 * @param $name The localname the element should have.
+	 * @param $nsURI The namespaceURI the element should have.
+	 * @return TRUE if both namespace and localname matches, FALSE otherwise.
+	 */
+	public static function isDOMElementOfType($element, $name, $nsURI) {
+		assert('$element instanceof DOMElement');
+		assert('is_string($name)');
+		assert('is_string($nsURI)');
+		assert('strlen($nsURI) > 0');
+
+		/* 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',
+				);
+
+			/* Check if it is a valid shortcut. */
+			if(!array_key_exists($nsURI, $shortcuts)) {
+				throw new 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;
+	}
 }
 
 ?>
\ No newline at end of file
-- 
GitLab