/** * Upgrade process. * * @param string $sZipFile * @param string $sZipDigests * @param string $sZipRoot * @param string $sRoot * @param string $sRootDigests * @throws Exception */ public function performUpgrade($sZipFile, $sZipDigests, $sZipRoot, $sRoot, $sRootDigests) { if (!is_readable($sZipFile)) { throw new Exception(__('Archive not found.')); } if (!is_readable($sRootDigests)) { @unlink($sZipFile); throw new Exception(__('Unable to read current digests file.')); } $oZip = new fileUnzip($sZipFile); if (!$oZip->hasFile($sZipDigests)) { @unlink($sZipFile); throw new Exception(__('Downloaded file does not seem to be a valid archive.')); } # force /install dir foreach ($oZip->getFilesList() as $sFile) { $sFile = str_replace($sZipRoot . '/', '', $sFile); if (substr($sFile, 0, 7) == 'install') { $this->setForcedFile($sFile); } } $opts = FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES; $cur_digests = file($sRootDigests, $opts); $new_digests = explode("\n", $oZip->unzip($sZipDigests)); $aNewFiles = self::getNewFiles($cur_digests, $new_digests); if (!empty($this->aForcedFiles)) { $aNewFiles = array_merge($aNewFiles, $this->aForcedFiles); } $aZipFiles = array(); $aNotWritable = array(); foreach ($aNewFiles as $file) { if (!$file) { continue; } if (!$oZip->hasFile($sZipRoot . '/' . $file)) { @unlink($sZipFile); throw new Exception(__('c_a_update_incomplete_archive')); } $sDest = $sDest_dir = $sRoot . '/' . $file; while (!is_dir($sDest_dir = dirname($sDest_dir))) { } if (file_exists($sDest) && !is_writable($sDest) || !file_exists($sDest) && !is_writable($sDest_dir)) { $aNotWritable[] = $file; continue; } $aZipFiles[] = $file; } # If only one file is not writable, stop everything now if (!empty($aNotWritable)) { $e = new Exception('Some files are not writable', self::ERR_FILES_UNWRITALBE); $e->bad_files = $aNotWritable; throw $e; } # Everything's fine, we can write files, then do it now $can_touch = function_exists('touch'); foreach ($aZipFiles as $file) { $oZip->unzip($sZipRoot . '/' . $file, $sRoot . '/' . $file); if ($can_touch) { @touch($sRoot . '/' . $file); } } @unlink($sZipFile); }
public static function installPackage($zip_file, dcModules &$modules) { $zip = new fileUnzip($zip_file); $zip->getList(false, '#(^|/)(__MACOSX|\\.svn|\\.hg|\\.git|\\.DS_Store|\\.directory|Thumbs\\.db)(/|$)#'); $zip_root_dir = $zip->getRootDir(); $define = ''; if ($zip_root_dir != false) { $target = dirname($zip_file); $destination = $target . '/' . $zip_root_dir; $define = $zip_root_dir . '/_define.php'; $has_define = $zip->hasFile($define); } else { $target = dirname($zip_file) . '/' . preg_replace('/\\.([^.]+)$/', '', basename($zip_file)); $destination = $target; $define = '_define.php'; $has_define = $zip->hasFile($define); } if ($zip->isEmpty()) { $zip->close(); unlink($zip_file); throw new Exception(__('Empty module zip file.')); } if (!$has_define) { $zip->close(); unlink($zip_file); throw new Exception(__('The zip file does not appear to be a valid Dotclear module.')); } $ret_code = 1; if (!is_dir($destination)) { try { files::makeDir($destination, true); $sandbox = clone $modules; $zip->unzip($define, $target . '/_define.php'); $sandbox->resetModulesList(); $sandbox->requireDefine($target, basename($destination)); unlink($target . '/_define.php'); $new_errors = $sandbox->getErrors(); if (!empty($new_errors)) { $new_errors = is_array($new_errors) ? implode(" \n", $new_errors) : $new_errors; throw new Exception($new_errors); } files::deltree($destination); } catch (Exception $e) { $zip->close(); unlink($zip_file); files::deltree($destination); throw new Exception($e->getMessage()); } } else { # test for update $sandbox = clone $modules; $zip->unzip($define, $target . '/_define.php'); $sandbox->resetModulesList(); $sandbox->requireDefine($target, basename($destination)); unlink($target . '/_define.php'); $new_modules = $sandbox->getModules(); if (!empty($new_modules)) { $tmp = array_keys($new_modules); $id = $tmp[0]; $cur_module = $modules->getModules($id); if (!empty($cur_module) && (defined('DC_DEV') && DC_DEV === true || dcUtils::versionsCompare($new_modules[$id]['version'], $cur_module['version'], '>', true))) { # delete old module if (!files::deltree($destination)) { throw new Exception(__('An error occurred during module deletion.')); } $ret_code = 2; } else { $zip->close(); unlink($zip_file); throw new Exception(sprintf(__('Unable to upgrade "%s". (older or same version)'), basename($destination))); } } else { $zip->close(); unlink($zip_file); throw new Exception(sprintf(__('Unable to read new _define.php file'))); } } $zip->unzipAll($target); $zip->close(); unlink($zip_file); return $ret_code; }
/** * Install a module from a zip file * * @param string $zip_file * @param oktModules $modules */ public static function installPackage($zip_file, oktModules $modules) { $zip = new fileUnzip($zip_file); $zip->getList(false, '#(^|/)(__MACOSX|\\.svn|\\.DS_Store|Thumbs\\.db)(/|$)#'); $zip_root_dir = $zip->getRootDir(); if ($zip_root_dir !== false) { $target = dirname($zip_file); $destination = $target . '/' . $zip_root_dir; $define = $zip_root_dir . '/_define.php'; $has_define = $zip->hasFile($define); } else { $target = dirname($zip_file) . '/' . preg_replace('/\\.([^.]+)$/', '', basename($zip_file)); $destination = $target; $define = '_define.php'; $has_define = $zip->hasFile($define); } if ($zip->isEmpty()) { $zip->close(); unlink($zip_file); throw new Exception(__('Empty module zip file.')); } if (!$has_define) { $zip->close(); unlink($zip_file); throw new Exception(__('The zip file does not appear to be a valid module.')); } $ret_code = 1; if (is_dir($destination)) { copy($target . '/_define.php', $target . '/_define.php.bak'); # test for update $sandbox = clone $modules; $zip->unzip($define, $target . '/_define.php'); $sandbox->resetCompleteList(); $sandbox->requireDefine($target, basename($destination)); unlink($target . '/_define.php'); $new_modules = $sandbox->getCompleteList(); $old_modules = $modules->getModulesFromFileSystem(); $modules->disableModule(basename($destination)); $modules->generateCacheList(); if (!empty($new_modules)) { $tmp = array_keys($new_modules); $id = $tmp[0]; $cur_module = $old_modules[$id]; if (!empty($cur_module) && $new_modules[$id]['version'] != $cur_module['version']) { # delete old module if (!files::deltree($destination)) { throw new Exception(__('An error occurred during module deletion.')); } $ret_code = 2; } else { $zip->close(); unlink($zip_file); if (file_exists($target . '/_define.php.bak')) { rename($target . '/_define.php.bak', $target . '/_define.php'); } throw new Exception(sprintf(__('Unable to upgrade "%s". (same version)'), basename($destination))); } } else { $zip->close(); unlink($zip_file); if (file_exists($target . '/_define.php.bak')) { rename($target . '/_define.php.bak', $target . '/_define.php'); } throw new Exception(sprintf(__('Unable to read new _define.php file'))); } } $zip->unzipAll($target); $zip->close(); unlink($zip_file); return $ret_code; }
$sTempDir = $okt->galleries->upload_dir . '/temp/'; files::makeDir($sTempDir, true); $sZipFile = $sTempDir . $_FILES['p_zip_file']['name']; if (!move_uploaded_file($_FILES['p_zip_file']['tmp_name'], $sZipFile)) { throw new Exception(__('m_galleries_zip_error_unable_move_uploaded_file')); } $oZip = new fileUnzip($sZipFile); foreach ($oZip->getList() as $sFileName => $aFileInfos) { $sFileExtension = pathinfo($sFileName, PATHINFO_EXTENSION); if ($aFileInfos['is_dir'] || !in_array(strtolower($sFileExtension), array('jpg', 'gif', 'png'))) { continue; } $iItemId = $okt->galleries->items->addItem($okt->galleries->items->openItemCursor(array('gallery_id' => $iGalleryId, 'active' => 1)), $aItemLocalesData); $sDestination = $okt->galleries->upload_dir . '/img/items/' . $iItemId . '/1.' . $sFileExtension; files::makeDir(dirname($sDestination), true); $oZip->unzip($sFileName, $sDestination); $aNewImagesInfos = $okt->galleries->items->getImageUploadInstance()->buildImagesInfos($iItemId, array(1 => basename($sDestination))); if (isset($aNewImagesInfos[1])) { $aNewItemImages = $aNewImagesInfos[1]; $aNewItemImages['original_name'] = utf8_encode(basename($sFileName)); } else { $aNewItemImages = array(); } $okt->galleries->items->updImages($iItemId, $aNewItemImages); } $oZip->close(); files::deltree($sTempDir); } catch (Exception $e) { $okt->error->set($e->getMessage()); } if ($okt->error->isEmpty()) {
/** Upgrade process. */ public function performUpgrade($zip_file, $zip_digests, $zip_root, $root, $root_digests) { if (!is_readable($zip_file)) { throw new Exception(__('Archive not found.')); } if (!is_readable($root_digests)) { @unlink($zip_file); throw new Exception(__('Unable to read current digests file.')); } $zip = new fileUnzip($zip_file); if (!$zip->hasFile($zip_digests)) { @unlink($zip_file); throw new Exception(__('Downloaded file seems not to be a valid archive.')); } $opts = FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES; $cur_digests = file($root_digests, $opts); $new_digests = explode("\n", $zip->unzip($zip_digests)); $new_files = self::getNewFiles($cur_digests, $new_digests); if (!empty($this->forced_files)) { $new_files = array_merge($new_files, $this->forced_files); } $zip_files = array(); $not_writable = array(); foreach ($new_files as $file) { if (!$file) { continue; } if (!$zip->hasFile($zip_root . '/' . $file)) { @unlink($zip_file); throw new Exception(__('Incomplete archive.')); } $dest = $root . '/' . $file; if (file_exists($dest) && !is_writable($dest) || !file_exists($dest) && !is_writable(dirname($dest))) { $not_writable[] = $file; continue; } $zip_files[] = $file; } # If only one file is not writable, stop everything now if (!empty($not_writable)) { $e = new Exception('Some files are not writable', self::ERR_FILES_UNWRITALBE); $e->bad_files = $not_writable; throw $e; } # Everything's fine, we can write files, then do it now $can_touch = function_exists('touch'); foreach ($zip_files as $file) { $zip->unzip($zip_root . '/' . $file, $root . '/' . $file); if ($can_touch) { @touch($root . '/' . $file); } } @unlink($zip_file); }
private function unzip($file) { $zip = new fileUnzip($file); if ($zip->isEmpty()) { $zip->close(); return false; //throw new Exception(__('File is empty or not a compressed file.')); } foreach ($zip->getFilesList() as $zip_file) { # Check zipped file name if (substr($zip_file, -4) != '.txt') { continue; } # Check zipped file contents $content = $zip->unzip($zip_file); if (strpos($content, '///DOTCLEAR|') !== 0) { unset($content); continue; } $target = path::fullFromRoot($zip_file, dirname($file)); # Check existing files with same name if (file_exists($target)) { $zip->close(); unset($content); throw new Exception(__('Another file with same name exists.')); } # Extract backup content if (file_put_contents($target, $content) === false) { $zip->close(); unset($content); throw new Exception(__('Failed to extract backup file.')); } $zip->close(); unset($content); # Return extracted file name return $target; } $zip->close(); throw new Exception(__('No backup in compressed file.')); }