<!--#include file="../inc/Conn.asp"-->
<!--#include file="Inc/Admin.Asp"-->
<!--#include file="Inc/Head.Asp"-->
<%
PurviewValue="Admin_Sql"
If not CheckAdmin(PurviewValue) then
Response.Write("<script language=""JavaScript"">alert(""越权操作!"");history.go(-1);")
response.end
End If
%>
<%

Server.ScriptTimeOut = 9999999
Dim EndTime
Const NeedCheckComeUrl = True   '是否需要检查外部访问

Const PurviewLevel = 2      '0--不检查，1--超级管理员，2--普通管理员
Const PurviewLevel_Channel = 1   '0--不检查，1--频道管理员，2--栏目总编，3--栏目管理员
Const PurviewLevel_Others = ""   '其他权限
AdminTitle="内容批量替换"
Call AdminHead

if trim(request("type1"))="step001" then
	Call ShowChird()
else
%>
<table width="100%" border="0" align="center" cellpadding="2" cellspacing="1" class="border">
	<tr class="title"> 
		<td height="22" colspan="2" align="center">数据库内容替换程序</td>
	</tr>
	<tr class="tdbg">
		<td width="70" height="30" ><strong>管理导航：</strong></td>
		<td><a href="Admin_SqlReplace.asp">数据库内容批量替换（普通模式）</a>&nbsp;|&nbsp;<a href="Admin_SqlReplace.asp?Action=Main2">数据库内容批量替换（高级模式）</a>&nbsp;|&nbsp;<a href="Admin_SqlReplace.asp?Action=Main3">数据库内容批量替换（赋值模式）</a></td></tr>
</table>
<%
Dim i
	i=0
	Action=trim(request("action"))
	Select Case Action
	Case "Replace1","Replace2","Replace3"
		call step1()
	Case "Main2"
		call Main2()
	Case "Main3"
		call Main3()
	Case Else
		call Main1()
	End Select
end if

Sub Main1()
%>
<script language="JavaScript" type="text/javascript">
function changedb(){
    tablechird.location.href="Admin_SqlReplace.asp?type1=step001&type="+document.myform.TableName.value;
}
function CheckForm(){
  if (document.myform.TableName.value==''){
    alert('数据表名不能为空！');
    document.myform.TableName.focus();
    return false;
  }
  if (document.myform.ColumnName.value==''){
    alert('字段名不能为空！');
    //document.myform.ColumnName.focus();
    return false;
  }
  if (document.myform.strOld.value==''){
    alert('替换字符不能为空！');
    document.myform.strOld.focus();
    return false;
  }
  return true;  
}
</script>
<form method="post" name="myform" onSubmit="return CheckForm();" action="Admin_SqlReplace.asp" target="_self">
<table width="100%" border="0" align="center" cellpadding="2" cellspacing="1" class="border">
	<tr class="title"><td height="22" colspan="2" align="center"><strong>数 据 库 内 容 批 量 替 换（普通模式）</strong></td></tr>
	<tr class="tdbg"><td height="40" align="right" width=80>数据表名：</td><td height="40"><%Call ShowMain()%><font color=red> *</font></td></tr>
	<tr class="tdbg"><td height="40" align="right">字 段 名：</td><td height="40"><input name="ColumnName" type="text" value="" size="10" readonly /><iframe style="top:2px;" ID="tablechird" src="Admin_SqlReplace.asp?type1=step001" frameborder=0 scrolling=no width="100%" height="20"></iframe></td></tr>
	<tr class="tdbg"><td height="40" align="right">其它条件：</td><td height="40"><input name="OtherSql" type="text" value="" size="72">(一般不需填写)</td></tr>
	<tr class="tdbg"><td height="40" align="right">替换数量：</td><td height="40"><input name="thnum" type="text" value="" size="72"> 替换多少条数据,0或不填写为不限</td></tr>
	<tr class="tdbg"><td height="40" align="right">将 字 符：</td><td height="40"><textarea name="strOld" cols="60" rows="4"></textarea><font color=red> *</font></td></tr>
	<tr class="tdbg"><td height="40" align="right">替 换 成：</td><td height="40"><textarea name="strNew" cols="60" rows="4"></textarea></td></tr>
	<tr class="tdbg"><td height="40" align="right">注意事项：</td><td height="40">	1、请备份您的魅力数据库文件。<br />2、本操作的更新时间视您数据的多少以及服务器（或本地机器）的配置决定，如果数据很多，更新可能很慢，在这过程千万不能刷新页面或关闭浏览器，如果出现超时或者错误提示，请使用备份数据重新进行操作。</td></tr>
	<tr class="tdbg"><td height="40" colspan="2" align="center"><input type="hidden" name="Action" value="Replace1"><input type="submit" name="Submit" value="开始替换"></td></tr>
