<?php
/*
===========================注册模块控制器
*/
class cls_tab_other_upload_attatch extends cls_base_table
{
	private $obj_db;
	private $config=array(),$arr_cfg_other=array();
	//构造函数
	function __construct()
	{
		parent::__construct();
		global $obj_db,$obj_ctl_user,$obj_tab_sys_config;
		$arr_cfg_other=$obj_tab_sys_config->get_model("other");
		$this->obj_db=$obj_db;
		$this->tab_file=DB_PRE."file";
		$lng_user_id=$obj_ctl_user->id;
		$this->cfg_upload["dir"]="/upload/attatch";//上传根目录
		$this->config["dir_root"]=WEB_ROOT;


		//初始系统设置
		$this->cfg_upload["uname"]="";//上传用户
		$this->cfg_upload["uid"]=0;//上传用户
		$this->cfg_upload["allow"]=1;//是否允许上传,1表求允许，非1表示不允许
		$this->cfg_upload["onlogin"]=0;//上传是否需要登陆,1表求允许，非1表示不允许
		$this->config["path"]=$this->cfg_upload["dir"]."/".date("Y",TIME)."/".date("m",TIME);//上传路径
		$this->cfg_upload["count"]=5;//支持一次最多文件上传数
		$this->cfg_upload["filesize"]="";//单个文件不能超过的大小,0表求不限
		$this->cfg_upload["filesize_x"]="";
		$this->cfg_upload["spacesize"]="";//可用空间大小
		$this->cfg_upload["allowext"]=array();//允许上传的文件扩展名,数组
		$this->cfg_upload["noallowext"]=array();//不允许上传的文件扩展名,数组
		if(isset($arr_cfg_other["upload_allow"])) $this->cfg_upload["allow"]=intval($arr_cfg_other["upload_allow"]);
		//if(isset($arr_cfg_other["upload_onlogin"])) $this->cfg_upload["onlogin"]=intval($arr_cfg_other["upload_onlogin"]);
		if(isset($arr_cfg_other["upload_count"])) $this->cfg_upload["count"]=intval($arr_cfg_other["upload_count"]);
		if(isset($arr_cfg_other["upload_filesize"])) {
			$this->cfg_upload["filesize"]=intval($arr_cfg_other["upload_filesize"])*1024*1024;
			$this->cfg_upload["filesize_x"]=intval($arr_cfg_other["upload_filesize"]);
		}
		if(isset($arr_cfg_other["upload_allowext"])) $this->cfg_upload["allowext"]=$arr_cfg_other["upload_allowext"];
		if(isset($arr_cfg_other["upload_noallowext"])) $this->cfg_upload["noallowext"]=$arr_cfg_other["upload_noallowext"];
		if(isset($obj_ctl_user)) $this->cfg_upload["uid"]=$obj_ctl_user->id;

	}
	//保存
	function on_save($arr_fields)
	{
		$arr_return=array("err"=>"","id"=>0,"path"=>"");
		(!isset($arr_fields["attatch_id"]))? $arr_fields["attatch_id"]=0:$arr_fields["attatch_id"]=intval($arr_fields["attatch_id"]);
		//保存操作
		$lng_id=$arr_fields["attatch_id"];
		unset($arr_fields["attatch_id"]);//释放id

		$arr_fields["attatch_updatetime"]=TIME;
		$arr_fields["attatch_addtime"]=TIME;
		$arr_return=self::_insert($arr_fields);
		if(isset($arr_fields["attatch_path"])){
			global $arr_cfg_other;
			$str_path=$arr_fields["attatch_path"];
			if(isset($arr_cfg_other["ftp_open"]) && $arr_cfg_other["ftp_open"]==1){
				require_once(SYS_ROOT."/cls_ctl/ctl.ftp.php");
				$obj_ctl_ftp=new cls_ctl_ftp;
				$arr_ftp_return=$obj_ctl_ftp->on_upload(get_real_path("",$str_path),dirname($str_path));
				$str_path=$arr_ftp_return["url"];
			}
			$arr_return["path"]=$str_path;
		}
		return $arr_return;
	}
	//添加
	private function _insert($arr_fields)
	{
		if(!isset($arr_fields["attatch_path"])||$arr_fields["attatch_path"]=="") return "文件路径不能为空！";
		if(!isset($arr_fields["attatch_ext"])){
			$arr_fields["attatch_ext"]=get_file_ext($arr_fields["attatch_path"]);
		}
		$arr_fields["attatch_type"]=self::get_upload_type($arr_fields["attatch_path"],$arr_fields["attatch_ext"]);
		$arr_return=parent::on_insert(DB_PRE."other_upload_attatch",$arr_fields);
		return $arr_return;
	}
	//删除
	function on_del($arr_id)
	{
		$arr_return=array("err"=>"");
		$str_id=get_arr_id($arr_id);
		if($str_id==""){
			$arr_return["err"]="没有指定要删除的记录！";
			return $arr_return;
		}
		$arr_path=array();
		$sql="select * from ".DB_PRE."other_upload_attatch"." where attatch_id in(".$str_id.")";
		$result = $this->obj_db->query($sql);
		while($rs = $this->obj_db->fetch_array($result))
		{
			$arr_path[]=$rs;
		}

		$arr_return=parent::on_del(DB_PRE."other_upload_attatch","attatch_id in(".$str_id.")");

		if($arr_return["err"]==""&&count($arr_path)>0){
			foreach($arr_path as $item)
			{
				$str_path=get_real_path($this->config["dir_root"],$item["attatch_path"]);
				file_delete($str_path);
			}
		}

		return $arr_return;
	}
	function get_upload_type($msg_ext)
	{
		if(is_pic($msg_ext)) return "pic";
		if(is_flash($msg_ext)) return "flash";
		if(is_medio($msg_ext)) return "medio";
		return "other";
	}
	function get_config($msg_v)
	{
		if(isset($this->config[$msg_v])) return $this->config[$msg_v];
	}
	function get_upload_config($msg_v)
	{
		if(isset($this->cfg_upload[$msg_v])) return $this->cfg_upload[$msg_v];
	}

