% '******************************************* 'ASP/VBscript 常用函数过程 'Filename:GFUN.asp '2005年5月18日 星期三 [R.W.] '******************************************* '******************************************* '文本字符操作类 2005年5月18日 星期三 [R.W.] '################################################################################################ ''+++++++++++++++++++++++++++++++++++++++++++++++++++ ''过程名:HTMLEncode(fString) ''作用 :过滤跨站攻击内容 ''参数 :fString 具体内容 ''+++++++++++++++++++++++++++++++++++++++++++++++++++ Function HTMLEncode(fString) If Not IsNull(fString) Then fString = replace(fString, ">", ">") fString = replace(fString, "<", "<") fString = Replace(fString, CHR(32), " ") fString = Replace(fString, CHR(9), " ") fString = Replace(fString, CHR(34), """) fString = Replace(fString, CHR(39), "'") fString = Replace(fString, CHR(13), "") fString = Replace(fString, CHR(10) & CHR(10), "
")
fString = Replace(fString, CHR(10), "
")
HTMLEncode = fString
End If
End Function
''+++++++++++++++++++++++++++++++++++++++++++++++++++
''过程名:HTMLDecode(fString)
''作用 :装回原html代码
''参数 :fString 具体内容
''+++++++++++++++++++++++++++++++++++++++++++++++++++
Function HTMLDecode(fString)
If Not IsNull(fString) Then
fString = Replace(fString, "'" , CHR(39))
fString = Replace(fString, """ , CHR(34))
fString = Replace(fString, " ", CHR(9))
fString = Replace(fString, " " , CHR(32))
fString = replace(fString, "<" , "<")
fString = replace(fString, ">" , ">")
fString = Replace(fString, "
", CHR(10))
fString = Replace(fString, "
" , CHR(10) & CHR(10)) fString = Replace(fString, "" , CHR(13)) HTMLDecode = fString End If End Function ''+++++++++++++++++++++++++++++++++++++++++++++++++++ ''过程名:NoHtml ''作用 :处理html代码 ''参数 :str 需要处理的字符串 ''+++++++++++++++++++++++++++++++++++++++++++++++++++ Function nohtml(str) dim re Set re=new RegExp re.IgnoreCase =true re.Global=True re.Pattern="(\<.*?\>)" str=re.replace(str,"") re.Pattern="(\<\/.*?\>)" str=re.replace(str,"") nohtml=str End Function ''+++++++++++++++++++++++++++++++++++++++++++++++++++ ''过程名:strlen ''作用 :求取字符长度 ''参数 :str 需要处理的字符串 ''+++++++++++++++++++++++++++++++++++++++++++++++++++ Function strLen(str) If isNull(str) Or Str = "" Then StrLen = 0 Exit Function End If Dim WINNT_CHINESE WINNT_CHINESE=(len("例子")=2) If WINNT_CHINESE Then Dim l,t,c Dim i l=len(str) t=l For i=1 To l c=asc(mid(str,i,1)) If c<0 Then c=c+65536 If c>255 Then t=t+1 Next strLen=t Else strLen=len(str) End If End Function ''+++++++++++++++++++++++++++++++++++++++++++++++++++ ''过程名:cutStr ''作用 :取特定长度的字符串 ''参数 :str 需要处理的字符串,strLen 长度 ''+++++++++++++++++++++++++++++++++++++++++++++++++++ function cutStr(str,strlen) dim l,t,c l=len(str) t=0 for i=1 to l c=Abs(Asc(Mid(str,i,1))) if c>255 then t=t+2 else t=t+1 end if if t>=strlen then cutStr=left(str,i)&"..." exit for else cutStr=str end if next cutStr=replace(cutStr,chr(10),"") end function '防止SQL注入 Function Checkstr(Str) If Isnull(Str) Then CheckStr = "" Exit Function End If Str = Replace(Str,Chr(0),"") CheckStr = Replace(Str,"'","''") End Function '+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ '过程名:formatDT '作 用:格式化日期显示 '参 数:Dtype 显示类型,DateTime 要格式化显示的时间 '+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Function formatDT(Dtype,DateTime) select case Dtype '2004-07-25 09:40:50 case "0" formatDT = year(DateTime) & "-" & doublenum(Month(DateTime)) & "-" & doublenum(Day(DateTime)) & " " & doublenum(Hour(DateTime)) & ":" & doublenum(Minute(DateTime)) & ":" & doublenum(Second(DateTime)) '2004-07-25 09:40 case "1" formatDT = year(DateTime) & "-" & doublenum(Month(DateTime)) & "-" & doublenum(Day(DateTime)) & " " & doublenum(Hour(DateTime)) & ":" & doublenum(Minute(DateTime)) '07/25/03 case "2" formatDT = doublenum(Month(DateTime)) & "/" & doublenum(Day(DateTime))& "/" & Right(year(DateTime),2) '2004-07 case "3" formatDT = year(DateTime) & "-" & doublenum(Month(DateTime)) '07-25 case "4" formatDT = doublenum(Month(DateTime)) & "-" & doublenum(Day(DateTime)) '09:40:50 case "5" formatDT = doublenum(Hour(DateTime)) & ":" & doublenum(Minute(DateTime)) & ":" & doublenum(Second(DateTime)) '09:40 case "6" formatDT = doublenum(Hour(DateTime)) & ":" & doublenum(Minute(DateTime)) '2004年07月25日 case "7" formatDT = year(DateTime) & "年" & doublenum(Month(DateTime)) & "月" & doublenum(Day(DateTime)) & "日" '2004年07月 CASE "8" formatDT = year(DateTime) & "年" & doublenum(Month(DateTime)) & "月" '07月25日 case "9" formatDT = doublenum(Month(DateTime)) & "月" & doublenum(Day(DateTime)) & "日" '07月25日 09:40 case "10" formatDT = doublenum(Month(DateTime)) & "月" & doublenum(Day(DateTime)) & "日 " & doublenum(Hour(DateTime)) & ":" & doublenum(Minute(DateTime)) 'Monday,Jul 25,2004 case "11" MonthArray = Array("January","February","March","April","May","June","July","August","September","October","November","December") WeekArray = Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday") formatDT = WeekArray(Weekday(DateTime )-1) & "," & MonthArray(Month(DateTime)-1) & " " &Day(DateTime) & "," & Year(DateTime) end select End Function '取两位以上的数据 '如果取指定的n位,可以使用Right函数(从字符串右边返回指定数目的字符。) Function DoubleNum(fNum) if fNum > 9 then doublenum = fNum else doublenum = "0" & fNum end if End Function '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ End ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ '替换字符 Function ReplaceText(StrContent,PatternStr,reText) Dim objRe Set objRe=New RegExp objRe.Pattern=PatternStr objRe.Global=True objRe.IgnoreCase=True ' objRe.MultiLine=True ReplaceText=objRe.Replace(StrContent,reText) Set objRe=nothing End Function function AnsiEnCode(vstrIn) Dim i, strReturn, innerCode, ThisChr Dim Hight8, Low8 strReturn = "" For i = 1 To Len(vstrIn) ThisChr = Mid(vStrIn,i,1) If Abs(Asc(ThisChr)) < &HFF Then strReturn = strReturn & ThisChr Else innerCode = Asc(ThisChr) If innerCode < 0 Then innerCode = innerCode + &H10000 End If Hight8 = (innerCode And &HFF00) \ &HFF Low8 = innerCode And &HFF strReturn = strReturn & "%" & Hex(Hight8) & "%" & Hex(Low8) End If Next AnsiEnCode = strReturn End Function Function AnsiDeCode(s) Dim i, sTmp, sResult, sTmp1 sResult = "" For i=1 To Len(s) If Mid(s,i,1)="%" Then sTmp = "&H" & Mid(s,i+1,2) If isNumeric(sTmp) Then If CInt(sTmp)=0 Then i = i + 2 ElseIf CInt(sTmp)>0 And CInt(sTmp)<128 Then sResult = sResult & Chr(sTmp) i = i + 2 Else If Mid(s,i+3,1)="%" Then sTmp1 = "&H" & Mid(s,i+4,2) If isNumeric(sTmp1) Then sResult = sResult & Chr(CInt(sTmp)*16*16 + CInt(sTmp1)) i = i + 5 End If Else sResult = sResult & Chr(sTmp) i = i + 2 End If End If Else sResult = sResult & Mid(s,i,1) End If Else sResult = sResult & Mid(s,i,1) End If Next AnsiDeCode = sResult End Function '******************************************* '客户端模拟交互类 2005年5月18日 星期三 [R.W.] '################################################################################################ '获取客户端的IP Public Function Web_GetClientIP() Web_GetClientIP = Request.ServerVariables("HTTP_X_FORWARDED_FOR") if Web_GetClientIP = "" then Web_GetClientIP = Request.ServerVariables("REMOTE_ADDR") end function '客户端脚本:alert(Msg) I; Public Sub Client_Alert(ByVal Msg) Response.Write("") End Sub '客户端脚本:alert(Msg) II; Public Sub Client_Alert2(ByVal Msg,ByVal returnURL) Response.Write("") End Sub '客户端脚本:confrim(Msg) I; Public Sub Client_Confirm(ByVal Msg,ByVal url) Response.Write("") End Sub '客户端脚本:confrim(Msg) II; Public Sub Client_Confirm2(ByVal Msg,ByVal cfmurl,ByVal retrunURL) Response.Write("") End Sub '客户端脚本:重定向网址 Public Sub Client_Redirect(ByVal URL,ByVal CopyHistory) if CopyHistory then Response.Write("") else Response.Write("") end if End Sub Sub ShowMsgPage(URLPath) Response.Clear() Response.Redirect(URLPath) Response.End End Sub '***************************************** '显示特定消息 '***************************************** Sub ShowMsg(strMsg,iSecond) if len(strMsg)>0 then %>