From 2e3d1114b173b635f28c328e72e7ecdd874e4685 Mon Sep 17 00:00:00 2001 From: Daniel Dimitrov Date: Sun, 3 Jun 2012 21:00:14 +0200 Subject: [PATCH] improving the extract method - now we can extract all archives within an archive --- .../com_jedchecker/controllers/uploads.php | 76 +++++++++++++++---- 1 file changed, 62 insertions(+), 14 deletions(-) diff --git a/source/administrator/components/com_jedchecker/controllers/uploads.php b/source/administrator/components/com_jedchecker/controllers/uploads.php index e1fca81..aeac545 100644 --- a/source/administrator/components/com_jedchecker/controllers/uploads.php +++ b/source/administrator/components/com_jedchecker/controllers/uploads.php @@ -14,8 +14,11 @@ jimport('joomla.filesystem.archive'); class jedcheckerControllerUploads extends JController { - public function __construct() { + public function __construct() + { $this->path = JPATH_COMPONENT_ADMINISTRATOR . '/tmp/'; + $this->pathArchive = $this->path . 'arhives/'; + $this->pathUnzipped = $this->path . 'unzipped/'; parent::__construct(); } @@ -23,24 +26,27 @@ class jedcheckerControllerUploads extends JController * basic upload * @return bool */ - public function upload() { + public function upload() + { $file = JRequest::getVar('extension', '', 'files', 'array'); - if($file['tmp_name']) { - $path = JPATH_COMPONENT_ADMINISTRATOR . '/tmp/'; + if ($file['tmp_name']) { + $path = $this->pathArchive; $filepath = $path . strtolower($file['name']); -// let us remove all previous uplaods - $folders = JFolder::folders($path); - foreach($folders as $folder) { - JFolder::delete($folder); +// let us remove all previous uploads + $archiveFiles = JFolder::files($path); + + foreach ($archiveFiles as $archive) { + if (!JFile::delete($this->pathArchive . $archive)) { + echo 'could not delete' . $archive; + } } $object_file = new JObject($file); $object_file->filepath = $filepath; - $file = (array) $object_file; + $file = (array)$object_file; // let us try to upload - if (!JFile::upload($file['tmp_name'], $file['filepath'])) - { + if (!JFile::upload($file['tmp_name'], $file['filepath'])) { // Error in upload JError::raiseWarning(100, JText::_('COM_JEDCHECKER_ERROR_UNABLE_TO_UPLOAD_FILE')); return false; @@ -56,12 +62,54 @@ class jedcheckerControllerUploads extends JController * unzip the file * @return bool */ - public function unzip() { + public function unzip() + { - $file = JFolder::files($this->path); + // let us remove all previous unzipped files + $folders = JFolder::folders($this->pathUnzipped); + foreach ($folders as $folder) { + JFolder::delete($this->pathUnzipped . $folder); + } - $result = JArchive::extract($this->path.$file[0], $this->path); + + $file = JFolder::files($this->pathArchive); + $result = JArchive::extract($this->pathArchive . $file[0], $this->pathUnzipped . $file[0]); + + if ($result) { + // scan unzipped folders if we find zip file -> unzip them as well + $this->unzipAll($this->pathUnzipped . $file[0]); + } return $result; } + + /** + * Recursively go through each folder and extract the archives + * + * @param $start + */ + public function unzipAll($start) + { + $iterator = new RecursiveDirectoryIterator($start); + + foreach ($iterator as $file) { + if ($file->isFile()) { + $extension = $file->getExtension(); + if ($extension == 'zip') { + $unzip = $file->getPath() . '/' . $file->getBasename('.' . $extension); + $result = JArchive::extract($file->getPathname(), $unzip); +// delete the archive once we extract it + if ($result) { + JFile::delete($file->getPathname()); + +// now check the new extracted folder for archive files + $this->unzipAll($unzip); + } + } + } else { + $this->unzipAll($file->getPathname()); + + } + } + } }