From 104fd02cb0c597d828903ef77bf3806e57872684 Mon Sep 17 00:00:00 2001
From: Olav Morken <olav.morken@uninett.no>
Date: Tue, 10 Jan 2012 13:10:52 +0000
Subject: [PATCH] Utilities: Introduce normalizeURL().

This function normalizes the URL to an absolute URL, and makes sure
that it is a link to a http or https site.

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

diff --git a/lib/SimpleSAML/Utilities.php b/lib/SimpleSAML/Utilities.php
index f09580f94..56ae886f1 100644
--- a/lib/SimpleSAML/Utilities.php
+++ b/lib/SimpleSAML/Utilities.php
@@ -1186,6 +1186,29 @@ class SimpleSAML_Utilities {
 	}
 
 
+	/**
+	 * Normalizes an URL to an absolute URL and validate it.
+	 *
+	 * In addition to resolving the URL, this function makes sure that it is
+	 * a link to a http or https site.
+	 *
+	 * @param string $url  The relative URL.
+	 * @return string  An absolute URL for the given relative URL.
+	 */
+	public static function normalizeURL($url) {
+		assert('is_string($url)');
+
+		$url = SimpleSAML_Utilities::resolveURL($url, SimpleSAML_Utilities::selfURL());
+
+		/* Verify that the URL is to a http or https site. */
+		if (!preg_match('@^https?://@i', $url)) {
+			throw new SimpleSAML_Error_Exception('Invalid URL: ' . $url);
+		}
+
+		return $url;
+	}
+
+
 	/**
 	 * Parse a query string into an array.
 	 *
-- 
GitLab