<?php
/*
 * $RCSfile: ArchiveExtractToolkit.class,v $
 *
 * Gallery - a web based photo album viewer and editor
 * Copyright (C) 2000-2005 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.
 */
/**
 * @version $Revision: 1.3 $ $Date: 2005/08/23 03:48:58 $
 * @package ArchiveUpload
 * @author Alan Harder <alan.harder@sun.com>
 */

/**
 * Load required classes
 */
GalleryCoreApi::relativeRequireOnce('modules/core/classes/GalleryToolkit.class');

/**
 * A version of GalleryToolkit to extract files from an archive.
 *
 * @package ArchiveUpload
 * @subpackage Classes
 */
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(GalleryStatus::error(ERROR_UNSUPPORTED_OPERATION, __FILE__, __LINE__,
					      "$operationName $mimeType"), null, null);
	}
	if (!$platform->is_dir($destFilename)) {
	    return array(GalleryStatus::error(ERROR_BAD_PARAMETER, __FILE__, __LINE__), null, null);
	}

	list ($ret, $unzip) =
	    GalleryCoreApi::getPluginParameter('module', 'archiveupload', 'unzipPath');
	if ($ret->isError()) {
	    return array($ret->wrap(__FILE__, __LINE__), null, null);
	}

	$cwd = $platform->getcwd();
	if (!$platform->chdir($destFilename)) {
	    return array(GalleryStatus::error(ERROR_PLATFORM_FAILURE, __FILE__, __LINE__),
			 null, null);
	}
	$gallery->guaranteeTimeLimit(60);

	list ($success) = $platform->exec(array(array($unzip, $sourceFilename)));
	if (!empty($cwd)) {
	    @$platform->chdir($cwd);
	}
	if (!$success) {
	    return array(GalleryStatus::error(ERROR_PLATFORM_FAILURE, __FILE__, __LINE__),
			 null, null);
	}

	return array(GalleryStatus::success(), $mimeType, $context);
    }
}
?>
