<!--#include file="../inc/Secure.htm"-->
<HTML>
<HEAD>
<TITLE> File System Manager  </TITLE>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link rel="stylesheet" type="text/css" href="styles.css">
</HEAD>
<%
Dim filename, overwrite, TextToPrint
Dim str
str=""

Dim FileStreamObject, Write, TextOutput, PrintOutput
faction = trim(request("faction"))
strReFileEdit = request("file2edit")

if strReFileEdit ="" then
	response.write "没有可编辑的文件！<a href=FileManager.htm>[返回]</a>"
	response.end
end if

if request("SaveAsFile")<>"" then
	strSaveAs = trim(request("SaveAs"))
	if strSaveAs = "" then response.end
	strNewContent = trim(request("NewContent"))
	if FindFile (strSaveAs) = true and strSaveAs<>strReFileEdit then
		response.write "<script language=javascript>alert ('该文件名已存在! \n请选择别的文件名。')</script>"
		str="该文件名"& strSaveAs &"已存在，操作失败！"
	else
		CreateTextFile Server.Mappath(strSaveAs), True, strNewContent
		str="文件保存成功！" & strSaveAs
		strReFileEdit = strSaveAs
	end if
end if

strFileEdit = Server.Mappath (strReFileEdit)
if faction<>"editfile" or strFileEdit="" then
	response.write "出现错误！进程终止！"
	response.end
end if

if request("SaveFile")<>"" then
	strNewContent = trim(request("NewContent"))
	Write_in_File strFileEdit ,strNewContent, False
	str="文件保存成功！" & strReFileEdit
end if

if request("ExitToManager")<>"" then
	strNewContent = trim(request("NewContent"))
	Write_in_File strFileEdit ,strNewContent, False
	response.redirect "FileManager.htm?nf="& unMapPath(GetParentFolder (strReFileEdit))
end if

if request("ExitSave")<>"" then
	response.redirect "FileManager.htm?nf="& unMapPath(GetParentFolder (strReFileEdit))
end if

if FindFolder (strFileEdit) then
	response.write "<p align=center><font color=#ff0000>你不能编辑文件夹</font><a href=# onclick=""javascript:history.back(-1);"">[返回]</a></p>"
	response.end
end if
if FindErrFile (strFileEdit) then
	response.write "<p align=center><font color=#ff0000>该编辑器不支持你所能编辑的文件！</font><a href=FileManager.htm?nf="& unMapPath(GetParentFolder (strReFileEdit)) &">[返回]</a></p>"
	response.end
end if
%>
<form name="EditForm" action="FileEdit.htm" method="post">
<input type="hidden" name="cmdok" value=''>
<div align="center">
<table border=0 align="center">
	<tr><td align="center">
	File Manager/File Edit <%response.write "[<font color=#ff0000>"& strReFileEdit &"</font>]"%>
	</td></tr>
	<tr><td align="center">
	<%if str<>"" then response.write "<font color=#ff0000>操作状态："& str &"</font>"%>
	</td></tr>
	<tr><td>
	<textarea cols=70 rows=20 name="NewContent"><%OpenTextFileForReading (strFileEdit)%></textarea>
	</td></tr>
	<tr><td align="center">
	另存为：<input name="SaveAs" size=50 maxlength=100 value="<%=strReFileEdit%>">
	</td></tr>
	<tr><td align="center">
	<INPUT TYPE="submit" name="SaveFile" value=" Save " style='background:#336699' class='Font9BoldWhite'>
	<INPUT TYPE="submit" name="SaveAsFile" value="Save As a New Name" style='background:#336699' class='Font9BoldWhite'>
	<INPUT TYPE="submit" name="ExitSave" value="Exit to File Manager" style='background:#336699' class='Font9BoldWhite'><BR>
	<INPUT TYPE="submit" name="ExitToManager" value="Save and Exit to File Manager" style='background:#336699' class='Font9BoldWhite'>
	
	</td></tr>
	<INPUT TYPE="hidden" name="file2edit" value="<%=strReFileEdit%>">
	<INPUT TYPE="hidden" name="faction" value="<%=faction%>">
