/
v-change-web-domain-docroot
executable file
·137 lines (118 loc) · 5.2 KB
/
v-change-web-domain-docroot
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/bin/bash
# info: Changes the document root for an existing web domain
# options: USER DOMAIN TARGET_DOMAIN [DIRECTORY] [PHP]
#
# example: v-change-web-domain-docroot admin domain.tld otherdomain.tld
# # add custom docroot
# # points domain.tld to otherdomain.tld's document root.
#
# example: v-change-web-domain-docroot admin test.local default
# # remove custom docroot
# # returns document root to default value for domain.
#
# This call changes the document root of a chosen web domain
# to another available domain under the user context.
#----------------------------------------------------------#
# Variables & Functions #
#----------------------------------------------------------#
# Argument definition
user=$1
domain=$2
# Export target domain and directory
# so they are correctly passed through to domain.sh
export target_domain=$3
export target_directory=$4
export php=$5
restart=$6
# 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
# shellcheck source=/usr/local/hestia/func/domain.sh
source $HESTIA/func/domain.sh
# load config file
source_conf "$HESTIA/conf/hestia.conf"
# Additional argument formatting
format_domain
#----------------------------------------------------------#
# Verifications #
#----------------------------------------------------------#
check_args '2' "$#" 'USER DOMAIN [TARGET_DOMAIN] [DIRECTORY] [PHP-DOCROOT] [RESTART]'
is_system_enabled "$WEB_SYSTEM" 'WEB_SYSTEM'
# Check to ensure that target domain is valid if we're
# not setting the docroot value back to defaults
if [ "$target_domain" != "default" ]; then
is_format_valid 'user' 'domain' 'target_domain' 'restart'
is_object_valid 'web' 'DOMAIN' "$target_domain"
else
is_format_valid 'user' 'domain' 'restart'
fi
is_object_valid 'user' 'USER' "$user" "$user"
is_object_unsuspended 'user' 'USER' "$user"
is_object_valid 'web' 'DOMAIN' "$domain"
is_object_unsuspended 'web' 'DOMAIN' "$domain"
is_object_value_empty 'web' 'DOMAIN' "$domain" '$docroot'
is_dir_symlink "$HOMEDIR/$user/web"
is_dir_symlink "$HOMEDIR/$user/web/$target_domain"
# Perform verification if read-only mode is enabled
check_hestia_demo_mode
#----------------------------------------------------------#
# Action #
#----------------------------------------------------------#
# Unset existing custom document root path
if [ -n "$CUSTOM_DOCROOT" ]; then
update_object_value 'web' 'DOMAIN' "$domain" '$CUSTOM_DOCROOT' ""
update_object_value 'web' 'DOMAIN' "$domain" '$CUSTOM_PHPROOT' ""
fi
# If target domain value is 'default', remove the custom document root
# value and rebuild web domain to restore default configuration.
# Otherwise, set target document root path accordingly based on passed values.
if [ "$target_domain" = "default" ]; then
update_object_value 'web' 'DOMAIN' "$domain" '$CUSTOM_DOCROOT' ""
update_object_value 'web' 'DOMAIN' "$domain" '$CUSTOM_PHPROOT' ""
else
# Check for existence of specified directory under target domain's public_html folder
target_domain_directory="$HOMEDIR/$user/web/$target_domain"
if [ -n "$target_directory" ]; then
# Checking destination path
real_target_directory="$(readlink -e "${target_domain_directory}/public_html/$target_directory/")"
if [ ! -e "$real_target_directory" ]; then
check_result "$E_NOTEXIST" "Directory $target_directory does not exist under $HOMEDIR/$user/$target_domain/public_html/."
elif [ -z "$(echo $real_target_directory | egrep "^$target_domain_directory\b")" ]; then
check_result "$E_FORBIDEN" "Target dir outside of target domain dir"
else
CUSTOM_DOCROOT="$real_target_directory"
if [ -n "$php" ]; then
custom_phproot="${target_domain_directory}/public_html/"
else
custom_phproot="$real_target_directory"
fi
fi
else
CUSTOM_DOCROOT="${target_domain_directory}/public_html/"
custom_phproot="${target_domain_directory}/public_html/"
fi
add_object_key 'web' 'DOMAIN' "$domain" 'CUSTOM_DOCROOT' 'IP6'
add_object_key 'web' 'DOMAIN' "$domain" 'CUSTOM_PHPROOT' 'IP6'
update_object_value 'web' 'DOMAIN' "$domain" '$CUSTOM_DOCROOT' "$CUSTOM_DOCROOT"
# CUSTOM_PHPROOT got overwriten by default
CUSTOM_PHPROOT=$custom_phproot
update_object_value 'web' 'DOMAIN' "$domain" '$CUSTOM_PHPROOT' "$CUSTOM_PHPROOT"
fi
#----------------------------------------------------------#
# Hestia #
#----------------------------------------------------------#
# Rebuild domain configuration
$BIN/v-rebuild-web-domain "$user" "$domain" "$restart"
# Logging
if [ "$target_domain" = "default" ]; then
$BIN/v-log-action "$user" "Info" "Web" "Domain document root reset (Domain: $domain)."
else
$BIN/v-log-action "$user" "Info" "Web" "Domain document root updated (Domain: $domain, Target: $target_domain)."
fi
log_event "$OK" "$ARGUMENTS"
# Unset variables
unset target_domain
unset target_directory
exit