</table>
</form>
<%
End Sub

Sub Main2()
%>
<script language="JavaScript" type="text/javascript">
function changedb(){
    tablechird.location.href="Admin_SqlReplace.asp?type1=step001&type="+document.myform.TableName.value;
}
function CheckForm1(){
  if (document.myform.TableName.value==''){
    alert('数据表名不能为空！');
    document.myform.TableName.focus();
    return false;
  }
  if (document.myform.ColumnName.value==''){
    alert('字段名不能为空！');
    //document.myform.ColumnName.focus();
    return false;
  }
  if (document.myform.strOld.value==''){
    alert('替换开始代码不能为空！');
    document.myform.strOld.focus();
    return false;
  }
   if (document.myform.strOld1.value==''){
    alert('替换结束代码不能为空！');
    document.myform.strOld1.focus();
    return false;
  }
  return true;  
}
</script>
<form method="post" name="myform" onSubmit="return CheckForm1();" action="Admin_SqlReplace.asp" target="_self">
<table width="100%" border="0" align="center" cellpadding="2" cellspacing="1" class="border">
	<tr class="title"><td height="22" colspan="2" align="center"><strong>数 据 库 内 容 批 量 替 换 （高级模式）</strong></td></tr>
	<tr class="tdbg"><td height="40" align="right" width=80><b>特别事项</b>：</td><td height="40"><font color=red>高级模式较易出错，且替换速度较慢、较占服务器CPU和内存资源,请谨慎使用!如无特殊需要,建议使用普通模式!</font></td></tr>
	<tr class="tdbg"><td height="40" align="right" width=80>数据表名：</td><td height="40"><%Call ShowMain()%><font color=red> *</font></td></tr>
	<tr class="tdbg"><td height="40" align="right">字 段 名：</td><td height="40"><input name="ColumnName" type="Text" value="" size="10" readonly /><iframe style="top:2px;" ID="tablechird" src="Admin_SqlReplace.asp?type1=step001" frameborder=0 scrolling=no width="100%" height="20"></iframe></td></tr>
	<tr class="tdbg"><td height="40" align="right">其它条件：</td><td height="40"><input name="OtherSql" type="text" value="" size="72">(一般不需填写)</td></tr>
	<tr class="tdbg"><td height="40" align="right">替换数量：</td><td height="40"><input name="thnum" type="text" value="" size="72"> 替换多少条数据,0或不填写为不限</td></tr>
	<tr class="tdbg"><td height="40" align="right">字符开始：</td><td height="40"><textarea name="strOld" cols="60" rows="3"></textarea><font color=red> *</font></td></tr>
	<tr class="tdbg"><td height="40" align="right">字符结束：</td><td height="40"><textarea name="strOld1" cols="60" rows="3"></textarea><font color=red> *</font></td></tr>
	<tr class="tdbg"><td height="40" align="right">替 换 成：</td><td height="40"><textarea name="strNew" cols="60" rows="3"></textarea></td></tr>
	<tr class="tdbg"><td height="40" align="right">注意事项：</td><td height="40">	1、请备份您的魅力的数据库文件。<br />2、本操作的更新时间视您数据的多少以及服务器（或本地机器）的配置决定，如果数据很多，更新可能很慢，在这过程千万不能刷新页面或关闭浏览器，如果出现超时或者错误提示，请使用备份数据重新进行操作。</td></tr>
	<tr class="tdbg"><td height="40" colspan="2" align="center"><input type="hidden" name="Action" value="Replace2"><input type="submit" name="Submit" value="开始替换"></td></tr>
