diff --git a/lib/SimpleSAML/Utilities.php b/lib/SimpleSAML/Utilities.php index 4bad8c29acbb5c9c1667cbfae29783fd8b0bba55..d7b57bf47410fbea4b31ce183dc4410fd20d421e 100644 --- a/lib/SimpleSAML/Utilities.php +++ b/lib/SimpleSAML/Utilities.php @@ -551,82 +551,10 @@ class SimpleSAML_Utilities { /** - * Resolve a (possibly) relative URL relative to a given base URL. - * - * 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. + * @deprecated This method will be removed in SSP 2.0. Please use SimpleSAML\Utils\HTTP::resolveURL() instead. */ public static function resolveURL($url, $base = NULL) { - if($base === NULL) { - $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; + return \SimpleSAML\Utils\HTTP::resolveURL($url, $base); } diff --git a/lib/SimpleSAML/Utils/HTTP.php b/lib/SimpleSAML/Utils/HTTP.php index 17340a0b8e8d21faf79b94db258a387bb760c312..d39e519516ef877605637cc03c55496a0b6012f9 100644 --- a/lib/SimpleSAML/Utils/HTTP.php +++ b/lib/SimpleSAML/Utils/HTTP.php @@ -515,4 +515,91 @@ class HTTP 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 diff --git a/modules/discopower/templates/disco-tpl.php b/modules/discopower/templates/disco-tpl.php index 8e6c0c5e48ec81c1f1103c353a9d7f8299c92c69..ef1ec7d52420956826e300c8e3882d911e565f0a 100644 --- a/modules/discopower/templates/disco-tpl.php +++ b/modules/discopower/templates/disco-tpl.php @@ -63,7 +63,7 @@ function showEntry($t, $metadata, $favourite = FALSE) { $html .= '' . htmlspecialchars(getTranslatedName($t, $metadata)) . ''; 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) . '" />'; } diff --git a/templates/selectidp-links.php b/templates/selectidp-links.php index c513838fb6d5c41014d9c957fd75a24a830c8230..54d6696f08ec5fa1cd42e8d7fbf2154b39c70a6d 100644 --- a/templates/selectidp-links.php +++ b/templates/selectidp-links.php @@ -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').'" />'; 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 "\n" . ' <h3 style="margin-top: 8px">' . htmlspecialchars($this->t('idpname_' . $idpentry['entityid'])) . '</h3>'; @@ -65,7 +65,7 @@ foreach ($this->data['idplist'] AS $idpentry) { if ($idpentry['entityid'] != $this->data['preferredidp']) { 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 "\n" . ' <h3 style="margin-top: 8px">' . htmlspecialchars($this->t('idpname_' . $idpentry['entityid'])) . '</h3>';