Skip to content

Commit 36c3dfd

Browse files
author
Kristan Kenney
committed
Merge branch 'feature/427-redirect' into main
2 parents 70db94d + b3908ca commit 36c3dfd

File tree

8 files changed

+322
-7
lines changed

8 files changed

+322
-7
lines changed

bin/v-add-web-domain-redirect

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
#!/bin/bash
2+
# info: Adding force redirect to domain
3+
# options: USER DOMAIN REDIRECT HTTPCODE [RESTART]
4+
# labels: hestia web
5+
#
6+
# example: v-add-web-domain-redirect user domain.tld domain.tld
7+
# example: v-add-web-domain-redirect user domain.tld www.domain.tld
8+
# example: v-add-web-domain-redirect user domain.tld shop.domain.tld
9+
# example: v-add-web-domain-redirect user domain.tld different-domain.com
10+
# example: v-add-web-domain-redirect user domain.tld shop.different-domain.com
11+
# example: v-add-web-domain-redirect user domain.tld different-domain.com 302
12+
#
13+
# Function creates a forced redirect to a domain
14+
15+
16+
#----------------------------------------------------------#
17+
# Variable&Function #
18+
#----------------------------------------------------------#
19+
20+
# Argument definition
21+
user=$1
22+
domain=$2
23+
redirect=$(echo $3 | idn);
24+
code=${4-301}
25+
restart=${5-no}
26+
27+
# Includes
28+
source $HESTIA/func/main.sh
29+
source $HESTIA/conf/hestia.conf
30+
31+
#----------------------------------------------------------#
32+
# Verifications #
33+
#----------------------------------------------------------#
34+
35+
check_args '3' "$#" 'USER DOMAIN REDIRECT [HTTP-CODE] [RESTART]'
36+
is_format_valid 'user' 'domain'
37+
is_number_format_valid "$code" "code"
38+
is_object_valid 'user' 'USER' "$user"
39+
is_object_unsuspended 'user' 'USER' "$user"
40+
is_object_valid 'web' 'DOMAIN' "$domain"
41+
is_object_unsuspended 'web' 'DOMAIN' "$domain"
42+
43+
44+
scheme=0
45+
if [[ "$3" =~ http://|https:// ]]; then
46+
scheme=1
47+
regex='(https?|ftp|file)://[-A-Za-z0-9\+&@#/%?=~_|!:,.;]*[-A-Za-z0-9\+&@#/%=~_|]'
48+
if ! [[ "$3" =~ $regex ]]; then
49+
echo "Invalid redirect"
50+
exit 2;
51+
fi
52+
else
53+
regex='[-A-Za-z0-9\+&@#/%?=~_|!:,.;]*[-A-Za-z0-9\+&@#/%=~_|]'
54+
if ! [[ "$3" =~ $regex ]]; then
55+
echo "Invalid redirect"
56+
exit 2;
57+
fi
58+
fi
59+
60+
# Perform verification if read-only mode is enabled
61+
check_hestia_demo_mode
62+
63+
64+
#----------------------------------------------------------#
65+
# Action #
66+
#----------------------------------------------------------#
67+
68+
# Check if proxy is active
69+
if [ "$WEB_SYSTEM" = 'nginx' ] || [ "$PROXY_SYSTEM" = 'nginx' ]; then
70+
conf="$HOMEDIR/$user/conf/web/$domain/nginx.conf_redirect"
71+
sconf="$HOMEDIR/$user/conf/web/$domain/nginx.ssl.conf_redirect"
72+
fi
73+
# Insert redirect commands
74+
if [ ! -z "$PROXY_SYSTEM" ] || [ "$WEB_SYSTEM" = 'nginx' ]; then
75+
if [ "$scheme" = 1 ]; then
76+
echo " return $code $redirect\$request_uri;" > $conf
77+
if [ ! -e "$sconf" ]; then
78+
ln -s "$conf" "$sconf"
79+
fi
80+
else
81+
echo "if (\$host != \"$redirect\") {" > $conf
82+
echo " return $code \$scheme://$redirect\$request_uri;" >> $conf
83+
echo "}" >> $conf
84+
85+
if [ ! -e "$sconf" ]; then
86+
ln -s "$conf" "$sconf"
87+
fi
88+
fi
89+
else
90+
echo "Non supported please use .htaccess instead"
91+
exit 2;
92+
fi
93+
94+
#----------------------------------------------------------#
95+
# Hestia #
96+
#----------------------------------------------------------#
97+
98+
if [ -z "$REDIRECT" ]; then
99+
add_object_key "web" 'DOMAIN' "$domain" 'REDIRECT' 'U_DISK'
100+
add_object_key "web" 'DOMAIN' "$domain" 'REDIRECT_CODE' 'U_DISK'
101+
fi
102+
103+
update_object_value 'web' 'DOMAIN' "$domain" '$REDIRECT' "$redirect"
104+
update_object_value 'web' 'DOMAIN' "$domain" '$REDIRECT_CODE' "$code"
105+
106+
if [ "$restart" = "yes" ]; then
107+
# Restarting web server
108+
$BIN/v-restart-web $restart
109+
check_result $? "Web restart failed" >/dev/null
110+
111+
$BIN/v-restart-proxy $restart
112+
check_result $? "Proxy restart failed" >/dev/null
113+
fi
114+
115+
# Logging
116+
log_history "Enable forced redirect $domain"
117+
log_event "$OK" "$ARGUMENTS"
118+
119+
exit

bin/v-delete-web-domain-redirect

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#!/bin/bash
2+
# info: Delete force redirect to domain
3+
# options: USER DOMAIN [RESTART]
4+
# labels: hestia web
5+
#
6+
# example: v-add-web-domain-redirect user domain.tld
7+
#
8+
# Function delete a forced redirect to a domain
9+
10+
11+
#----------------------------------------------------------#
12+
# Variable&Function #
13+
#----------------------------------------------------------#
14+
15+
# Argument definition
16+
user=$1
17+
domain=$2
18+
restart=${3-no}
19+
20+
21+
# Includes
22+
source $HESTIA/func/main.sh
23+
source $HESTIA/conf/hestia.conf
24+
25+
26+
#----------------------------------------------------------#
27+
# Verifications #
28+
#----------------------------------------------------------#
29+
30+
check_args '2' "$#" 'USER DOMAIN [RESTART]'
31+
is_format_valid 'user' 'domain'
32+
is_object_valid 'user' 'USER' "$user"
33+
is_object_unsuspended 'user' 'USER' "$user"
34+
is_object_valid 'web' 'DOMAIN' "$domain"
35+
is_object_unsuspended 'web' 'DOMAIN' "$domain"
36+
37+
# Perform verification if read-only mode is enabled
38+
check_hestia_demo_mode
39+
40+
41+
#----------------------------------------------------------#
42+
# Action #
43+
#----------------------------------------------------------#
44+
45+
46+
47+
# Check if proxy is active
48+
if [ "$WEB_SYSTEM" = 'nginx' ] || [ "$PROXY_SYSTEM" = 'nginx' ]; then
49+
rm $HOMEDIR/$user/conf/web/$domain/nginx.conf_redirect
50+
rm $HOMEDIR/$user/conf/web/$domain/nginx.ssl.conf_redirect
51+
else
52+
echo "Non supported please use .htaccess instead"
53+
exit 2;
54+
fi
55+
56+
#----------------------------------------------------------#
57+
# Hestia #
58+
#----------------------------------------------------------#
59+
60+
update_object_value 'web' 'DOMAIN' "$domain" '$REDIRECT' ""
61+
update_object_value 'web' 'DOMAIN' "$domain" '$REDIRECT_CODE' ""
62+
63+
if [ "$restart" = "yes" ]; then
64+
# Restarting web server
65+
$BIN/v-restart-web $restart
66+
check_result $? "Web restart failed" >/dev/null
67+
68+
$BIN/v-restart-proxy $restart
69+
check_result $? "Proxy restart failed" >/dev/null
70+
fi
71+
72+
# Logging
73+
log_history "Enable forced redirect $domain"
74+
log_event "$OK" "$ARGUMENTS"
75+
76+
exit

bin/v-list-web-domain

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ json_list() {
4545
"PROXY": "'$PROXY'",
4646
"PROXY_EXT": "'$PROXY_EXT'",
4747
"FASTCGI_CACHE": "'$FASTCGI_CACHE'",
48+
"REDIRECT": "'$REDIRECT'",
49+
"REDIRECT_CODE": "'$REDIRECT_CODE'",
4850
"CUSTOM_DOCROOT": "'$CUSTOM_DOCROOT'",
4951
"SUSPENDED": "'$SUSPENDED'",
5052
"TIME": "'$TIME'",

web/css/src/themes/default.css

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,9 @@ input[type="radio"] {
451451
/* 1 */
452452
padding: 0;
453453
/* 2 */
454+
margin-left: 0px;
455+
margin-right: 8px;
456+
margin-top: 6px;
454457
}
455458

456459
/**
@@ -2505,7 +2508,7 @@ body.mobile .l-icon-shortcuts {
25052508
}
25062509

25072510
.input-label {
2508-
padding-top: 20px;
2511+
padding-top: 8px;
25092512
}
25102513

25112514
.data input[type="checkbox"] {

web/css/themes/default.min.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

web/edit/web/index.php

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@
9494
}
9595
}
9696

97+
$redirect_code_options = array(301,302);
98+
$v_redirect = $data[$v_domain]['REDIRECT'];
99+
$v_redirect_code = $data[$v_domain]['REDIRECT_CODE'];
100+
if ( !in_array($v_redirect, array('www.'.$v_domain, $v_domain))){
101+
$v_redirect_custom = $v_redirect;
102+
}
97103

98104
$v_ftp_user = $data[$v_domain]['FTP_USER'];
99105
$v_ftp_path = $data[$v_domain]['FTP_PATH'];
@@ -567,6 +573,8 @@
567573
check_return_code($return_var,$output);
568574
unset($output);
569575
$v_ssl_forcessl = 'yes';
576+
$restart_web = 'yes';
577+
$restart_proxy = 'yes';
570578
}
571579

572580
// Add SSL HSTS
@@ -575,22 +583,28 @@
575583
check_return_code($return_var,$output);
576584
unset($output);
577585
$v_ssl_hsts = 'yes';
586+
$restart_web = 'yes';
587+
$restart_proxy = 'yes';
578588
}
579589

580590
// Delete Force SSL
581591
if (( $v_ssl_forcessl == 'yes' ) && (empty($_POST['v_ssl_forcessl'])) && (empty($_SESSION['error_msg']))) {
582-
exec (HESTIA_CMD."v-delete-web-domain-ssl-force ".$user." ".escapeshellarg($v_domain)." yes", $output, $return_var);
592+
exec (HESTIA_CMD."v-delete-web-domain-ssl-force ".$user." ".escapeshellarg($v_domain), $output, $return_var);
583593
check_return_code($return_var,$output);
584594
unset($output);
585595
$v_ssl_forcessl = 'no';
596+
$restart_web = 'yes';
597+
$restart_proxy = 'yes';
586598
}
587599

588600
// Delete SSL HSTS
589601
if (( $v_ssl_hsts == 'yes' ) && (empty($_POST['v_ssl_hsts'])) && (empty($_SESSION['error_msg']))) {
590-
exec (HESTIA_CMD."v-delete-web-domain-ssl-hsts ".$user." ".escapeshellarg($v_domain)." yes", $output, $return_var);
602+
exec (HESTIA_CMD."v-delete-web-domain-ssl-hsts ".$user." ".escapeshellarg($v_domain), $output, $return_var);
591603
check_return_code($return_var,$output);
592604
unset($output);
593605
$v_ssl_hsts = 'no';
606+
$restart_web = 'yes';
607+
$restart_proxy = 'yes';
594608
}
595609

596610
// Delete web stats
@@ -816,7 +830,9 @@
816830
exec(HESTIA_CMD."v-change-web-domain-docroot ".$v_username." ".escapeshellarg($v_domain)." default", $output, $return_var);
817831
check_return_code($return_var,$output);
818832
unset($output);
819-
unset($_POST['v-custom-doc-domain'], $_POST['v-custom-doc-folder']);
833+
unset($_POST['v-custom-doc-domain'], $_POST['v-custom-doc-folder']);
834+
$restart_web = 'yes';
835+
$restart_proxy = 'yes';
820836
}
821837

822838
if ( !empty($_POST['v-custom-doc-domain']) && !empty($_POST['v_custom_doc_root_check']) && $v_custom_doc_root_prepath.$v_custom_doc_domain.'/public_html'.$v_custom_doc_folder != $v_custom_doc_root){
@@ -832,11 +848,51 @@
832848
check_return_code($return_var,$output);
833849
unset($output);
834850
$v_custom_doc_root = 1;
851+
835852
}
853+
$restart_web = 'yes';
854+
$restart_proxy = 'yes';
836855
}else{
837856
unset($v_custom_doc_root);
838857
}
858+
859+
if ( !empty($v_redirect) && empty($_POST['v-redirect-checkbox']) ) {
860+
exec(HESTIA_CMD."v-delete-web-domain-redirect ".$v_username." ".escapeshellarg($v_domain), $output, $return_var);
861+
check_return_code($return_var,$output);
862+
unset($output);
863+
unset($_POST['v-redirect']);
864+
$restart_web = 'yes';
865+
$restart_proxy = 'yes';
866+
}
867+
868+
if (!empty($_POST['v-redirect']) && !empty($_POST['v-redirect-checkbox']) ){
869+
if (empty($v_redirect)){
870+
if ($_POST['v-redirect'] == 'custom' && empty($_POST['v-redirect-custom'])){
871+
}else{
872+
if($_POST['v-redirect'] == 'custom'){
873+
$_POST['v-redirect'] = $_POST['v-redirect-custom'];
874+
}
875+
exec(HESTIA_CMD."v-add-web-domain-redirect ".$v_username." ".escapeshellarg($v_domain)." ".escapeshellarg($_POST['v-redirect'])." ".escapeshellarg($_POST['v-redirect-code']), $output, $return_var);
876+
check_return_code($return_var,$output);
877+
unset($output);
878+
$restart_web = 'yes';
879+
$restart_proxy = 'yes';
880+
}
839881

882+
}else {
883+
if ($_POST['v-redirect'] == 'custom') {
884+
$_POST['v-redirect'] = $_POST['v-redirect-custom'];
885+
}
886+
if ( $_POST['v-redirect'] != $v_redirect || $_POST['v-redirect-code'] != $v_redirect_code ) {
887+
exec(HESTIA_CMD."v-add-web-domain-redirect ".$v_username." ".escapeshellarg($v_domain)." ".escapeshellarg($_POST['v-redirect'])." ".escapeshellarg($_POST['v-redirect-code']), $output, $return_var);
888+
check_return_code($return_var,$output);
889+
unset($output);
890+
$restart_web = 'yes';
891+
$restart_proxy = 'yes';
892+
}
893+
}
894+
895+
}
840896
// Restart web server
841897
if (!empty($restart_web) && (empty($_SESSION['error_msg']))) {
842898
exec (HESTIA_CMD."v-restart-web", $output, $return_var);

web/js/pages/edit_web.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ App.Actions.WEB.update_custom_doc_root = function(elm, hint) {
22
var prepath = $('input[name="v-custom-doc-root_prepath"]').val();
33
var domain = $('select[name="v-custom-doc-domain"]').val();
44
var folder = $('input[name="v-custom-doc-folder"]').val();
5-
console.log(domain, folder);
5+
66
$('.custom_docroot_hint').html(prepath+domain+'/public_html/'+folder);
77
}
88
App.Listeners.DB.keypress_custom_folder = function() {
@@ -294,3 +294,12 @@ function elementHideShow(elementToHideOrShow){
294294
var el = document.getElementById(elementToHideOrShow);
295295
el.style.display = el.style.display === 'none' ? 'block' : 'none';
296296
}
297+
298+
$('.v-redirect-custom-value').change( function(){
299+
300+
if(this.value == "custom"){
301+
$('#custom_redirect').show();
302+
}else{
303+
$('#custom_redirect').hide();
304+
}
305+
})

0 commit comments

Comments
 (0)