* @version $Revision: 15513 $ */ class ArchiveExtractToolkit extends GalleryToolkit { /** * @see GalleryToolkit::performOperation */ function performOperation($mimeType, $operationName, $sourceFilename, $destFilename, $parameters, $context=array()) { global $gallery; $platform =& $gallery->getPlatform(); if ($operationName != 'extract' || $mimeType != 'application/zip') { return array(GalleryCoreApi::error(ERROR_UNSUPPORTED_OPERATION, __FILE__, __LINE__, "$operationName $mimeType"), null, null); } if (!$platform->is_dir($destFilename)) { return array(GalleryCoreApi::error(ERROR_BAD_PARAMETER), null, null); } list ($ret, $params) = GalleryCoreApi::fetchAllPluginParameters('module', 'archiveupload'); if ($ret) { return array($ret, null, null); } $cwd = $platform->getcwd(); if (!$platform->chdir($destFilename)) { return array(GalleryCoreApi::error(ERROR_PLATFORM_FAILURE), null, null); } $gallery->guaranteeTimeLimit(60); list ($success) = $platform->exec(array(array($params['unzipPath'], $sourceFilename))); if (!empty($cwd)) { @$platform->chdir($cwd); } if (!$success) { return array(GalleryCoreApi::error(ERROR_PLATFORM_FAILURE), null, null); } /* Set permissions according to core parameter settings */ if (!$platform->recursiveChmod($destFilename)) { return array(GalleryCoreApi::error(ERROR_PLATFORM_FAILURE), null, null); } if ($params['removeMeta'] && !$this->_pruneDirectory($destFilename, /* Thumbs.db is Windows, the rest are MacOSX */ array('file' => array('Thumbs.db', '.DS_Store', '.Trashes'), 'dir' => array('__MACOSX')), $platform)) { return array(GalleryCoreApi::error(ERROR_PLATFORM_FAILURE), null, null); } return array(null, $mimeType, $context); } /** * Remove specified patterns in a directory tree. * @param string $dir path * @param array $prune ('file' => array of filenames, 'dir' => array of dirnames) * @param object GalleryPlatform * @return bool true for success * @access private */ function _pruneDirectory($dir, $prune, &$platform) { if (!($dh = $platform->opendir($dir))) { return false; } $list = array(); while ($file = $platform->readdir($dh)) { if ($file != '.' && $file != '..') { $list[] = $file; } } $platform->closedir($dh); $dir .= $platform->getDirectorySeparator(); foreach ($list as $file) { $path = $dir . $file; if ($platform->is_dir($path)) { if (in_array($file, $prune['dir'])) { if (!$platform->recursiveRmdir($path)) { return false; } } else if (!$this->_pruneDirectory($path, $prune, $platform)) { return false; } } else if (in_array($file, $prune['file']) && !$platform->unlink($path)) { return false; } } return true; } } ?>