Add LSurl::public_url() method and globally use it to retrieve a public URL

This commit is contained in:
Benjamin Renard 2024-08-22 18:43:17 +02:00
parent f4bdd430b2
commit efecc67af0
Signed by: bn8
GPG key ID: 3E2E1CE1907115BC
2 changed files with 35 additions and 31 deletions

View file

@ -198,11 +198,7 @@ class LStemplate extends LSlog_staticLoggerClass {
self :: registerFunction("var_dump", "LStemplate_smarty_var_dump");
// Define public root URL
$public_root_url = LSconfig :: get('public_root_url', '/', 'string');
// Remove trailing slash
if (substr($public_root_url, -1) == '/')
$public_root_url = substr($public_root_url, 0, -1);
self :: assign('public_root_url', $public_root_url);
self :: assign('public_root_url', LSsession :: loadLSclass("LSurl") ? LSurl :: public_url() : null);
// Trigger started event
self :: fireEvent('started');

View file

@ -181,6 +181,30 @@ class LSurl extends LSlog_staticLoggerClass {
return False;
}
/**
* Get public URL
* @param bool $absolute Set to true to obtain an absolute URL (optional, default: false)
* @param string|null $relative_url Specify a relative URL to decline as its absolute form
* (optional, default: root URL without trailing slash)
* @return string The public URL
*/
public static function public_url($absolute=false, $relative_url=null) {
$public_root_url = LSconfig :: get('public_root_url', '/', 'string');
if ($absolute && $public_root_url[0] == '/') {
self :: log_debug("LSurl :: public_root_url(absolute=true): configured public root URL is relative ($public_root_url) => try to detect it from current request infos.");
$public_root_url = 'http'.(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on'?'s':'').'://'.$_SERVER['HTTP_HOST'].$public_root_url;
self :: log_debug("LSurl :: public_root_url(absolute=true): detected public absolute root URL: $public_root_url");
}
if ($relative_url) {
if ($public_root_url[0] == '/') $public_root_url .= "/";
return $public_root_url.$relative_url;
}
return self :: remove_trailing_slash($public_root_url);
}
/**
* Get the public absolute URL
*
@ -189,18 +213,10 @@ class LSurl extends LSlog_staticLoggerClass {
* @return string The public absolute URL
**/
public static function get_public_absolute_url($relative_url=null) {
if (!is_string($relative_url))
$relative_url = self :: get_current_url();
$public_root_url = LSconfig :: get('public_root_url', '/', 'string');
if ($public_root_url[0] == '/') {
self :: log_debug("LSurl :: get_public_absolute_url($relative_url): configured public root URL is relative ($public_root_url) => try to detect it from current request infos.");
$public_root_url = 'http'.(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on'?'s':'').'://'.$_SERVER['HTTP_HOST'].$public_root_url;
self :: log_debug("LSurl :: get_public_absolute_url($relative_url): detected public_root_url: $public_root_url");
}
$url = self :: remove_trailing_slash($public_root_url)."/$relative_url";
$url = self :: public_url(
true,
is_string($relative_url)?$relative_url:self :: get_current_url()
);
self :: log_debug("LSurl :: get_public_absolute_url($relative_url): result = $url");
return $url;
}
@ -213,20 +229,12 @@ class LSurl extends LSlog_staticLoggerClass {
* @return void
**/
public static function redirect($go=false) {
$public_root_url = LSconfig :: get('public_root_url', '/', 'string');
if ($go===false)
$go = "";
if (preg_match('#^(https?:)?//#',$go)) {
$url = $go;
}
else {
// Check $public_root_url end
if (substr($public_root_url, -1)=='/') {
$public_root_url=substr($public_root_url, 0, -1);
}
$url="$public_root_url/$go";
}
if ($go===false) $go = "";
$url = (
preg_match('#^(https?:)?//#', $go)?
$go:
self :: public_url(false, $go)
);
// Prevent loop
if (isset($_SESSION['last_redirect']) && $_SESSION['last_redirect'] == $url)