<!--#include file="../../../inc/cms_admin.asp" -->
<%
dim act,path,id,adminname,adminip,dbbackname,SQL
act=getForm("act","both")
path = getForm("path","both")
adminname=getcookie("adminname")
adminip=getip()
id=isnum(getForm("id","both"))
		
Select  case act
	case "del" 		: delFile path
	case "delback" 	: dbsql "delete [dbpre]log_dbbackup where id="&id&""	: aspgo "Datebackup_List.asp"
	case "bakup" 	: bakup
	case "connsql"	: connsql
	case "restore" 	: restore
	case "compress" : compress
	case "sql_list" : sql_list
End Select 

Sub connsql
	dim sqlstr,sqltype
	sqlstr=request.queryString("sqlstr")
	sqlstr=replacestr(sqlstr," "," ")
	sqltype=split(sqlstr,chr(32))(0)
	select case sqltype
		case "sql_list"	:	sql_list()
		case "select"	:	connselect(sqlstr)
		case "update"	:	connexe(sqlstr)
		case "insert"	:	connexe(sqlstr)
		case "delete"	:	connexe(sqlstr)
		case else	:	echo "0SQl语句不正确，请检查"&sqltype
	End Select 
	'dbconn(sqlstr)
End Sub

Sub connexe(sql)
	dim dbbackname
	if dbType=0 then
		dbbackname=sitePath&dbPath&"backup/"&getrndname()&"_bak.asa"
		copyfile sitePath&dbPath&dbfile,dbbackname		
		dbsql("insert into [dbpre]log_dbbackup (dbname,addtime,username,ip)values ('"&dbbackname&"','"&sqltime&"','"&adminname&"','"&adminip&"')")
	end if	
	dbconn(sql)
	if err then
		echo "0执行sql出错，已备份数据库，如有问题可马上恢复数据"
	else
		echo "2执行sql成功，已备份数据库，如有问题可马上恢复数据"
	end if	
End Sub

Sub connselect(sql)
	dim rstData,trLine,strLine,responsestr,x
	trLine=""
	strLine=""  
	set rstData=dbsql(sql)
	if not rstData.EOF and not rstData.BOF then     	
		echo 1 	
   		 For each x in rstData.fields  
  		      trLine = trLine & x.name & chr(9)  
  		 Next 
		 echo trLine  
		 Do while Not rstData.EOF         		
       	 	for each x in rstData.Fields  
            strLine = strLine & x.value &  chr(9)  
       	 	next  
      	 	echo strLine  
        rstData.MoveNext  
    	loop   
	else
		echo "0SQl语句不正确，请检查"&sqltype
	end if
End Sub

Sub restore
	if dbType=0 then
 		copyfile path,sitePath&dbPath&dbfile	
	elseif dbType=1 then		
		dbsql "use master"
		dbsql "Restore database "&dataName&" from disk='"&path&"' WITH REPLACE " 
	end if
	if err<>0 then
	   echo "还原失败："&err.Descripting 
	else
	  echo "1"
	end if
 End Sub
 
Sub bakup	
	if dbType=0 then
		dbbackname=sitePath&dbPath&"backup/"&getrndname()&"_bak.asa"
		copyfile sitePath&dbPath&dbfile,dbbackname
		dbsql("insert into [dbpre]log_dbbackup (dbname,addtime,username,ip)values ('"&dbbackname&"','"&sqltime&"','"&adminname&"','"&adminip&"')")
	elseif dbType=1 then
		dbbackname=getrndname()&"_bak.dat"
		SQL="backup database "&dataName&" to disk='"&dbbackname&"'" 
		
		dbsql sql
		dbsql "insert into [dbpre]log_dbbackup (dbname,addtime,username,ip)values ('"&dbbackname&"','"&sqltime&"','"&adminname&"','"&adminip&"')"		
	end if
	if err<>0 then
	 echo "0备份失败："&err.Descripting 
	else
	 echo "1备份成功"
	end if
End Sub

Sub compress
	dim engineObj,tempDbPath
	tempDbPath = sitePath&dbpath&"tempdata.asp"
	conn.Class_Terminate
	set engineObj = server.CreateObject("JRO.JetEngine")
	engineObj.CompactDatabase "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & server.MapPath(sitePath&dbpath&dbfile),"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="& server.MapPath(tempDbPath)
	copyfile tempDbPath,sitePath&dbpath&dbfile
	delfile tempDbPath
	set engineObj = Nothing
	echo "压缩成功"
End Sub

sub sql_list
 Dim rs,sql,json,listjson,page,numPerPage,allRecordset,limit,i,orderby,descby,orderbystr,search,searchstr
	page=getform("offset","both")
	numPerPage=getform("limit","both")
	orderby=getform("sort","both")
	descby=getform("order","both")
	search=getform("search","both")
	if not isnul(search) then searchstr=""
	if isnul(orderby) then
		orderbystr="order by logid desc"
	else
		orderbystr="order by "&orderby&" "&descby
	end if
 	if isnum(numPerPage) =0 then numPerPage=10
	if isnum(page) =0 then
	  page=1
	else
	  page=page/numPerPage+1
	end if	
	sql="select * from [dbpre]log_dbsql "&orderbystr
 	set rs=dbsqlr1(sql)
	if not rs.eof then  	
	  rs.pagesize	=numPerPage
	  allRecordset = rs.recordcount
	  Set json = jsObject()
		rs.absolutepage = page
		for i = 1 to numPerPage		
		  json("logID") = rs("logid")
		  json("username") = rs("username")
		  json("ip") = rs("ip")
		  json("addtime") =  rs("addtime")
		  json("content") =  "<input class='form-control' value='"&rs("content")&"'>"
		  listjson=listjson& json.jsString()&","
		  rs.movenext
		  if rs.eof then exit for
		next
		 echo "{""total"":"&allRecordset&",""rows"":["&endstr(listjson,",")&"]}"
	else
	 	echo sql
	end if
closers rs	
end sub
%>