Location: PHPKode > projects > OpenNitro > trunk/Nitro/Modules/Image.inc.php
<?php
//
// +---------------------------------------------------------------------------+
// | Nitro :: Module :: Image                                                  |
// +---------------------------------------------------------------------------+
// | Copyright (c) 2006 June Systems BV                                        |
// +---------------------------------------------------------------------------+
// | This library is free software; you can redistribute it and/or modify it   |
// | under the terms of the GNU Lesser General Public License as published by  |
// | the Free Software Foundation; either version 2.1 of the License, or (at   |
// | your option) any later version.                                           |
// |                                                                           |
// | This library 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 Lesser   |
// | General Public License for more details.                                  |
// |                                                                           |
// | You should have received a copy of the GNU Lesser General Public License  |
// | along with this library; if not, write to the Free Software Foundation,   |
// | Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA            |
// +---------------------------------------------------------------------------+
// | Authors: Siggi Oskarsson <hide@address.com>                          |
// |          Jesper Avot <hide@address.com                              |
// +---------------------------------------------------------------------------+
//
// $Id: Image.inc.php 229 2008-04-17 09:20:31Z oli $
//
// Nitro's default Image Module class
//

/**
 * This file contains the Nitro Image Module
 *
 * @author			Siggi Oskarsson
 * @copyright		June Systems BV, 2006
 * @version			$Revision: 1.4 $
 * @package			Modules
 */

/**
 * Include prototype module class
 */
require_once "Nitro/Module.inc.php";

/**
 * Image module class
 *
 * This module contains the Nitro Image class.
 *
 * @package	Modules
 * @author	Siggi Oskarsson <hide@address.com>
 * @author  Jesper Avot <hide@address.com>
 * @copyright	June Systems BV, 2006
 * @access public
 */
class NitroModuleImage extends NitroModule {
	/**
	 * @var	string	Module ID string
	 */
	var $IDString = "NitroModuleImage";
	
	/**
	 * Image module constructor function
	 *
	 * @param string $ObjectID String with the current ObjectID
	 * @param mixed $Settings Array with the Module Settings, default is FALSE
	 * @access	public
	 */
	function NitroModuleImage($ObjectID, $Settings = FALSE)
	{
		/**
		 * Call the parent constructor
		 */
		parent::NitroModule();

		/**
		 * Set ObjectID and ModuleSettings
		 */
		$this->ObjectID = $ObjectID;
		$this->SetModuleSettings($Settings);
	}
	
	function GetModuleConfiguration()
	{
		return Array("titel" => "TEXT", 
								 "intro" => "TEXT", 
								 "text" => "TEXT"
);
	}
	
	/**
	 * GetModuleDataConfiguration function
	 *
	 * @return array Array for the form libraries.
	 */
	function GetModuleDataConfiguration()
	{
		return Array("Name" => "HTML", 
								 "Type" => "HTML", 
								 "Height" => "HTML", 
								 "Width" => "HTML", 
								 "Image" => "FILE/TYPE=IMAGE", 
								 "Preview" => "PREVIEW/TYPE=IMAGE");
	}

