最近迷恋上作网站了,使用asp开发,发现asp有好多的漏洞,而且在一个网站中有好多的代码是重复使用的,所以就查询了一些资料发现在asp中可以使用类的思想,所以就写了这个类,写的不好,但是比较实用。
<meta http-equiv=”content-type” content=”text/html; charset=gb2312″>
<%
const btn_first=”<font face=webdings>9</font>” 定义第一页按钮显示样式
const btn_prev=”<font face=webdings>3</font>” 定义前一页按钮显示样式
const btn_next=”<font face=webdings>4</font>” 定义下一页按钮显示样式
const btn_last=”<font face=webdings>:</font>” 定义最后一页按钮显示样式
class clsdsj
private p_error,p_ipadd,p_debug,errs
private p_invaildwords
private tmpstr
private objconndivpage,rsdivpage,p_getsql,p_npage
private p_rspagesize,p_intcurpage,p_strurl,p_inttotalpage,p_inttotalrecords
private p_dbtype,p_dbfolder,p_database,dbpath,p_dbuserid,p_dbpassword,p_sqldatasource
private param_database,param_dbuserid,param_dbpassword,param_dbtype
property
public objconn,connstr,sqlquerynum
public logdb,weburl
public savelog
public errcodes,errcode,errmsg
public scriptfolder,scriptname
private sub class_initialize
p_debug=true
clientconnected
savelog=true
errmsg=””
p_error=0
sqlquerynum=0
rem 数据库连接初始化
dbtype=12 取1x——access,2x——sql;x1——oledb,x2——odbc
dbfolder=”data”
database=”zhubizidatabase.mdb”
dbuserid=””
dbpassword=””
sqldatasource=””
p_rspagesize=10
p_npage=2
if request(“page”)=”” then
p_intcurpage=1
elseif not isnumeric(request(“page”)) then
p_intcurpage=1
elseif cint(trim(request(“page”)))<1 then
p_intcurpage=1
else
p_intcurpage=cint(trim(request(“page”)))
end if
weburl=”http://blog.csdn.net/alonesword/”
tmpstr=request.servervariables(“path_info”)
tmpstr=split(tmpstr,”/”,-1)
scriptname=lcase(tmpstr(ubound(tmpstr)))
scriptfolder=lcase(tmpstr(ubound(tmpstr)-1)) & “/”
p_invaildwords=”select|update|delete|insert|@|–|;||#|%|xp|cmd|shell”
end sub
private sub class_terminate()
if isobject(rsdivpage) then rsdivpage.close:set rsdivpage=nothing
if isobject(objconn) then objconn.close:set objconn=nothing
end sub
public property get version
version=”dsj 1.1.0 beta”
end property
public property get error
error=p_error
end property
public property get debug
debug=p_debug
end property
public property let debug(booldebug)
if booldebug then
p_debug=true
else
p_debug=false
end if
end property
public property get dbtype
dbtype=p_dbtype
end property
public property let dbtype(lngdbtype)
if isnumeric(lngdbtype) then
p_dbtype=lngdbtype
else
p_dbtype=12
end if
end property
public property get dbfolder
dbfolder=p_dbfolder
end property
public property let dbfolder(strfolder)
if right(strfolder,1)=”\” or right(strfolder,1)=”/” then
p_dbfolder=strfolder
else
if (not isnull(strfolder)) or (strfolder<>””) then
p_dbfolder=strfolder & “\”
else
p_dbfoler=””
end if
end if
end property
public property get database
database=p_database
end property
public property let database(strdatabase)
p_database=strdatabase
end property
public property get dbuserid
dbuserid=p_dbuserid
end property
public property let dbuserid(strdatabaseuserid)
p_dbuserid=strdatabaseuserid
end property
public property get dbpassword
dbpassword=p_dbpassword
end property
public property let dbpassword(strdatabasepassword)
p_dbpassword=strdatabasepassword
end property
public property get sqldatasource
sqldatasource=p_sqldatasource
end property
public property let sqldatasource(strsqldatasource)
if strsqldatasource<>”” then
if left(p_dbtype,1)=2 then
p_sqldatasource=strsqldatasource
else
adderrorcode(110)
p_sqldatasource=””
end if
end if
end property
public property let pagesize(int_pagesize)
if isnumeric(int_pagesize) then
p_rspagesize=clng(int_pagesize)
end if
end property
public property get pagesize
if p_rspagesize=”” or not isnumeric(p_rspagesize) then
pagesize=8
else
pagesize=p_rspagesize
end if
end property
public property get getrs()
if not debug then on error resume next
if not isobject(objconn) then connectiondatabase dbdatabase,dbuserid,dbpassword,dbtype
if not isobject(rsdivpage) then
set rsdivpage=server.createobject(“adodb.recordset”)
rsdivpage.open getsql,objconn,1,1
rsdivpage.pagesize=pagesize
if not(rsdivpage.eof and rsdivpage.bof) then
if p_intcurpage>rsdivpage.pagecount then
p_intcurpage=rsdivpage.pagecount
end if
rsdivpage.absolutepage=p_intcurpage
end if
if err then
err.clear
rsdivpage.close
set rsdivpage=nothing
if savelog then
tmpmsg=”连接数据库错误,请检查您的查询代码是否正确。<br>基于安全的理由,只显示本信息,要查看详细的错误信息,请设置dsj.debug=true”
response.write savesqllog(cmd,tmpmsg)
else
response.write “连接数据库错误,请检查您的查询代码是否正确。”
end if
response.end()
end if
sqlquerynum=sqlquerynum+1
set getrs=rsdivpage
end if
end property
public property let getsql(str_sql)
if str_sql<>”” then
p_getsql=str_sql
else
adderrorcode(111)
if debug then showerrmsg()
p_getsql=”the property of getsql is null.”
end if
end property
public property get getsql()
getsql=p_getsql
end property
******************************
名称: gottopic
参数:str,strlen
返回值:被限制后的字符串
创建时间:2005年3月3日
作用:限制被限制的字符串
******************************
public function gottopic(str,strlen)
rem 限制字符串显示长度在一定的范围内
dim i,stringlen,charsize,echochartype
stringlen=len(str)
charsize=0
for i=1 to stringlen
echochartype=abs(asc(mid(str,i,1)))
if echochartype>255 then
charsize=charsize+2
else
charsize=charsize+1
end if
if charsize>strlen then
gottopic=left(str,i) & “…”
exit for
else
gottopic=str & “”
end if
next
end function
******************************
名称: chkinvstr
参数:str
返回值:true/false
创建时间:2005年5月2日
作用:检查参数是否有非法字符
******************************
public function chkinvstr(str)
rem 定义需要过滤得非法字符
dim invaildword,inwords,i
str=cstr(str)
chkinvstr=false
if len(replace(p_invaildwords,chr(0),””))<1 then
adderrorcode(103) invaildwords is null
exit function
else
if instr(1,p_invaildwords,”|”)>0 then
invaildword=split(p_invaildwords,”|”)
inwords=lcase(trim(str))
for i=lbound(invaildword) to ubound(invaildword)
if instr(inwords,invaildword(i))>0 then
p_error=1982
adderrorcode(105)
chkinvstr=true
exit function
end if
next
else
adderrorcode(104) “|” is requried.
exit function
end if
end if
end function
******************************
名称: getip
参数:null
返回值:null
创建时间:2005年5月3日
作用:得到用户的ip地址
******************************
public function getip()
p_ipadd=request.servervariables(“http_x_forwarded_for”)
if isnull(p_ipadd) or p_ipadd=”” then
p_ipadd=request.servervariables(“remote_addr”)
end if
getip=p_ipadd
end function
******************************
名称: ipaddress
参数:sip
返回值:county,city
作用:对数据内容进行排版转换
创建时间:2005年5月6日
来源:www.knowsky.com
******************************
public function ipaddress(sip)
dim ipconnstr,ipconn,ipaddressdb
dim str1,str2,str3,str4
dim num
dim country,city,address
dim iprs,sql
address=”未知”
if isnumeric(left(sip,2)) then
if sip=”127.0.0.1″ then sip=”192.168.0.1″
str1=left(sip,instr(sip,”.”)-1)
sip=mid(sip,instr(sip,”.”)+1)
str2=left(sip,instr(sip,”.”)-1)
sip=mid(sip,instr(sip,”.”)+1)
str3=left(sip,instr(sip,”.”)-1)
str4=mid(sip,instr(sip,”.”)+1)
if isnumeric(str1)=0 or isnumeric(str2)=0 or isnumeric(str3)=0 or isnumeric(str4)=0 then
else
num=clng(str1)*16777216+clng(str2)*65536+clng(str3)*256+clng(str4)-1
ipaddressdb = “dsj_ipaddress.mdb”
ipconnstr = “provider = microsoft.jet.oledb.4.0;data source = ” & server.mappath(p_dbfolder & ipaddressdb)
set ipconn = server.createobject(“adodb.connection”)
ipconn.open ipconnstr
country=”亚洲”
city=””
sql=”select top 1 country,city from dsj_ipaddress where ip1 <=”&num&” and ip2 >=”&num&””
set iprs=ipconn.execute(sql)
if not(iprs.eof and iprs.bof) then
country=iprs(0)
city=iprs(1)
end if
iprs.close:set iprs=nothing
ipconn.close:set ipconn = nothing
sqlquerynum = sqlquerynum+1
end if
ipaddress=country&city
end if
end function
******************************
名称: clientconnected
参数:null
返回值:null
创建时间:2005年5月5日
作用:得到用户是否与服务器连接
******************************
public sub clientconnected()
if response.isclientconnected then
response.flush()
else
response.end()
end if
end sub
******************************
名称: chkpost
参数:null
返回值:true/false
创建时间:2005年5月5日
作用:检查被提交的数据来源
******************************
public function chkpost()
dim server_v1,server_v2
chkpost=false
server_v1=cstr(request.servervariables(“http_referer”))
server_v2=cstr(request.servervariables(“server_name”))
if mid(server_v1,8,len(server_v2))=server_v2 then chkpost=true
end function
******************************
名称: adderrorcode
参数:errcode
返回值:errcodes
创建时间:2005年5月3日
作用:添加错误代码到errcodes中
******************************
public sub adderrorcode(errcode)
if errcodes=”” then
errcodes=errcode
else
errcodes=errcodes & “,” & errcode
end if
end sub
******************************
名称: showerrmsg
参数:null
返回值:null
创建时间:2005年5月3日
作用:显示详细的错误信息
******************************
public function showerrmsg()
if not p_debug then on error resume next
if errcodes<>”” then
ierrcodes=split(errcodes,”,”,-1,binary)
for i=lbound(ierrcodes) to ubound(ierrcodes)
tmperrcode=clng(ierrcodes(i))
if isnumeric(tmperrcode) then
errmsg=errmsg & errdetails(tmperrcode)
end if
next
end if
showerrmsg=errmsg
end function
******************************
名称: connectiondatabase
参数:database,p_dbuserid,p_dbpassword,p_dbtype
返回值:null
作用:连接到数据库
创建时间:2005年5月6日
******************************
public function connectiondatabase(param_database,param_dbuserid,param_dbpassword,param_dbtype)
if not p_debug then on error resume next
clientconnected
if isnumeric(param_dbtype) then
set objconn=server.createobject(“adodb.connection”)
objconn.connectiontimeout=10 设置连接超时为10秒
dbpath=server.mappath(p_dbfolder¶m_database)
response.write dbpath
select case param_dbtype
case 11: access data base(oledb)
connstr=”provider=microsoft.jet.oledb.4.0;data source=”&dbpath&”;user id=”¶m_dbuserid&”;password=”¶m_dbpassword&””
case 21: sql server (oledb)
connstr=”provider=sqloledb;initial catalog=”¶m_database&”;data source=”&p_sqldatasource&”;user id=”¶m_dbuserid&”;password=”¶m_dbpassword&””
case 12: access data base(odbc)
connstr=”driver={microsoft access driver (*.mdb)};dbq=”&dbpath&”;user id=”¶m_dbuserid&”;password=”¶m_dbpassword
case 22: sql server (odbc)
connstr=”driver={sql server};database=”¶m_database&”;server=”&p_sqldatasource&”;user id=”¶m_dbuserid&”;password=”¶m_dbpassword&””
case else:
adderrorcode(100)
if p_debug then response.write showerrmsg()
exit function
end select
objconn.connectionstring=connstr
objconn.open
set connectiondatabase=objconn
if err then
err.clear
if savelog then
tmpmsg=”数据库连接时出现错误,请检查您的查询代码是否正确。<br>基于安全的理由,只显示本信息,要查看详细的错误信息,请设置dsj.debug=true”
cmd=”execute the command of :”¶m_dbtype
response.write savesqllog(cmd,tmpmsg)
else
response.write “数据库连接时出现错误,请检查您的查询代码是否正确。”
end if
adderrorcode(101)
set objconn=nothing
response.end()
end if
end if
end function
******************************
名称: savesqllog
参数:scommand,msg
返回值:msg
作用:存储日志
创建时间:2005年5月6日
******************************
public function savesqllog(scommand,msg)
if not p_debug then on error resume next
logdb=”dsj_sqllog.mdb”
connectiondatabase logdb,p_dbuserid,p_dbpassword,12
logconnstr = “provider = microsoft.jet.oledb.4.0;data source = ” & server.mappath(p_dbfolder & logdb)
set logconn = server.createobject(“adodb.connection”)
logconn.open logconnstr
if savelog then
dim logcmd
logcmd=”insert into dsj_sql_log (command,scriptname,loguser,ip) values “
logcmd=logcmd & “(” & replace(left(scommand,255),””,””) &”,” & (scriptfolder & scriptname) &”,” & session(“user”) & “,”& getip & “)”
response.write “need to execute command is:<br><font color=red>” & logcmd & “</font><br>”
logconn.execute(logcmd)
end if
logconn.close
set logconn=nothing
savesqllog=msg
sqlquerynum=sqlquerynum+1
end function
******************************
名称: executecmd
参数:cmd
返回值:recordset
作用:返回由cmd创建的记录集
创建时间:2005年5月6日
******************************
public function executecmd(cmd)
if not isobject(objconn) then connectiondatabase p_database,p_dbuserid,p_dbpassword,p_dbtype
if not p_debug then on error resume next
clientconnected
set executecmd=objconn.execute(cmd)
if err then
err.clear
objconn.close
set objconn=nothing
if savelog then
tmpmsg=”查询数据的时候发现错误,请检查您的查询代码是否正确。<br>基于安全的理由,只显示本信息,要查看详细的错误信息,请设置dsj.debug=true”
response.write savesqllog(cmd,tmpmsg)
else
response.write “查询数据的时候发现错误,请检查您的查询代码是否正确。”
end if
response.end()
end if
sqlquerynum=sqlquerynum+1
end function
******************************
名称: htmlencode
参数:fstring
返回值:htmlencode
作用:对数据内容进行排版转换
创建时间:2005年5月6日
******************************
public function htmlencode(fstring)
if not isnull(fstring) or fstring<>”” then
fstring=replace(fstring,”<“,”<”)
fstring=replace(fstring,”>”,”>”)
fstring=replace(fstring,chr(9),” “) 倾斜字体
fstring=replace(fstring,chr(13),””)
fstring=replace(fstring,chr(32),” “) 转换空格
fstring=replace(fstring,chr(34),”"”) 转换双引号
fstring=replace(fstring,chr(39),”'”) 转换单引号
fstring=replace(fstring,chr(10),”</p><p>”) 转换成段落格式
fstring=replace(fstring,chr(10),”<br> ”) 转换为下一行
fstring=replace(fstring,chr(13),”<br>”)
fstring=replace(fsting,vbcrlf,”<br>”)
fstring=linkfriend(fstring)
fstring=chkbadwords(fstring)
htmlencode=fstring
end if
end function
******************************
名称: linkfriend
参数:fcontent
返回值:linkfriend
作用:进行友情连接替换
创建时间:2005年5月6日
******************************
public function linkfriend(fcontent)
if not p_debug then on error resume next
if not isobject(objconn) then connectiondatabase database,dbuserid,dbpassword,dbtype
set rsfriend=executecmd(“select title,url from dsj_friendlink”)
if rsfriend.eof and rsfriend.bof then
else
do while not rsfriend.eof
strtitle=rsfriend.fields.item(“title”)
url=rsfriend.fields.item(“url”)
if lcase(left(url,1))<>”h” then url=replace(url,left(url,1),””)
strlink=”<a target=_blank title=” & strtitle &” href=” & url & “>”
strlink=strlink&strtitle&”</a>”
if instr(fcontent,strtitle)>0 then fcontent=replace(fcontent,strtitle,strlink)
rsfriend.movenext
loop
linkfriend=fcontent
end if
rsfriend.close
set rsfriend=nothing
sqlquerynum=sqlquerynum+1
end function
***************************************************************
名称: showpage
参数:null
返回值:null
作用:显示经过分页后的记录导航
说明:
原作:zykj2000 网站:http://bbs.513soft.net
修改:孤剑 网站:http://blog.csdn.net/alonesword/
时间:2005年5月7日
******************************************************************
public sub showpage()
dim str_tmp
p_inttotalrecords=rsdivpage.recordcount
if p_inttotalrecords<=0 then
p_error=p_error & “总记录数为零,请输入数据”
call showerror()
end if
if p_inttotalrecords <=pagesize then
p_inttotalpage=1
else
if p_inttotalrecords mod pagesize =0 then
p_inttotalpage = clng(p_inttotalrecords / pagesize * -1)*-1
else
p_inttotalpage = clng(p_inttotalrecords / pagesize * -1)*-1+1
end if
end if
if p_intcurpage>p_inttotalpage then
p_intcurpage=p_inttotalpage
end if
response.write showfirstprv
shownumbtn
response.write shownextlast&” “
response.write showpageinfo
response.write str_tmp
end sub
private function showfirstprv()
dim str_tmp,int_prvpage
int_prvpage=p_intcurpage-1
if int_prvpage<1 then int_prvpage=1
btn_firstlink=”<a title=第1页 href=”&addnpageurl&”1>”&btn_first
btn_prevlink=”<a title=第”&int_prvpage&”页 href=”&addnpageurl&p_intcurpage-1&”>”&btn_prev
if p_intcurpage=1 then
str_tmp=btn_firstlink&”</a> “&btn_prevlink&”</a> “
else
int_prvpage=p_intcurpage-1
str_tmp=””&btn_firstlink&”</a> “& btn_prevlink&”</a> “
end if
showfirstprv=str_tmp
end function
private function shownextlast()
dim str_tmp,int_nextpage
int_nextpage=p_intcurpage+1
if p_intcurpage+1>p_inttotalpage then int_nextpage=p_inttotalpage
btn_nextlink=”<a title=第”&int_nextpage&”页 href=”&addnpageurl&p_intcurpage+1&”>”&btn_next
btn_lastlink=”<a title=第”&p_inttotalpage&”页 href=”&addnpageurl&p_inttotalpage&”>”&btn_last
if p_intcurpage>=p_inttotalpage then
str_tmp=btn_nextlink & “</a> ” & btn_lastlink&”</a>”
else
int_nextpage=p_intcurpage+1
str_tmp=””&btn_nextlink&”</a> “& btn_lastlink&”</a>”
end if
shownextlast=str_tmp
end function
private function shownumbtn()
dim i,str_tmp
dim pagelink
str_tmp=””
for i=1 to p_inttotalpage step 1
pagelink=” <a title=第”&i&”页 href=”&addnpageurl&i&”>”&i&”</a> ”
if i=p_intcurpage then pagelink=” <a title=第”&i&”页 href=”&addnpageurl&i&”><font color=red>”&i&”</font></a> “
response.write pagelink
next
shownumbtn=str_tmp
end function
private function showpageinfo()
dim str_tmp
str_tmp=”页次:”&p_intcurpage&”/”&p_inttotalpage&”页 共”&p_inttotalrecords&”条记录 “&p_rspagesize&”条/每页”
showpageinfo=str_tmp
end function
private function addnpageurl()
dim i,j,search_str,result_url
search_str=”page=”
str_params=request.servervariables(“query_string”)
if str_params=”” then
result_url=scriptname & “?page=”
else
if instrrev(str_params,search_str)=0 then
result_url=scriptname & “?” & str_params &”&page=”
else
j=instrrev(str_params,search_str)-2
if j=-1 then
result_url=scriptname & “?page=”
else
str_params=left(str_params,j)
result_url=scriptname & “?” & str_params &”&page=”
end if
end if
end if
addnpageurl=result_url
end function
******************************************************************
public function getname(options)
dim tmpname
tmpname=request.servervariables(“path_info”)
arrtmpname=split(tmpname,”/”)
if options=0 then getname=server.mappath(“.”)&getname(1)
if options=1 then getname=lcase(arrtmpname(ubound(arrtmpname)))
if options=2 then getname=lcase(arrtmpname(ubound(arrtmpname)-1))&”/”
end function
end class
%>