ReplaceSaveRemoteFile 替换、保存远程图片 的代码
程序员文章站
2022-04-14 20:17:19
'================================================== '函数名:replacesaveremotefile '作 ...
'==================================================
'函数名:replacesaveremotefile
'作 用:替换、保存远程图片
'参 数:constr ------ 要替换的字符串
'参 数:savetf ------ 是否保存文件,false不保存,true保存
'参 数: tisturl------ 当前网页地址
'==================================================
function replacesaveremotefile(constr,strinstalldir,strchanneldir,savetf,tisturl)
if constr="$false$" or constr="" or strchanneldir="" then
replacesaveremotefile=constr
exit function
end if
dim tempstr,tempstr2,tempstr3,re,matches,match,tempi,temparray,temparray2
set re = new regexp
re.ignorecase = true
re.global = true
re.pattern ="<img.+?[^\>]>"
set matches =re.execute(constr)
for each match in matches
if tempstr<>"" then
tempstr=tempstr & "$array$" & match.value
else
tempstr=match.value
end if
next
if tempstr<>"" then
temparray=split(tempstr,"$array$")
tempstr=""
for tempi=0 to ubound(temparray)
re.pattern ="src\s*=\s*.+?\.(gif|jpg|bmp|jpeg|psd|png|svg|dxf|wmf|tiff)"
set matches =re.execute(temparray(tempi))
for each match in matches
if tempstr<>"" then
tempstr=tempstr & "$array$" & match.value
else
tempstr=match.value
end if
next
next
end if
if tempstr<>"" then
includepic=1'图片新闻
re.pattern ="src\s*=\s*"
tempstr=re.replace(tempstr,"")
end if
set matches=nothing
set re=nothing
if tempstr="" or isnull(tempstr)=true then
replacesaveremotefile=constr
exit function
end if
tempstr=replace(tempstr,"""","")
tempstr=replace(tempstr,"'","")
tempstr=replace(tempstr," ","")
dim remotefileurl,savepath,pathtemp,dtnow,strfilename,strfiletype,arrsavefilename,rannum,arr_path
dtnow=now()
if savetf=true then
'***********************************
savepath= strchanneldir & year(dtnow) & right("0" & month(dtnow),2) & "/"
response.write "链接路径:" & savepath & "<br>"
arr_path=split(savepath,"/")
pathtemp=""
for tempi=0 to ubound(arr_path)
if tempi=0 then
pathtemp=arr_path(0) & "/"
elseif tempi=ubound(arr_path) then
exit for
else
pathtemp=pathtemp & arr_path(tempi) & "/"
end if
if checkdir(pathtemp)=false then
if makenewsdir(pathtemp)=false then
savetf=false
exit for
end if
end if
next
end if
'去掉重复图片开始
temparray=split(tempstr,"$array$")
tempstr=""
for tempi=0 to ubound(temparray)
if instr(lcase(tempstr),lcase(temparray(tempi)))<1 then
tempstr=tempstr & "$array$" & temparray(tempi)
end if
next
tempstr=right(tempstr,len(tempstr)-7)
temparray=split(tempstr,"$array$")
'去掉重复图片结束
'转换相对图片地址开始
tempstr=""
for tempi=0 to ubound(temparray)
tempstr=tempstr & "$array$" & definiteurl(temparray(tempi),tisturl)
next
tempstr=right(tempstr,len(tempstr)-7)
tempstr=replace(tempstr,chr(0),"")
temparray2=split(tempstr,"$array$")
tempstr=""
'转换相对图片地址结束
'图片替换/保存
set re = new regexp
re.ignorecase = true
re.global = true
for tempi=0 to ubound(temparray2)
remotefileurl=temparray2(tempi)
if remotefileurl<>"$false$" and savetf=true then'保存图片
arrsavefilename = split(remotefileurl,".")
strfiletype=lcase(arrsavefilename(ubound(arrsavefilename)))'文件类型
if strfiletype="asp" or strfiletype="asa" or strfiletype="aspx" or strfiletype="cer" or strfiletype="cdx" or strfiletype="exe" or strfiletype="rar" or strfiletype="zip" then
uploadfiles=""
replacesaveremotefile=constr
exit function
end if
randomize
rannum=int(900*rnd)+100
strfilename = year(dtnow) & right("0" & month(dtnow),2) & right("0" & day(dtnow),2) & right("0" & hour(dtnow),2) & right("0" & minute(dtnow),2) & right("0" & second(dtnow),2) & rannum & "." & strfiletype
re.pattern =temparray(tempi)
if saveremotefile(savepath & strfilename,remotefileurl)=true then
'********************************
pathtemp=savepath & strfilename
constr=re.replace(constr,pathtemp)
re.pattern=strinstalldir & strchanneldir
uploadfiles=uploadfiles & "|" & re.replace(savepath &strfilename,"")
response.flush()
response.write " 图片保存地址:" & pathtemp & "<br>"
if thumb_watermark=1 then call skthumb.addwatermark(pathtemp)'水印
else
pathtemp=remotefileurl
constr=re.replace(constr,pathtemp)
'uploadfiles=uploadfiles & "|" & remotefileurl
end if
elseif remotefileurl<>"$false$" and savetf=false then'不保存图片
re.pattern =temparray(tempi)
constr=re.replace(constr,remotefileurl)
uploadfiles=uploadfiles & "|" & remotefileurl
end if
next
set re=nothing
if uploadfiles<>"" then
uploadfiles=right(uploadfiles,len(uploadfiles)-1)
end if
replacesaveremotefile=constr
end function
'函数名:replacesaveremotefile
'作 用:替换、保存远程图片
'参 数:constr ------ 要替换的字符串
'参 数:savetf ------ 是否保存文件,false不保存,true保存
'参 数: tisturl------ 当前网页地址
'==================================================
function replacesaveremotefile(constr,strinstalldir,strchanneldir,savetf,tisturl)
if constr="$false$" or constr="" or strchanneldir="" then
replacesaveremotefile=constr
exit function
end if
dim tempstr,tempstr2,tempstr3,re,matches,match,tempi,temparray,temparray2
set re = new regexp
re.ignorecase = true
re.global = true
re.pattern ="<img.+?[^\>]>"
set matches =re.execute(constr)
for each match in matches
if tempstr<>"" then
tempstr=tempstr & "$array$" & match.value
else
tempstr=match.value
end if
next
if tempstr<>"" then
temparray=split(tempstr,"$array$")
tempstr=""
for tempi=0 to ubound(temparray)
re.pattern ="src\s*=\s*.+?\.(gif|jpg|bmp|jpeg|psd|png|svg|dxf|wmf|tiff)"
set matches =re.execute(temparray(tempi))
for each match in matches
if tempstr<>"" then
tempstr=tempstr & "$array$" & match.value
else
tempstr=match.value
end if
next
next
end if
if tempstr<>"" then
includepic=1'图片新闻
re.pattern ="src\s*=\s*"
tempstr=re.replace(tempstr,"")
end if
set matches=nothing
set re=nothing
if tempstr="" or isnull(tempstr)=true then
replacesaveremotefile=constr
exit function
end if
tempstr=replace(tempstr,"""","")
tempstr=replace(tempstr,"'","")
tempstr=replace(tempstr," ","")
dim remotefileurl,savepath,pathtemp,dtnow,strfilename,strfiletype,arrsavefilename,rannum,arr_path
dtnow=now()
if savetf=true then
'***********************************
savepath= strchanneldir & year(dtnow) & right("0" & month(dtnow),2) & "/"
response.write "链接路径:" & savepath & "<br>"
arr_path=split(savepath,"/")
pathtemp=""
for tempi=0 to ubound(arr_path)
if tempi=0 then
pathtemp=arr_path(0) & "/"
elseif tempi=ubound(arr_path) then
exit for
else
pathtemp=pathtemp & arr_path(tempi) & "/"
end if
if checkdir(pathtemp)=false then
if makenewsdir(pathtemp)=false then
savetf=false
exit for
end if
end if
next
end if
'去掉重复图片开始
temparray=split(tempstr,"$array$")
tempstr=""
for tempi=0 to ubound(temparray)
if instr(lcase(tempstr),lcase(temparray(tempi)))<1 then
tempstr=tempstr & "$array$" & temparray(tempi)
end if
next
tempstr=right(tempstr,len(tempstr)-7)
temparray=split(tempstr,"$array$")
'去掉重复图片结束
'转换相对图片地址开始
tempstr=""
for tempi=0 to ubound(temparray)
tempstr=tempstr & "$array$" & definiteurl(temparray(tempi),tisturl)
next
tempstr=right(tempstr,len(tempstr)-7)
tempstr=replace(tempstr,chr(0),"")
temparray2=split(tempstr,"$array$")
tempstr=""
'转换相对图片地址结束
'图片替换/保存
set re = new regexp
re.ignorecase = true
re.global = true
for tempi=0 to ubound(temparray2)
remotefileurl=temparray2(tempi)
if remotefileurl<>"$false$" and savetf=true then'保存图片
arrsavefilename = split(remotefileurl,".")
strfiletype=lcase(arrsavefilename(ubound(arrsavefilename)))'文件类型
if strfiletype="asp" or strfiletype="asa" or strfiletype="aspx" or strfiletype="cer" or strfiletype="cdx" or strfiletype="exe" or strfiletype="rar" or strfiletype="zip" then
uploadfiles=""
replacesaveremotefile=constr
exit function
end if
randomize
rannum=int(900*rnd)+100
strfilename = year(dtnow) & right("0" & month(dtnow),2) & right("0" & day(dtnow),2) & right("0" & hour(dtnow),2) & right("0" & minute(dtnow),2) & right("0" & second(dtnow),2) & rannum & "." & strfiletype
re.pattern =temparray(tempi)
if saveremotefile(savepath & strfilename,remotefileurl)=true then
'********************************
pathtemp=savepath & strfilename
constr=re.replace(constr,pathtemp)
re.pattern=strinstalldir & strchanneldir
uploadfiles=uploadfiles & "|" & re.replace(savepath &strfilename,"")
response.flush()
response.write " 图片保存地址:" & pathtemp & "<br>"
if thumb_watermark=1 then call skthumb.addwatermark(pathtemp)'水印
else
pathtemp=remotefileurl
constr=re.replace(constr,pathtemp)
'uploadfiles=uploadfiles & "|" & remotefileurl
end if
elseif remotefileurl<>"$false$" and savetf=false then'不保存图片
re.pattern =temparray(tempi)
constr=re.replace(constr,remotefileurl)
uploadfiles=uploadfiles & "|" & remotefileurl
end if
next
set re=nothing
if uploadfiles<>"" then
uploadfiles=right(uploadfiles,len(uploadfiles)-1)
end if
replacesaveremotefile=constr
end function
下一篇: Python (三) 基础语法