From 4fbe9934cd65adfbab707f9d2dab345faa3df9d8 Mon Sep 17 00:00:00 2001
From: Jaime Perez Crespo <jaime.perez@uninett.no>
Date: Tue, 21 Apr 2015 13:33:58 +0200
Subject: [PATCH] Move SimpleSAML_Utilities::selfURL() to
 SimpleSAML\Utils\HTTP::getSelfURL() and deprecate the former.

---
 lib/SimpleSAML/Auth/Simple.php                |  8 +++----
 lib/SimpleSAML/Error/NotFound.php             |  2 +-
 lib/SimpleSAML/Utilities.php                  | 17 ++++----------
 lib/SimpleSAML/Utils/Auth.php                 |  2 +-
 lib/SimpleSAML/Utils/HTTP.php                 | 22 +++++++++++++++++++
 modules/authX509/templates/X509error.php      |  2 +-
 .../consentAdmin/templates/consentadmin.php   |  2 +-
 modules/cron/www/cron.php                     |  2 +-
 modules/oauth/www/authorize.php               |  4 ++--
 modules/oauth/www/registry.edit.php           |  2 +-
 modules/oauth/www/registry.php                |  2 +-
 modules/saml/lib/IdP/SAML1.php                |  4 ++--
 modules/saml/lib/IdP/SAML2.php                |  2 +-
 templates/includes/header.php                 |  2 +-
 www/admin/hostnames.php                       |  2 +-
 www/authmemcookie.php                         |  2 +-
 16 files changed, 45 insertions(+), 32 deletions(-)