</table>
</div>
</form>
</html>
<%
Function FindFile (FileName)
	PersonalPath=Server.Mappath(FileName)
	dim fs
	Set fs = server.CreateObject("Scripting.FileSystemObject")
	if trim(PersonalPath)<>"" and fs.FileExists(PersonalPath) then 
		FindFile = true
	else
		FindFile = false
	end if
	set fs=nothing	
end Function

Function FindFolder (PersonalPath)
	dim fs
	Set fs = server.CreateObject("Scripting.FileSystemObject")
	if trim(PersonalPath)<>"" and fs.FolderExists(PersonalPath) then 
		FindFolder = true
	else
		FindFolder = false
	end if
	set fs=nothing	
end Function

Function FindErrFile (PersonalPath)
	dim fs, ExtName
	Set fs = server.CreateObject("Scripting.FileSystemObject")
	if trim(PersonalPath)<>"" and fs.FileExists(PersonalPath) then 
		FindErrFile = false
		ExtName = fs.GetExtensionName(PersonalPath)
		if lcase(ExtName) = "html" or lcase(ExtName) = "htm" or lcase(ExtName) = "asp" or lcase(ExtName) = "jsp" or lcase(ExtName) = "php" or lcase(ExtName) = "txt" or lcase(ExtName) ="cmf" or lcase(ExtName) ="aspx" or lcase(ExtName)= "xml" then
			FindErrFile = false
		else
			FindErrFile=true
		end if
	else
		FindErrFile = true
	end if
	set fs=nothing	
end Function

Sub CreateTextFile (filename,overwrite,TextToPrint)
	Set FileStreamObject = CreateObject("Scripting.FileSystemObject")
	If overwrite = "" Or overwrite = "0" Or overwrite = "FALSE" Then
		overwrite = "FALSE"
	Else
		overwrite = "TRUE"
	End If
	
	filename = BaseLocationForTextFiles & filename
	Set Write = FileStreamObject.CreateTextFile(filename, overwrite)
	Write.write(TextToPrint)
	Write.Close
	Set FileStreamObject = Nothing
	Set Write = Nothing
End Sub

Sub OpenTextFileForReading(filename)
	Set FileStreamObject = CreateObject("Scripting.FileSystemObject")
	Set TextOutput = FileStreamObject.OpenTextFile (filename,1,"FALSE")
	Do until TextOutput.AtEndOfStream
		PrintOutput = TextOutput.ReadLine
		Response.Write PrintOutput & chr(13)		
	Loop
	Set TextOutput = Nothing
	Set FileStreamObject = Nothing
End Sub

Sub OpenTextFileForAppending (filename, mode, create, TextToPrint)
	Set FileStreamObject = CreateObject("Scripting.FileSystemObject")
	If create = "" Then
		create = "FALSE"
	End If
	If mode = "" Or mode = "8" Or mode = "ForAppending" Then
		mode = "8"
	Else
		mode = "2"
	End If
	filename = BaseLocationForTextFiles & filename
	Set TextOutput = FileStreamObject.OpenTextFile (filename,mode,create)
	TextOutput.WriteLine(TextToPrint)
	Set TextOutput = Nothing
	Set FileStreamObject = Nothing
End Sub


Function Write_in_File(File, Content, Add)
    On Error Resume Next
    If Add = True Then
    Mode = 8
    Else 
    Mode = 2
    End If
    Set Fs = Server.Createobject("Scripting.FileSystemObject")
    Set File_Name = Fs.OpenTextFile(File, Mode, True)
    File_Name.Write Content
    File_Name.Close
    Set File_Name = Nothing
    Set Fs = Nothing
End Function

Function GetParentFolder (PersonalPath)
	set fs=server.CreateObject("scripting.filesystemobject")
	PersonalPath=Server.MapPath (PersonalPath)
	Set f = fs.GetFile(PersonalPath)
		GetParentFolder = f.ParentFolder
	set f=nothing
	set fs = nothing
End Function

Private Function UnMappath(byVal FullPath)
		dim root, tmp1, tmp2
		root = server.mappath("../")
		tmp1 = Replace(fullPath, root, "")
		tmp2 = Replace(tmp1, "\", "/")
		'if not right(tmp2, 1) = "/" then tmp2 = tmp2 & "/"
		UnMappath = Trim(tmp2)
End Function
%>