/** * Extract a ZIP compressed file to a given path * @param string $archive Path to ZIP archive to extract * @param string $destination Path to extract archive into * @return boolean True if successful */ public static function extractZip($archive, $destination) { if (!function_exists('zip_open')) { throw new Exception("ZIP extension is not enabled on this installation of PHP. Recompile your installation of PHP with --enable-zip parameter."); } // echo "Extracting archive " . $archive . " to " . $destination. "<br>"; $zip = zip_open($archive); if (is_resource($zip)) { // Create the destination folder if (!mkdir($destination)) { self::$strLastError = "Unable to create extraction destination folder " . $destination; return false; } // Read files in the archive $createdFolders = array(); while ($file = zip_read($zip)) { if (is_resource($file)) { if (zip_entry_open($zip, $file, "r")) { if (substr(zip_entry_name($file), strlen(zip_entry_name($file)) - 1) != "/") { // echo zip_entry_name($file) . "<br>"; $folderStack = explode("/", zip_entry_name($file)); if (sizeof($folderStack) > 1) { for ($i = 0; $i < sizeof($folderStack) - 1; $i++) { $arraySubsection = array_slice($folderStack, 0, $i + 1); $item = implode("/", $arraySubsection); if (!in_array($item, $createdFolders)) { // echo "- Creating folder: " . $destination . $item . "<br>"; $createdFolders[] = $item; mkdir($destination . $item); } } } $strSectionToAppend = zip_entry_read($file, zip_entry_filesize($file)); $strSavePath = $destination . zip_entry_name($file); QFile::writeFile($strSavePath, $strSectionToAppend); zip_entry_close($file); } } else { self::$strLastError = "Unable to read zip entry"; return false; } } else { self::$strLastError = self::zipFileErrMsg($file); return false; } } zip_close($zip); } else { self::$strLastError = self::zipFileErrMsg($zip); return false; } return true; }
public static function installPluginFromZip($strFileName) { $entropy = substr(md5(uniqid()), 0, 6); $expandedDir = __INCLUDES__ . self::PLUGIN_EXTRACTION_DIR . $entropy . '/'; $extractionResult = QArchive::extractZip($strFileName, $expandedDir); if (!$extractionResult) { self::$strLastError = QArchive::getLastError(); return null; } // Check to see if plugin config is defined as a PHP file, // not XML file - and if so, run the PHP file to generate an XML config // file. if (file_exists($expandedDir . self::PLUGIN_CONFIG_GENERATION_FILE)) { // we'll need this constant to know where to save the XML config file global $__PLUGIN_FILES_DIR__; $__PLUGIN_FILES_DIR__ = $expandedDir; // execute the configuration file from the plugin - it will create a plugin // config file in the XML format which we will process include $expandedDir . self::PLUGIN_CONFIG_GENERATION_FILE; } return $entropy; }