</table>
</form>
<%
End Sub

Sub Main3()
%>
<script language="JavaScript" type="text/javascript">
function changedb(){
    tablechird.location.href="Admin_SqlReplace.asp?type1=step001&type="+document.myform.TableName.value;
}
function CheckForm1(){
  if (document.myform.TableName.value==''){
    alert('数据表名不能为空！');
    document.myform.TableName.focus();
    return false;
  }
  if (document.myform.ColumnName.value==''){
    alert('字段名不能为空！');
    //document.myform.ColumnName.focus();
    return false;
  }
  return true;  
}
</script>
<form method="post" name="myform" onSubmit="return CheckForm1();" action="Admin_SqlReplace.asp" target="_self">
<table width="100%" border="0" align="center" cellpadding="2" cellspacing="1" class="border">
	<tr class="title"><td height="22" colspan="2" align="center"><strong>数 据 库 内 容 批 量 替 换 （赋值模式）</strong></td></tr>
	<tr class="tdbg"><td height="40" align="right" width=80><b>特别事项</b>：</td><td height="40"><font color=red>赋值模式不理会原有数据,而是把选定字段的值直接修改为新的值,这会导致原数据丢失并变为新数据!请谨慎使用!</font></td></tr>
	<tr class="tdbg"><td height="40" align="right" width=80>数据表名：</td><td height="40"><%Call ShowMain()%><font color=red> *</font></td></tr>
	<tr class="tdbg"><td height="40" align="right">字 段 名：</td><td height="40"><input name="ColumnName" type="text" value="" size="10" readonly /><iframe style="top:2px;" ID="tablechird" src="Admin_SqlReplace.asp?type1=step001" frameborder=0 scrolling=no width="100%" height="20"></iframe></td></tr>
	<tr class="tdbg"><td height="40" align="right">其它条件：</td><td height="40"><input name="OtherSql" type="text" value="" size="72">(一般不需填写)</td></tr>
	<tr class="tdbg"><td height="40" align="right">修改数量：</td><td height="40"><input name="thnum" type="text" value="" size="72"> 替换多少条数据,0或不填写为不限</td></tr>
	<tr class="tdbg"><td height="40" align="right">修 改 成：</td><td height="40"><textarea name="strNew" cols="60" rows="3"></textarea></td></tr>
	<tr class="tdbg"><td height="40" align="right">注意事项：</td><td height="40">	1、请备份您的魅力的数据库文件。<br />2、本操作的更新时间视您数据的多少以及服务器（或本地机器）的配置决定，如果数据很多，更新可能很慢，在这过程千万不能刷新页面或关闭浏览器，如果出现超时或者错误提示，请使用备份数据重新进行操作。</td></tr>
	<tr class="tdbg"><td height="40" colspan="2" align="center"><input type="hidden" name="Action" value="Replace3"><input type="submit" name="Submit" value="开始替换"></td></tr>
</table>
</form>
<%
End Sub