diff --git a/lib/SimpleSAML/Auth/Simple.php b/lib/SimpleSAML/Auth/Simple.php
index da0881d48..33fc4e552 100644
--- a/lib/SimpleSAML/Auth/Simple.php
+++ b/lib/SimpleSAML/Auth/Simple.php
@@ -110,7 +110,7 @@ class SimpleSAML_Auth_Simple {
 		} else if (array_key_exists('ReturnCallback', $params)) {
 			$returnTo = (array)$params['ReturnCallback'];
 		} else {
-			$returnTo = SimpleSAML_Utilities::selfURL();
+			$returnTo = \SimpleSAML\Utils\HTTP::getSelfURL();
 		}
 
 		if (is_string($returnTo) && $keepPost && $_SERVER['REQUEST_METHOD'] === 'POST') {
@@ -159,7 +159,7 @@ class SimpleSAML_Auth_Simple {
 		assert('is_array($params) || is_string($params) || is_null($params)');
 
 		if ($params === NULL) {
-			$params = SimpleSAML_Utilities::selfURL();
+			$params = \SimpleSAML\Utils\HTTP::getSelfURL();
 		}
 
 		if (is_string($params)) {
@@ -290,7 +290,7 @@ class SimpleSAML_Auth_Simple {
 		assert('is_null($returnTo) || is_string($returnTo)');
 
 		if ($returnTo === NULL) {
-			$returnTo = SimpleSAML_Utilities::selfURL();
+			$returnTo = \SimpleSAML\Utils\HTTP::getSelfURL();
 		}
 
 		$login = SimpleSAML_Module::getModuleURL('core/as_login.php', array(
@@ -313,7 +313,7 @@ class SimpleSAML_Auth_Simple {
 		assert('is_null($returnTo) || is_string($returnTo)');
 
 		if ($returnTo === NULL) {
-			$returnTo = SimpleSAML_Utilities::selfURL();
+			$returnTo = \SimpleSAML\Utils\HTTP::getSelfURL();
 		}
 
 		$logout = SimpleSAML_Module::getModuleURL('core/as_logout.php', array(
diff --git a/lib/SimpleSAML/Error/NotFound.php b/lib/SimpleSAML/Error/NotFound.php
index cb868e8ac..251ff1909 100644
--- a/lib/SimpleSAML/Error/NotFound.php
+++ b/lib/SimpleSAML/Error/NotFound.php
@@ -27,7 +27,7 @@ class SimpleSAML_Error_NotFound extends SimpleSAML_Error_Error {
 
 		assert('is_null($reason) || is_string($reason)');
 
-		$url = SimpleSAML_Utilities::selfURL();
+		$url = \SimpleSAML\Utils\HTTP::getSelfURL();
 
 		if($reason === NULL) {
 			parent::__construct(array('NOTFOUND', '%URL%' => $url));
diff --git a/lib/SimpleSAML/Utilities.php b/lib/SimpleSAML/Utilities.php
index b6aaa95e4..44f2ff14c 100644
--- a/lib/SimpleSAML/Utilities.php
+++ b/lib/SimpleSAML/Utilities.php
@@ -96,20 +96,11 @@ class SimpleSAML_Utilities {
 	}
 
 
+    /**
+     * @deprecated This method will be removed in SSP 2.0. Please use SimpleSAML\Utils\HTTP::getSelfURL() instead.
+     */
 	public static function selfURL() {
-
-		$selfURLhost = self::selfURLhost();
-
-		$requestURI = $_SERVER['REQUEST_URI'];
-		if ($requestURI[0] !== '/') {
-			/* We probably have a URL of the form: http://server/. */
-			if (preg_match('#^https?://[^/]*(/.*)#i', $requestURI, $matches)) {
-				$requestURI = $matches[1];
-			}
-		}
-
-		return $selfURLhost . $requestURI;
-
+		return \SimpleSAML\Utils\HTTP::getSelfURL();
 	}
 
 
diff --git a/lib/SimpleSAML/Utils/Auth.php b/lib/SimpleSAML/Utils/Auth.php
index 48dd8ffe0..16aec172f 100644
--- a/lib/SimpleSAML/Utils/Auth.php
+++ b/lib/SimpleSAML/Utils/Auth.php
@@ -21,7 +21,7 @@ class Auth
         assert('is_string($returnTo) || is_null($returnTo)');
 
         if ($returnTo === null) {
-            $returnTo = \SimpleSAML_Utilities::selfURL();
+            $returnTo = \SimpleSAML\Utils\HTTP::getSelfURL();
         }
 
         return \SimpleSAML_Module::getModuleURL('core/login-admin.php', array('ReturnTo' => $returnTo));
diff --git a/lib/SimpleSAML/Utils/HTTP.php b/lib/SimpleSAML/Utils/HTTP.php
index 1d6828535..eed837a28 100644
--- a/lib/SimpleSAML/Utils/HTTP.php
+++ b/lib/SimpleSAML/Utils/HTTP.php
@@ -371,6 +371,28 @@ class HTTP
     }
 
 
+    /**
+     * Retrieve the current URL.
+     *
+     * @return string The current URL.
+     *
+     * @author Andreas Solberg, UNINETT AS <andreas.solberg@uninett.no>
+     * @author Olav Morken, UNINETT AS <olav.morken@uninett.no>
+     */
+    public static function getSelfURL()
+    {
+        $selfURLhost = self::getSelfURLHost();
+        $requestURI = $_SERVER['REQUEST_URI'];
+        if ($requestURI[0] !== '/') {
+            // we probably have a URL of the form: http://server/
+            if (preg_match('#^https?://[^/]*(/.*)#i', $requestURI, $matches)) {
+                $requestURI = $matches[1];
+            }
+        }
+        return $selfURLhost.$requestURI;
+    }
+
+
     /**
      * Retrieve a URL containing the protocol, the current host and optionally, the port number.
      *
diff --git a/modules/authX509/templates/X509error.php b/modules/authX509/templates/X509error.php
index 90e2dbd54..c55ae65c4 100644
--- a/modules/authX509/templates/X509error.php
+++ b/modules/authX509/templates/X509error.php
@@ -21,7 +21,7 @@ if ($this->data['errorcode'] !== NULL) {
 
 	<p><?php echo $this->t('{authX509:X509error:certificate_text}'); ?></p>
 
-	<a href="<?php echo htmlspecialchars(SimpleSAML_Utilities::selfURL()); ?>">
+	<a href="<?php echo htmlspecialchars(\SimpleSAML\Utils\HTTP::getSelfURL()); ?>">
 		<?php echo $this->t('{login:login_button}'); ?>
 	</a>
 
diff --git a/modules/consentAdmin/templates/consentadmin.php b/modules/consentAdmin/templates/consentadmin.php
index aac3c499f..496d51452 100644
--- a/modules/consentAdmin/templates/consentadmin.php
+++ b/modules/consentAdmin/templates/consentadmin.php
@@ -131,6 +131,6 @@ TRSTART;
 		
 		<h2>Logout</h2>
 
-			<p><a href="<?php echo SimpleSAML_Utilities::selfURL() . '?logout'; ?>">Logout</a></p>
+			<p><a href="<?php echo \SimpleSAML\Utils\HTTP::getSelfURL() . '?logout'; ?>">Logout</a></p>
 		
 <?php $this->includeAtTemplateBase('includes/footer.php');
diff --git a/modules/cron/www/cron.php b/modules/cron/www/cron.php
index 3180ca5d5..682c24205 100644
--- a/modules/cron/www/cron.php
+++ b/modules/cron/www/cron.php
@@ -22,7 +22,7 @@ $croninfo = array(
 	'summary' => &$summary,
 	'tag' => $_REQUEST['tag'],
 );
-$url = SimpleSAML_Utilities::selfURL();
+$url = \SimpleSAML\Utils\HTTP::getSelfURL();
 $time = date(DATE_RFC822);
 
 SimpleSAML_Module::callHooks('cron', $croninfo);
diff --git a/modules/oauth/www/authorize.php b/modules/oauth/www/authorize.php
index 523670ecf..17392ad73 100644
--- a/modules/oauth/www/authorize.php
+++ b/modules/oauth/www/authorize.php
@@ -30,7 +30,7 @@ try {
 
 	$as = $oauthconfig->getString('auth');
 	if (!$session->isValid($as)) {
-		SimpleSAML_Auth_Default::initLogin($as, SimpleSAML_Utilities::selfURL());
+		SimpleSAML_Auth_Default::initLogin($as, \SimpleSAML\Utils\HTTP::getSelfURL());
 	}
 
 
@@ -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\Utils\HTTP::addURLParameters(SimpleSAML_Utilities::selfURL(), array("consent" => "yes"));
+		$t->data['urlAgree'] = \SimpleSAML\Utils\HTTP::addURLParameters(\SimpleSAML\Utils\HTTP::getSelfURL(), array("consent" => "yes"));
 		$t->data['logouturl'] = SimpleSAML_Utilities::selfURLNoQuery() . '?logout';
 	
 		$t->show();
diff --git a/modules/oauth/www/registry.edit.php b/modules/oauth/www/registry.edit.php
index 1ce979ae4..555e77b67 100644
--- a/modules/oauth/www/registry.edit.php
+++ b/modules/oauth/www/registry.edit.php
@@ -17,7 +17,7 @@ if ($session->isValid($authsource)) {
 		throw new Exception('User ID is missing');
 	$userid = $attributes[$useridattr][0];
 } else {
-	SimpleSAML_Auth_Default::initLogin($authsource, SimpleSAML_Utilities::selfURL());
+	SimpleSAML_Auth_Default::initLogin($authsource, \SimpleSAML\Utils\HTTP::getSelfURL());
 }
 
 function requireOwnership($entry, $userid) {
diff --git a/modules/oauth/www/registry.php b/modules/oauth/www/registry.php
index dfea92831..bd36ac448 100644
--- a/modules/oauth/www/registry.php
+++ b/modules/oauth/www/registry.php
@@ -17,7 +17,7 @@ if ($session->isValid($authsource)) {
 		throw new Exception('User ID is missing');
 	$userid = $attributes[$useridattr][0];
 } else {
-	SimpleSAML_Auth_Default::initLogin($authsource, SimpleSAML_Utilities::selfURL());
+	SimpleSAML_Auth_Default::initLogin($authsource, \SimpleSAML\Utils\HTTP::getSelfURL());
 }
 
 function requireOwnership($entry, $userid) {
diff --git a/modules/saml/lib/IdP/SAML1.php b/modules/saml/lib/IdP/SAML1.php
index 1d7b02a67..2cd520bec 100644
--- a/modules/saml/lib/IdP/SAML1.php
+++ b/modules/saml/lib/IdP/SAML1.php
@@ -69,7 +69,7 @@ class sspmod_saml_IdP_SAML1 {
 				 * Less than five seconds has passed since we were
 				 * here the last time. Cookies are probably disabled.
 				 */
-				SimpleSAML_Utilities::checkCookie(SimpleSAML_Utilities::selfURL());
+				SimpleSAML_Utilities::checkCookie(\SimpleSAML\Utils\HTTP::getSelfURL());
 			}
 		}
 
@@ -116,7 +116,7 @@ class sspmod_saml_IdP_SAML1 {
 		));
 
 		$sessionLostURL = \SimpleSAML\Utils\HTTP::addURLParameters(
-			SimpleSAML_Utilities::selfURL(),
+            \SimpleSAML\Utils\HTTP::getSelfURL(),
 			array('cookieTime' => time()));
 
 		$state = array(
diff --git a/modules/saml/lib/IdP/SAML2.php b/modules/saml/lib/IdP/SAML2.php
index a6bf33a4c..e8e5a8b8d 100644
--- a/modules/saml/lib/IdP/SAML2.php
+++ b/modules/saml/lib/IdP/SAML2.php
@@ -247,7 +247,7 @@ class sspmod_saml_IdP_SAML2 {
 					 * Less than five seconds has passed since we were
 					 * here the last time. Cookies are probably disabled.
 					 */
-					SimpleSAML_Utilities::checkCookie(SimpleSAML_Utilities::selfURL());
+					SimpleSAML_Utilities::checkCookie(\SimpleSAML\Utils\HTTP::getSelfURL());
 				}
 			}
 
diff --git a/templates/includes/header.php b/templates/includes/header.php
index 10f75765b..03f195358 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\Utils\HTTP::addURLParameters(SimpleSAML_Utilities::selfURL(), array($this->languageParameterName => $lang))) . '">' .
+					$textarray[] = '<a href="' . htmlspecialchars(\SimpleSAML\Utils\HTTP::addURLParameters(\SimpleSAML\Utils\HTTP::getSelfURL(), array($this->languageParameterName => $lang))) . '">' .
 						$langnames[$lang] . '</a>';
 				}
 			}
diff --git a/www/admin/hostnames.php b/www/admin/hostnames.php
index f8ef96dbd..142cf16bf 100644
--- a/www/admin/hostnames.php
+++ b/www/admin/hostnames.php
@@ -22,7 +22,7 @@ $attributes['Utilities_selfURLhost()'] = array(\SimpleSAML\Utils\HTTP::getSelfUR
 $attributes['Utilities_selfURLNoQuery()'] = array(SimpleSAML_Utilities::selfURLNoQuery());
 $attributes['Utilities_getSelfHostWithPath()'] = array(\SimpleSAML\Utils\HTTP::getSelfHostWithPath());
 $attributes['Utilities_getFirstPathElement()'] = array(\SimpleSAML\Utils\HTTP::getFirstPathElement());
-$attributes['Utilities_selfURL()'] = array(SimpleSAML_Utilities::selfURL());
+$attributes['Utilities_selfURL()'] = array(\SimpleSAML\Utils\HTTP::getSelfURL());
 
 $et = new SimpleSAML_XHTML_Template($config, 'hostnames.php');
 
diff --git a/www/authmemcookie.php b/www/authmemcookie.php
index 00cf2e656..96e318ca4 100644
--- a/www/authmemcookie.php
+++ b/www/authmemcookie.php
@@ -93,7 +93,7 @@ try {
 	$session->registerLogoutHandler($sourceId, 'SimpleSAML_AuthMemCookie', 'logoutHandler');
 
 	/* Redirect the user back to this page to signal that the login is completed. */
-	SimpleSAML_Utilities::redirectTrustedURL(SimpleSAML_Utilities::selfURL());
+	SimpleSAML_Utilities::redirectTrustedURL(\SimpleSAML\Utils\HTTP::getSelfURL());
 } catch(Exception $e) {
 	throw new SimpleSAML_Error_Error('CONFIG', $e);
 }
-- 
GitLab