#!/bin/bash
# info: Adding force redirect to domain
# options: USER DOMAIN REDIRECT HTTPCODE [RESTART]
#
# example: v-add-web-domain-redirect user domain.tld domain.tld
# example: v-add-web-domain-redirect user domain.tld www.domain.tld
# example: v-add-web-domain-redirect user domain.tld shop.domain.tld
# example: v-add-web-domain-redirect user domain.tld different-domain.com
# example: v-add-web-domain-redirect user domain.tld shop.different-domain.com
# example: v-add-web-domain-redirect user domain.tld different-domain.com 302
#
# Function creates a forced redirect to a domain

#----------------------------------------------------------#
#                Variables & Functions                     #
#----------------------------------------------------------#

# Argument definition
user=$1
domain=$2
redirect=$3
code=${4-301}
restart=${5-no}

# Includes
# shellcheck source=/etc/hestiacp/hestia.conf
source /etc/hestiacp/hestia.conf
# shellcheck source=/usr/local/hestia/func/main.sh
source $HESTIA/func/main.sh
# load config file
source_conf "$HESTIA/conf/hestia.conf"

#----------------------------------------------------------#
#                    Verifications                         #
#----------------------------------------------------------#

check_args '3' "$#" 'USER DOMAIN REDIRECT [HTTP-CODE] [RESTART]'
is_format_valid 'user' 'domain'
is_number_format_valid "$code" "code"
is_object_valid 'user' 'USER' "$user"
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"
	fi
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"
	fi
fi

# Perform verification if read-only mode is enabled
check_hestia_demo_mode

#----------------------------------------------------------#
#                       Action                             #
#----------------------------------------------------------#

# Check if proxy is active
if [ "$WEB_SYSTEM" = 'nginx' ] || [ "$PROXY_SYSTEM" = 'nginx' ]; then
	conf="$HOMEDIR/$user/conf/web/$domain/nginx.conf_redirect"
	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
		redirect=${redirect:0:-1}
	fi
else
	requesturi=1
fi

# Insert redirect commands
if [ -n "$PROXY_SYSTEM" ] || [ "$WEB_SYSTEM" = 'nginx' ]; then
	if [ "$scheme_check" = 1 ]; then
		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
		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
			ln -s "$conf" "$sconf"
		fi
	fi
else
	echo "Non supported please use .htaccess instead"
	exit 2
fi

#----------------------------------------------------------#
#                       Hestia                             #
#----------------------------------------------------------#

# Update object keys
if [ -z "$REDIRECT" ]; then
	add_object_key "web" 'DOMAIN' "$domain" 'REDIRECT' 'U_DISK'
	add_object_key "web" 'DOMAIN' "$domain" 'REDIRECT_CODE' 'U_DISK'
fi

# Update values for domain
update_object_value 'web' 'DOMAIN' "$domain" '$REDIRECT' "$redirect"
update_object_value 'web' 'DOMAIN' "$domain" '$REDIRECT_CODE' "$code"

if [ "$restart" = "yes" ]; then
	# Restarting web server
	$BIN/v-restart-web "$restart"
	check_result $? "Web restart failed" > /dev/null

	$BIN/v-restart-proxy "$restart"
	check_result $? "Proxy restart failed" > /dev/null
fi

# Logging
$BIN/v-log-action "$user" "Info" "Web" "Domain redirection enabled (Domain: $domain, Redirect to: $redirect)."
log_event "$OK" "$ARGUMENTS"

exit