From 281452b0314550298f0dbf7351757c384bb7bd02 Mon Sep 17 00:00:00 2001 From: Abhishek Kumar Date: Fri, 11 Aug 2023 11:41:55 +0530 Subject: [PATCH 1/3] Fix invalid zip file issue during module upload --- controllers/admin/AdminModulesController.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/controllers/admin/AdminModulesController.php b/controllers/admin/AdminModulesController.php index 0eae6b275..b54542f0c 100644 --- a/controllers/admin/AdminModulesController.php +++ b/controllers/admin/AdminModulesController.php @@ -1700,6 +1700,9 @@ class AdminModulesControllerCore extends AdminController 'author' => $module->author, 'image' => $module->image ? $module->image : '' ); + } else { + $this->errors[] = $this->l('The uploaded file does not contain a valid module or zip file name is incorrect.'); + $response['errors'] = $this->errors; } } else { $response['errors'] = $this->errors; From 504575d1a489e4a2996fcd09df04c4ccef58abe6 Mon Sep 17 00:00:00 2001 From: Abhishek Kumar Date: Fri, 13 Oct 2023 19:18:14 +0530 Subject: [PATCH 2/3] Fix module validation on upload --- controllers/admin/AdminModulesController.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/controllers/admin/AdminModulesController.php b/controllers/admin/AdminModulesController.php index b54542f0c..669f76b65 100644 --- a/controllers/admin/AdminModulesController.php +++ b/controllers/admin/AdminModulesController.php @@ -59,6 +59,8 @@ class AdminModulesControllerCore extends AdminController protected $iso_default_country; protected $filter_configuration = array(); + protected $uploaded_module_name = ''; + /** * Admin Modules Controller Constructor * Init list modules categories @@ -444,6 +446,8 @@ class AdminModulesControllerCore extends AdminController @unlink($file); $this->recursiveDeleteOnDisk($tmp_folder); + $this->uploaded_module_name = $folder; + if ($success && $redirect) { Tools::redirectAdmin(self::$currentIndex.'&conf=8&anchor='.ucfirst($folder).'&token='.$this->token); } @@ -1679,9 +1683,9 @@ class AdminModulesControllerCore extends AdminController $response = array('success' => false); $this->postProcessDownload(false); if (!count($this->errors)) { - $filename = $_FILES['file']['name']; - $filename = explode('.', $filename); - if ($module = Module::getInstanceByName($filename[0])) { + if ($this->uploaded_module_name + && $module = Module::getInstanceByName($this->uploaded_module_name) + ) { $response['success'] = true; if (Module::isInstalled($module->name)) { $response['msg'] = $this->l('module already installed, update if available'); From d16a49545e1f575f9a50e729911e4f18cb402e3b Mon Sep 17 00:00:00 2001 From: Abhishek Kumar Date: Fri, 24 Nov 2023 20:29:18 +0530 Subject: [PATCH 3/3] Refactor --- controllers/admin/AdminModulesController.php | 22 +++++++++----------- 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/controllers/admin/AdminModulesController.php b/controllers/admin/AdminModulesController.php index 669f76b65..c324075c9 100644 --- a/controllers/admin/AdminModulesController.php +++ b/controllers/admin/AdminModulesController.php @@ -59,8 +59,6 @@ class AdminModulesControllerCore extends AdminController protected $iso_default_country; protected $filter_configuration = array(); - protected $uploaded_module_name = ''; - /** * Admin Modules Controller Constructor * Init list modules categories @@ -446,10 +444,12 @@ class AdminModulesControllerCore extends AdminController @unlink($file); $this->recursiveDeleteOnDisk($tmp_folder); - $this->uploaded_module_name = $folder; - - if ($success && $redirect) { - Tools::redirectAdmin(self::$currentIndex.'&conf=8&anchor='.ucfirst($folder).'&token='.$this->token); + if ($success) { + if ($redirect) { + Tools::redirectAdmin(self::$currentIndex.'&conf=8&anchor='.ucfirst($folder).'&token='.$this->token); + } else { + $success = $folder; + } } return $success; @@ -614,7 +614,7 @@ class AdminModulesControllerCore extends AdminController } elseif (!move_uploaded_file($_FILES['file']['tmp_name'], _PS_MODULE_DIR_.$_FILES['file']['name'])) { $this->errors[] = Tools::displayError('An error occurred while copying the archive to the module directory.'); } else { - $this->extractArchive(_PS_MODULE_DIR_.$_FILES['file']['name'], $redirect); + return $this->extractArchive(_PS_MODULE_DIR_.$_FILES['file']['name'], $redirect); } } else { $this->errors[] = Tools::displayError('You do not have permission to add this.'); @@ -1681,11 +1681,9 @@ class AdminModulesControllerCore extends AdminController public function ajaxProcessUploadModule() { $response = array('success' => false); - $this->postProcessDownload(false); + $folder = $this->postProcessDownload(false); if (!count($this->errors)) { - if ($this->uploaded_module_name - && $module = Module::getInstanceByName($this->uploaded_module_name) - ) { + if ($folder && $module = Module::getInstanceByName($folder)) { $response['success'] = true; if (Module::isInstalled($module->name)) { $response['msg'] = $this->l('module already installed, update if available'); @@ -1705,7 +1703,7 @@ class AdminModulesControllerCore extends AdminController 'image' => $module->image ? $module->image : '' ); } else { - $this->errors[] = $this->l('The uploaded file does not contain a valid module or zip file name is incorrect.'); + $this->errors[] = $this->l('The uploaded file does not contain a valid module.'); $response['errors'] = $this->errors; } } else {