From 2b53dc85d8cb259c65613a081d38fe7627c576ac Mon Sep 17 00:00:00 2001
From: Olav Morken <olav.morken@uninett.no>
Date: Wed, 2 Jul 2008 06:10:51 +0000
Subject: [PATCH] Utilities: Updated addURLparameter to merge the new query
 string with the old query string.

This should fix a problem where one can end up with multiple parameters with
the same name in the query string. This patch also changes the $parameter of
addURLparameter to be an associative array. For backwards compatibility it can
still be a string.


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

diff --git a/lib/SimpleSAML/Utilities.php b/lib/SimpleSAML/Utilities.php
index 51185e843..a97e9fb7b 100644
--- a/lib/SimpleSAML/Utilities.php
+++ b/lib/SimpleSAML/Utilities.php
@@ -134,15 +134,41 @@ class SimpleSAML_Utilities {
 		}
 		return $requesturi;
 	}
-	
+
+
+	/**
+	 * Add one or more query parameters to the given URL.
+	 *
+	 * @param $url  The URL the query parameters should be added to.
+	 * @param $parameter  The query parameters which should be added to the url. This should be
+	 *                    an associative array. For backwards comaptibility, it can also be a
+	 *                    query string representing the new parameters.
+	 * @return The URL with the new query parameters.
+	 */
 	public static function addURLparameter($url, $parameter) {
-		if (strstr($url, '?')) {
-			return $url . '&' . $parameter;
+
+		/* For backwards compatibility - allow $parameter to be a string. */
+		if(is_string($parameter)) {
+			$parameter = self::parseQueryString($parameter);
+		}
+		assert('is_array($parameter)');
+
+		$queryStart = strpos($url, '?');
+		if($queryStart === FALSE) {
+			$oldQuery = array();
+			$url .= '?';
 		} else {
-			return $url . '?' . $parameter;
+			$oldQuery = self::parseQueryString(substr($url, $queryStart + 1));
+			$url = substr($url, 0, $queryStart + 1);
 		}
+
+		$query = array_merge($oldQuery, $parameter);
+		$url .= http_build_query($query);
+
+		return $url;
 	}
-	
+
+
 	public static function strleft($s1, $s2) {
 		return substr($s1, 0, strpos($s1, $s2));
 	}
-- 
GitLab