<?php
	require dirname(__FILE__).'/include/common.inc.php';
	require DIRCMS_ROOT.'/include/admin/global.func.php';
	$action=empty($action)?'files':trim($action);
	!$_userid && exit('网站已禁止游客上传文件!');
	switch($action)
	{
		case 'files':
			if(isset($isupload) && $isupload==1)
			{
				require_once(DIRCMS_ROOT.'include/upload.class.php');
				$upload=new upload('files','upload/file/'.date('m').'/'.date('d'));
				$imagearr=$upload->files;
				showmsg("文件上传成功！<script language='javascript'>$(window.opener.document).find(\"form[@name='myform'] #".$objid."\").val(\"$imagearr[0]\");</script>");
			}
			include admin_tlp('upload_files');
			break;	
		case 'images':
			if(isset($isupload) && $isupload==1)
			{
				require_once(DIRCMS_ROOT.'include/upload.class.php');
				$upload=new upload('files','upload/file/'.date('m').'/'.date('d'),'',array('jpg','jpeg','gif'));
				$imagearr=$upload->files;
				require_once(DIRCMS_ROOT.'include/image.class.php');
				$image=new image();

/*
				if($is_form_thumb==1) //先生成缩略图
				{
					foreach($imagearr as $val)
					{
						$imgs[]=$image->thumb($val,$thumb_width,$thumb_height);
					}
					$imagearr=$imgs;
				}
*/
				if($is_form_watermark==1) //再加水印
				{
					foreach($imagearr as $val)
					{
						$image->watermark($val,WATERMARK_POS,WATERMARK_FILE,WATERMARK_PCT,WATERMARK_WORDS,WATERMARK_COLOR);
					}
				}
				showmsg("文件上传成功！<script language='javascript'>$(window.opener.document).find(\"form[@name='myform'] #".$objid."\").val(\"$imagearr[0]\");</script>");
			}
			include admin_tlp('upload_images');
			break;
		case 'imagess':
			if(isset($isupload) && $isupload==1)
			{
				@set_time_limit(0);
				$sort=12;
				$f_type=strtolower(str_replace('|',',',UPLOAD_FTYPE));//设置可上传的文件类型 
				$file_size_max=UPLOAD_SIZE;//限制单个文件上传最大容量 
				$overwrite = 0;//是否允许覆盖相同文件,1:允许,0:不允许 
				$f_input="Files";//设置上传域名称
				$t_array=array();
				foreach($_FILES[$f_input]["error"] as $key => $error)
				{ 
					$up_error="no"; 
					if ($error == UPLOAD_ERR_OK)
					{ 
						$f_name=$_FILES[$f_input]['name'][$key];
						$uploadfile=$uploaddir.strtolower(basename($f_name)); 
						$tmp_type=substr(strrchr($f_name,"."),1);//获取文件扩展名
						if(!in_array($tmp_type,array('jpg','jpeg','gif')))showmsg("文件格式不正确！");
						$tmp_type=strtolower($tmp_type); 
						if(!stristr($f_type,$tmp_type))
						{ 
							echo "<script>alert('对不起,不能上传".$tmp_type."格式文件, ".$f_name." 文件上传失败!')</script>"; 
							$up_error="yes"; 
						} 
						 
						if ($_FILES[$f_input]['size'][$key]>$file_size_max) 
						{ 
							echo "<script>alert('对不起,你上传的文件 ".$f_name." 容量为".round($_FILES[$f_input]['size'][$key]/1024)."Kb,大于规定的".($file_size_max/1024)."Kb,上传失败!')</script>"; 
							$up_error="yes"; 
						} 
							 
						if (file_exists($uploadfile)&&!$overwrite)
						{ 
							echo "<script>alert('对不起,文件 ".$f_name." 已经存在,上传失败!')</script>"; 
							$up_error="yes"; 
						} 
						$string = 'abcdefghijklmnopgrstuvwxyz0123456789';
						$rand = '';
						for ($x=0;$x<12;$x++)$rand .= substr($string,mt_rand(0,strlen($string)-1),1);
						$t=date("ymdHis").substr($gettime[0],2,6).$rand;
						$attdir="upload/file/".date('m').'/'.date('d').'/';  
						if(!is_dir($attdir))@mkdir($attdir);
						$uploadfile=$attdir.$t.".".$tmp_type; 
						$t_array[]=$uploadfile;
						if(($up_error!="yes") and (move_uploaded_file($_FILES[$f_input]['tmp_name'][$key], $uploadfile)))
						{ 
							$_msg=$_msg.$f_name.'上传成功\n';
						} 
						else
						{
							$_msg=$_msg.$f_name.'上传失败\n';
						}
					} 					
				}
				exit("<script src='images/js/jquery.min.js'></script><script language='javascript'>alert('".implode('\r\n',$t_array)."上传成功！');$(window.parent.document).find(\"form[@name='upload'] #imagess_value\").val(\"".implode('\r\n',$t_array)."\");</script>");
			}
			include admin_tlp('upload_imagess');
			break;
		case 'thumb':
			if(isset($isupload) && $isupload==1)
			{
				require_once(DIRCMS_ROOT.'include/upload.class.php');
				$upload=new upload('files','upload/file/'.date('m').'/'.date('d'),'',array('jpg','jpeg','gif'));
				$imagearr=$upload->files;
				require_once(DIRCMS_ROOT.'include/image.class.php');
				$image=new image();

				$catid=intval($catid);
				$r=cache_read('category'.$catid.'.cache.php',DIRCMS_ROOT.'data/cache_category/');
				if($catid && $r)
				{
					$thumb_width=$r['thumb_width'];
					$thumb_height=$r['thumb_height'];
					if($r['isthumb'])
					{
						foreach($imagearr as $val)
						{
							$imgs[]=$image->thumb($val,$thumb_width,$thumb_height);
						}
						$imagearr=$imgs;
					}
				}
				else
				{
					$thumb_width=THUMB_WIDTH;
					$thumb_height=THUMB_HEIGHT;
					if(ISTHUMB)
					{
						foreach($imagearr as $val)
						{
							$imgs[]=$image->thumb($val,$thumb_width,$thumb_height);
						}
						$imagearr=$imgs;
					}
				}
				
				if($is_form_watermark==1) //缩略图不加水印
				{
					foreach($upload->files as $val)
					{
						$image->watermark($val,WATERMARK_POS,WATERMARK_FILE,WATERMARK_PCT,WATERMARK_WORDS,WATERMARK_COLOR);
					}
				}
				showmsg("文件上传成功！<script language='javascript'>$(window.opener.document).find(\"form[@name='myform'] #thumb\").val(\"$imagearr[0]\");</script>");
			}
			include admin_tlp('upload_thumb');
			break;
		default:
			exit('Bad request!');
			break;
	}
?>