diff --git a/plogical/vhost.py b/plogical/vhost.py index bf702e588..5d2aac093 100644 --- a/plogical/vhost.py +++ b/plogical/vhost.py @@ -25,7 +25,7 @@ from managePHP.phpManager import PHPManager from plogical.vhostConfs import vhostConfs from ApachController.ApacheVhosts import ApacheVhost try: - from websiteFunctions.models import Websites, ChildDomains, aliasDomains, DockerSites + from websiteFunctions.models import Websites, ChildDomains, aliasDomains, DockerSites, WPSites, WPStaging from databases.models import Databases except: pass @@ -410,6 +410,21 @@ class vhost: if ACLManager.FindIfChild() == 0: + ### Delete WordPress Sites and Staging Sites first + try: + wpSites = WPSites.objects.filter(owner=delWebsite) + for wpSite in wpSites: + # Delete any staging sites associated with this WP site + stagingSites = WPStaging.objects.filter(wpsite=wpSite) + for staging in stagingSites: + staging.delete() + logging.CyberCPLogFileWriter.writeToFile(f"Deleted staging site record: {staging.id}") + # Delete the WP site itself + wpSite.delete() + logging.CyberCPLogFileWriter.writeToFile(f"Deleted WP site: {wpSite.id}") + except Exception as msg: + logging.CyberCPLogFileWriter.writeToFile(f"Error cleaning up WP/Staging sites: {str(msg)}") + ### Delete Docker Sites first before website deletion if os.path.exists('/home/docker/%s' % (virtualHostName)): @@ -503,6 +518,21 @@ class vhost: ## child check to make sure no database entires are being deleted from child server if ACLManager.FindIfChild() == 0: + ### Delete WordPress Sites and Staging Sites first + try: + wpSites = WPSites.objects.filter(owner=delWebsite) + for wpSite in wpSites: + # Delete any staging sites associated with this WP site + stagingSites = WPStaging.objects.filter(wpsite=wpSite) + for staging in stagingSites: + staging.delete() + logging.CyberCPLogFileWriter.writeToFile(f"Deleted staging site record: {staging.id}") + # Delete the WP site itself + wpSite.delete() + logging.CyberCPLogFileWriter.writeToFile(f"Deleted WP site: {wpSite.id}") + except Exception as msg: + logging.CyberCPLogFileWriter.writeToFile(f"Error cleaning up WP/Staging sites: {str(msg)}") + for items in databases: mysqlUtilities.deleteDatabase(items.dbName, items.dbUser) diff --git a/plogical/virtualHostUtilities.py b/plogical/virtualHostUtilities.py index 95d201d33..b0588ab39 100644 --- a/plogical/virtualHostUtilities.py +++ b/plogical/virtualHostUtilities.py @@ -33,7 +33,7 @@ from ApachController.ApacheVhosts import ApacheVhost from managePHP.phpManager import PHPManager try: - from websiteFunctions.models import Websites, ChildDomains, aliasDomains + from websiteFunctions.models import Websites, ChildDomains, aliasDomains, WPSites, WPStaging from loginSystem.models import Administrator from packages.models import Package from CLManager.models import CLPackages @@ -599,6 +599,41 @@ local_name %s { 'This website already exists as child domain. [404]') return 0, "This website already exists as child domain." + # Check for orphaned staging site domain conflicts + try: + # Check if there are any WP sites with FinalURL matching this domain + conflicting_wp_sites = WPSites.objects.filter(FinalURL__icontains=virtualHostName) + for wp_site in conflicting_wp_sites: + # Check if the WP site's owner website still exists + try: + owner_website = wp_site.owner + if not Websites.objects.filter(id=owner_website.id).exists(): + # Orphaned WP site found, clean it up + wp_site.delete() + logging.CyberCPLogFileWriter.writeToFile(f"Cleaned up orphaned WP site: {wp_site.id} with URL: {wp_site.FinalURL}") + except: + # WP site owner is missing, delete it + wp_site.delete() + logging.CyberCPLogFileWriter.writeToFile(f"Cleaned up orphaned WP site: {wp_site.id} (missing owner)") + + # Check for orphaned staging sites + orphaned_staging = WPStaging.objects.filter(wpsite__FinalURL__icontains=virtualHostName) + for staging in orphaned_staging: + try: + # Check if the staging site's wpsite still exists and has valid owner + wpsite = staging.wpsite + owner_website = wpsite.owner + if not Websites.objects.filter(id=owner_website.id).exists(): + # Owner website doesn't exist, clean up staging + staging.delete() + logging.CyberCPLogFileWriter.writeToFile(f"Cleaned up orphaned staging site: {staging.id}") + except: + # Staging site has invalid references, delete it + staging.delete() + logging.CyberCPLogFileWriter.writeToFile(f"Cleaned up orphaned staging site: {staging.id} (invalid references)") + except Exception as e: + logging.CyberCPLogFileWriter.writeToFile(f"Error during staging site cleanup: {str(e)}") + ####### Limitations Check End logging.CyberCPLogFileWriter.statusWriter(tempStatusPath, 'Creating DNS records..,10') diff --git a/websiteFunctions/website.py b/websiteFunctions/website.py index 8246f85b9..acfc5cc76 100644 --- a/websiteFunctions/website.py +++ b/websiteFunctions/website.py @@ -153,7 +153,25 @@ class WebsiteManager: WPDelete = WPSites.objects.get(pk=DeleteID) if ACLManager.checkOwnership(WPDelete.owner.domain, admin, currentACL) == 1: - WPDelete.delete() + # Check if this is a staging site (referenced by WPStaging as wpsite) + staging_records = WPStaging.objects.filter(wpsite=WPDelete) + + if staging_records.exists(): + # This is a staging site - perform complete cleanup + staging_website = WPDelete.owner + + # Use the same robust deletion method as regular websites + execPath = "/usr/local/CyberCP/bin/python " + virtualHostUtilities.cyberPanel + "/plogical/virtualHostUtilities.py" + execPath = execPath + " deleteVirtualHostConfigurations --virtualHostName " + staging_website.domain + ProcessUtilities.popenExecutioner(execPath) + + # Delete all staging records + staging_records.delete() # Delete WPStaging records + WPDelete.delete() # Delete WPSites record + staging_website.delete() # Delete Websites record + else: + # Regular WP site deletion + WPDelete.delete() except BaseException as msg: pass @@ -216,10 +234,28 @@ class WebsiteManager: if DeleteID != None: wstagingDelete = WPStaging.objects.get(pk=DeleteID, owner=WPobj) + + # Get the associated staging WPSites and Websites records + staging_wpsite = wstagingDelete.wpsite + staging_website = staging_wpsite.owner + + # Delete the staging Websites record and all associated data BEFORE deleting DB records + # Use the same robust deletion method as regular websites + execPath = "/usr/local/CyberCP/bin/python " + virtualHostUtilities.cyberPanel + "/plogical/virtualHostUtilities.py" + execPath = execPath + " deleteVirtualHostConfigurations --virtualHostName " + staging_website.domain + ProcessUtilities.popenExecutioner(execPath) + + # Delete the WPStaging record wstagingDelete.delete() + # Delete the staging WPSites record + staging_wpsite.delete() + + # Delete the staging Websites record + staging_website.delete() + except BaseException as msg: - da = str(msg) + logging.CyberCPLogFileWriter.writeToFile(f"Error cleaning up WP/Staging sites: {str(msg)}") proc = httpProc(request, 'websiteFunctions/WPsiteHome.html', Data, 'createDatabase')