Skip to content

Commit

Permalink
external db
Browse files Browse the repository at this point in the history
Fixes:
- wp replace-content not checking if wp is installed.
- some random errors when mysql not installed.
- parked con domain mapping not working with external db.
- verify mysql warning message when no installed but using external dbs.
- cloning site with external DB and overwrite not working.
- savedDB failing when mysql not installed.
- savedDB now is not encrypted in conf file.
  • Loading branch information
QROkes committed Feb 28, 2022
1 parent d2c58e7 commit 4a7cab0
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 24 deletions.
29 changes: 15 additions & 14 deletions lib/general
Original file line number Diff line number Diff line change
Expand Up @@ -281,19 +281,20 @@ check_for_mysql() {
}
check_for_mysql_client() {
if [[ $(conf_read mysql-client) != "true" ]]; then
echo "${gre}MySQL Client is not installed and we need it to stablish a connection with your external server.${end}"
echo "${dim}Wait while we install MySQL Client...${end}"
echo "${gre}${dim}MySQL Client is not installed and we need it to stablish a connection with your external server.${end}" >&2
echo "${dim}Wait while we install MySQL Client...${end}" >&2
sudo stack -mysql=client > /dev/null 2>&1 &
wait $!
echo "${gre}MySQL Client has been successfully installed!${end}"
echo "${gre}MySQL Client has been successfully installed!${end}" >&2
fi
}


