<?php
require_once(SYS_ROOT. '/cls_ctl/ctl.pic.php');
require_once(SYS_ROOT. '/cls_tab/tab.file.php');
class cls_ctl_upload
{
	private $sysconfig=array();
	private $config=array();
	function __construct()
	{
		global $obj_ctl_user,$obj_tab_sys_config;
		$arr_cfg_other=$obj_tab_sys_config->get_model("other");
		//初始系统设置
		$this->sysconfig["uname"]="";//上传用户
		$this->sysconfig["root"]=WEB_ROOT;//上传根目录
		$this->sysconfig["allow"]=1;//是否允许上传,1表求允许，非1表示不允许
		$this->sysconfig["onlogin"]=1;//上传是否需要登陆,1表求允许，非1表示不允许
		$this->sysconfig["path"]="";//上传路径
		$this->sysconfig["count"]="";//支持一次最多文件上传数
		$this->sysconfig["filesize"]="";//单个文件不能超过的大小,0表求不限
		$this->sysconfig["spacesize"]="";//可用空间大小
		$this->sysconfig["allowext"]=array();//允许上传的文件扩展名,数组
		$this->sysconfig["noallowext"]=array();//不允许上传的文件扩展名,数组
		if(isset($arr_cfg_other["upload_allow"])) $this->sysconfig["allow"]=intval($arr_cfg_other["upload_allow"]);
		if(isset($arr_cfg_other["upload_onlogin"])) $this->sysconfig["onlogin"]=intval($arr_cfg_other["upload_onlogin"]);
		if(isset($arr_cfg_other["path"])) $this->sysconfig["path"]=$arr_cfg_other["path"];
		if(isset($arr_cfg_other["upload_count"])) $this->sysconfig["count"]=intval($arr_cfg_other["upload_count"]);
		if(isset($arr_cfg_other["upload_filesize"])) $this->sysconfig["filesize"]=intval($arr_cfg_other["upload_filesize"])*1024*1024;
		if(isset($arr_cfg_other["spacesize"])) $this->sysconfig["spacesize"]=intval($arr_cfg_other["spacesize"])*1024*1024;
		if(isset($arr_cfg_other["upload_allowext"])) $this->sysconfig["allowext"]=$arr_cfg_other["upload_allowext"];
		if(isset($arr_cfg_other["upload_noallowext"])) $this->sysconfig["noallowext"]=$arr_cfg_other["upload_noallowext"];
		if(isset($obj_ctl_user)) $this->sysconfig["uname"]=$obj_ctl_user->get_perms("uname");
		if($this->sysconfig["uname"]!="") self::_init_userconfig();

	}
	function chk_allow()
	{
		global $obj_ctl_user,$arr_cfg_other;
		if(isset($this->sysconfig["allow"])&&intval($this->sysconfig["allow"])!=1) return "系统关闭了上传功能！";

		if(isset($this->sysconfig["onlogin"])&&intval($this->sysconfig["onlogin"])==1)
		{
			if(!isset($obj_ctl_user)) return "系统错误，未定义用户变量！";
			if(!$obj_ctl_user->is_login()) return "需要登陆才能进行上传操作！";
		}
		if(isset($this->config["allow"])&&intval($this->config["allow"])!=1) return "您没有上传的权限！";

	}
	//检查扩展名
	function chk_allow_ext($msg_ext)
	{
		if(substr($msg_ext,0,1)==".") $msg_ext=substr($msg_ext,1);
		if(!isset($this->sysconfig["allowext"])||!is_array($this->sysconfig["allowext"])) return "系统错误，未定义allowext变量！";
		if(!in_array($msg_ext,$this->sysconfig["allowext"]))
		{
			return "系统不允许上传扩展名为".$msg_ext."的文件！";
		}
		if(in_array($msg_ext,$this->sysconfig["noallowext"]))
		{
			return "系统不允许上传扩展名为".$msg_ext."的文件！";
		}
		if(isset($this->config["allowext"])&&is_array($this->config["allowext"])){
			if(!in_array($msg_ext,$this->config["allowext"]))
			{
				return "您无权上传该类型的文件！";
			}
		}
		if(isset($this->config["noallowext"])&&is_array($this->config["noallowext"])){
			if(in_array($msg_ext,$this->config["noallowext"]))
			{
				return "您无权上传该类型的文件！";
			}
		}
		return "";
	}
	function chk_space_size($msgsize)
	{
		if(isset($this->sysconfig["spacesize"]))
		{
			if($msgsize>intval($this->sysconfig["spacesize"])&&intval($this->sysconfig["spacesize"])>1) return "系统上传空间不够";
		}
		if(isset($this->config["spacesize"]))
		{
			if($msgsize>intval($this->config["spacesize"])&&intval($this->config["spacesize"])>1) return "上传空间不够";
		}
		return "";
	}
	function chk_file_size($msgsize)
	{
		if(isset($this->sysconfig["filesize"]))
		{
			if($msgsize>intval($this->sysconfig["filesize"])&&intval($this->sysconfig["filesize"])>1) return "系统上传单个文件不能超过".$this->sysconfig["filesize"]."(M)";
		}
		if(isset($this->config["filesize"]))
		{
			if($msgsize>intval($this->config["filesize"])&&intval($this->config["filesize"])>1) return "上传单个文件不能超过".$this->config["filesize"]."(M)";
		}
		return "";
	}
	function get_upload_path($msg_name,$msg_ext)
	{

		$str_dir=get_real_path($this->sysconfig["root"],$this->config["path"]);
		if(!is_dir($str_dir)) return "系统错误，设置上传路径不正确！";
		if($this->sysconfig["uname"]!=""){
			$str_dir.="/".$this->sysconfig["uname"];
		}else{
			$str_date=date("Y_m");
			$str_dir.="/".$str_date;
		}
		dir_create($str_dir);
		//$str_path=$str_dir."/".$msg_name;
		$str_path=$str_dir."/".TIME.".".$msg_ext;
		return $str_path;
	}
	function get_upload_url($msg_path)
	{
		if($msg_path=="") return "";
		if(strlen($this->sysconfig["root"])>strlen($msg_path)) return $msg_path;
		$str_path=substr($msg_path,strlen($this->sysconfig["root"]));
		return $str_path;
	}
	private function _init_userconfig()
	{
		global $obj_ctl_user;
		//初始用户设置
		$arr_userconfig=$obj_ctl_user->get_tab_userconfig();
		$this->config["allow"]=0;//是否允许上传,1表求允许，非1表示不允许
		$this->config["path"]=$this->sysconfig["path"];//上传路径	
		$this->config["filesize"]=0;//单个文件不能超过的大小,0表求不限
		$this->config["spacesize"]=0;//可用空间大小
		$this->config["allowext"]=array();//允许上传的文件扩展名,数组
		$this->config["noallowext"]=array();//不允许上传的文件扩展名,数组
		if(is_array($arr_userconfig["userconfig_config"])){
			$arr_config=$arr_userconfig["userconfig_config"];
			if(isset($arr_config["upload_allow"])){
				if($arr_config["upload_allow"]=="是"||$arr_config["upload_allow"]=="on"||intval($arr_config["upload_allow"])==1) $this->config["allow"]=1;
			}
			if(isset($arr_config["upload_filesize"])) $this->config["filesize"]=intval($arr_config["upload_filesize"])*1024*1024;
			if(isset($arr_config["upload_spacesize"])) $this->config["spacesize"]=$arr_config["upload_spacesize"]*1024*1024-$arr_userconfig["userconfig_upload_space"];
			if(isset($arr_config["upload_allowext"])){
				$arr_x=explode("|",$arr_config["upload_allowext"]);
				$this->config["allowext"]=$arr_x;
			}
			if(isset($arr_config["upload_noallowext"])){
				$arr_x=explode("|",$arr_config["upload_noallowext"]);
				$this->config["noallowext"]=$arr_x;
			}
		}
	}
	function on_upload()
	{
		$arr_return=array("err"=>"");
		//检查是否可以上传
		$msg_info=self::chk_allow();
		if($msg_info!=""){
			$arr_return["err"]=$msg_info;
			return $arr_return;
		}
		global $obj_db;
		$lng_upload_size=0;
		for($i=1;$i<=$this->sysconfig["count"];$i++)
		{
			if(!isset($_FILES["file_".$i]))
			{	
				continue;
			}
			if($_FILES["file_".$i]["size"]<10) continue;
			$lng_upload_size+=$_FILES["file_".$i]["size"];
			//检查上传空间是否够用，0表示不限
			$msg_info=self::chk_space_size($lng_upload_size);
			if($msg_info!=""){
				$arr_return["err"]=$msg_info;
				return $arr_return;
			}

			//上传单个文件的大小,以M为单位，0表示不限
			$msg_info=self::chk_space_size($_FILES["file_".$i]["size"]);
			if($msg_info!=""){
				$arr_return["err"]=$msg_info;
				return $arr_return;
			}

			//检查扩展名是否允许
			$str_ext=get_file_ext($_FILES["file_".$i]["name"]);
			$msg_info=self::chk_allow_ext($str_ext);
			if($msg_info!=""){
				$arr_return["err"]=$msg_info;
				return $arr_return;
			}

			//获取上传路径及名称
			$str_name=$_FILES["file_".$i]["name"];
			$str_path=self::get_upload_path($str_name,$str_ext);
			move_uploaded_file($_FILES['file_'.$i]['tmp_name'],$str_path);
			if(is_file($str_path)){
				$obj_tab_file=new cls_tab_file($obj_db);
				$arr_fields=array();
				$arr_fields["file_uname"]=$this->sysconfig["uname"];
				$arr_fields["file_name"]=$str_name;
				$arr_fields["file_ext"]=$str_ext;
				$arr_fields["file_size"]=$_FILES["file_".$i]["size"];
				$arr_fields["file_url"]=self::get_upload_url($str_path);
				$arr_fields["file_addtime"]=TIME;
				$obj_tab_file->on_save($arr_fields);
				//如果是图片的话,生成缩图
				self::_upload_pic($str_path,$str_name);
			}
		}
		return $arr_return;
	}
	private function _upload_pic($msg_path,$msg_name)
	{
		global $obj_db;
		if(!is_pic($msg_path)) return;
		$obj_ctl_pic=new cls_ctl_pic;
		$obj_ctl_pic->on_watermark($msg_path);
		if(intval($_POST["smallpic"])==1)
		{
			$str_name=basename($msg_path);
			if($msg_name!="")$str_name=$msg_name;
			$str_ext=get_file_ext($msg_path);
			$str_name=get_file_name($str_name,$str_ext)."(略).".$str_ext;
			$str_dir=dirname($msg_path);
			$str_new_path=$str_dir."/".$str_name;
			$msg_info=$obj_ctl_pic->on_resize($msg_path,$str_new_path);
			if(is_file($str_new_path))
			{
				$arr_file=get_file_perms($str_new_path);
				$obj_tab_file=new cls_tab_file($obj_db);
				$arr_fields=array();
				$arr_fields["file_uname"]=$this->sysconfig["uname"];
				$arr_fields["file_name"]=$str_name;
				$arr_fields["file_ext"]=$arr_file["ext"];
				$arr_fields["file_size"]=$arr_file["size"];
				$arr_fields["file_url"]=self::get_upload_url($str_new_path);
				$arr_fields["file_addtime"]=TIME;
				$msg_info=$obj_tab_file->on_save($arr_fields);
			}
		}
	}
	function on_upload_web($msg_url)
	{
		//检查是否可以上传
		$msg_info=self::chk_allow();
		if($msg_info!="") return $msg_info;
		//if(@file($msg_url)==FALSE) return "网址无效！";
		if(!exists_remote_file($msg_url)) return "网址无效!";
		$str_filename=basename($msg_url);
		//检查扩展名是否允许
		$str_ext=get_file_ext($str_filename);
		$msg_info=self::chk_allow_ext($str_ext);
		if($msg_info!="") return $msg_info;

		$data=file_get_contents($msg_url);
		$str_path=self::get_upload_path($str_filename,$str_ext);
		file_put_contents($str_path,$data);
		if(is_file($str_path)){
			$arr_file=get_file_perms($str_path);
			$obj_tab_file=new cls_tab_file($obj_db);
			$arr_fields=array();
			$arr_fields["file_uname"]=$this->sysconfig["uname"];
			$arr_fields["file_name"]=$str_filename;
			$arr_fields["file_ext"]=$arr_file["ext"];
			$arr_fields["file_size"]=$arr_file["size"];
			$arr_fields["file_url"]=self::get_upload_url($str_path);
			$arr_fields["file_addtime"]=TIME;
			$obj_tab_file->on_save($arr_fields);
			//如果是图片的话,生成缩图
			self::_upload_pic($str_path);
		}
		return "";

	}

}
?>