Browse Source

Fix #3439 and #3438 (#3440)

* Fix: #3438 Do not remove port / Check if redirect ends on /

* #3439 Remove spaces in domain

* Update main.sh

Only do leading / trailings spaces
Jaap Marcus 2 years ago
parent
commit
1fb3062b3f
2 changed files with 35 additions and 2 deletions
  1. 33 2
      bin/v-add-web-domain-redirect
  2. 2 0
      func/main.sh

+ 33 - 2
bin/v-add-web-domain-redirect

@@ -42,13 +42,20 @@ is_object_unsuspended 'user' 'USER' "$user"
 is_object_valid 'web' 'DOMAIN' "$domain"
 is_object_unsuspended 'web' 'DOMAIN' "$domain"
 
+requesturi=0
 if [[ "$3" =~ http://|https:// ]]; then
 	scheme_check=1
 	scheme=$($HESTIA_PHP -r '$url=parse_url($argv[1]); echo $url["scheme"];' "$redirect")
 	host=$($HESTIA_PHP -r '$url=parse_url($argv[1]); echo $url["host"];' "$redirect")
 	path=$($HESTIA_PHP -r '$url=parse_url($argv[1]); if(!empty($url["path"])){echo $url["path"];}' "$redirect")
+	port=$($HESTIA_PHP -r '$url=parse_url($argv[1]); if(!empty($url["port"])){echo $url["port"];}' "$redirect")
+
 	host=$(idn2 --quiet "$host")
 	redirect="$scheme://$host$path"
+	if [ -n "$port" ]; then
+		redirect="$scheme://$host:$port$path"
+	fi
+
 	isValidUrl=$(php -r '$url=$argv[1]; $url=filter_var($url,FILTER_VALIDATE_URL); echo $url;' "$redirect")
 	if [ -z "$isValidUrl" ]; then
 		check_result $E_INVALID "Invalid redirect"
@@ -56,8 +63,15 @@ if [[ "$3" =~ http://|https:// ]]; then
 else
 	host=$($HESTIA_PHP -r '$url=parse_url($argv[1]); echo $url["host"];' "http://$redirect")
 	path=$($HESTIA_PHP -r '$url=parse_url($argv[1]); if(!empty($url["path"])){echo $url["path"];}' "http://$redirect")
+	port=$($HESTIA_PHP -r '$url=parse_url($argv[1]); if(!empty($url["port"])){echo $url["port"];}' "$redirect")
+
 	host=$(idn2 --quiet "$host")
+
 	redirect="$host$path"
+	if [ -n "$port" ]; then
+		redirect="$host:$port$path"
+	fi
+
 	isValidUrl=$(php -r '$url=$argv[1]; $url=filter_var($url,FILTER_VALIDATE_URL); echo $url;' "http://$redirect")
 	if [ -z "$isValidUrl" ]; then
 		check_result $E_INVALID "Invalid redirect"
@@ -77,16 +91,33 @@ if [ "$WEB_SYSTEM" = 'nginx' ] || [ "$PROXY_SYSTEM" = 'nginx' ]; then
 	sconf="$HOMEDIR/$user/conf/web/$domain/nginx.ssl.conf_redirect"
 fi
 
+# Prevent $request_uri being added if ends in .html
+requesturi=0
+if [ -n "$path" ]; then
+	lastchr=${path#${path%?}}
+	if [ "$lastchr" = "/" ]; then
+		requesturi=1
+	fi
+fi
+
 # Insert redirect commands
 if [ -n "$PROXY_SYSTEM" ] || [ "$WEB_SYSTEM" = 'nginx' ]; then
 	if [ "$scheme_check" = 1 ]; then
-		echo "   return $code $redirect\$request_uri;" > $conf
+		if [ "$requesturi" = 1 ]; then
+			echo "   return $code $redirect\$request_uri;" > $conf
+		else
+			echo "   return $code $redirect;" > $conf
+		fi
 		if [ ! -e "$sconf" ]; then
 			ln -s "$conf" "$sconf"
 		fi
 	else
 		echo "if (\$host != \"$redirect\") {" > $conf
-		echo "   return $code \$scheme://$redirect\$request_uri;" >> $conf
+		if [ "$requesturi" = 1 ]; then
+			echo "   return $code \$scheme://$redirect\$request_uri;" >> $conf
+		else
+			echo "   return $code \$scheme://$redirect;" >> $conf
+		fi
 		echo "}" >> $conf
 
 		if [ ! -e "$sconf" ]; then

+ 2 - 0
func/main.sh

@@ -1399,6 +1399,8 @@ format_domain() {
 	if [[ "$domain" =~ ^\. ]]; then
 		domain=$(echo "$domain" | sed -e "s/^[.]*//")
 	fi
+	# Remove white spaces
+	domain=$(echo $domain | sed 's/^[ \t]*//;s/[ \t]*$//')
 }
 
 format_domain_idn() {