Location: PHPKode > projects > OpenNitro > trunk/Nitro/Modules/File.inc.php
<?php
//
// +---------------------------------------------------------------------------+
// | Nitro :: Module :: File                                                   |
// +---------------------------------------------------------------------------+
// | 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: File.inc.php 229 2008-04-17 09:20:31Z oli $
//
// Nitro's default uploaded File handling class
//

/**
 * This file contains the Nitro File 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";

/**
 * File module class
 *
 * This module contains the Nitro File class.
 *
 * @package	Modules
 * @author	Siggi Oskarsson <hide@address.com>
 * @author  Jesper Avot <hide@address.com>
 * @copyright	June Systems BV, 2006
 * @access public
 */
class NitroModuleFile extends NitroModule {
	/**
	 * @var	string	Module ID string
	 */
	var $IDString = "NitroModuleFile";
	
	/**
	 * File 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 NitroModuleFile($ObjectID, $Settings = FALSE)
	{
		/**
		 * Call the parent constructor
		 */
		parent::NitroModule();

		/**
		 * Set ObjectID and ModuleSettings
		 */
		$this->ObjectID = $ObjectID;
		$this->SetModuleSettings($Settings);
	}
	
	/**
	 * GetModuleDataConfiguration
	 *
	 * @return array Array for the form libraries.
	 */
	function GetModuleDataConfiguration()
	{
		return Array("Name" => "HTML", 
								 "Type" => "HTML", 
								 "Description" => "TEXTAREA/ROWS=5/COLS=35", 
								 "File" => "FILE/TYPE=FILE");
	}

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

			@unlink($FileName);
			
			if (@move_uploaded_file($SaveSettings["File"]["tmp_name"], $FileName)) {
				$Name = $SaveSettings["File"]["name"];
				$Type = $SaveSettings["File"]["type"];
				$Settings = Array(
					"Name" => $Name, 
					"Type" => $Type,
					"Description" => $SaveSettings['Description']
				);
				
				return $SaveSettings;
			} else {
				return $SaveSettings;
			}
		} else {
			return $SaveSettings;
		}
	}

	/**
	 * Draw function
	 *
	 * @param mixed $RunTimeSettings RunTimeSettings for the Object, default is FALSE
	 * @return string The drawn HTML link to the File.
	 */
	function Draw($RuntimeSettings = FALSE)
	{
		$this->ObjectIDString = $this->DB->getOne("SELECT 
																									IDString 
																							 FROM 
																							 		`Object` 
																							 WHERE 
																							 		ObjectID = " . (int)$this->ObjectID);
																							 		
		$Image = "./images/Icons/mime/" . str_replace('/', '_', $this->Settings["Type"]) . ".gif";
		
		list($Width, $Height) = getimagesize($Image);
																							 		
		return "<a href='/GetObject.php?ID=" . $this->ObjectIDString . "'><img src='/images/Icons/mime/" . $Image . "' bordser='0' width='" . $Width . "' height='" . $Height . "' alt='" . $this->Settings['Description'] . "' title='" . $this->Settings['Description'] . "' /></a>";
	}

	/**
	 * DrawObject function
	 *
	 * @param mixed $RunTimeSettings RunTimeSettings for the object, default is FALSE
	 */
	function DrawObject($RuntimeSettings = FALSE)
	{
		$FileDir = $this->Conf["Paths"]["Files"] . ($this->ObjectLanguage ? $this->ObjectLanguage : "Default") . DIRECTORY_SEPARATOR . $this->ObjectVersion . DIRECTORY_SEPARATOR;
		$FileName = $FileDir . $this->ObjectID;
		
		if (file_exists($FileName) && is_file($FileName)) {
			header("Content-Type: " . $this->Settings["Type"]);
			header("Content-Length: " . filesize($FileName));
			header("Content-Disposition: inline; filename=\"" . $this->Settings["Name"] . "\"");
			header("Expires: " . gmdate("D, d M Y H:i:s", strtotime("+1 day")) . " GMT");
			
			/**
			 * The Header below is very important, Internet Explorer does not works with inline files over a secured connection (HTTPS [SSL]).
			 * So we need to set this header, because PHP sets the Pragma default to 'no-cache'
			 */
			Header("Pragma: Cache"); 
			header("Cache-Control: Private"); 
			readfile($FileName);
		} else {
			echo "File " . $FileName . " cannot be found!";
		}
	}
}
?>
Return current item: OpenNitro