From fb16f4cf56c712fd0136d038d2df3c21198c1621 Mon Sep 17 00:00:00 2001 From: usmannasir Date: Mon, 22 Sep 2025 13:31:20 +0500 Subject: [PATCH 1/5] bug fix: staging site issue --- plogical/vhost.py | 32 ++++++++++++++++++++++++++- plogical/virtualHostUtilities.py | 37 +++++++++++++++++++++++++++++++- websiteFunctions/website.py | 13 +++++++++++ 3 files changed, 80 insertions(+), 2 deletions(-) diff --git a/plogical/vhost.py b/plogical/vhost.py index a3f5df68e..990e9d7f9 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 @@ -404,6 +404,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)): @@ -497,6 +512,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 f0142db77..041c640d1 100644 --- a/plogical/virtualHostUtilities.py +++ b/plogical/virtualHostUtilities.py @@ -32,7 +32,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 @@ -598,6 +598,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 eb9e60848..e04fe0a8d 100644 --- a/websiteFunctions/website.py +++ b/websiteFunctions/website.py @@ -216,8 +216,21 @@ 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 WPStaging record first wstagingDelete.delete() + # Delete the staging WPSites record + staging_wpsite.delete() + + # Delete the staging Websites record and all associated data + from plogical.vhost import vhost + vhost.deleteVirtualHostConfigurations(staging_website.domain, staging_website.adminEmail, staging_website.package.packageName) + except BaseException as msg: da = str(msg) From ecd44c9d6a58dfe3b249acbeddc65912247277bd Mon Sep 17 00:00:00 2001 From: usmannasir Date: Mon, 22 Sep 2025 14:08:51 +0500 Subject: [PATCH 2/5] bug fix: staging site issue --- websiteFunctions/website.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/websiteFunctions/website.py b/websiteFunctions/website.py index e04fe0a8d..3009305ae 100644 --- a/websiteFunctions/website.py +++ b/websiteFunctions/website.py @@ -221,15 +221,18 @@ class WebsiteManager: staging_wpsite = wstagingDelete.wpsite staging_website = staging_wpsite.owner - # Delete the WPStaging record first + # Delete the staging Websites record and all associated data BEFORE deleting DB records + from plogical.vhost import vhost + vhost.deleteVirtualHostConfigurations(staging_website.domain) + + # Delete the WPStaging record wstagingDelete.delete() # Delete the staging WPSites record staging_wpsite.delete() - # Delete the staging Websites record and all associated data - from plogical.vhost import vhost - vhost.deleteVirtualHostConfigurations(staging_website.domain, staging_website.adminEmail, staging_website.package.packageName) + # Delete the staging Websites record + staging_website.delete() except BaseException as msg: da = str(msg) From f48e7286dfa03877d251ac769267d6a27ab5d0ab Mon Sep 17 00:00:00 2001 From: usmannasir Date: Mon, 22 Sep 2025 16:57:35 +0500 Subject: [PATCH 3/5] bug fix: staging site issue --- websiteFunctions/website.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/websiteFunctions/website.py b/websiteFunctions/website.py index 3009305ae..751b52571 100644 --- a/websiteFunctions/website.py +++ b/websiteFunctions/website.py @@ -223,7 +223,7 @@ class WebsiteManager: # Delete the staging Websites record and all associated data BEFORE deleting DB records from plogical.vhost import vhost - vhost.deleteVirtualHostConfigurations(staging_website.domain) + vhost.deleteVirtualHostConfigurations(staging_website.owner.domain) # Delete the WPStaging record wstagingDelete.delete() @@ -235,7 +235,7 @@ class WebsiteManager: 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') From 503c464e484b8746bdf1a78c510c31106c30e0a2 Mon Sep 17 00:00:00 2001 From: usmannasir Date: Mon, 22 Sep 2025 17:46:10 +0500 Subject: [PATCH 4/5] bug fix: staging site issue --- websiteFunctions/website.py | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/websiteFunctions/website.py b/websiteFunctions/website.py index 751b52571..78a1555bc 100644 --- a/websiteFunctions/website.py +++ b/websiteFunctions/website.py @@ -153,7 +153,24 @@ 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 + + # Delete virtual host configurations before deleting records + from plogical.vhost import vhost + vhost.deleteVirtualHostConfigurations(staging_website.domain) + + # 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 @@ -223,7 +240,7 @@ class WebsiteManager: # Delete the staging Websites record and all associated data BEFORE deleting DB records from plogical.vhost import vhost - vhost.deleteVirtualHostConfigurations(staging_website.owner.domain) + vhost.deleteVirtualHostConfigurations(staging_website.domain) # Delete the WPStaging record wstagingDelete.delete() From 4408ca04c8798966f679d1a1b8254b0f6d660aeb Mon Sep 17 00:00:00 2001 From: usmannasir Date: Mon, 22 Sep 2025 18:16:05 +0500 Subject: [PATCH 5/5] bug fix: staging site issue --- websiteFunctions/website.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/websiteFunctions/website.py b/websiteFunctions/website.py index 78a1555bc..c82e5d70b 100644 --- a/websiteFunctions/website.py +++ b/websiteFunctions/website.py @@ -160,9 +160,10 @@ class WebsiteManager: # This is a staging site - perform complete cleanup staging_website = WPDelete.owner - # Delete virtual host configurations before deleting records - from plogical.vhost import vhost - vhost.deleteVirtualHostConfigurations(staging_website.domain) + # 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 @@ -239,8 +240,10 @@ class WebsiteManager: staging_website = staging_wpsite.owner # Delete the staging Websites record and all associated data BEFORE deleting DB records - from plogical.vhost import vhost - vhost.deleteVirtualHostConfigurations(staging_website.domain) + # 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()