<%@language=vbscript codepage=936 %>
<%
Option Explicit
Const PurviewLevel = 1
%>
<%
'|----------------------------------------------------------------------------|
'|---------飞越在线文档替换器v1.0 for 动易网站管理系统2005/2006 --------------|
'|--------------------- 作者:科技创造自由  QQ:330950407 ----------------------|
'|-------------------- 飞越快乐家园  http://www.fy66.net/ --------------------|
'|------- 转载请保留版权信息,手下留情,留下的是对别人的尊重,谢谢! -------------|
'|----------------------------------------------------------------------------|
%>
<!--#include file="../conn.asp"-->
<!--#include file="../inc/Function.asp"-->
<!--#include file="Admin_Fy_Config.asp"-->
<!--#include file="Admin_ChkPurview.asp"-->
<html>
<head>
<title>动易在线文档替换程序</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link href="admin_STYLE.CSS" rel="stylesheet" type="text/css">
<script language = "JavaScript">
function CheckForm(){
  if (document.myform.Pathstr.value==''){
    alert('请输入查找目录！');
    document.myform.Pathstr.focus();
    return false;
  }
  return true;  
}
</script>
</head>
<body>
<table width='100%' border='0' align='center' cellpadding='2' cellspacing='1' class='border'>
	<tr class='topbg'> 
		<td height='22' colspan='10'><table width='100%'><tr class='topbg'><td align='center'><b>
			动易在线文档替换器</b></td><td width='60' align='right'></td></tr></table></td>
	</tr>
</table>



<%
Dim sAction,inum1,inum2,Pathstr1,thnum
Dim strOld,strNew
	strOld=trim(Request("strOld"))
	strNew=trim(Request("strNew"))
	thnum=trim(Request("thnum"))
	if thnum="" then thnum=0
	thnum=Clng(thnum)

	inum1=0
	inum2=0

sAction=Trim(Request("Action"))
if sAction="" then
	Call step1()
Elseif sAction="step1" then
	Call step2()
End if
%>


<%Sub step1()%>
<form method="post" name="myform" onSubmit="return CheckForm();"  action="admin_Fy_TextReplace.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' width=70 align='right'>查找目录：</td><td height='40'><input name="Pathstr" type="text" value="/" size="72">(请填写相对路径,如/Article/HTML/)<br>
	<Input name='searchtype' type='radio' id='searchtype' value='1' checked> 包含子目录
	<Input name='searchtype' type='radio' id='searchtype' value='0'> 不包含子目录
	
	</td></tr>
	<tr class='tdbg'><td height='40' align='right'>文件类型：</td><td height='40'><input name="FileType" type="text" value="txt|htm|html|shtml|asp|php|cgi" 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='step1'><input type="submit" name="Submit" value="开始替换"></td></tr>
</table>
</form>
<%End Sub%>


<%
Sub step2()
	Dim sFso,stheFolder,stheFile,Pathstr,SearchType
	SearchType=Clng(trim(Request("SearchType")))
	Pathstr=trim(Request("Pathstr"))
	Pathstr1=Replace(Server.MapPath("/"),"\","/")
	if SearchType=0 then
		Call DoReplace(Server.MapPath(Pathstr),strOld,strNew)		
	Else
		bianli(Server.MapPath(Pathstr))
	End if
	Response.write "替换结束！本次共查找到"&inum1&"个文件，成功替换"&inum2&"个"

End sub
Sub DoReplace(Pathstr,strOld,strNew)
	if inum1>=thnum and thnum>0 then Exit Sub
	Pathstr=Replace(Pathstr,"\","/")
	if right(Pathstr,1)<>"/" and right(Pathstr,1)<>"\" then Pathstr=Pathstr&"/"
	Pathstr=Replace(Pathstr,"//","/")
	dim c,theFile,theFolder,strFileType,fso,TruePath,FileType,FileType1,isTrue,i,InStream,Out,Out1,FileObject,Tempstr
	Response.Flush()'刷新
	FileType =trim(Request("FileType"))
	if FileType<>"" then
		FileType1=Split("|"&FileType&"|","|")
		isTrue=False
	Else
		isTrue=True
	End if
	Set fso = Server.CreateObject("Scripting.FileSystemObject")
	Set theFolder=fso.GetFolder(Pathstr)
	For Each theFile In theFolder.Files
		c=c+1
		strFileType=lcase(mid(theFile.Name,instrrev(theFile.Name,".")+1))
		if FileType<>"" then
			isTrue=False
			for i=0 to Ubound(FileType1)
				if Ucase(FileType1(i))=Ucase(strFileType) then isTrue=True
			Next
		Else
			isTrue=True
		End if
		if isTrue=True Then
			Response.write Replace("<li>正在查找第 <font color=red><b>"&inum1+1&"</b></font> 个文件："&Pathstr&theFile.Name&" ",Pathstr1,"")
		
			Set FileObject=Server.CreateObject("Scripting.FileSystemObject")
			Set Out=FileObject.OpenTextFile(Pathstr&theFile.Name,1,FALSE,FALSE) 
				tempstr= Out.readall
			Out.Close
			if  instr(tempstr,strOld)<>0 then
				Set Out1=FileObject.CreateTextFile(Pathstr&theFile.Name,True,FALSE) 
					Out1.Write(Replace(tempstr,strOld,strNew))
				Out1.Close
				Response.write "<font color=green>替换成功！</font>"
				inum2=inum2+1
			'Else
				'Response.write "<font color=black>无匹配内容！</font>"
			End if
			Response.write "</li><br>"
			'……………………………………………… 
			If Response.IsClientConnected Then 
				Response.Flush 
			Else 
				Response.End 
			End If
			'………………………………………………
			inum1=inum1+1
			if inum1>=thnum and thnum>0 then Exit For
		end if
	Next

End sub

function bianli(path) 
	Dim fso,objFolder,objSubFolders,objSubFolder,nowpath
		Call DoReplace(path,strOld,strNew)
        set fso=server.CreateObject("scripting.filesystemobject")     
        on error resume next 
        set objFolder=fso.GetFolder(path) 
        set objSubFolders=objFolder.Subfolders 
        for each objSubFolder in objSubFolders 
            nowpath=path + "/" + objSubFolder.name 
			Call DoReplace(nowpath,strOld,strNew)
            bianli(nowpath)'递归 
        next 
        set objFolder=nothing 
        set objSubFolders=nothing 
        set fso=nothing 
end function 

%>


<br>
<table width="100%" border="0" cellpadding="0" cellspacing="1" class="border" >
  <tr>
   <td colspan='2' height="20" align="center"><a href='http://www.fy66.net/' target=_blank style='color:#449AE8'>插件作者:飞越快乐家园-科技创造自由</a></td>
  </tr>
</table> 
</body>
</html>