From 2db657c1e6a2148d8fe7b8bb0777990ba6acd280 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Fri, 21 Jul 2023 17:17:01 -0700 Subject: [PATCH] (REF) Extract method CRM_Utils_Url::toRelative() --- CRM/Utils/Url.php | 14 ++++++++++++++ Civi/Core/Paths.php | 5 +---- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/CRM/Utils/Url.php b/CRM/Utils/Url.php index 51340c3f72..5a40beb5a0 100644 --- a/CRM/Utils/Url.php +++ b/CRM/Utils/Url.php @@ -36,4 +36,18 @@ class CRM_Utils_Url { return $parsed->__toString(); } + /** + * Convert to a relative URL (if host/port matches). + * + * @param string $value + * @return string + */ + public static function toRelative(string $value): string { + $parsed = parse_url($value); + if (isset($_SERVER['HTTP_HOST']) && isset($parsed['host']) && $_SERVER['HTTP_HOST'] == $parsed['host']) { + $value = $parsed['path']; + } + return $value; + } + } diff --git a/Civi/Core/Paths.php b/Civi/Core/Paths.php index 478141eb12..910353c1ac 100644 --- a/Civi/Core/Paths.php +++ b/Civi/Core/Paths.php @@ -256,10 +256,7 @@ class Paths { $value = rtrim($this->getVariable($defaultContainer, 'url'), '/') . ($isDot ? '' : "/$value"); if ($preferFormat === 'relative') { - $parsed = parse_url($value); - if (isset($_SERVER['HTTP_HOST']) && isset($parsed['host']) && $_SERVER['HTTP_HOST'] == $parsed['host']) { - $value = $parsed['path']; - } + $value = \CRM_Utils_Url::toRelative($value); } if ($ssl || ($ssl === NULL && \CRM_Utils_System::isSSL())) { -- 2.25.1