From 33231d24dab942b31c924c4c8e352dd73f80939b Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Tue, 22 Sep 2015 14:15:04 +0200 Subject: [PATCH] Url: Add support macros refs #5600 --- library/Icinga/Web/Url.php | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/library/Icinga/Web/Url.php b/library/Icinga/Web/Url.php index 6f39bbab7..617ac7a4e 100644 --- a/library/Icinga/Web/Url.php +++ b/library/Icinga/Web/Url.php @@ -142,7 +142,7 @@ class Url return $urlObject; } - $urlParts = parse_url($url); + $urlParts = parse_url(static::resolveUrlMacros($url)); if (isset($urlParts['scheme']) && $urlParts['scheme'] !== $request->getScheme()) { $baseUrl = $urlParts['scheme'] . '://' . $urlParts['host'] . (isset($urlParts['port']) ? (':' . $urlParts['port']) @@ -189,6 +189,33 @@ class Url return $urlObject; } + /** + * Return the given url with all macros being resolved + * + * @param string $url + * + * @return string + */ + public static function resolveUrlMacros($url) + { + $serverName = static::getRequest()->getServer('SERVER_NAME'); + $macros = array( + 'HTTP_AUTHORITY' => 'http://' . $serverName, + 'HTTPS_AUTHORITY' => 'https://' . $serverName + ); + + if (preg_match_all('@\$([^\$\s]+)\$@', $url, $matches)) { + foreach ($matches[1] as $macroIndex => $macroName) { + if (isset($macros[$macroName])) { + $placeholder = $matches[0][$macroIndex]; + $url = str_replace($placeholder, $macros[$macroName], $url); + } + } + } + + return $url; + } + /** * Create a new filter that needs to fullfill the base filter and the optional filter (if it exists) *