From 4a7cab0953fd0dc2ac8176f8f507214c6bbb205a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cristhian=20Mart=C3=ADnez=20Ochoa?= Date: Mon, 28 Feb 2022 14:06:18 -0700 Subject: [PATCH] external db 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. --- lib/general | 29 +++++++++++++++-------------- lib/sites | 28 +++++++++++++++++++++++----- lib/verify | 2 +- lib/webin | 2 +- usr/site | 13 ++++++++++--- 5 files changed, 50 insertions(+), 24 deletions(-) diff --git a/lib/general b/lib/general index c56cea0..0dd9910 100644 --- a/lib/general +++ b/lib/general @@ -281,11 +281,11 @@ 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 } @@ -293,7 +293,8 @@ check_for_mysql_client() { 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 @@ -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 } @@ -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 @@ -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 @@ -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 diff --git a/lib/sites b/lib/sites index cd467b5..28137a9 100644 --- a/lib/sites +++ b/lib/sites @@ -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 @@ -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}" @@ -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 @@ -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 @@ -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_ @@ -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}" diff --git a/lib/verify b/lib/verify index d63e07b..bcede20 100644 --- a/lib/verify +++ b/lib/verify @@ -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 diff --git a/lib/webin b/lib/webin index d7727eb..65379f6 100644 --- a/lib/webin +++ b/lib/webin @@ -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 diff --git a/usr/site b/usr/site index add2d65..ccd5fa4 100644 --- a/usr/site +++ b/usr/site @@ -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 @@ -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