Newer
Older
Import / web / www.xiaofrog.com / gallery / modules / archiveupload / classes / ArchiveExtractToolkit.class
<?php
/*
 * Gallery - a web based photo album viewer and editor
 * Copyright (C) 2000-2008 Bharat Mediratta
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA  02110-1301, USA.
 */

GalleryCoreApi::requireOnce('modules/core/classes/GalleryToolkit.class');

/**
 * A version of GalleryToolkit to extract files from an archive.
 * @package ArchiveUpload
 * @subpackage Classes
 * @author Alan Harder <alan.harder@sun.com>
 * @version $Revision: 17999 $
 */
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 (!$this->_pruneDirectory($destFilename,
		    /* Thumbs.db is Windows, the rest are MacOSX */
		    array('file' => array('Thumbs.db', '.DS_Store', '.Trashes'),
			  'dir' => array('__MACOSX')),
		    $params['removeMeta'])) {
	    return array(GalleryCoreApi::error(ERROR_PLATFORM_FAILURE), null, null);
	}

	return array(null, $mimeType, $context);
    }

    /**
     * Remove specified patterns or symlinks in a directory tree.
     * @param string $dir path
     * @param array $prune ('file' => array of filenames, 'dir' => array of dirnames)
     * @param bool $removeMeta flag to indicate that the meta files should be removed
     * @return bool true for success
     * @access private
     */
    function _pruneDirectory($dir, $prune, $removeMeta) {
	global $gallery;
	$platform =& $gallery->getPlatform();

	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_link($path)) {
		if (!$platform->unlink($path)) {
		    return false;
		}
	    } else if ($platform->is_dir($path)) {
		if ($removeMeta && in_array($file, $prune['dir'])) {
		    if (!$platform->recursiveRmdir($path)) {
			return false;
		    }
		} else if (!$this->_pruneDirectory($path, $prune, $removeMeta)) {
		    return false;
		}
	    } else if ($removeMeta && in_array($file, $prune['file']) 
		    && !$platform->unlink($path)) {
		return false;
	    }
	}
	return true;
    }
}
?>