花褪残红青杏小。燕子飞时,绿水人家绕。

ZBLOG内置过滤函数

ZBLOG教程 十五楼的鸟儿 25523浏览 0评论

在ASP版本和PHP版本都有这个函数的,只是内容有些诧异,因为PHP原生没有UBB功能,所以PHP的过滤函数里忽略了UBB功能(其实老子是需要的啊)

ASP版本的内容如下:

'*********************************************************
' 目的:    检查引用
' 输入:    
' 输入:    要替换的字符代号
' 返回:    
'*********************************************************
Function TransferHTML(ByVal source,ByVal para)

    Dim objRegExp

    If IsNull(source)=True Then Exit Function


    If InStr(para,"[mobilerequest]") Then
        para=para&"[enter][closehtml]"    
        '如何判断HTML标签和用户输入的类似0<1这种数据,还真是个大麻烦    
    End If

    '先换"&"
    If Instr(para,"[&]")>0 Then  source=Replace(source,"&","&amp;")
    If Instr(para,"[<]")>0 Then  source=Replace(source,"<","&lt;")
    If Instr(para,"[>]")>0 Then  source=Replace(source,">","&gt;")
    If Instr(para,"[""]")>0 Then source=Replace(source,"""","&quot;")
    If Instr(para,"[space]")>0 Then source=Replace(source," ","&nbsp;")
    If Instr(para,"[delspace]")>0 Then
        Source=Replace(source," ","")
        Source=Replace(source," ","")
    End If
    If Instr(para,"[enter]")>0 Then
        source=Replace(source,vbCrLf,"<br/>")
        source=Replace(source,vbLf,"<br/>")
    End If
    If Instr(para,"[vbCrlf]")>0 Then 

        Set objRegExp=New RegExp
        objRegExp.IgnoreCase =True
        objRegExp.Global=True

        objRegExp.Pattern="((</?form[^\n<]*>)|(<select[^\n<]*>)|(<textarea[^\n<]*>)|(</?option[^\n<]*>)|(</?dl[^\n<]*>)|(</?dt[^\n<]*>)|(</?dd[^\n<]*>)|(</?th[^\n<]*>)|(</?tr[^\n<]*>)|(</?td[^\n<]*>)|(</?tbody[^\n<]*>)|(</?table[^\n<]*>)|(</?hr[^\n<]*>)|(</?div[^\n<]*>)|(</?ul[^\n<]*>)|(</?li[^\n<]*>)|(</?ol[^\n<]*>)|(</?h1[^\n<]*>)|(</?h2[^\n<]*>)|(</?h3[^\n<]*>)|(</?h4[^\n<]*>)|(</?h5[^\n<]*>)|(</?h6[^\n<]*>)|(</?p[^\n<]*>))(\x20*(\r\n|\n))"

        source= objRegExp.Replace(source,"$1")

        objRegExp.Pattern="(\r\n|\n)"
        source= objRegExp.Replace(source,"<br/>")

        source=Replace(source,"<html>","")
        source=Replace(source,"</html>","")
        source=Replace(source,"<body>","")
        source=Replace(source,"</body>","")

    End If
    If Instr(para,"[vbTab]")>0 Then source=Replace(source,vbTab,"&nbsp;&nbsp;")
    If Instr(para,"[upload]")>0 Then
        source=Replace(source,"src=""upload/","src=""<#ZC_BLOG_HOST#>" & ZC_UPLOAD_DIRECTORY & "/")
        source=Replace(source,"href=""upload/","href=""<#ZC_BLOG_HOST#>" & ZC_UPLOAD_DIRECTORY & "/")
        source=Replace(source,"value=""upload/","value=""<#ZC_BLOG_HOST#>" & ZC_UPLOAD_DIRECTORY & "/")
        source=Replace(source,"href=""http://upload/","href=""<#ZC_BLOG_HOST#>" & ZC_UPLOAD_DIRECTORY & "/")
        source=Replace(source,"(this.nextSibling,'upload/","(this.nextSibling,'<#ZC_BLOG_HOST#>" & ZC_UPLOAD_DIRECTORY & "/")

        source=Replace(source,"src=""image/face/","src=""<#ZC_BLOG_HOST#>zb_users/emotion/face/")
    End If
    If Instr(para,"[anti-upload]")>0 Then
        source=Replace(source,"src="""& GetCurrentHost() & ZC_UPLOAD_DIRECTORY & "/","src=""upload/")
        source=Replace(source,"href="""& GetCurrentHost() & ZC_UPLOAD_DIRECTORY & "/","href=""upload/")
        source=Replace(source,"value="""& GetCurrentHost() & ZC_UPLOAD_DIRECTORY & "/","value=""upload/")
        source=Replace(source,"href="""& GetCurrentHost() & ZC_UPLOAD_DIRECTORY & "/","href=""http://upload/")
        source=Replace(source,"(this.nextSibling,'"& GetCurrentHost() & ZC_UPLOAD_DIRECTORY & "/","(this.nextSibling,'upload/")

        source=Replace(source,"src="""& GetCurrentHost() & "zb_users/emotion/face/","src=""<#ZC_BLOG_HOST#>zb_users/emotion/face/")
    End If
    If Instr(para,"[zc_blog_host]")>0 Then
        source=Replace(source,"<#ZC_BLOG_HOST#>",GetCurrentHost())
    End If
    If Instr(para,"[no-asp]")>0 Then
        source=Replace(source,"<"&"%","&lt;"&"%")
        source=Replace(source,"%"&">","%"&"&gt;")
    End If
    If Instr(para,"[nofollow]")>0 Then
        source=Replace(source,"<a ","<a rel=""nofollow"" ")
    End If
    If Instr(para,"[nohtml]")>0  Then

        Set objRegExp=New RegExp
        objRegExp.IgnoreCase =True
        objRegExp.Global=True
        objRegExp.Pattern="<[^>]*>"
        source= objRegExp.Replace(source,"")

    End If
    If Instr(para,"[filename]")>0  Then
        source=Replace(source,"/","")
        source=Replace(source,"\","")
        source=Replace(source,":","")
        source=Replace(source,"?","")
        source=Replace(source,"*","")
        source=Replace(source,"""","")
        source=Replace(source,"<","")
        source=Replace(source,">","")
        source=Replace(source,"|","")
        source=Replace(source," ","")
    End If
    If Instr(para,"[directory&file]")>0  Then
        source=Replace(source,"/","/")
        source=Replace(source,"\","/")
        source=Replace(source,":","")
        source=Replace(source,"?","")
        source=Replace(source,"*","")
        source=Replace(source,"""","")
        source=Replace(source,"<","")
        source=Replace(source,">","")
        source=Replace(source,"|","")
        source=Replace(source," ","")
    End If
    If Instr(para,"[normalname]")>0  Then
        source=Replace(source,"$","")
        source=Replace(source,"(","")
        source=Replace(source,")","")
        source=Replace(source,"*","")
        source=Replace(source,"+","")
        source=Replace(source,",","")
        source=Replace(source,"[","")
        source=Replace(source,"]","")
        source=Replace(source,"{","")
        source=Replace(source,"}","")
        source=Replace(source,"?","")
        source=Replace(source,"\","")
        source=Replace(source,"^","")
        source=Replace(source,"|","")
        source=Replace(source,":","")
        source=Replace(source,"""","")
        source=Replace(source,"'","")
    End If
    If Instr(para,"[normaltag]")>0  Then
        source=Replace(source,"$","")
        source=Replace(source,"(","")
        source=Replace(source,")","")
        source=Replace(source,"*","")
        source=Replace(source,"+","")
        source=Replace(source,"[","")
        source=Replace(source,"]","")
        source=Replace(source,"{","")
        source=Replace(source,"}","")
        source=Replace(source,"?","")
        source=Replace(source,"\","")
        source=Replace(source,"^","")
        source=Replace(source,"|","")
        source=Replace(source,":","")
        source=Replace(source,"""","")
        source=Replace(source,"'","")
        Set objRegExp=New RegExp
        objRegExp.IgnoreCase =True
        objRegExp.Global=True
        objRegExp.Pattern=",+"
        source= objRegExp.Replace(source,",")
        objRegExp.Pattern="(^,|,$)"
        source= objRegExp.Replace(source,"")
    End If
    If Instr(para,"[textarea]")>0 Then
        'Set objRegExp=New RegExp
        'objRegExp.IgnoreCase =True
        'objRegExp.Global=True
        'objRegExp.Pattern="(&)([#a-z0-9]{2,10})(;)"
        'source= objRegExp.Replace(source,"&amp;$2$3")
        source=Replace(source,"&","&amp;")
        source=Replace(source,"%","&#037;")
        source=Replace(source,"<","&lt;")
        source=Replace(source,">","&gt;")
    End If
    If Instr(para,"[japan-html]")>0 Then
        source=Replace(source,"ガ","&#12460;")
        source=Replace(source,"ギ","&#12462;")
        source=Replace(source,"ア","&#12450;")
        source=Replace(source,"ゲ","&#12466;")
        source=Replace(source,"ゴ","&#12468;")
        source=Replace(source,"ザ","&#12470;")
        source=Replace(source,"ジ","&#12472;")
        source=Replace(source,"ズ","&#12474;")
        source=Replace(source,"ゼ","&#12476;")
        source=Replace(source,"ゾ","&#12478;")
        source=Replace(source,"ダ","&#12480;")
        source=Replace(source,"ヂ","&#12482;")
        source=Replace(source,"ヅ","&#12485;")
        source=Replace(source,"デ","&#12487;")
        source=Replace(source,"ド","&#12489;")
        source=Replace(source,"バ","&#12496;")
        source=Replace(source,"パ","&#12497;")
        source=Replace(source,"ビ","&#12499;")
        source=Replace(source,"ピ","&#12500;")
        source=Replace(source,"ブ","&#12502;")
        source=Replace(source,"ブ","&#12502;")
        source=Replace(source,"プ","&#12503;")
        source=Replace(source,"ベ","&#12505;")
        source=Replace(source,"ペ","&#12506;")
        source=Replace(source,"ボ","&#12508;")
        source=Replace(source,"ポ","&#12509;")
        source=Replace(source,"ヴ","&#12532;")
    End If
    If Instr(para,"[html-japan]")>0 Then
        source=Replace(source,"&#12460;","ガ")
        source=Replace(source,"&#12462;","ギ")
        source=Replace(source,"&#12450;","ア")
        source=Replace(source,"&#12466;","ゲ")
        source=Replace(source,"&#12468;","ゴ")
        source=Replace(source,"&#12470;","ザ")
        source=Replace(source,"&#12472;","ジ")
        source=Replace(source,"&#12474;","ズ")
        source=Replace(source,"&#12476;","ゼ")
        source=Replace(source,"&#12478;","ゾ")
        source=Replace(source,"&#12480;","ダ")
        source=Replace(source,"&#12482;","ヂ")
        source=Replace(source,"&#12485;","ヅ")
        source=Replace(source,"&#12487;","デ")
        source=Replace(source,"&#12489;","ド")
        source=Replace(source,"&#12496;","バ")
        source=Replace(source,"&#12497;","パ")
        source=Replace(source,"&#12499;","ビ")
        source=Replace(source,"&#12500;","ピ")
        source=Replace(source,"&#12502;","ブ")
        source=Replace(source,"&#12502;","ブ")
        source=Replace(source,"&#12503;","プ")
        source=Replace(source,"&#12505;","ベ")
        source=Replace(source,"&#12506;","ペ")
        source=Replace(source,"&#12508;","ボ")
        source=Replace(source,"&#12509;","ポ")
        source=Replace(source,"&#12532;","ヴ")
    End If
    If Instr(para,"[html-format]")>0 Then
        source=Replace(source,"&","&amp;")
        source=Replace(source,"<","&lt;")
        source=Replace(source,">","&gt;")
        source=Replace(source,"""","&quot;")
    End If
    If Instr(para,"[anti-html-format]")>0 Then
        source=Replace(source,"&quot;","""")
        source=Replace(source,"&lt;","<")
        source=Replace(source,"&gt;",">")
        source=Replace(source,"&amp;","&")
    End If
    If Instr(para,"[wapnohtml]")>0 Then
        source=Replace(source,"<br/>",vbCrLf)
        source=Replace(source,"<br>",vbCrLf)
        Set objRegExp=New RegExp
        objRegExp.IgnoreCase =True
        objRegExp.Global=True
        objRegExp.Pattern="(<[^>]*)|([^<]*>)"
        source= objRegExp.Replace(source,"")
        objRegExp.Pattern="(\r\n|\n)"
        source= objRegExp.Replace(source,"<br/>")
    End If
    If Instr(para,"[nbsp-br]")>0 Then
        Set objRegExp=New RegExp
        objRegExp.IgnoreCase =True
        objRegExp.Global=True
        objRegExp.Pattern="&lt;$|&lt;b$|&lt;br$|&lt;br/$"
        source= objRegExp.Replace(source,"")
        objRegExp.Pattern="^br/&gt;|^r/&gt;|^/&gt;|^&gt;"
        source= objRegExp.Replace(source,"")
        objRegExp.Pattern="&lt;br/&gt;"
        source= objRegExp.Replace(source,"<br/>")
        objRegExp.Pattern="&amp;nbsp;"
        source= objRegExp.Replace(source," ")
    End If

    If Instr(para,"[closehtml]")>0 Then
        source=closeHTML(source)
    End If
    If Instr(para,"[anti-zc_blog_host]")>0 Then
        source=Replace(source,GetCurrentHost(),"<#ZC_BLOG_HOST#>")
    End If

    TransferHTML=source

End Function
'*********************************************************

这是PHP版本的:

/**
 *  通过正则表达式格式化字符串
 * @param string $source 字符串
 * @param string $para 正则表达式,可用[html-format]|[nohtml]|[noscript]|[enter]|[noenter]|[filename]|[normalname]或自定义表达式
 * @return string 
*/
function TransferHTML($source, $para) {

    if (strpos($para, '[html-format]') !== false) {
        $source = htmlspecialchars($source);
    }

    if (strpos($para, '[nohtml]') !== false) {
        $source = preg_replace("/<([^<>]*)>/si", "", $source);
        $source = str_replace("<", "˂", $source);
        $source = str_replace(">", "˃", $source);
    }

    if (strpos($para, '[noscript]') !== false) {
        $source = preg_replace("/<(script.*?)>(.*?)<(\/script.*?)>/si", "", $source);
        $source = preg_replace("/<(\/?script.*?)>/si", "", $source);
        $source = preg_replace("/javascript/si", "", $source);
        $source = preg_replace("/vbscript/si", "", $source);
        $source = preg_replace("/on([a-z]+)\s*=/si", "on\\=", $source);
    }
    if (strpos($para, '[enter]') !== false) {
        $source = str_replace("\r\n", "<br/>", $source);
        $source = str_replace("\n", "<br/>", $source);
        $source = str_replace("\r", "<br/>", $source);
        $source = preg_replace("/(<br\/>)+/", "<br/>", $source);
    }
    if (strpos($para, '[noenter]') !== false) {
        $source = str_replace("\r\n", "", $source);
        $source = str_replace("\n", "", $source);
        $source = str_replace("\r", "", $source);
    }
    if (strpos($para, '[filename]') !== false) {
        $source = str_replace(array("/", "#", "$", "\\", ":", "?", "*", "\"", "<", ">", "|", " "), array(""), $source);
    }
    if (strpos($para, '[normalname]') !== false) {
        $source = str_replace(array("#", "$", "(", ")", "*", "+", "[", "]", "{", "}", "?", "\\", "^", "|", ":", "'", "\"", ";", "@", "~", "=", "%", "&"), array(""), $source);
    }

    return $source;
}

突然想说ASP版本需要考虑的东西太尼玛多了...

转载请注明:鸟儿博客 » ZBLOG内置过滤函数

与本文相关的文章

游客
发表我的评论 换个身份
取消评论

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址