Skip to content
Snippets Groups Projects
Commit 712c56b9 authored by Jaime Perez Crespo's avatar Jaime Perez Crespo
Browse files

Move SimpleSAML_Utilities::resolveURL() to SimpleSAML\Utils\HTTP::resolveURL()...

Move SimpleSAML_Utilities::resolveURL() to SimpleSAML\Utils\HTTP::resolveURL() and deprecate the former.
parent a5af4077
No related branches found
No related tags found
No related merge requests found
...@@ -551,82 +551,10 @@ class SimpleSAML_Utilities { ...@@ -551,82 +551,10 @@ class SimpleSAML_Utilities {
/** /**
* Resolve a (possibly) relative URL relative to a given base URL. * @deprecated This method will be removed in SSP 2.0. Please use SimpleSAML\Utils\HTTP::resolveURL() instead.
*
* This function supports these forms of relative URLs:
* ^\w+: Absolute URL
* ^// Same protocol.
* ^/ Same protocol and host.
* ^? Same protocol, host and path, replace query string & fragment
* ^# Same protocol, host, path and query, replace fragment
* The rest: Relative to the base path.
*
* @param $url The relative URL.
* @param $base The base URL. Defaults to the base URL of this installation of simpleSAMLphp.
* @return An absolute URL for the given relative URL.
*/ */
public static function resolveURL($url, $base = NULL) { public static function resolveURL($url, $base = NULL) {
if($base === NULL) { return \SimpleSAML\Utils\HTTP::resolveURL($url, $base);
$base = self::getBaseURL();
}
if(!preg_match('/^((((\w+:)\/\/[^\/]+)(\/[^?#]*))(?:\?[^#]*)?)(?:#.*)?/', $base, $baseParsed)) {
throw new Exception('Unable to parse base url: ' . $base);
}
$baseDir = dirname($baseParsed[5] . 'filename');
$baseScheme = $baseParsed[4];
$baseHost = $baseParsed[3];
$basePath = $baseParsed[2];
$baseQuery = $baseParsed[1];
if(preg_match('$^\w+:$', $url)) {
return $url;
}
if(substr($url, 0, 2) === '//') {
return $baseScheme . $url;
}
$firstChar = substr($url, 0, 1);
if($firstChar === '/') {
return $baseHost . $url;
}
if($firstChar === '?') {
return $basePath . $url;
}
if($firstChar === '#') {
return $baseQuery . $url;
}
/* We have a relative path. Remove query string/fragment and save it as $tail. */
$queryPos = strpos($url, '?');
$fragmentPos = strpos($url, '#');
if($queryPos !== FALSE || $fragmentPos !== FALSE) {
if($queryPos === FALSE) {
$tailPos = $fragmentPos;
} elseif($fragmentPos === FALSE) {
$tailPos = $queryPos;
} elseif($queryPos < $fragmentPos) {
$tailPos = $queryPos;
} else {
$tailPos = $fragmentPos;
}
$tail = substr($url, $tailPos);
$dir = substr($url, 0, $tailPos);
} else {
$dir = $url;
$tail = '';
}
$dir = self::resolvePath($dir, $baseDir);
return $baseHost . $dir . $tail;
} }
......
...@@ -515,4 +515,91 @@ class HTTP ...@@ -515,4 +515,91 @@ class HTTP
return $ret; return $ret;
} }
/**
* Resolve a (possibly relative) URL relative to a given base URL.
*
* This function supports these forms of relative URLs:
* - ^\w+: Absolute URL. E.g. "http://www.example.com:port/path?query#fragment".
* - ^// Same protocol. E.g. "//www.example.com:port/path?query#fragment".
* - ^/ Same protocol and host. E.g. "/path?query#fragment".
* - ^? Same protocol, host and path, replace query string & fragment. E.g. "?query#fragment".
* - ^# Same protocol, host, path and query, replace fragment. E.g. "#fragment".
* - The rest: Relative to the base path.
*
* @param string $url The relative URL.
* @param string $base The base URL. Defaults to the base URL of this installation of SimpleSAMLphp.
*
* @return string An absolute URL for the given relative URL.
* @throws \SimpleSAML_Error_Exception If the base URL cannot be parsed into a valid URL, or the given parameters
* are not strings.
*
* @author Olav Morken, UNINETT AS <olav.morken@uninett.no>
* @author Jaime Perez, UNINETT AS <jaime.perez@uninett.no>
*/
public static function resolveURL($url, $base = null)
{
if ($base === null) {
$base = self::getBaseURL();
}
if (!is_string($url) || !is_string($base)) {
throw new \SimpleSAML_Error_Exception('Invalid input parameters.');
}
if (!preg_match('/^((((\w+:)\/\/[^\/]+)(\/[^?#]*))(?:\?[^#]*)?)(?:#.*)?/', $base, $baseParsed)) {
throw new \SimpleSAML_Error_Exception('Unable to parse base url: '.$base);
}
$baseDir = dirname($baseParsed[5].'filename');
$baseScheme = $baseParsed[4];
$baseHost = $baseParsed[3];
$basePath = $baseParsed[2];
$baseQuery = $baseParsed[1];
if (preg_match('$^\w+:$', $url)) {
return $url;
}
if (substr($url, 0, 2) === '//') {
return $baseScheme.$url;
}
$firstChar = substr($url, 0, 1);
if ($firstChar === '/') {
return $baseHost.$url;
}
if ($firstChar === '?') {
return $basePath.$url;
}
if ($firstChar === '#') {
return $baseQuery.$url;
}
// we have a relative path. Remove query string/fragment and save it as $tail
$queryPos = strpos($url, '?');
$fragmentPos = strpos($url, '#');
if ($queryPos !== false || $fragmentPos !== false) {
if ($queryPos === false) {
$tailPos = $fragmentPos;
} elseif ($fragmentPos === false) {
$tailPos = $queryPos;
} elseif ($queryPos < $fragmentPos) {
$tailPos = $queryPos;
} else {
$tailPos = $fragmentPos;
}
$tail = substr($url, $tailPos);
$dir = substr($url, 0, $tailPos);
} else {
$dir = $url;
$tail = '';
}
$dir = self::resolvePath($dir, $baseDir);
return $baseHost.$dir.$tail;
}
} }
\ No newline at end of file
...@@ -63,7 +63,7 @@ function showEntry($t, $metadata, $favourite = FALSE) { ...@@ -63,7 +63,7 @@ function showEntry($t, $metadata, $favourite = FALSE) {
$html .= '' . htmlspecialchars(getTranslatedName($t, $metadata)) . ''; $html .= '' . htmlspecialchars(getTranslatedName($t, $metadata)) . '';
if(array_key_exists('icon', $metadata) && $metadata['icon'] !== NULL) { if(array_key_exists('icon', $metadata) && $metadata['icon'] !== NULL) {
$iconUrl = SimpleSAML_Utilities::resolveURL($metadata['icon']); $iconUrl = \SimpleSAML\Utils\HTTP::resolveURL($metadata['icon']);
$html .= '<img alt="Icon for identity provider" class="entryicon" src="' . htmlspecialchars($iconUrl) . '" />'; $html .= '<img alt="Icon for identity provider" class="entryicon" src="' . htmlspecialchars($iconUrl) . '" />';
} }
......
...@@ -46,7 +46,7 @@ foreach ($this->data['idplist'] AS $idpentry) { ...@@ -46,7 +46,7 @@ foreach ($this->data['idplist'] AS $idpentry) {
echo ' <img src="/' . $this->data['baseurlpath'] .'resources/icons/experience/gtk-about.64x64.png" class="float-r" alt="'.$this->t('icon_prefered_idp').'" />'; echo ' <img src="/' . $this->data['baseurlpath'] .'resources/icons/experience/gtk-about.64x64.png" class="float-r" alt="'.$this->t('icon_prefered_idp').'" />';
if(array_key_exists('icon', $idpentry) && $idpentry['icon'] !== NULL) { if(array_key_exists('icon', $idpentry) && $idpentry['icon'] !== NULL) {
$iconUrl = SimpleSAML_Utilities::resolveURL($idpentry['icon']); $iconUrl = \SimpleSAML\Utils\HTTP::resolveURL($idpentry['icon']);
echo '<img class="float-l" style="margin: 1em; padding: 3px; border: 1px solid #999" src="' . htmlspecialchars($iconUrl) . '" />'; echo '<img class="float-l" style="margin: 1em; padding: 3px; border: 1px solid #999" src="' . htmlspecialchars($iconUrl) . '" />';
} }
echo "\n" . ' <h3 style="margin-top: 8px">' . htmlspecialchars($this->t('idpname_' . $idpentry['entityid'])) . '</h3>'; echo "\n" . ' <h3 style="margin-top: 8px">' . htmlspecialchars($this->t('idpname_' . $idpentry['entityid'])) . '</h3>';
...@@ -65,7 +65,7 @@ foreach ($this->data['idplist'] AS $idpentry) { ...@@ -65,7 +65,7 @@ foreach ($this->data['idplist'] AS $idpentry) {
if ($idpentry['entityid'] != $this->data['preferredidp']) { if ($idpentry['entityid'] != $this->data['preferredidp']) {
if(array_key_exists('icon', $idpentry) && $idpentry['icon'] !== NULL) { if(array_key_exists('icon', $idpentry) && $idpentry['icon'] !== NULL) {
$iconUrl = SimpleSAML_Utilities::resolveURL($idpentry['icon']); $iconUrl = \SimpleSAML\Utils\HTTP::resolveURL($idpentry['icon']);
echo '<img class="float-l" style="clear: both; margin: 1em; padding: 3px; border: 1px solid #999" src="' . htmlspecialchars($iconUrl) . '" />'; echo '<img class="float-l" style="clear: both; margin: 1em; padding: 3px; border: 1px solid #999" src="' . htmlspecialchars($iconUrl) . '" />';
} }
echo "\n" . ' <h3 style="margin-top: 8px">' . htmlspecialchars($this->t('idpname_' . $idpentry['entityid'])) . '</h3>'; echo "\n" . ' <h3 style="margin-top: 8px">' . htmlspecialchars($this->t('idpname_' . $idpentry['entityid'])) . '</h3>';
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment