From 1e079f45adf97fee458965183f1d4b6845970079 Mon Sep 17 00:00:00 2001 From: Jaime Perez Crespo <jaime.perez@uninett.no> Date: Tue, 21 Apr 2015 11:31:03 +0200 Subject: [PATCH] Move SimpleSAML_Utilities::addURLparameter() to SimpleSAML\Utils\HTTP::addURLParameters() and deprecate the former. --- lib/SimpleSAML/Module.php | 2 +- lib/SimpleSAML/Utilities.php | 46 ++----------------- lib/SimpleSAML/Utils/HTTP.php | 42 ++++++++++++++++- modules/aselect/lib/Auth/Source/aselect.php | 4 +- .../authtwitter/lib/Auth/Source/Twitter.php | 2 +- modules/cas/lib/Auth/Source/CAS.php | 4 +- modules/casserver/www/login.php | 2 +- modules/cdc/lib/Server.php | 2 +- modules/discojuice/www/central.php | 2 +- modules/oauth/lib/Consumer.php | 2 +- modules/oauth/lib/OAuthStore.php | 2 +- modules/oauth/www/authorize.php | 2 +- modules/saml/lib/IdP/SAML1.php | 2 +- modules/saml/lib/IdP/SAML2.php | 2 +- templates/includes/header.php | 2 +- www/shib13/idp/metadata.php | 2 +- 16 files changed, 61 insertions(+), 59 deletions(-) diff --git a/lib/SimpleSAML/Module.php b/lib/SimpleSAML/Module.php index b5d8f0245..829fa7b11 100644 --- a/lib/SimpleSAML/Module.php +++ b/lib/SimpleSAML/Module.php @@ -157,7 +157,7 @@ class SimpleSAML_Module { $url = SimpleSAML_Utilities::getBaseURL() . 'module.php/' . $resource; if (!empty($parameters)) { - $url = SimpleSAML_Utilities::addURLparameter($url, $parameters); + $url = \SimpleSAML\Utils\HTTP::addURLParameters($url, $parameters); } return $url; } diff --git a/lib/SimpleSAML/Utilities.php b/lib/SimpleSAML/Utilities.php index a0dab57b6..334f5846b 100644 --- a/lib/SimpleSAML/Utilities.php +++ b/lib/SimpleSAML/Utilities.php @@ -252,48 +252,10 @@ class SimpleSAML_Utilities { /** - * 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. This will write a warning - * to the log. - * @return The URL with the new query parameters. - */ - public static function addURLparameter($url, $parameter) { - - /* For backwards compatibility - allow $parameter to be a string. */ - if(is_string($parameter)) { - /* Print warning to log. */ - $backtrace = debug_backtrace(); - $where = $backtrace[0]['file'] . ':' . $backtrace[0]['line']; - SimpleSAML_Logger::warning( - 'Deprecated use of SimpleSAML_Utilities::addURLparameter at ' . $where . - '. The parameter "$parameter" should now be an array, but a string was passed.'); - - $parameter = self::parseQueryString($parameter); - } - assert('is_array($parameter)'); - - $queryStart = strpos($url, '?'); - if($queryStart === FALSE) { - $oldQuery = array(); - $url .= '?'; - } else { - $oldQuery = substr($url, $queryStart + 1); - if($oldQuery === FALSE) { - $oldQuery = array(); - } else { - $oldQuery = self::parseQueryString($oldQuery); - } - $url = substr($url, 0, $queryStart + 1); - } - - $query = array_merge($oldQuery, $parameter); - $url .= http_build_query($query, '', '&'); - - return $url; + * @deprecated This method will be removed in SSP 2.0. Please use SimpleSAML\Utils\HTTP::addURLParameters() instead. + */ + public static function addURLparameter($url, $parameters) { + return \SimpleSAML\Utils\HTTP::addURLParameter($url, $parameters); } diff --git a/lib/SimpleSAML/Utils/HTTP.php b/lib/SimpleSAML/Utils/HTTP.php index d501162af..bf7ac0c45 100644 --- a/lib/SimpleSAML/Utils/HTTP.php +++ b/lib/SimpleSAML/Utils/HTTP.php @@ -65,6 +65,46 @@ class HTTP } + /** + * Add one or more query parameters to the given URL. + * + * @param string $url The URL the query parameters should be added to. + * @param array $parameters The query parameters which should be added to the url. This should be an associative + * array. + * + * @return string The URL with the new query parameters. + * @throws \SimpleSAML_Error_Exception If $url is not a string or $parameters is not an array. + * + * @author Andreas Solberg, UNINETT AS <andreas.solberg@uninett.no> + * @author Olav Morken, UNINETT AS <olav.morken@uninett.no> + */ + public static function addURLParameters($url, $parameters) + { + if (!is_string($url) || !is_array($parameters)) { + throw new \SimpleSAML_Error_Exception('Invalid input parameters.'); + } + + $queryStart = strpos($url, '?'); + if ($queryStart === false) { + $oldQuery = array(); + $url .= '?'; + } else { + $oldQuery = substr($url, $queryStart + 1); + if ($oldQuery === false) { + $oldQuery = array(); + } else { + $oldQuery = self::parseQueryString($oldQuery); + } + $url = substr($url, 0, $queryStart + 1); + } + + $query = array_merge($oldQuery, $parameters); + $url .= http_build_query($query, '', '&'); + + return $url; + } + + /** * Retrieve the port number from $_SERVER environment variables. * @@ -107,7 +147,7 @@ class HTTP public static function parseQueryString($query_string) { if (!is_string($query_string)) { - throw new \SimpleSAML_Error_Exception('Invalid input parameters'); + throw new \SimpleSAML_Error_Exception('Invalid input parameters.'); } $res = array(); diff --git a/modules/aselect/lib/Auth/Source/aselect.php b/modules/aselect/lib/Auth/Source/aselect.php index 874b5ca72..fca989693 100644 --- a/modules/aselect/lib/Auth/Source/aselect.php +++ b/modules/aselect/lib/Auth/Source/aselect.php @@ -125,7 +125,7 @@ class sspmod_aselect_Auth_Source_aselect extends SimpleSAML_Auth_Source { $signable .= $parameters[$p]; $parameters['signature'] = $this->base64_signature($signable); } - return SimpleSAML_Utilities::addURLparameter($this->server_url, $parameters); + return \SimpleSAML\Utils\HTTP::addURLParameters($this->server_url, $parameters); } /** @@ -177,7 +177,7 @@ class sspmod_aselect_Auth_Source_aselect extends SimpleSAML_Auth_Source { $as_url = $res['as_url']; unset($res['as_url']); - return SimpleSAML_Utilities::addURLparameter($as_url, $res); + return \SimpleSAML\Utils\HTTP::addURLParameters($as_url, $res); } /** diff --git a/modules/authtwitter/lib/Auth/Source/Twitter.php b/modules/authtwitter/lib/Auth/Source/Twitter.php index c07106633..58e7ba64a 100644 --- a/modules/authtwitter/lib/Auth/Source/Twitter.php +++ b/modules/authtwitter/lib/Auth/Source/Twitter.php @@ -72,7 +72,7 @@ class sspmod_authtwitter_Auth_Source_Twitter extends SimpleSAML_Auth_Source { // Authorize the request token $url = 'https://api.twitter.com/oauth/authenticate'; if ($this->force_login) { - $url = SimpleSAML_Utilities::addURLparameter($url, array('force_login' => 'true')); + $url = \SimpleSAML\Utils\HTTP::addURLParameters($url, array('force_login' => 'true')); } $consumer->getAuthorizeRequest($url, $requestToken); } diff --git a/modules/cas/lib/Auth/Source/CAS.php b/modules/cas/lib/Auth/Source/CAS.php index 611fd85c2..81ae59e28 100644 --- a/modules/cas/lib/Auth/Source/CAS.php +++ b/modules/cas/lib/Auth/Source/CAS.php @@ -89,7 +89,7 @@ class sspmod_cas_Auth_Source_CAS extends SimpleSAML_Auth_Source { * @return list username and attributes */ private function casValidate($ticket, $service){ - $url = SimpleSAML_Utilities::addURLparameter($this->_casConfig['validate'], array( + $url = \SimpleSAML\Utils\HTTP::addURLParameters($this->_casConfig['validate'], array( 'ticket' => $ticket, 'service' => $service, )); @@ -112,7 +112,7 @@ class sspmod_cas_Auth_Source_CAS extends SimpleSAML_Auth_Source { * @return list username and attributes */ private function casServiceValidate($ticket, $service){ - $url = SimpleSAML_Utilities::addURLparameter($this->_casConfig['serviceValidate'], array( + $url = \SimpleSAML\Utils\HTTP::addURLParameters($this->_casConfig['serviceValidate'], array( 'ticket' => $ticket, 'service' => $service, )); diff --git a/modules/casserver/www/login.php b/modules/casserver/www/login.php index 43e08fb35..f660a98c9 100644 --- a/modules/casserver/www/login.php +++ b/modules/casserver/www/login.php @@ -49,7 +49,7 @@ storeTicket($ticket, $path, array('service' => $service, 'validbefore' => time() + 5)); SimpleSAML_Utilities::redirectTrustedURL( - SimpleSAML_Utilities::addURLparameter($service, + \SimpleSAML\Utils\HTTP::addURLParameters($service, array('ticket' => $ticket) ) ); \ No newline at end of file diff --git a/modules/cdc/lib/Server.php b/modules/cdc/lib/Server.php index 2aa885061..8c2798fd7 100644 --- a/modules/cdc/lib/Server.php +++ b/modules/cdc/lib/Server.php @@ -324,7 +324,7 @@ class sspmod_cdc_Server { 'Signature' => $signature, ); - $url = SimpleSAML_Utilities::addURLparameter($to, $params); + $url = \SimpleSAML\Utils\HTTP::addURLParameters($to, $params); if (strlen($url) < 2048) { SimpleSAML_Utilities::redirectTrustedURL($url); } else { diff --git a/modules/discojuice/www/central.php b/modules/discojuice/www/central.php index 6f1a7ead6..9404fd546 100644 --- a/modules/discojuice/www/central.php +++ b/modules/discojuice/www/central.php @@ -12,7 +12,7 @@ $entityid = $_REQUEST['entityID']; // Return to... $returnidparam = !empty($_REQUEST['returnIDParam']) ? $_REQUEST['returnIDParam'] : 'entityID'; -$href = SimpleSAML_Utilities::addURLparameter( +$href = \SimpleSAML\Utils\HTTP::addURLParameters( $_REQUEST['return'], array($returnidparam => '') ); diff --git a/modules/oauth/lib/Consumer.php b/modules/oauth/lib/Consumer.php index 8e3e5aed6..265b1cab9 100644 --- a/modules/oauth/lib/Consumer.php +++ b/modules/oauth/lib/Consumer.php @@ -91,7 +91,7 @@ class sspmod_oauth_Consumer { if ($callback) { $params['oauth_callback'] = $callback; } - $authorizeURL = SimpleSAML_Utilities::addURLparameter($url, $params); + $authorizeURL = \SimpleSAML\Utils\HTTP::addURLParameters($url, $params); if ($redirect) { SimpleSAML_Utilities::redirectTrustedURL($authorizeURL); exit; diff --git a/modules/oauth/lib/OAuthStore.php b/modules/oauth/lib/OAuthStore.php index 6a239a6c0..974387a21 100644 --- a/modules/oauth/lib/OAuthStore.php +++ b/modules/oauth/lib/OAuthStore.php @@ -63,7 +63,7 @@ class sspmod_oauth_OAuthStore extends OAuthDataStore { if ($oConsumer && ($oConsumer->callback_url)) $url = $oConsumer->callback_url; $verifier = SimpleSAML\Utils\Random::generateID(); - $url = SimpleSAML_Utilities::addURLparameter($url, array("oauth_verifier"=>$verifier)); + $url = \SimpleSAML\Utils\HTTP::addURLParameters($url, array("oauth_verifier"=>$verifier)); $this->store->set('authorized', $requestTokenKey, $verifier, $data, $this->config->getValue('requestTokenDuration', 60*30) ); diff --git a/modules/oauth/www/authorize.php b/modules/oauth/www/authorize.php index afcc395b9..523670ecf 100644 --- a/modules/oauth/www/authorize.php +++ b/modules/oauth/www/authorize.php @@ -40,7 +40,7 @@ try { $t = new SimpleSAML_XHTML_Template($config, 'oauth:consent.php'); $t->data['header'] = '{status:header_saml20_sp}'; $t->data['consumer'] = $consumer; // array containint {name, description, key, secret, owner} keys - $t->data['urlAgree'] = SimpleSAML_Utilities::addURLparameter( SimpleSAML_Utilities::selfURL(), array("consent" => "yes") ); + $t->data['urlAgree'] = \SimpleSAML\Utils\HTTP::addURLParameters(SimpleSAML_Utilities::selfURL(), array("consent" => "yes")); $t->data['logouturl'] = SimpleSAML_Utilities::selfURLNoQuery() . '?logout'; $t->show(); diff --git a/modules/saml/lib/IdP/SAML1.php b/modules/saml/lib/IdP/SAML1.php index 9ef95b78a..79c8617ea 100644 --- a/modules/saml/lib/IdP/SAML1.php +++ b/modules/saml/lib/IdP/SAML1.php @@ -115,7 +115,7 @@ class sspmod_saml_IdP_SAML1 { 'protocol' => 'saml1', )); - $sessionLostURL = SimpleSAML_Utilities::addURLparameter( + $sessionLostURL = \SimpleSAML\Utils\HTTP::addURLParameters( SimpleSAML_Utilities::selfURL(), array('cookieTime' => time())); diff --git a/modules/saml/lib/IdP/SAML2.php b/modules/saml/lib/IdP/SAML2.php index ae9a6d4fb..a6bf33a4c 100644 --- a/modules/saml/lib/IdP/SAML2.php +++ b/modules/saml/lib/IdP/SAML2.php @@ -361,7 +361,7 @@ class sspmod_saml_IdP_SAML2 { $sessionLostParams['RelayState'] = $relayState; } - $sessionLostURL = SimpleSAML_Utilities::addURLparameter( + $sessionLostURL = \SimpleSAML\Utils\HTTP::addURLParameters( SimpleSAML_Utilities::selfURLNoQuery(), $sessionLostParams); diff --git a/templates/includes/header.php b/templates/includes/header.php index cb59059fc..10f75765b 100644 --- a/templates/includes/header.php +++ b/templates/includes/header.php @@ -186,7 +186,7 @@ if($onLoad !== '') { if ($current) { $textarray[] = $langnames[$lang]; } else { - $textarray[] = '<a href="' . htmlspecialchars(SimpleSAML_Utilities::addURLparameter(SimpleSAML_Utilities::selfURL(), array($this->languageParameterName => $lang))) . '">' . + $textarray[] = '<a href="' . htmlspecialchars(\SimpleSAML\Utils\HTTP::addURLParameters(SimpleSAML_Utilities::selfURL(), array($this->languageParameterName => $lang))) . '">' . $langnames[$lang] . '</a>'; } } diff --git a/www/shib13/idp/metadata.php b/www/shib13/idp/metadata.php index fc0a35834..92eb4ec05 100644 --- a/www/shib13/idp/metadata.php +++ b/www/shib13/idp/metadata.php @@ -87,7 +87,7 @@ try { $t->data['header'] = 'shib13-idp'; - $t->data['metaurl'] = SimpleSAML_Utilities::addURLparameter(SimpleSAML_Utilities::selfURLNoQuery(), array('output' => 'xml')); + $t->data['metaurl'] = \SimpleSAML\Utils\HTTP::addURLParameters(SimpleSAML_Utilities::selfURLNoQuery(), array('output' => 'xml')); $t->data['metadata'] = htmlspecialchars($metaxml); $t->data['metadataflat'] = htmlspecialchars($metaflat); -- GitLab