check_mysql_connection() {
local query="quit"

if [[ -n $1 && ${1,,} != "localhost" && -n $2 && -n $3 && -n $4 && $(conf_read mysql-client) == "true" ]]; then
if [[ -n $1 && ${1,,} != "localhost" && -n $2 && -n $3 && -n $4 ]]; then
check_for_mysql_client
[[ -n $5 ]] && local query="use $5"
sudo mysql --connect-timeout=10 -h "$1" -P "$2" -u"$3" -p"$4" -e "$query"
if [[ $? != "0" ]]; then
Expand Down Expand Up @@ -338,7 +339,7 @@ check_mysql_connection() {

check_external_db_saved() {
if [[ -n $(conf_read external-dbh) && -n $(conf_read external-dbu) && -n $(conf_read external-dbp) && -n $(conf_read external-dbx) ]]; then
external_db="[$(conf_read external-dbu),$( echo $(conf_read external-dbp) | openssl enc -d -a -salt ),$(conf_read external-dbh):$(conf_read external-dbx)]"
external_db="[$(conf_read external-dbu),$(conf_read external-dbp),$(conf_read external-dbh):$(conf_read external-dbx)]"
[[ $(conf_read quiet) != "true" ]] && echo "${blu}${dim}External DB credentials found!${end}" >&2
fi
}
Expand All @@ -356,16 +357,14 @@ external_db_parse() {
local user=$(echo "${dbdata}" | cut -d',' -f 1 -s)
local pass=$(echo "${dbdata}" | cut -d',' -f 2 -s)
local host=$(echo "${dbdata}" | cut -d',' -f 3 -s)
[[ -z $host && -n $wp_dbhost ]] && local host=$wp_dbhost # This is very shitty!
local url=$(echo "$host" | cut -f 1 -d ':')
local port=$(echo "$host" | cut -f 2 -d ':' -s)

if [[ $(echo "${external_db}" | cut -c-1) != "[" || $(echo "${external_db}" | rev | cut -c-1) != "]" ]]; then
echo "${red}[ERROR] Invalid syntax for External Database!${end}"
return
elif [[ $1 == "-nohost" && ( -z $user || -z $pass ) ]]; then
echo "${red}[ERROR] Invalid data for External Database!${end}"
return
elif [[ $1 != "-nohost" && ( -z $user || -z $pass || -z $url || -z $port ) ]]; then
elif [[ -z $user || -z $pass ]]; then
echo "${red}[ERROR] Invalid data for External Database!${end}"
return
fi
Expand Down Expand Up @@ -530,9 +529,9 @@ wp_conf_retrieve() {
fi
if [[ -z $external_db && -n $wp_dbhost && $wp_dbhost != "localhost" ]]; then
echo "" >&2
echo "${gre}External DB${blu} '${wp_dbhost}' ${gre}found in${blu} ${1}${4} ${gre}(Press 'Enter' key twice to skip)" >&2
read -p "${blu}External DB username: " extdb_user
read -p "External DB password: " extdb_pass
echo "${gre}External DB${blu} '${wp_dbhost}' ${gre}found in:${blu}${dim} ${1}${4} ${end}" >&2
read -p "${blu}External DB username: ${end}" extdb_user
read -p "${blu}External DB password: ${end}" extdb_pass
extdb_host=$wp_dbhost
extdb_url=$wp_extdb_url
extdb_port=$wp_extdb_port
Expand Down Expand Up @@ -1060,13 +1059,15 @@ edit_wp_db_url_multisite() {
# $3 - WP BlogID to force

if [[ -n $1 && -n $2 && -n $3 && $3 =~ ^[0-9]+$ && $(is_wp_multisite $1) =~ ^(subdomain|subdirectory)$ ]]; then
wp_conf_retrieve $1 true false
wp_conf_retrieve $1 true false $subfolder

# Force WP blogID
local wp_dbpref="${wp_dbpref}${3}_"
local wp_blogid=$3
local dbsetup="SELECT * FROM information_schema.tables WHERE table_schema = '$wp_dbname' AND table_name = '${wp_dbpref}options' LIMIT 1;"
if [[ -n $(sudo mysql --connect-timeout=10 --user=admin -p$ADMIN_PASS -e "$dbsetup") ]]; then
if [[ $wp_dbhost == "localhost" && -n $(sudo mysql --connect-timeout=10 --user=admin -p$ADMIN_PASS -e "$dbsetup") ]]; then
echo "${blu}${dim}WordPress blog ID (${wp_dbpref}) found and validated in a WP Multisite Network!${end}" >&2
elif [[ $wp_dbhost != "localhost" && -n $(sudo mysql --connect-timeout=10 -h "$extdb_url" -P "$extdb_port" -u"$extdb_user" -p"$extdb_pass" -e "$dbsetup") ]]; then
echo "${blu}${dim}WordPress blog ID (${wp_dbpref}) found and validated in a WP Multisite Network!${end}" >&2
else
echo "${red}${dim}[ERROR] WordPress blog ID (${wp_dbpref}) not found!${end}" >&2
Expand Down
28 changes: 23 additions & 5 deletions lib/sites
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ wpinstall() {
if [[ -n $(conf_read external-dbh) && -n $(conf_read external-dbu) && -n $(conf_read external-dbp) ]]; then
[[ -n $(conf_read external-dbx) ]] && local dbhost_suggested="$(conf_read external-dbh):$(conf_read external-dbx)" || local dbhost_suggested=$(conf_read external-dbh)
dburoot=$(conf_read external-dbu)
dbproot=$( echo $(conf_read external-dbp) | openssl enc -d -a -salt )
dbproot=$(conf_read external-dbp)
fi

[[ $type == [2345] ]] || read -p "Database Host [${dbhost_suggested}]: " dbhost
Expand Down Expand Up @@ -1167,6 +1167,9 @@ parked_domain() {
sed -i "/include \/var\/www\/$domain/c \ ${cusconl}" /etc/nginx/sites-available/$domain
sed -i "/include \/var\/www\/$parked/a \ include \/var\/www\/${parked}/*-$(echo $domain | sed "s/[^0-9A-Za-z]/_/g")_parked.conf;" /etc/nginx/sites-available/$domain

# Prevent asking multiple times in case of external DB.
[[ -n $domain_mapping_wp_id && $domain_mapping_wp_id =~ ^[0-9]+$ && $(is_wp $parked) == "true" ]] && wp_conf_retrieve $parked true false $subfolder

# WordPress Domain Mapping
if [[ $domain_mapping_wp_id == 1 ]]; then
echo "${red}${dim}[ERROR] Domain Mapping failed because WP blog ID cannot be 1 (main site)! ${end}"
Expand Down Expand Up @@ -1556,7 +1559,20 @@ cloning_site() {
local custom_wp_dbpref=$( grep -F "table_prefix" $(wp_config_path $domain $subfolder) | cut -f 2 -d "'" -s)
fi

[[ -z $subfolder ]] && sudo site $domain -delete=force > /dev/null 2>&1 || sudo site $domain -subfolder=$subfolder -delete=force > /dev/null 2>&1
if [[ $( wp_config_read $domain DB_HOST $subfolder ) == "localhost" ]]; then
[[ -z $subfolder ]] && sudo site $domain -delete=force > /dev/null 2>&1 || sudo site $domain -subfolder=$subfolder -delete=force > /dev/null 2>&1
else
if [[ -n $extdb_user && -n $extdb_pass && -n $extdb_host ]]; then
if [[ -z $subfolder ]]; then
sudo site $domain -delete=force -external_db=[${extdb_user},${extdb_pass},${extdb_host}] > /dev/null 2>&1
else
sudo site $domain -subfolder=$subfolder -delete=force -external_db=[${extdb_user},${extdb_pass},${extdb_host}] > /dev/null 2>&1
fi
else
echo "${red}[ERROR] External DB data corrupted!${end}"
exit 1
fi
fi
fi

if [[ -n $subfolder ]]; then
Expand Down Expand Up @@ -1711,7 +1727,6 @@ wp_replace_content() {
exit 1
fi

wp_conf_retrieve $domain true true $subfolder
if [[ -n $wp_dbhost && -n $wp_dbname && -n $wp_dbpref ]]; then
if [[ ( $wp_dbhost == "localhost" && $(check_mysql_connection localhost) != "true" ) || ( $wp_dbhost != "localhost" && $(check_mysql_connection $extdb_url $extdb_port $extdb_user $extdb_pass) != "true" ) ]]; then
exit 1
Expand Down Expand Up @@ -1978,7 +1993,9 @@ wp_env_type() {
# WordPress Reading Settings: Discourage Search Engines
wp_conf_retrieve $domain true true $subfolder

if [[ -n $wp_dbhost && -n $wp_dbname && -n $wp_dbpref ]]; then
if [[ -n $wp_dbhost && $wp_dbhost != "localhost" && ( -z $extdb_url || -z $extdb_port || -z $extdb_user || -z $extdb_pass ) ]]; then
echo "${red}${dim}[ERROR] Discourage Search Engines WP option not updated! (External DB data not available)${end}"
elif [[ -n $wp_dbhost && -n $wp_dbname && -n $wp_dbpref ]]; then
if [[ $(is_wp_installed $domain $subfolder) == "true" ]]; then
if [[ $wp_dbhost == "localhost" && $(check_mysql_connection localhost) == "true" ]]; then
sudo mysql --connect-timeout=10 --user=admin -p$ADMIN_PASS <<_EOF_
Expand All @@ -2000,10 +2017,11 @@ _EOF_

[[ $index == "0" ]] && echo "${gre}Discourage Search Engines WP option has been enabled for${blu} $domain$subfolder ${gre}site!${end}"
[[ $index == "1" ]] && echo "${gre}Discourage Search Engines WP option has been disabled for${blu} $domain$subfolder ${gre}site!${end}"
else
echo "${blu}${dim}Database cannot be updated because WP is still empty! ${end}"
fi
else
echo "${red}${dim}[ERROR] Discourage Search Engines WP option not updated! (WP Configuration corrupted)${end}"
exit 1
fi

echo "${gre}WordPress Environment Type has been set successfully!${end}"
Expand Down
2 changes: 1 addition & 1 deletion lib/verify
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,7 @@ if [[ -z $critical_mode ]]; then
local ver_four_war="1"
fi

if [[ -d /etc/mysql && $(conf_read mysql) != "true" ]]; then
if [[ -d /etc/mysql && $(conf_read mysql) != "true" && $(conf_read mysql-client) != "true" ]]; then
echo "${dim}- [WARNING] Seems like some MySQL data remains but MySQL is not installed or Webinoly can not detect it!${end}${red}"
local ver_four_war="1"
fi
Expand Down
2 changes: 1 addition & 1 deletion lib/webin
Original file line number Diff line number Diff line change
Expand Up @@ -1077,7 +1077,7 @@ ext_db_save() {
fi

conf_write external-dbu $user
conf_write external-dbp $( echo $pass | openssl enc -a -salt )
conf_write external-dbp $pass

echo "${gre}External DB data successfully saved!${end}"
fi
Expand Down
13 changes: 10 additions & 3 deletions usr/site
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,8 @@ if [[ -n $wp || -n $mysql ]]; then
exth=$(conf_read external-dbh)
fi

extp=$( echo $(conf_read external-dbp) | openssl enc -d -a -salt )
[[ $wp == "true" ]] && wp="[true,true,${exth},${dona},${dona},random,wp_,$(conf_read external-dbu),${extp}]"
[[ $mysql == "true" ]] && mysql="[${exth},${dona},${dona},random,$(conf_read external-dbu),${extp}]"
[[ $wp == "true" ]] && wp="[true,true,${exth},${dona},${dona},random,wp_,$(conf_read external-dbu),$(conf_read external-dbp)]"
[[ $mysql == "true" ]] && mysql="[${exth},${dona},${dona},random,$(conf_read external-dbu),$(conf_read external-dbp)]"
echo "${blu}${dim}External DB credentials found!${end}"
fi

Expand Down Expand Up @@ -411,6 +410,14 @@ elif [[ -n $replace_content && -f /etc/nginx/sites-available/$domain ]]; then
if [[ $(is_wp $domain $subfolder) != "true" ]]; then
echo "${red}[ERROR] Site${blu} $domain$subfolder ${red}is not a WordPress site!${end}"
exit 1
fi

#We need to call this function in the parent shell to preserve variables and avoid asking two times for the same values in case of external db.
wp_conf_retrieve $domain true true $subfolder

if [[ $(is_wp_installed $domain $subfolder) != "true" ]]; then
echo "${red}[ERROR] WordPress is empty and still not configured in ${domain}${subfolder} site!${end}"
exit 1
else
wp_replace_content
fi
Expand Down

0 comments on commit 4a7cab0

Please sign in to comment.