<?php
/**
 * @version $Id: Upload.class.php v2.0 $
 * @package PBDigg
 * @copyright Copyright (C) 2007 - 2008 PBDigg.com. All Rights Reserved.
 * @license PBDigg is free software and use is subject to license terms
 */

class Upload
{
	/**
	 * 上传文件
	 */
	var $_files = array ();
	/**
	 * 上传文件数量
	 */
	var $_count = 0;

	/**
	 * 构造函数
	 */
	function Upload()
	{
		if (is_array($_FILES))
		{
			foreach ($_FILES as $field => $struct)
			{
				if (isset ($struct['error']) && $struct['error'] != UPLOAD_ERR_NO_FILE && is_uploaded_file($struct['tmp_name']))
				{
					$this->_files[$field] = & new SingleFile($field, $struct);
				}
			}
			$this->_count = count($this->_files);
		}
	}
	/**
	 * 返回文件对象
	 */
	function & getFiles()
	{
		return $this->_files;
	}
	/**
	 * 取得上传文件数量
	 */
	function getCount()
	{
		return $this->_count;
	}
}

class SingleFile
{
	/**
	 * 上传文件
	 */
	var $_file = array ();
	/**
	 * 是否图片
	 */
	var $_isimg;
	/**
	 * 文件是否移动到指定目录
	 */
	var $_moved;
	/**
	 * 上传文件编号
	 */
	var $_name;

	/**
	 * 构造函数
	 */
	function SingleFile($field, $struct)
	{
		$this->_file = $struct;
		$this->_name = $field;
		$this->_isimg = (strpos($this->_file['type'], 'image') === FALSE) ? '0' : '1';
		$this->_moved = FALSE;
	}
	/**
	 * 返回上传文件属性
	 */
	function getAttribut($item)
	{
		return $this->_file[$item];
	}
	/**
	 * 设置上传文件属性
	 */
	function setAttribut($item, $value)
	{
		$this->_file[$item] = $value;
	}
	/**
	 * 返回原文件名
	 */
	function getFileName()
	{
		return $this->_file['name'];
	}
	/**
	 * 返回临时文件名
	 */
	function getTmpName()
	{
		return $this->_file['tmp_name'];
	}
	/**
	 * 返回文件大小
	 */
	function getFileSize()
	{
		return $this->_file['size'];
	}
	/**
	 * 返回浏览器提供的文件类型
	 */
	function getFileType()
	{
		return $this->_file['type'];
	}
	/**
	 * 返回错误代码
	 */
	function getErrorCode()
	{
		return $this->_file['error'];
	}
	/**
	 * 返回上传是否成功
	 */
	function isSuccess()
	{
		return ($this->_file['error'] == UPLOAD_ERR_OK) ? TRUE : FALSE;
	}
	/**
	 * 返回上传是否为图片
	 */
	function isImg()
	{
		return $this->_isimg;
	}
	/**
	 * 返回移动后文件路径
	 */
	function getRealPath()
	{
		if ($this->_moved)
		{
			return $this->_file['real_path'];
		} else {
			return ;
		}
	}
	/**
	 * 返回移动后文件名称
	 */
	function getRealName()
	{
		return $this->_file['real_name'];
	}
	/**
	 * 生产唯一文件名
	 */
	function makeFileName()
	{
		$this->_file['real_name'] = md5($this->getFileName() . $this->getFileSize() . $this->getTmpName() . time());
	}
	/**
	 * 返回文件后缀
	 */
	function getExtName()
	{
		if ($this->_moved)
		{
			return pathinfo($this->getRealPath(), PATHINFO_EXTENSION);
		}
		else
		{
			return pathinfo($this->getFileName(), PATHINFO_EXTENSION);
		}
	}
	/**
	 * 检查上传文件类型和大小是否符合要求
	 * 
	 * @param $type Array 上传文件类型数组
	 * @param Int $maxSize 文件最大尺寸
	 */
	function checkFile($type = array(), $maxSize = 0)
	{
		global $func_message;
		if (!$this->isSuccess())
		{
			return $func_message['attachment_upload_failed'];
		}
		$fileExt = strtolower($this->getExtName());
		if (is_array($type))
		{
			$ck = FALSE;
			foreach ($type as $ext)
			{
				if ($fileExt == strtolower($ext))
				{
					$ck = TRUE;
					break;
				}
			}
			if (!$ck)
			{
				return $func_message['attachment_illegal_ext'];
			}
		}
		if ($maxSize && $this->getFileSize() > $maxSize)
		{
			return sprintf($func_message['attachment_filesize_error'], floor($maxSize / 1024));
		}
	}
	/**
	 * 移动文件
	 * 
	 * @param String $path 文件路径，以'/'结尾.
	 * @param String $filename 自定义文件名称
	 */
	function moveFile($path, $filename = '')
	{
		global $func_message;
		$path = str_replace('\\', '/', $path);
		if ($filename)
		{
			$fc = array ("\\","&","'","\"","/","*",",","<",">","\r","\t","\n","#","$","(",")","%","@","+","?",";","^","|");
			foreach ($fc as $value)
			{
				if (strpos($filename, $value) !== FALSE)
				{
					return $func_message['attachment_fchar_exist'];
				}
			}
			$this->_file['real_name'] = $filename;
		}
		else
		{
			$this->makeFileName();
		}
		$this->_file['real_path'] = $path.$this->_file['real_name'].'.'.$this->getExtName();
		if (!is_dir($path))
		{
			if (!mkdir($path, 0777))
			{
				return $func_message['attachment_mkdir_failed'];
			}
		}
		$this->_moved = TRUE;
		return (move_uploaded_file($this->_file['tmp_name'], $this->_file['real_path']) ? '' : $func_message['attachment_upload_failed']);
	}
	/**
	 * 删除临时文件
	 */
	function delTmpFile()
	{
		if ($this->isSuccess())
		{
			@ unlink($this->getTmpName());
		}
	}
	/**
	 * 删除上传文件
	 */
	function delRealFile()
	{
		if ($this->_moved)
		{
			@ unlink($this->getRealPath());
		}
	}
}
?>
