<!--#include file="conn.asp"-->
<!--#include file="inc/class_sys.asp"-->
<!--#include file="inc/md5.asp"-->
<%
'图片标题
Const g_photo_title = 20
'图标简介
Const g_photo_about = 200
dim oblog,action,userid
set oblog=new class_sys
oblog.autoupdate=false
oblog.start
Response.contentType="application/xml"
Response.Expires=0
Response.Write("<?xml version=""1.0"" encoding=""gb2312""?>")
action=trim(request("action"))
userid=Int(request("userid"))
select case action
	case "menu"
		call menu()
	case "photo"
		call photo()
	case "sysclass"
		call sysclass()
	case "subject"
		call subject()
	case "chklogin"
		call chklogin()
	case "islogin"
		call islogin()
	case "delfile"
		call delfile()
	case "writeabout"
		call writeabout()
end select

sub menu()
	dim rs,menustr,i
	set rs=oblog.execute("select subjectname,subjectid from oblog_subject where userid=" & userid & " And SubjectType=1 order by ordernum")
	while not rs.eof
		menustr=menustr&"<menu label="""&rs("subjectname")&""" subjectid="""&rs("subjectid")&""" />"
		rs.movenext
	wend
%>
<menu>
	<menu label="所有图片" subjectid="0" />
	<%=menustr%>
	<menu label="大头贴" subjectid="-1" />
</menu>
<%
	set rs=nothing
end sub

sub photo()
	dim rs,ssql,subjectid,pstr
	subjectid=clng(request("subjectid"))
	if subjectid>0 then ssql=" and userClassId="&subjectid
	if subjectid=-1 then ssql=" and isBigHead=1"
	set rs=oblog.execute("select photo_title,file_readme,file_name,file_path,fileid from oblog_upfile where userid="&userid&ssql&" and isphoto=1 order by fileid desc")
	while not rs.eof 
		pstr=pstr&"<p _name="""&rs("photo_title")&""" _url="""&rs("file_path")&""" _fileid="""&rs("fileid")&""" _about="""&rs("file_readme")&""" />"
	rs.movenext
	wend
	response.Write "<photo>"&pstr&"</photo>"
	set rs=nothing
end sub

sub sysclass()
	dim rs,pstr
	set rs=oblog.execute("select id,classname from oblog_logclass where idType=1 order by RootID,OrderID")
	while not rs.eof
		pstr=pstr&"<c _name="""&rs("classname")&""" _id="""&rs("id")&"""/>"
		rs.movenext
	wend
	response.Write "<sysclass>"&pstr&"</sysclass>" 
	set rs=nothing
end sub

sub subject()
	dim rs,pstr
	set rs=oblog.execute("select subjectid,subjectname from oblog_subject where subjectType=1 and userid="&userid&" order by Ordernum")
	while not rs.eof
		pstr=pstr&"<s _name="""&rs("subjectname")&""" _id="""&rs("subjectid")&"""/>"
		rs.movenext
	wend
	response.Write "<subject>"&pstr&"</subject>" 
	set rs=nothing
end sub

sub chklogin()
	
end sub

sub islogin()
	dim xmlstr
	if not oblog.checkuserlogined() then
		xmlstr="<m msg=""未登录"" login=""0"" />"
	else
		if oblog.l_ulevel=6 then
			xmlstr="<m msg=""您的账号未通过审核"" login=""0"" />"
		elseif oblog.l_uid<>userid then
			xmlstr="<m msg=""您不是当前用户，无权限上传"" login=""0"" />"
		else
			'xmlstr="<m msg=""已登录"" login=""1"" />"
			xmlstr="<m msg=""已登录"" login=""1"" maxsize="""&int(oblog.l_Group(24,0)-oblog.l_uUpUsed/1024)&""" onesize="""&oblog.l_Group(23,0)&""" />"
		end if
	end if
	response.Write "<islogin>"&xmlstr&"</islogin>" 
end sub

sub chklogin()
	dim username,password,xmlstr
	username=oblog.filt_badstr(trim(request("username")))
	password=md5(request("password"))
	oblog.ob_chklogin username,password,0
	if oblog.errstr<>"" then
		xmlstr="<m msg="""&oblog.errstr&""" login=""0"" />"
	else
		xmlstr="<m msg=""登录成功"" login=""1"" />"
	end if
	response.Write "<chklogin>"&xmlstr&"</chklogin>" 
end sub

sub writeabout()
	dim pname,pabout,fileid,xmlstr,myuserid
	pname = ProtectSQL(trim(request("pname")))
	pabout = ProtectSQL(trim(request("pabout")))
	fileid=Int(trim(request("fileid")))
	myuserid=int(trim(request("userid")))
	'初始化返回字符串
	xmlstr="<m msg="""&oblog.checkuserlogined()&""" isadd=""0"" />"
	'未登陆
	if not oblog.checkuserlogined() then
		xmlstr="<m msg=""未登录"" isadd=""0"" />"
	else
		if oblog.l_ulevel=6 then
			xmlstr="<m msg=""您的账号未通过审核"" isadd=""0"" />"
		elseif oblog.l_uid<>myuserid then
			xmlstr="<m msg=""不是当前用户"" isadd=""0"" />"
		else
				'检测字串长度
				if len(pname)>g_photo_title then
					xmlstr="<m msg='图片名称字数不能超过:"&g_photo_title&"' isadd='2' />"
					response.Write "<file>"&xmlstr&"</file>"
					exit sub
				elseif len(pabout)>g_photo_about then
					xmlstr="<m msg='图片简介字数不能超过:"&g_photo_about&"' isadd='2' />"
					response.Write "<file>"&xmlstr&"</file>"
					exit sub
				end if
				
				dim userid,sql,rs
				sql="select * from [oblog_upfile] where fileid="&fileid&" and userid="&oblog.l_uid
				set rs=server.createobject("adodb.recordset")
				link_database
				rs.open sql,conn,1,3
				if not rs.eof then
					userid=rs("userid")
					oblog.execute("update [oblog_upfile] set photo_title='"&pname&"',file_readme='"&pabout&"' where userid="&userid&" and fileid="&fileid)
					xmlstr="<m msg=""操作成功"" isadd=""1"" />"
				else
					rs.close
					set rs=nothing
				End If
		end if
	end if
	response.Write "<file>"&xmlstr&"</file>" 

end sub

sub delfile()
	dim fileid,xmlstr
	fileid=trim(request("fileid"))
	if not oblog.checkuserlogined() then
		response.Write("<delfile><m msg=""请登录后执行删除操作"" isdel=""0"" /></delfile>")
		exit sub
	end if
	 If InStr(fileid, ",") > 0 Then
        Dim n, i
        fileid = FilterIDs(fileid)
        n = Split(fileid, ",")
        For i = 0 To UBound(n)
            if delonefile (n(i)) then
				xmlstr="<m msg=""删除成功"" isdel=""1"" />"
			else
				xmlstr="<m msg=""删除失败"" isdel=""0"" />"
			end if
        Next
    Else
        if delonefile (fileid) then
			xmlstr="<m msg=""删除成功"" isdel=""1"" />"
		else
			xmlstr="<m msg=""删除失败"" isdel=""0"" />"
		end if
    End If
	response.Write "<delfile>"&xmlstr&"</delfile>" 
end sub


function delonefile(fileid)
	fileid=clng(fileid)
	dim userid,filesize,filepath,fso,photofile,isphoto,imgsrc,sql,rs	
	sql="select * from [oblog_upfile] where fileid=" & fileid&" and userid="&oblog.l_uid
	set rs=server.createobject("adodb.recordset")
	link_database
	rs.open sql,conn,1,3
	if not rs.eof then
		userid=rs("userid")
		filesize=Int(rs("file_size"))
		filepath=rs("file_path")
		photofile=rs("photofile")
		isphoto=rs("isphoto")
		rs.delete
		rs.update
		rs.close
		oblog.execute("update [oblog_user] set user_upfiles_size=user_upfiles_size-"&filesize&" where userid="&userid)
		if filepath<>"" then
			imgsrc=filepath
			Set fso = server.CreateObject(oblog.CacheCompont(1))
			if instr("jpg,bmp,gif,png,pcx",right(imgsrc,3))>0 then
				imgsrc=replace(imgsrc,right(imgsrc,3),"jpg")
				imgsrc=replace(imgsrc,right(imgsrc,len(imgsrc)-InstrRev(imgsrc,"/")),"pre"&right(imgsrc,len(imgsrc)-InstrRev(imgsrc,"/")))
				if  fso.FileExists(Server.MapPath(imgsrc)) then
					fso.DeleteFile Server.MapPath(imgsrc)
				end if
			end if
			if fso.FileExists(Server.MapPath(filepath)) then 
				fso.DeleteFile Server.MapPath(filepath)
			end if
			set fso=nothing
		end if
		delonefile=true
	else
		delonefile=false
		rs.close
		set rs=nothing
	End If
end function
%>