	function get_real_path($msg_path){
		$str_path=get_real_path($this->config["dir_root"].$this->config["path"],$msg_path);
		return $str_path;
	}

	function on_upload()
	{
		$arr_return=array("err"=>"");
		//检查是否可以上传
		$msg_info=self::upload_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->cfg_upload["count"];$i++)
		{
			if(!isset($_FILES["file_".$i]))
			{	
				continue;
			}
			if($_FILES["file_".$i]["size"]<1) continue;
			$lng_upload_size+=$_FILES["file_".$i]["size"];
			if($msg_info!=""){
				$arr_return["err"]=$msg_info;
				return $arr_return;
			}
			//上传单个文件的大小,以M为单位，0表示不限
			$msg_info=self::upload_chk_upload_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::upload_chk_allow_ext($str_ext);
			if($msg_info!=""){
				$arr_return["err"]=$msg_info;
				return $arr_return;
			}
			//获取上传路径及名称
			$str_name=TIME."_".rand(1,100).".".$str_ext;
			$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)){
				$arr_fields=array();
				$arr_fields["attatch_user_id"]=$this->cfg_upload["uid"];
				$arr_fields["attatch_name"]=$str_name;
				$arr_fields["attatch_ext"]=$str_ext;
				$arr_fields["attatch_size"]=$_FILES["file_".$i]["size"];
				$arr_fields["attatch_path"]=self::get_upload_url($str_name);
				$arr_fields["attatch_addtime"]=TIME;
				$arr_return=self::on_save($arr_fields);
				//如果是图片的话,生成缩图
				self::on_upload_pic($str_path,$str_name);
			}
		}
		return $arr_return;
	}
	function upload_chk_allow()
	{
		global $obj_ctl_user,$arr_cfg_other;
		if(isset($this->cfg_upload["allow"])&&intval($this->cfg_upload["allow"])!=1) return "系统关闭了上传功能！";

		if(isset($this->cfg_upload["onlogin"])&&intval($this->cfg_upload["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 upload_chk_upload_size($msgsize)
	{
		if(isset($this->cfg_upload["filesize"]))
		{
			if($msgsize>intval($this->cfg_upload["filesize"])&&intval($this->cfg_upload["filesize"])>1) return "系统上传单个文件不能超过".$this->cfg_upload["filesize_x"]."(M)";

		}
		return "";
	}
	//检查扩展名
	function upload_chk_allow_ext($msg_ext)
	{
		$msg_ext=strtolower($msg_ext);
		if(substr($msg_ext,0,1)==".") $msg_ext=substr($msg_ext,1);
		if(!isset($this->cfg_upload["allowext"])||!is_array($this->cfg_upload["allowext"])) return "系统错误，未定义allowext变量！";
		if(!in_array($msg_ext,$this->cfg_upload["allowext"]))
		{
			return "系统不允许上传扩展名为".$msg_ext."的文件！";
		}
		if(in_array($msg_ext,$this->cfg_upload["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 get_upload_path($msg_name,$msg_ext)
	{
		$str_dir=get_real_path($this->config["dir_root"],$this->config["path"]);
		if(!is_dir($str_dir)){
			dir_create($str_dir);
		}
		//$str_path=$str_dir."/".$msg_name;
		$str_path=format_path($str_dir."/".$msg_name);
		return $str_path;
	}
	function on_upload_pic($msg_path,$msg_name)
	{
		global $obj_db;
		require_once(SYS_ROOT. '/cls_ctl/ctl.pic.php');
		if(!is_pic($msg_path)) return;
		$obj_ctl_pic=new cls_ctl_pic;
		$obj_ctl_pic->on_chk_resize($msg_path);
		$arr_return=$obj_ctl_pic->on_watermark($msg_path);
	}
	function on_upload_web($msg_url)
	{
		//检查是否可以上传
		$arr_return=array("err"=>"");
		$msg_info=self::upload_chk_allow();
		if($msg_info!=""){
			$arr_return["err"]=$msg_info;
			return $arr_return;
		}
		//if(@upload($msg_url)==FALSE) return "网址无效！";
		if(!exists_remote_file($msg_url)) return "网址无效!";
		$str_uploadname=basename($msg_url);
		//检查扩展名是否允许
		$str_ext=get_file_ext($str_uploadname);
		$msg_info=self::upload_chk_allow_ext($str_ext);
		if($msg_info!=""){
			$arr_return["err"]=$msg_info;
			return $arr_return;
		}

		$data=file_get_contents($msg_url);
		$str_path=self::get_upload_path($str_uploadname,$str_ext);
		file_put_contents($str_path,$data);
		if(is_file($str_path)){
			$arr_upload=get_file_perms($str_path);
			$arr_fields=array();
			$arr_fields["attatch_user_id"]=$this->cfg_upload["uid"];
			$arr_fields["upload_name"]=$str_uploadname;
			$arr_fields["attatch_ext"]=$arr_upload["ext"];
			$arr_fields["attatch_size"]=$arr_upload["size"];
			$arr_fields["attatch_path"]=self::get_upload_url($str_uploadname);
			$arr_fields["attatch_addtime"]=TIME;
			self::on_save($arr_fields);
			//如果是图片的话,生成缩图
			self::on_upload_pic($str_path,$str_uploadname);
		}else{
			$arr_return["err"]="上传失败！";
		}
		return $arr_return;

	}
	function get_upload_url($msg_name)
	{
		if($msg_name=="") return "";
		$str_path=$this->config["path"]."/".$msg_name;
		$str_path=format_path($str_path);
		return $str_path;
	}
	function get_allow_pic(){
		$arr_x=$this->cfg_upload["allowext"];
		$arr_pic_ext=array();
		foreach($arr_x as $item){
			if(is_pic($item)){
				$arr_pic_ext[]=$item;
			}
		}
		return $arr_pic_ext;
	}
}
?>