From de5b4ffca74c7de2b8715d8ad78781d676a527a2 Mon Sep 17 00:00:00 2001 From: dgrammatiko Date: Fri, 3 Dec 2021 21:58:59 +0100 Subject: [PATCH] Prevent disaster --- administrator/language/en-GB/lib_joomla.ini | 1 + language/en-GB/lib_joomla.ini | 1 + .../src/Installer/Adapter/TemplateAdapter.php | 21 ++++++++++++++++++- 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/administrator/language/en-GB/lib_joomla.ini b/administrator/language/en-GB/lib_joomla.ini index 5f718b1de0c14..4cebf64ed4ef4 100644 --- a/administrator/language/en-GB/lib_joomla.ini +++ b/administrator/language/en-GB/lib_joomla.ini @@ -630,6 +630,7 @@ JLIB_INSTALLER_ERROR_TPL_REFRESH_MANIFEST_CACHE="Template Refresh manifest cache JLIB_INSTALLER_ERROR_TPL_UNINSTALL_ERRORUNKOWNEXTENSION="Template Uninstall: Unknown Extension." JLIB_INSTALLER_ERROR_TPL_UNINSTALL_INVALID_CLIENT="Template Uninstall: Invalid client." JLIB_INSTALLER_ERROR_TPL_UNINSTALL_INVALID_NOTFOUND_MANIFEST="Template Uninstall: Manifest file invalid or not found." +JLIB_INSTALLER_ERROR_TPL_UNINSTALL_PARENT_TEMPLATE="Template Uninstall: Can't remove parent template. Please remove all children templates first." JLIB_INSTALLER_ERROR_TPL_UNINSTALL_TEMPLATE_DEFAULT="Template Uninstall: Can't remove default template." JLIB_INSTALLER_ERROR_TPL_UNINSTALL_TEMPLATE_DIRECTORY="Template Uninstall: Folder does not exist, can't remove files." JLIB_INSTALLER_ERROR_TPL_UNINSTALL_TEMPLATE_ID_EMPTY="Template Uninstall: Template ID is empty, can't uninstall files." diff --git a/language/en-GB/lib_joomla.ini b/language/en-GB/lib_joomla.ini index ef9676294b321..8cee7dc26824c 100644 --- a/language/en-GB/lib_joomla.ini +++ b/language/en-GB/lib_joomla.ini @@ -629,6 +629,7 @@ JLIB_INSTALLER_ERROR_TPL_REFRESH_MANIFEST_CACHE="Template Refresh manifest cache JLIB_INSTALLER_ERROR_TPL_UNINSTALL_ERRORUNKOWNEXTENSION="Template Uninstall: Unknown Extension." JLIB_INSTALLER_ERROR_TPL_UNINSTALL_INVALID_CLIENT="Template Uninstall: Invalid client." JLIB_INSTALLER_ERROR_TPL_UNINSTALL_INVALID_NOTFOUND_MANIFEST="Template Uninstall: Manifest file invalid or not found." +JLIB_INSTALLER_ERROR_TPL_UNINSTALL_PARENT_TEMPLATE="Template Uninstall: Can't remove parent template. Please remove all children templates first." JLIB_INSTALLER_ERROR_TPL_UNINSTALL_TEMPLATE_DEFAULT="Template Uninstall: Can't remove default template." JLIB_INSTALLER_ERROR_TPL_UNINSTALL_TEMPLATE_DIRECTORY="Template Uninstall: Folder does not exist, can't remove files." JLIB_INSTALLER_ERROR_TPL_UNINSTALL_TEMPLATE_ID_EMPTY="Template Uninstall: Template ID is empty, can't uninstall files." diff --git a/libraries/src/Installer/Adapter/TemplateAdapter.php b/libraries/src/Installer/Adapter/TemplateAdapter.php index ddda178f92282..5ca13ddf54190 100644 --- a/libraries/src/Installer/Adapter/TemplateAdapter.php +++ b/libraries/src/Installer/Adapter/TemplateAdapter.php @@ -464,6 +464,7 @@ protected function setupUninstall() { $this->parent->extension = $this->extension; + $db = $this->parent->getDbo(); $name = $this->extension->element; $clientId = $this->extension->client_id; @@ -473,8 +474,26 @@ protected function setupUninstall() throw new \RuntimeException(Text::_('JLIB_INSTALLER_ERROR_TPL_UNINSTALL_TEMPLATE_ID_EMPTY')); } + // Deny removing a parent template if there are children + $query = $db->getQuery(true) + ->select('COUNT(*)') + ->from($db->quoteName('#__template_styles')) + ->where( + [ + $db->quoteName('parent') . ' = :template', + $db->quoteName('client_id') . ' = :client_id', + ] + ) + ->bind(':template', $name) + ->bind(':client_id', $clientId); + $db->setQuery($query); + + if ($db->loadResult() != 0) + { + throw new \RuntimeException(Text::_('JLIB_INSTALLER_ERROR_TPL_UNINSTALL_PARENT_TEMPLATE')); + } + // Deny remove default template - $db = $this->parent->getDbo(); $query = $db->getQuery(true) ->select('COUNT(*)') ->from($db->quoteName('#__template_styles'))