From efecc67af07f8e02df3c8f59ae984a29109627a0 Mon Sep 17 00:00:00 2001 From: Benjamin Renard Date: Thu, 22 Aug 2024 18:43:17 +0200 Subject: [PATCH] Add LSurl::public_url() method and globally use it to retrieve a public URL --- src/includes/class/class.LStemplate.php | 6 +-- src/includes/class/class.LSurl.php | 60 ++++++++++++++----------- 2 files changed, 35 insertions(+), 31 deletions(-) diff --git a/src/includes/class/class.LStemplate.php b/src/includes/class/class.LStemplate.php index 67836cdd..100512c1 100644 --- a/src/includes/class/class.LStemplate.php +++ b/src/includes/class/class.LStemplate.php @@ -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'); diff --git a/src/includes/class/class.LSurl.php b/src/includes/class/class.LSurl.php index 0c03e614..7ba64e82 100644 --- a/src/includes/class/class.LSurl.php +++ b/src/includes/class/class.LSurl.php @@ -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)