jreklund / php4dvd

php4dvd is an open source php/mysql powered movie database. Catalog your video collection with ease. Automatic update of information and images.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Wrong baseurl when running behind proxy

tmakinen opened this issue · comments

I have setup where php4dvd is running behing proxy. Looks like baseurl is set according to last hop so all assets (js, css, etc) are loaded from wrong URL. If i'm reading code correctly problem is here:

https://github.com/jreklund/php4dvd/blob/master/common.inc.php#L77

This should first check if header HTTP_X_FORWARDED_HOST is set and use that and only fallback to $_SERVER["HTTP_HOST"] if header is not set.

That value should be statically set by the user instead of automatically trying to figure out the right one. Or it's better to just use relative URI and stop comparing it to the domain. 🤔

Will figure something out.

Please check if this modified code works on your setup.

$baseurl = "/";
$Website->assign("baseurl", $baseurl);

// Current URL
$currentUrl = $baseurl . ltrim($_SERVER["REQUEST_URI"], "/");
$Website->assign("currentUrl", $currentUrl);

// Webroot
$basepath = $settings["url"]["base"];
if(strlen($basepath) > 0 && !preg_match("/\/$/", $basepath)) {
	$basepath .= "/";
}
$webroot = $baseurl . ltrim($basepath, "/");
$Website->assign("webroot", $webroot);

I also removed "base url" and "force the use of HTTPS" parts from this file as well.

Yes this fixes errors. Just in case here is diff that i used:

--- common.inc.php.orig	2023-08-14 16:49:52.064423051 +0000
+++ common.inc.php	2023-08-14 16:50:33.635497024 +0000
@@ -53,32 +53,16 @@
 	}
 }
 
-// Base url
-$protocol = "http";
-if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS']) {
-	$protocol = "https";
-}
-if(isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https') {
-	$protocol = "https";
-}
-
-// Force the use of HTTPS
-if(
-	isset($settings["url"]["HTTPS"]) && $settings["url"]["HTTPS"] && $protocol !== 'https'
-) {
-	header('Location: https://'. $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"], true, 301);
-	exit();
-}
 
 // Load smarty template parser
 require_once($loc . "lib/Website.class.php");
 $Website = new Website($settings);
 
-$baseurl = $protocol . "://" . $_SERVER["HTTP_HOST"];
+$baseurl = "/";
 $Website->assign("baseurl", $baseurl);
 
 // Current URL
-$currentUrl = $baseurl . $_SERVER["REQUEST_URI"];
+$currentUrl = $baseurl . ltrim($_SERVER["REQUEST_URI"], "/");
 $Website->assign("currentUrl", $currentUrl);
 
 // Webroot
@@ -86,7 +70,7 @@
 if(strlen($basepath) > 0 && !preg_match("/\/$/", $basepath)) {
 	$basepath .= "/";
 }
-$webroot = $baseurl . "/" . $basepath;
+$webroot = $baseurl . ltrim($basepath, "/");
 $Website->assign("webroot", $webroot);
 
 // Include util functions