From c8d3aa25171ff1eb079ce83492d03c4262ae16dc Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Mon, 21 Sep 2015 13:10:35 +0200 Subject: [PATCH] Url: Support external urls in fromPath() refs #5600 --- library/Icinga/Web/Url.php | 55 +++++++++++++++++++++++++++++++++----- 1 file changed, 49 insertions(+), 6 deletions(-) diff --git a/library/Icinga/Web/Url.php b/library/Icinga/Web/Url.php index 6d61e60a1..3a07c9034 100644 --- a/library/Icinga/Web/Url.php +++ b/library/Icinga/Web/Url.php @@ -13,12 +13,17 @@ use Icinga\Data\Filter\Filter; * returns Urls reflecting all changes made to the url and to the parameters. * * Direct instantiation is prohibited and should be done either with @see Url::fromRequest() or - * @see Url::fromUrlString() - * - * Currently, protocol, host and port are ignored and will be implemented when required + * @see Url::fromPath() */ class Url { + /** + * Whether this url points to an external resource + * + * @var bool + */ + protected $external; + /** * An array of all parameters stored in this Url * @@ -132,8 +137,6 @@ class Url } $urlObject = new Url(); - $baseUrl = $request->getBaseUrl(); - $urlObject->setBaseUrl($baseUrl); if ($url === '#') { $urlObject->setPath($url); @@ -141,8 +144,25 @@ class Url } $urlParts = parse_url($url); + if (isset($urlParts['scheme']) && $urlParts['scheme'] !== $request->getScheme()) { + $baseUrl = $urlParts['scheme'] . '://' . $urlParts['host'] . (isset($urlParts['port']) + ? (':' . $urlParts['port']) + : ''); + $urlObject->setIsExternal(); + } elseif ( + (isset($urlParts['host]']) && $urlParts['host'] !== $request->getServer('SERVER_NAME')) + || (isset($urlParts['port']) && $urlParts['port'] != $request->getServer('SERVER_PORT')) + ) { + $baseUrl = $urlParts['host'] . (isset($urlParts['port']) ? (':' . $urlParts['port']) : ''); + $urlObject->setIsExternal(); + } else { + $baseUrl = $request->getBaseUrl(); + } + + $urlObject->setBaseUrl($baseUrl); + if (isset($urlParts['path'])) { - if ($baseUrl !== '' && strpos($urlParts['path'], $baseUrl) === 0) { + if ($baseUrl && !$urlObject->isExternal() && strpos($urlParts['path'], $baseUrl) === 0) { $urlObject->setPath(substr($urlParts['path'], strlen($baseUrl))); } else { $urlObject->setPath($urlParts['path']); @@ -236,6 +256,29 @@ class Url return $this->path; } + /** + * Set whether this url points to an external resource + * + * @param bool $state + * + * @return $this + */ + public function setIsExternal($state = true) + { + $this->external = (bool) $state; + return $this; + } + + /** + * Return whether this url points to an external resource + * + * @return bool + */ + public function isExternal() + { + return $this->external; + } + /** * Return the relative url with query parameters as a string *