Skip to content

Commit

Permalink
Civi::url() - Fix inconsistent generation of absolute URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
totten committed Sep 13, 2024
1 parent 119e0e7 commit 2e2559a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
19 changes: 19 additions & 0 deletions CRM/Utils/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,25 @@ public static function toRelative(string $value, ?string $currentHostPort = NULL
return $value;
}

/**
* Convert to an absolute URL (if relative).
*
* @param string $value
* @param string|null $currentHostPort
* The value of HTTP_HOST. (NULL means "lookup HTTP_HOST")
* @return string
* Either the relative version of $value (if on the same HTTP_HOST), or else
* the absolute version.
*/
public static function toAbsolute(string $value, ?string $currentHostPort = NULL): string {
if ($value[0] === '/') {
$currentHostPort = $currentHostPort ?: $_SERVER['HTTP_HOST'] ?? NULL;
$scheme = CRM_Utils_System::isSSL() ? 'https' : 'http';
return $scheme . '://' . $currentHostPort . $value;
}
return $value;
}

/**
* Parse an internal URL. Extract the CiviCRM route.
*
Expand Down
3 changes: 3 additions & 0 deletions Civi/Core/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,9 @@ public function __toString(): string {
if ($preferFormat === 'relative') {
$result = \CRM_Utils_Url::toRelative($result);
}
elseif ($preferFormat === 'absolute') {
$result = \CRM_Utils_Url::toAbsolute($result);
}

// TODO decide if the current default is good enough for future
$ssl = $this->getSsl() ?: \CRM_Utils_System::isSSL();
Expand Down

0 comments on commit 2e2559a

Please sign in to comment.