<?php

/**
 * TemporaryFileManager.inc.php
 *
 * Copyright (c) 2000-2007 John Willinsky
 * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
 *
 * @package file
 *
 * Class defining operations for temporary file management.
 *
 * $Id: TemporaryFileManager.inc.php,v 1.5 2007/04/10 20:45:06 asmecher Exp $
 */

import('file.FileManager');

class TemporaryFileManager extends FileManager {
	
	/** @var string the path to location of the files */
	var $filesDir;
	
	/**
	 * Constructor.
	 * Create a manager for handling temporary file uploads.
	 */
	function TemporaryFileManager() {
		$this->filesDir = Config::getVar('files', 'files_dir') . '/temp/';

		$this->_performPeriodicCleanup();
	}
	
	/**
	 * Retrieve file information by file ID.
	 * @return TemporaryFile
	 */
	function &getFile($fileId, $userId) {
		$temporaryFileDao = &DAORegistry::getDAO('TemporaryFileDAO');
		$temporaryFile = &$temporaryFileDao->getTemporaryFile($fileId, $userId);
		return $temporaryFile;
	}
	
	/**
	 * Read a file's contents.
	 * @param $output boolean output the file's contents instead of returning a string
	 * @return boolean
	 */
	function readFile($fileId, $userId, $output = false) {
		$temporaryFile = &$this->getFile($fileId, $userId);
		
		if (isset($temporaryFile)) {
			$filePath = $this->filesDir . $temporaryFile->getFileName();
			return parent::readFile($filePath, $output);
		} else {
			return false;
		}
	}
	
	/**
	 * Delete a file by ID.
	 * @param $fileId int
	 */
	function deleteFile($fileId, $userId) {
		$temporaryFile = &$this->getFile($fileId, $userId);
		
		parent::deleteFile($this->filesDir . $temporaryFile->getFileName());

		$temporaryFileDao = &DAORegistry::getDAO('TemporaryFileDAO');
		$temporaryFileDao->deleteTemporaryFileById($fileId, $userId);
	}
	
	/**
	 * Download a file.
	 * @param $fileId int the file id of the file to download
	 * @param $inline print file as inline instead of attachment, optional
	 * @return boolean
	 */
	function downloadFile($fileId, $userId, $inline = false) {
		$temporaryFile = &$this->getFile($fileId, $userId);
		if (isset($temporaryFile)) {
			$filePath = $this->filesDir . $temporaryFile->getFileName();
			return parent::downloadFile($filePath, null, $inline);
		} else {
			return false;
		}
	}
	
	/**
	 * View a file inline (variant of downloadFile).
	 * @see TemporaryFileManager::downloadFile
	 */
	function viewFile($fileId) {
		$this->downloadFile($fileId, true);
	}
	
	/**
	 * Parse the file extension from a filename/path.
	 * @param $fileName string
	 * @return string
	 */
	function parseFileExtension($fileName) {
		$fileParts = explode('.', $fileName);
		if (is_array($fileParts)) {
			$fileExtension = $fileParts[count($fileParts) - 1];
		}
		
		// FIXME Check for evil
		if (!isset($fileExtension) || strstr($fileExtension, 'php') || strlen($fileExtension) > 6 || !preg_match('/^\w+$/', $fileExtension)) {
			$fileExtension = 'txt';
		}
		
		return $fileExtension;
	}
	
	/**
	 * Upload the file and add it to the database.
	 * @param $fileName string index into the $_FILES array
	 * @param $userId int
	 * @return object The new TemporaryFile or false on failure
	 */
	function handleUpload($fileName, $userId) {
		// Get the file extension, then rename the file.
		$fileExtension = $this->parseFileExtension($this->getUploadedFileName($fileName));			
		
		if (!$this->fileExists($this->filesDir, 'dir')) {
			// Try to create destination directory
			$this->mkdirtree($this->filesDir);
		}
	
		$newFileName = basename(tempnam($this->filesDir, $fileExtension));
		if (!$newFileName) return false;

		if ($this->uploadFile($fileName, $this->filesDir . $newFileName)) {
			$temporaryFileDao = &DAORegistry::getDAO('TemporaryFileDAO');
			$temporaryFile = &new TemporaryFile();

			$temporaryFile->setUserId($userId);
			$temporaryFile->setFileName($newFileName);
			$temporaryFile->setFileType($_FILES[$fileName]['type']);
			$temporaryFile->setFileSize($_FILES[$fileName]['size']);
			$temporaryFile->setOriginalFileName(TemporaryFileManager::truncateFileName($_FILES[$fileName]['name'], 127));
			$temporaryFile->setDateUploaded(Core::getCurrentDate());
		
			$temporaryFileDao->insertTemporaryFile($temporaryFile);
			
			return $temporaryFile;
			
		} else {
			return false;
		}
	}

	/**
	 * Create a new temporary file from a paper file.
	 * @param $paperFile object
	 * @param $userId int
	 * @return object The new TemporaryFile or false on failure
	 */
	function paperToTemporaryFile($paperFile, $userId) {
		// Get the file extension, then rename the file.
		$fileExtension = $this->parseFileExtension($paperFile->getFileName());			
		
		if (!$this->fileExists($this->filesDir, 'dir')) {
			// Try to create destination directory
			$this->mkdirtree($this->filesDir);
		}
	
		$newFileName = basename(tempnam($this->filesDir, $fileExtension));
		if (!$newFileName) return false;

		if (copy($paperFile->getFilePath(), $this->filesDir . $newFileName)) {
			$temporaryFileDao = &DAORegistry::getDAO('TemporaryFileDAO');
			$temporaryFile = &new TemporaryFile();

			$temporaryFile->setUserId($userId);
			$temporaryFile->setFileName($newFileName);
			$temporaryFile->setFileType($paperFile->getFileType());
			$temporaryFile->setFileSize($paperFile->getFileSize());
			$temporaryFile->setOriginalFileName(TemporaryFileManager::truncateFileName($paperFile->getOriginalFileName(), 127));
			$temporaryFile->setDateUploaded(Core::getCurrentDate());
		
			$temporaryFileDao->insertTemporaryFile($temporaryFile);
			
			return $temporaryFile;
			
		} else {
			return false;
		}
	}

	function _performPeriodicCleanup() {
		if (time() % 100 == 0) {
			$temporaryFileDao = &DAORegistry::getDAO('TemporaryFileDAO');
			$expiredFiles = $temporaryFileDao->getExpiredFiles();
			foreach ($expiredFiles as $expiredFile) {
				$this->deleteFile($expiredFile->getFileId(), $expiredFile->getUserId());
			}
		}
	}
}

?>