<?php
/*
 * $RCSfile: DcrawToolkitHelper.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.7 $ $Date: 2005/08/23 03:49:39 $
 * @package Dcraw
 * @author Bharat Mediratta <bharat@menalto.com>
 */

/**
 * A helper class for the DcrawToolkit class
 *
 * @package Dcraw
 * @subpackage Classes
 */
class DcrawToolkitHelper {

    /**
     * Figure out what operations and properties are supported by the
     * DcrawToolkit and return them.
     *
     * @return array object GalleryStatus a status code
     *               array('operations' => ...
     *                     'properties' => ...)
     * @static
     */
    function getOperationsAndProperties() {
	global $gallery;

	$results = array('operations' => array(
			     'convert-to-image/x-portable-pixmap' => array(
				 'params' => array(),
				 'description' => $gallery->i18n('Convert to a PPM'),
				 'mimeTypes' => array('image/x-dcraw'),
				 'outputMimeType' => 'image/x-portable-pixmap')),
			 'properties' => array(
			     'dimensions' => array(
				 'type' => 'int,int',
				 'description' => $gallery->i18n(
				     'Get the width and height of the image'),
				 'mimeTypes' => 'image/x-dcraw')));

	return array(GalleryStatus::success(), $results);
    }

    /**
     * Test if the given path has a working dcraw binary.
     *
     * @param string path to the dcraw binary
     * @return array object GalleryStatus general status of tests
     *	             array ('name' => string: the name of the binary,
     *		            'success' => boolean: test successful?
     *		            'message' => string: the error message
     *
     * @static
     */
    function testBinary($dcrawPath) {
	global $gallery;
	$platform = $gallery->getPlatform();

	/*
	 * If the path is not restricted by open_basedir, then verify that it's
	 * legal.  Else just hope that it's valid and use it.
	 */
	if (!$platform->isRestrictedByOpenBaseDir($dcrawPath) &&
		!@$platform->is_file($dcrawPath)) {
	    return array(GalleryStatus::error(ERROR_BAD_PATH, __FILE__, __LINE__), null);
	}

	/* We need to translate some strings */
	list($ret, $module) = GalleryCoreApi::loadPlugin('module', 'dcraw');
	if ($ret->isError()) {
	    return array($ret->wrap(__FILE__, __LINE__), null);
	}

	list ($success, $stdout, $stderr) = $platform->exec(array(array($dcrawPath)));
	/* $success will be 0 here, since we gave no arguments */
	$verCheck = implode(',', $stderr);
	if (preg_match('/Raw Photo Decoder(?: "dcraw")? v(\S+)/', $verCheck, $matches)) {
	    $version = $matches[1];
	    if (version_compare($version, '5.40', '>=')) {
		$testArray[] = array('name' => 'dcraw', 'success' => true);
	    }
	}

	if (empty($testArray)) {
	    $testArray[] = array('name' => 'dcraw',
				 'success' => false,
				 'message' => array_merge(
				     array($module->translate('Binary output:')),
				     $stdout,
				     $stderr));
	}

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