<!--#include file="base.asp"-->
<!--#include file="../lib/class/eycms.upload.asp"-->
<!--#include file="../lib/class/eycms.draw.asp"-->
<%
''' EYCMS 上传
''' ==================================================================

	sub getjson(byval t0,byval t1,byval t2,byval t3)
		if t0=0 Then
			eycms.echo "{""url"":"""",""original"":"""",""title"":"""",""state"":"""&t3&"""}"
		else
			eycms.echo "{""url"":"""&t1&""",""original"":"""&t2&""",""title"":"""&t2&""",""state"":"""&t3&"""}"
		end if
	end sub
	
	sub setjson(byval t0,byval t1)
		if t0=0 Then
			eycms.echo "{""status"":""n"",""info"":"""&t1&"""}"
		else
			eycms.echo "{""status"":""y"",""info"":"""&t1&"""}"
		end if
	end sub
	
	function get_uploaderr(byval t0)
		dim t1:t1=""
		select case t0
			case -1:t1="没有文件上传"
			case 1:t1="文件过大，上传失败"
			case 2:t1="不允许被上传的文件"
			case 3:t1="文件过大，且不允许被上传"
			case 4:t1="文件保存失败"
			case 5:t1="禁止上传此类文件"
		end select
		get_uploaderr=t1
	end function
	
	sub errdb()
		eycms.echo "&#38169;&#35823;&#30340;&#21442;&#25968;"
	end sub
	
	sub adddb(byval t0,byval t1,byval t2,byval t3,byval t4,byval t5,byval t6,byval t7,byval t8,byval t9,byval t10)
		eycms.db.insert "ey_attachment",array(array("classid",t9,0,0),array("cid",t8,0,0),array("filepath",t0,255,1),array("filename",t1,255,1),array("filesize",eycms.getnum(t2),255,1),array("oldname",t3,255,1),array("fileext",t4,50,1),array("ispic",eycms.is_pic("."&t4),1,0),array("createip",eycms.getip,255,1),array("createdate",t5,255,0),array("adminid",t6,0,0),array("userid",t7,0,0),array("zt",t10,0,0),array("fullname",t0&t1,0,1))
		if eycms.is_pic(t1)=1 then
			If CLng(t9)<>0 then
				dim draw
				set draw=new eycms_draw
					draw.add t0&t1
				set draw=Nothing
			End if
		end if
	end sub
	
	function getfile_ext(t0)
		dim t1
		select case lcase(t0)
			case "jpg","gif","png":t1="pic"
			case "docx":t1="doc"
			case "xlsx":t1="xls"
			case "pptx":t1="ppt"
			case "rar","zip","7z":t1="rar"
			case  "ppt","xls","doc","pdf","txt","flv","swf":t1=t0
			case else:t1="blank"
		end select
		getfile_ext=t1
	end function
	
	sub editordb()
		if isuplogin=0 then
			getjson 0,"","","登录失败，请退出重新登陆_editor"
			exit sub
		end if
		dim upload,uptype,classid,cid,pictitle
		classid=eycms.getint(eycms.fget("classid",0),0)
		cid=eycms.getint(eycms.fget("cid",0),0)

		select case act
			case "video"
				uptype=eycms.getsys("uptype_video")
			case "file"
				uptype=eycms.getsys("uptype_pic")&"|"&eycms.getsys("uptype_video")&"|"&eycms.getsys("uptype_file")
			case else
				uptype=eycms.getsys("uptype_pic")
		end select
		
		set upload=new eycms_upload
			upload.filetype=uptype
			upload.savepath=eycms.uploaddir("")
			upload.charset="utf-8"
			upload.maxsize=eycms.getsys("upmaxsize")
			upload.autosave=0
			upload.open()
			dim i,filepath,filenew,filename,filesize,oldname,fileext,errstr
			for i=1 to ubound(upload.fileitem)
				filepath=upload.savepath
				filenew=upload.form(upload.fileitem(i))
				filename=filepath&filenew
				filesize=upload.form(upload.fileitem(i)&"_size")/1024
				fileext=upload.form(upload.fileitem(i)&"_ext")
				oldname=upload.form(upload.fileitem(i)&"_name")
				pictitle=upload.form("pictitle")
				errstr=get_uploaderr(upload.form(upload.fileitem(i)&"_Err"))
				if pictitle="" then pictitle=oldname
				if eycms.strlen(errstr)>0 then
					getjson 0,"","",errstr
				else
					adddb filepath,filenew,filesize,oldname,fileext,sqltime,adminid,0,cid,classid,-3   '编辑器上传标记为 -3
					getjson 1,filename,pictitle,"SUCCESS"
				end if
			next
		set upload=nothing
	end sub
	
	sub swfdb()
		on error resume Next
		if isuplogin=0 then
			eycms.echo "{""error"":""操作失败，请退出重新登陆_swf""}"
			exit sub
		end if
		dim upload,filetype,uptype,classid,cid,url,fname
		filetype=eycms.fget("filetype",0)
		classid=eycms.getint(eycms.fget("classid",0),0)
		cid=eycms.getint(eycms.fget("cid",0),0)
		url=eycms.enhtml(eycms.fget("url",0))
		if eycms.chkhack(url) Then
			eycms.echo "{""error"":""操作失败，文件名错误_swf""}"
			exit sub
		end if
		select case filetype
			case "2":uptype=eycms.getsys("uptype_file")
			case "3":uptype=eycms.getsys("uptype_video")
			case "4":uptype=eycms.getsys("uptype_pic")&"|"&eycms.getsys("uptype_video")&"|"&eycms.getsys("uptype_file")
			case "5":uptype="swf"
			case else:uptype=eycms.getsys("uptype_pic")
		end select
		set upload=new eycms_upload
			upload.filetype=uptype
			if classid=0 and cid=2 Then
				If InStr(url,"/")<=0 Then
					set upload=Nothing
					eycms.echo "{""error"":""操作失败，参数错误_swf""}"
					exit Sub
				Else
					fname=Split(url,"/")(UBound(Split(url,"/")))
					If eycms.is_pic(fname) then
						url=Replace(url,fname,"")
						upload.savename=fname
						upload.savepath=url
						upload.charset="utf-8"
						upload.maxsize=eycms.getsys("upmaxsize")
						upload.autosave=1
					else
						set upload=Nothing
						eycms.echo "{""error"":""操作失败，参数错误_swf""}"
						exit Sub
					end If
				end If
			else
				upload.savepath=eycms.uploaddir("")
				upload.charset="utf-8"
				upload.maxsize=eycms.getsys("upmaxsize")
				upload.autosave=0
			end if

			upload.open()
			dim i,filepath,filenew,filename,filesize,oldname,fileext,errstr
			for i=1 to ubound(upload.fileitem)
				filepath=upload.savepath
				filenew=upload.form(upload.fileitem(i))
				filename=filepath&filenew
				filesize=upload.form(upload.fileitem(i)&"_size")/1024
				fileext=upload.form(upload.fileitem(i)&"_ext")
				oldname=upload.form(upload.fileitem(i)&"_name")
				errstr=get_uploaderr(upload.form(upload.fileitem(i)&"_Err"))
				if eycms.strlen(errstr)>0 then
					eycms.echo "{""error"":"""&errstr&"""}"
				Else
					if classid=0 and cid=2 Then
						eycms.echo "{""fileurl"":"""&filename&"?"&timer()&"""}"
					else
						adddb filepath,filenew,filesize,oldname,fileext,sqltime,adminid,0,cid,classid,-1
						eycms.echo "{""fileurl"":"""&filename&"""}"
					end if
				end if
			next
		set upload=Nothing
		if err then
			eycms.errlog "上传函数：swfdb<br>详情："&err.description,0
			err.clear
		End if
	end sub
	
	sub jsondb(byval t0)
		if isuplogin=0 then
			setjson 0,"","登录失败，请退出重新登陆_json"
			exit sub
		end if
		dim upload,classid,cid
		classid=eycms.getint(eycms.fget("classid",0),0)
		cid=eycms.getint(eycms.fget("cid",0),0)
		
		set upload=new eycms_upload
			upload.filetype=t0
			upload.savepath=eycms.uploaddir("")
			upload.charset="utf-8"
			upload.maxsize=eycms.getsys("upmaxsize")
			upload.autosave=0
			upload.open()
			dim i,filepath,filenew,filename,filesize,oldname,fileext,errstr
			for i=1 to ubound(upload.fileitem)
				filepath=upload.savepath
				filenew=upload.form(upload.fileitem(i))
				filename=filepath&filenew
				filesize=upload.form(upload.fileitem(i)&"_size")/1024
				fileext=upload.form(upload.fileitem(i)&"_ext")
				oldname=upload.form(upload.fileitem(i)&"_name")
				errstr=get_uploaderr(upload.form(upload.fileitem(i)&"_Err"))
				if eycms.strlen(errstr)>0 then
					setjson 0,"",errstr
				else
					adddb filepath,filenew,filesize,oldname,fileext,sqltime,adminid,0,cid,classid,-1
					setjson 1,filename
				end if
			next
		set upload=nothing
	end sub
	
	sub configdb()
		if adminid="" then exit sub
		Response.AddHeader "Content-Type", "text/plain"
		dim html:html=""
		html=html&"{"&vbcrlf
		html=html&"    ""imageActionName"": ""image"","&vbcrlf
		html=html&"    ""imageFieldName"": ""upfile"","&vbcrlf
		html=html&"    ""imageMaxSize"": "&eycms.getsys("upmaxsize")*1000&","&vbcrlf
		html=html&"    ""imageAllowFiles"": ["".png"", "".jpg"", "".gif"", "".bmp""],"&vbcrlf
		html=html&"    ""imageCompressEnable"": true,"&vbcrlf
		html=html&"    ""imageCompressBorder"": 1600,"&vbcrlf
		html=html&"    ""imageInsertAlign"": ""none"","&vbcrlf
		html=html&"    ""imageUrlPrefix"": """","&vbcrlf
		html=html&"    ""imagePathFormat"": """","&vbcrlf

		html=html&"    ""videoActionName"": ""video"","&vbcrlf
		html=html&"    ""videoFieldName"": ""upfile"","&vbcrlf
		html=html&"    ""videoPathFormat"": """","&vbcrlf
		html=html&"    ""videoUrlPrefix"": """","&vbcrlf
		html=html&"    ""videoMaxSize"": "&eycms.getsys("upmaxsize")*1000&","&vbcrlf
		html=html&"    ""videoAllowFiles"": ["".flv"", "".swf"", "".mp4"","".mp3""],"&vbcrlf

		html=html&"    ""fileActionName"": ""file"","&vbcrlf
		html=html&"    ""fileFieldName"": ""upfile"","&vbcrlf
		html=html&"    ""filePathFormat"": """","&vbcrlf
		html=html&"    ""fileUrlPrefix"": """","&vbcrlf
		html=html&"    ""fileMaxSize"": "&eycms.getsys("upmaxsize")*1000&","&vbcrlf
		html=html&"    ""fileAllowFiles"": ["&vbcrlf
		html=html&"        "".png"", "".jpg"", "".gif"", "".bmp"","&vbcrlf
		html=html&"        "".flv"", "".swf"", "".mp3"", "".mp4"","&vbcrlf
		html=html&"        "".rar"", "".zip"", "".tar"", "".gz"", "".7z"", "".bz2"", "".cab"", "".iso"","&vbcrlf
		html=html&"        "".doc"", "".docx"", "".xls"", "".xlsx"", "".ppt"", "".pptx"", "".pdf"", "".txt"", "".md"", "".xml"""&vbcrlf
		html=html&"    ]"&vbcrlf
		html=html&"}"
		eycms.echo html
	end sub
	
	on error resume next
	dim act:act=eycms.fget("act",0)
	select case act
		case "swf":swfdb
		case "config":configdb
		case "json":jsondb(eycms.getsys("uptype_pic"))
		case "json_down":jsondb(eycms.getsys("uptype_file"))
		case "other":islogin:errdb
		case else:editordb
	end select
%>