'**************************************************
'过程名：Step1
'作  用：数据替换主调用程序
'参  数：无
'**************************************************
Sub Step1()
	dim rs,sql
	BeginTime=timer
	dim TableName,ColumnName,OtherSql,strOld,strOld1,strNew,thnum
	TableName	= trim(request("TableName"))
	ColumnName	= trim(request("ColumnName"))
	OtherSql	= trim(request("OtherSql"))
	strOld		= request("strOld")
	strOld1		= request("strOld1")
	strNew		= request("strNew")
	thnum		= trim(request("thnum"))
	if thnum="" then
		thnum=0
	End if
	if TableName="" then
		response.write "请输入要替换的数据表名！"
		exit sub
	end if
	if ColumnName="" then
		response.write "请输入要替换的字段名！"
		exit sub
	end if
	if action="Replace1" then
		if strOld="" then
			response.write "请输入要替换的字符！"
			exit sub
		end if
	else 
		if action="Replace2" then
			if strOld="" then
				response.write "请输入要替换的字符开始代码！"
				exit sub
			end if
			if strOld1="" then
				response.write "请输入要替换的字符结束代码！"
				exit sub
			end if
		End if		
	End if
	on error resume next
	Set rs = Server.CreateObject("ADODB.Recordset")
	if thnum<>0 then
		sql = "select Top " & thnum & " " & ColumnName & "  from " & TableName
	else
		sql = "select " & ColumnName & "  from " & TableName
	End if
	if OtherSql<>"" then
	  sql = sql & " where " & OtherSql
	end if
	OpenConn : rs.open sql,conn,1,1
	if err.number<>0 then
		response.write "<font color=red>数据库操作失败，请检查数据表名和字段名添写是否正确</font>"
		exit sub
	end if
	set rs=nothing
	on error GoTo 0
	response.write "正在替换有关数据……<font color=red>在此过程中请勿刷新页面或关闭窗口！</font><br />"
	call ReplaceData(TableName,ColumnName,OtherSql,strOld,strOld1,strNew,thnum)	
	EndTime=timer	

    Response.Write "<table cellpadding=2 cellspacing=1 border=0 width=400 class='border' align=center><tr align='center' class='title'><td height='22'><strong>恭喜你！</strong></td></tr>" & vbCrLf
    Response.Write "  <tr class='tdbg'><td height='100' valign='top'><br />" & "共替换：<font color='#0000ff'>"&i&"</font> 项数据。<br />共耗时：<font color='#0000ff'>"&FormatNumber((EndTime-BeginTime)*1000,2)&"</font> 毫秒。" & "</td></tr>" & vbCrLf
    Response.Write "  <tr align='center' class='tdbg'><td><a href='#' onclick=history.go(-1) >&lt;&lt; 返回上一页</a></td></tr></table>"& vbCrLf

End Sub

'**************************************************
'过程名：ReplaceData
'作  用：显示数据表列表
'参  数：TableName	----数据表名
'        ColumnName	----字段名
'        OtherSql	----其他条件
'        strOld		----查找字符(或查找开始代码)
'        strOld1	----查找结束代码
'        strNew		----替换字符
'        thnum		----替换项目数
'**************************************************
Sub ReplaceData(TableName,ColumnName,OtherSql,strOld,strOld1,strNew,thnum)
	dim rs,sql,tt
	Response.Write "<li>正在替换数据</li>&nbsp;&nbsp;"
	Set rs = Server.CreateObject("ADODB.Recordset")
	if thnum<>0 then
		sql = "select top " & thnum & " " & ColumnName & "  from " & TableName 
	else
		sql = "select " & ColumnName & "  from " & TableName 
	End if
	if OtherSql<>"" then
	  sql = sql & " where " & OtherSql
	end if
	rs.open sql,conn,1,3
	
	Select Case Action
	Case "Replace1"
		do while not rs.eof
			if instr(rs(ColumnName),strOld)<>0 then
				rs(ColumnName)=replace(rs(ColumnName),strOld,strNew)
				i=i+1
				Response.Write "."
			end if
			rs.update
			rs.movenext
		loop
	Case "Replace2"
		Set tt=new RegExp
  		tt.IgnoreCase =true
  		tt.Global=True
  		tt.Pattern=strOld&"[^"&strOld1&"]*"&strOld1
		do while not rs.eof
 			rs(ColumnName) = tt.Replace(rs(ColumnName),strNew) 
  			i=i+1
			Response.Write "."
			rs.update
			rs.movenext
		loop
		Set tt=Nothing
	Case "Replace3"
		do while not rs.eof
			Response.Write strNew
			rs(ColumnName)=strNew
			i=i+1
			Response.Write "."
			rs.update
			rs.movenext
		loop
	End Select
	rs.close
	set rs=nothing
	response.write "&nbsp;&nbsp;<font color='#009900'>替换数据成功！</font>"
