Browse Source

subfolders

Fix - ssl update wp db for subfolders.
Some additional small code improvements.
Cristhian Martínez Ochoa 3 years ago
parent
commit
c6512bce44
2 changed files with 24 additions and 11 deletions
  1. 15 3
      lib/site-ssl
  2. 9 8
      lib/sites

+ 15 - 3
lib/site-ssl

@@ -4,6 +4,18 @@
 # Functions for SSL On/Off
 
 
+ssl_wp_db_update() {
+	# if main site is WP.
+	[[ $(is_wp $domain) == "true" ]] && edit_wp_db_url $domain $1
+	
+	# Check if site contains WP in subfolders.
+	for site in "/etc/nginx/apps.d/${domain}_"*-wpcommon.conf
+	do
+		local subwp="/$(echo $site | cut -f 2- -d "_" -s | cut -f -1 -d "-" -s | sed "s/_/\//g")"
+		[[ -n $subwp && -f /var/www/$domain/htdocs$subwp/wp-config.php ]] && edit_wp_db_url $domain ${1}${subwp} $subwp
+	done
+}
+
 ssl_nginx() {
 	sudo sed -i '/listen 80/c \	listen 443 ssl http2;' /etc/nginx/sites-available/$domain
 	sudo sed -i '/listen \[::\]:80/c \	listen [::]:443 ssl http2;' /etc/nginx/sites-available/$domain
@@ -33,7 +45,7 @@ ssl_nginx() {
 		sudo chown -R root:root $(dirname -- $ssl_crt)
 		
 		# Not duplicate: Custom cert function does not run the same way as LE requests.
-		[[ $(is_wp $domain) == "true" ]] && edit_wp_db_url $domain "https://${domain}"
+		ssl_wp_db_update "https://${domain}"
 		
 		if [[ -n $ssl_ocsp && -f $ssl_ocsp ]]; then
 			sudo sed -i "/ssl_trusted_certificate/c \	ssl_trusted_certificate $ssl_ocsp;" /etc/nginx/sites-available/$domain
@@ -170,7 +182,7 @@ site_ssl_on() {
 		[[ -z $cronmail && -n $cermail && -z $cronrene ]] && echo "MAILTO=${cermail}" | sudo tee -a /var/spool/cron/crontabs/root > /dev/null
 		[[ -z $cronrene ]] && echo '15 3 * * 7 certbot renew --post-hook "service nginx restart"' | sudo tee -a /var/spool/cron/crontabs/root > /dev/null
 
-		[[ $(is_wp $domain) == "true" ]] && edit_wp_db_url $domain "https://${domain}"
+		ssl_wp_db_update "https://${domain}"
 		
 		echo "${gre}SSL have been successfully enabled for your site${blu} $domain${end}"
 	elif [[ -f /etc/letsencrypt/live/$domain/fullchain.pem && $ssl == "force-renewal" ]]; then
@@ -246,7 +258,7 @@ site_ssl_off() {
 		echo "${gre}${dim}Tools Site updated with SSL!${end}"
 	fi
 	
-	[[ -z $no_db_update && $(is_wp $domain) == "true" ]] && edit_wp_db_url $domain "http://${domain}"
+	[[ -z $no_db_update ]] && ssl_wp_db_update "http://${domain}"
 	
 	# Check if exist to not try to revoke a cert that don't exist, for example a site with a cert added to a wildcard! (-add-to-wildcard)
 	if [[ -f /etc/letsencrypt/renewal/${domain}.conf ]]; then

+ 9 - 8
lib/sites

@@ -484,29 +484,30 @@ _EOF_
 }
 
 
-db_delete() {	
-	wp_conf_retrieve $1 true false $subfolder
+db_delete() {
+	# Example: db_delete $domain $subfolder
+	wp_conf_retrieve $1 true false $2
 	[[ -z $wp_config ]] && return
 	[[ $wp_dbhost != "localhost" ]] && check_for_mysql_client
 	
 	if [[ -z $wp_dbhost || -z $wp_dbname || -z $wp_dbuser ]]; then
-		echo "${red}[ERROR] Database${blu} ${domain}${subfolder} ${red}cannot be deleted! ${dim}(WP configuration corrupted)${end}"
+		echo "${red}[ERROR] Database${blu} ${1}${2} ${red}cannot be deleted! ${dim}(WP configuration corrupted)${end}"
 		return
 	elif [[ ( $wp_dbhost == "localhost" &&  ( $(conf_read mysql) != "true" || $(check_mysql_connection localhost) != "true" )) || ( $wp_dbhost != "localhost" && $(check_mysql_connection $extdb_url $extdb_port $extdb_user $extdb_pass) != "true" ) ]]; then
-		echo "${red}[ERROR] Database${blu} ${domain}${subfolder} ${red}cannot be deleted! ${dim}(DB Connection failed)${end}"
+		echo "${red}[ERROR] Database${blu} ${1}${2} ${red}cannot be deleted! ${dim}(DB Connection failed)${end}"
 		return
 	fi
 	
 	dbsetup="DELETE FROM mysql.user WHERE User='$wp_dbuser';DROP DATABASE IF EXISTS $wp_dbname;DELETE FROM mysql.db WHERE Db='$wp_dbname' OR Db='$wp_dbname\\_%';FLUSH PRIVILEGES;"
 	if [[ $wp_dbhost == "localhost" ]]; then
 		sudo mysql --connect-timeout=10 --user=admin -p$ADMIN_PASS -e "$dbsetup"
-		echo "${gre}${dim}Database of your site ${blu}${domain}${subfolder}${gre} has been successfully deleted! ${end}"
+		echo "${gre}${dim}Database of your site ${blu}${1}${2}${gre} has been successfully deleted! ${end}"
 	else
 		if [[ $extdb_user == "root" && -z $extdb_pass ]]; then
 			echo "${red}Action aborted, database not deleted!${end}"
 		else
 			sudo mysql --connect-timeout=10 -h "$extdb_url" -P "$extdb_port" -u"$extdb_user" -p"$extdb_pass" -e "$dbsetup"
-			echo "${gre}${dim}Database of your site ${blu}${domain}${subfolder}${gre} has been successfully deleted! ${end}"
+			echo "${gre}${dim}Database of your site ${blu}${1}${2}${gre} has been successfully deleted! ${end}"
 		fi
 	fi
 }
@@ -548,7 +549,7 @@ deletesite() {
 					  [[ $dbsubdel = [YyNn] ]] && break
 					done
 				fi
-				[[ $dbsubdel == [yY] || $dbdel == [yY] ]] && db_delete $domain
+				[[ $dbsubdel == [yY] || $dbdel == [yY] ]] && db_delete $domain $subfolder
 			fi
 			subfolder=""
 		done
@@ -619,7 +620,7 @@ deletesite_subfolder() {
 					  [[ $dbsubdel = [YyNn] ]] && break
 					done
 				fi
-				[[ $dbsubdel == [yY] || $dbdel == [yY] ]] && db_delete $domain
+				[[ $dbsubdel == [yY] || $dbdel == [yY] ]] && db_delete $domain $subfolder
 			fi
 			
 			sudo rm -rf /etc/nginx/apps.d/$domain$subnames-*.conf