protected function _initFileHelper() { $this->_fileHelper = new HCMS_File_Helper($this->_application, $this->getInvokeArg('bootstrap')->getOption('fileserver')); Zend_Registry::set('fileHelper', $this->_fileHelper); $this->_filePaths = $this->_fileHelper->getPath(""); $this->view->fileWebRoot = $this->_filePaths['web']; }
/** * Unzip file * * @param string $path * @return boolean */ public function unzip($path, $newDir = false) { $filePath = $this->getRealPath($path); if (!file_exists($filePath)) { $this->returnError('File does not exists ' . $path); return false; } // get the absolute path to $file $dir = pathinfo($filePath, PATHINFO_DIRNAME); $packageName = pathinfo($filePath, PATHINFO_FILENAME); if ($newDir) { $dir .= DIRECTORY_SEPARATOR . $packageName; } $zip = new ZipArchive(); $res = $zip->open($filePath); if (!$res) { $this->returnError('Cannot extract file'); return false; } $tmpDir = APPLICATION_PATH . '/../tmp/' . session_id() . '-' . $packageName; $zip->extractTo($tmpDir); $zip->close(); //check size $extractSize = $this->_helper->getDirSize($tmpDir); if ($extractSize > $this->_helper->getFreeSpace()) { $this->_rrmdir($tmpDir); $this->returnError('Not enough space on disk'); return false; } $files = array(); $this->_rlistFiles($tmpDir, $files); $mimeValidator = new Zend_Validate_File_MimeType($this->_helper->getDefaultMimeTypes()); $extValidator = new Zend_Validate_File_Extension($this->_helper->getDefaultExtensions()); foreach ($files as $currFile) { if (!$extValidator->isValid($currFile)) { $this->_rrmdir($tmpDir); $this->returnError(sprintf('File [%s] has invalid extension.', basename($currFile))); return false; } if (!$mimeValidator->isValid($currFile)) { $this->_rrmdir($tmpDir); $this->returnError(sprintf('File [%s] has invalid mime type.', basename($currFile))); return false; } } $this->_rcopy($tmpDir, $dir); $this->_rrmdir($tmpDir); return true; }