End Sub

'**************************************************
'过程名：ShowMain
'作  用：显示数据表列表
'参  数：无
'**************************************************
Sub ShowMain()
	dim rs,tablename,temptable
	Set rs = Conn.OpenSchema(4)
	tablename=""
	response.write "<select name='TableName' onChange='changedb()'><option value=''>请选择一个数据表</option>"
	Do Until rs.EOF
		temptable=rs("Table_name")
		'sql系统表替换
		If temptable="syscomments" then temptable=""
		If temptable="sysconstraints" then temptable=""
		If temptable="sysdepends" then temptable=""
		If temptable="sysfiles1" then temptable=""
		If temptable="sysfulltextcatalogs" then temptable=""
		If temptable="sysfulltextnotify" then temptable=""
		If temptable="sysindexes" then temptable=""
		If temptable="sysindexkeys" then temptable=""
		If temptable="sysmembers" then temptable=""
		If temptable="sysproperties" then temptable=""
		If temptable="sysprotects" then temptable=""
		If temptable="sysreferences" then temptable=""
		If temptable="syssegments" then temptable=""
		'------------
		If temptable="dtproperties" then temptable="" 
		If temptable="sysaltfiles" then temptable="" 
		If temptable="syscharsets" then temptable=""
		If temptable="sysconfigures" then temptable=""
		If temptable="syscurconfigs" then temptable=""
		If temptable="sysdatabases" then temptable=""
		If temptable="syslanguages" then temptable=""
		If temptable="syslogins" then temptable=""
		If temptable="sysoledbusers" then temptable="" 
		If temptable="sysprocesses" then temptable=""
		If temptable="sysremotelogins" then temptable=""
		If temptable="syscolumns" then temptable=""
		If temptable="sysconstrains" then temptable=""
		If temptable="sysfilegroups" then temptable=""
		If temptable="sysfiles" then temptable="" 
		If temptable="sysforeignkeys" then temptable=""
		If temptable="sysindexs" then temptable=""
		If temptable="sysmenbers" then temptable=""
		If temptable="sysobjects" then temptable=""
		If temptable="syspermissions" then temptable=""
		If temptable="systypes" then temptable="" 
		If temptable="sysusers" then temptable=""
		'sql系统表替换结束
		if temptable <> tablename and temptable <> "" and temptable <> "MSysAccessXML" and temptable <> "MSysAccessObjects" then
			Response.write "<option value='" & temptable & "'>" & temptable & "</option>"
			Tablename = temptable
		end if
	rs.MoveNext
	Loop
	response.write "</select>"
	rs.close:set rs=nothing
End Sub

'**************************************************
'过程名：ShowChird
'作  用：显示指定数据表的字段列表
'参  数：无
'**************************************************
Sub ShowChird()
	dim rs
	response.write "<body class='tdbg'><form method='post' name='myform11' action='Admin_SqlReplace.asp'><select name='dbname2' onChange=parent.document.myform.ColumnName.value=document.myform11.dbname2.value><option value=''>请选择一个字段　</option>"
	if trim(request("type"))<>"" then	
		Set rs = Conn.OpenSchema(4)	
		Do Until rs.EOF or rs("Table_name") = trim(request("type"))
			rs.MoveNext
		Loop
		Do Until rs.EOF or rs("Table_name") <> trim(request("type"))
			response.write "<option value='"&rs("column_Name")&"'>"&rs("column_Name")&"</option>"
			rs.MoveNext
		loop
		rs.close:set rs=nothing
	End if
	response.write "</select><font color=red> *</font></form><script language = 'JavaScript'>parent.document.myform.ColumnName.value=document.myform11.dbname2.value;</script></body>"
