From 2ab141787cbbc4627ae08cbb3b9935bdaebb12c2 Mon Sep 17 00:00:00 2001
From: Olav Morken <olav.morken@uninett.no>
Date: Wed, 4 Nov 2009 13:51:43 +0000
Subject: [PATCH] SimpleSAML_Utilities: Add getDefaultEndpoint-function.

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

diff --git a/lib/SimpleSAML/Utilities.php b/lib/SimpleSAML/Utilities.php
index f3d8822ab..4de7246ee 100644
--- a/lib/SimpleSAML/Utilities.php
+++ b/lib/SimpleSAML/Utilities.php
@@ -2113,6 +2113,57 @@ class SimpleSAML_Utilities {
 		self::$logMask = $lastMask[1];
 	}
 
+
+	/**
+	 * Find the default endpoint in an endpoint array.
+	 *
+	 * @param array $endpoints  Array with endpoints.
+	 * @param array $bindings  Array with acceptable bindings. Can be NULL if any binding is allowed.
+	 * @return  array|NULL  The default endpoint, or NULL if no acceptable endpoints are used.
+	 */
+	public static function getDefaultEndpoint(array $endpoints, array $bindings = NULL) {
+
+		$firstNotFalse = NULL;
+		$firstAllowed = NULL;
+
+		/* Look through the endpoint list for acceptable endpoints. */
+		foreach ($endpoints as $i => $ep) {
+			if ($bindings !== NULL && !in_array($ep['Binding'], $ep, TRUE)) {
+				/* Unsupported binding. Skip it. */
+				continue;
+			}
+
+			if (array_key_exists('isDefault', $ep)) {
+				if ($ep['isDefault'] === TRUE) {
+					/* This is the first endpoitn with isDefault set to TRUE. */
+					return $ep;
+				}
+				/* isDefault is set to FALSE, but the endpoint is still useable as a last resort. */
+				if ($firstAllowed === NULL) {
+					/* This is the first endpoint that we can use. */
+					$firstAllowed = $ep;
+				}
+			} else {
+				if ($firstNotFalse === NULL) {
+					/* This is the first endpoint without isDefault set. */
+					$firstNotFalse = $ep;
+				}
+			}
+		}
+
+		if ($firstNotFalse !== NULL) {
+			/* We have an endpoint without isDefault set to FALSE. */
+			return $firstNotFalse;
+		}
+
+		/*
+		 * $firstAllowed either contains the first endpoint we can use, or it
+		 * contains NULL if we cannot use any of the endpoints. Either way we
+		 * return the value of it.
+		 */
+		return $firstAllowed;
+	}
+
 }
 
 ?>
\ No newline at end of file
-- 
GitLab