	/**
	 * SaveModuleDataConfiguration function
	 *
	 * @param mixed $SaveSettings The settings to save. (We use only the Image savesettings to save the image.)
	 * @return mixed The saved settings or in case of failure FALSE.
	 */
	function SaveModuleDataConfiguration($SaveSettings)
	{
		if ($SaveSettings["Image"]["tmp_name"]) {
			/**
			 * Create the required directories.
			 */
			if (strlen($this->Conf['Paths']['Image'])) {
				$ImageDir = $this->Conf['Paths']['Image'];
			} else {
				$ImageDir = $this->Conf['Paths']['NitroCommon'].'Image'.DIRECTORY_SEPARATOR;
			}
			// If path set in ini is relative, prepend NITRO_ROOT to it
			//TODO: this will not work on Windows, fix!
			if (strpos($ImageDir, DIRECTORY_SEPARATOR) !== 0) $ImageDir = NITRO_ROOT.$ImageDir;
			
			if (!file_exists($ImageDir)) mkdir($ImageDir);
			
			$ImageDir.= ($this->ObjectLanguage ? $this->ObjectLanguage : 'Default').DIRECTORY_SEPARATOR;
			
			if (!file_exists($ImageDir)) mkdir($ImageDir);
			
			$ImageDir.= $this->ObjectVersion.DIRECTORY_SEPARATOR;
			
			if (!file_exists($ImageDir)) mkdir($ImageDir);
			
			/**
			 * Create the link to the image file and delete it (instead of replacing) before uploading it.
			 */
			$ImageFile = $ImageDir . $this->ObjectID;

			@unlink($ImageFile);
			
			if (@move_uploaded_file($SaveSettings["Image"]["tmp_name"], $ImageFile)) {
				$Name = $SaveSettings["Image"]["name"];
				$Type = $SaveSettings["Image"]["type"];
				
				list($Width, $Height) = getimagesize($ImageFile);
				
				$Settings = Array( "Name" => $Name, 
													 "Type" => $Type, 
													 "Height" => $Height, 
													 "Width" => $Width );
				
				return $Settings;
			} else {
				return FALSE;
			}
		} else {
			return FALSE;
		}
	}

	/**
	 * Draw function
	 *
	 * @param mixed $RunTimeSettings RunTimeSettings for the Object, default is FALSE
	 * @return string The drawn HTML link to the Image.
	 */
	function Draw($RunTimeSettings = FALSE)
	{
		if (!strlen($this->Settings['Width']) && !strlen($this->Settings['Height'])) {
			$ImageDir = $this->Conf["Paths"]["Image"].DIRECTORY_SEPARATOR.($this->ObjectLanguage ? $this->ObjectLanguage : "Default").DIRECTORY_SEPARATOR.$this->ObjectVersion.DIRECTORY_SEPARATOR;
			$tmp = getimagesize($ImageDir.$this->ObjectID);
			$this->Settings['Width'] = $tmp[0];
			$this->Settings['Height'] = $tmp[1];
		}
		
		$this->ObjectIDString = $this->DB->getOne("
			SELECT 
				IDString 
			FROM 
				`Object` 
			WHERE 
				ObjectID = ".(int)$this->ObjectID."
		");
																							 		
		return "<img src='" . $this->GetObjectURL($this->ObjectIDString) . "&V=" . $this->ObjectVersion . "' alt='" . $this->Settings["Alt"] . "' width='" . $this->Settings["Width"] . "' height='" . $this->Settings["Height"] . "' border='0' hspace='" . $this->Settings['HSpace'] . "' vspace='" . $this->Settings['VSpace'] . "' />";
	}

	/**
	 * DrawObject function
	 *
	 * @param mixed $RunTimeSettings RunTimeSettings for the object, default is FALSE
	 */
	function DrawObject($RunTimeSettings = FALSE)
	{
		$ImageDir = $this->Conf["Paths"]["Image"] . DIRECTORY_SEPARATOR . ($this->ObjectLanguage ? $this->ObjectLanguage : "Default") . DIRECTORY_SEPARATOR . $this->ObjectVersion . DIRECTORY_SEPARATOR;
		$ImageFile = $ImageDir . $this->ObjectID;
		
		if (file_exists($ImageFile) && is_file($ImageFile)) {
			header("Content-Type: " . $this->Settings["Type"]);
			header("Content-Length: " . filesize($ImageFile));
			header("Content-Disposition: inline; filename=\"" . $this->Settings["Name"] . "\"");
			header("Expires: " . gmdate("D, d M Y H:i:s", strtotime("+1 day")) . " GMT");
			header("Pragma: cache");
			header("Cache-Control: public"); 
			readfile($ImageFile);
		} else {
			echo "Image " . $ImageFile . " cannot be found!";
		}
	}

	/**
	 * isCacheable function
	 *
	 * Check if this Module is cacheable or not.
	 *
	 * @return mixed Is the Module allowed to be cached or not.
	 */
	function isCacheable()
	{
		return $this->cacheID();
	}
}
?>
Return current item: OpenNitro