End Sub
Sub DoSql()
	Cl.SaveAdminLog
	Dim StrSql,DataBase,StrMsg
	StrSql=Replace(Request("StrSql"),vbcrlf,"@$@$@")
	DataBase=Cl.Checkstr(Request("DataBase"))
	if StrSql="" then
		Call Cl.OutMsg(0,"执行SQL语句失败，命令不能为空！","javascript:history.go(-1)")
	else
		if Instr(StrSql,"@$@$@")>0 then
			StrSql=Split(StrSql,"@$@$@")
			for i=0 to Ubound(StrSql)
				On Error Resume Next
				if DataBase="User" then
					Cl.Execute_U(StrSql(i))
				else
					Cl.Execute(StrSql(i))
				end if
				if Err then
					StrMsg=StrMsg & "<li><font Color=Red>执行SQL语句“"&StrSql(i)&"”失败，原因："&Err.Description&"，请检查SQL语法是否正确！</Font></li>" & vbcrlf
					Err.Clear
				else
					StrMsg=StrMsg & "<li><font Color=blue>执行SQL语句“"&StrSql(i)&"”成功！</Font></li>" & vbcrlf
					'Call OutMsg("执行SQL语句成功！","Admin_Data.asp?Action=DoSqlMain")
				end if
			Next
			Session("DoSql")=DataBase
			Call Cl.ShowSuc(StrMsg)
		else
			On Error Resume Next
			if DataBase="User" then
				Cl.Execute_U(StrSql)
			else
				Cl.Execute(StrSql)
			end if
			if Err then
				Err.Clear
				Call Cl.OutMsg(0,"执行SQL语句失败，原因："&Err.Description&"，请检查SQL语法是否正确！","javascript:history.go(-1)")
			else
				Session("DoSql")=DataBase
				Call Cl.OutMsg(0,"执行SQL语句成功！","Admin_Data.asp?Action=DoSqlMain")
			end if
		end if
	end if
end sub
sub DoSqlMain()
%>
<br />
  <table width="100%" border="0" align="center" cellpadding="2" cellspacing="1" Class="border">
	<form method="POST" action="Admin_Data.asp" id="form1" name="form1">
	<tr> 
	  <td height="22" colspan="2" class="title" align="center"><strong>数据库Sql执行程序</strong></td>
	</tr>
	<tr> 
	  <td class="tdbg" colspan="2" align="center"><font color="red">(警告：执行语句要万分小心！)</font><br /><font color="blue">提示：可批量执行SQL语句，每条SQL语句请用回车分开，但最后一条结尾不要加回车。</font></td>
	</tr>
	<tr> 
	  <td width="20%" class="tdbg"><strong>请输入SQL语句：</strong></td>
	  <td width="80%" class="tdbg"> 
		<textarea name="StrSql" style="width:100%" rows="12" id="StrSql"></textarea>
		<br /><a href="javascript:admin_Size(-5,'StrSql')"><img src="images/minus.gif" unselectable="on" border="0"></a> <a href="javascript:admin_Size(5,'StrSql')"><img src="images/plus.gif" unselectable="on" border="0"></a>
	  </td>
	</tr>
	<tr> 
	  <td class="tdbg" colspan="2" align="center"><input type="radio" name="DataBase" value="Article" <%if Session("DoSql")<>"User" then response.write "checked"%> >主数据库&nbsp;&nbsp;&nbsp;&nbsp;<input type="radio" name="DataBase" value="User" <%if Session("DoSql")="User" then response.write "checked"%> >用户表数据库</td>
	</tr>
	 <tr> 
	  <td height="40" colspan="2" align="center" class="tdbg"> 
		<input name="Action" type="hidden" id="Action" value="DoSql">
		<input name="cmdSave" type="submit" id="cmdSave" value="&nbsp;执行SQL语句&nbsp;">
	 </td>
	</tr>
</form>
</table>
<%
end sub
%>
