萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> ASP 程式設計

ASP UBB 標籤轉 HTML 完整函數 Ubb2Html

支援標籤

  • 基本樣式:[b]/[/b]、[u]/[/u]
  • 區塊:[center]/[/center]、[fly]/[/fly] (marquee)
  • 連結:[url=網址]文字[/url]、自動偵測 <a> 加 target=_blank
  • 圖片/多媒體:[img]網址[/img]、[swf]、[dir]、[rm]、[mp]、[qt]
  • 表情符號::em1: ~ :em16: 對應 emot/em1.gif...
  • 引用/字型/顏色/大小:[quote]、[color]、[size]、[face] (透過 UbbStr 處理,未列出完整碼)

主函數

Function Ubb2Html(str, showEmot, showImg)
    On Error Resume Next
    If Not str <> "" Then Exit Function
    
    Dim tmpStr : tmpStr = "uNobwab"
    str = UbbStr(str, "url")
    str = UbbStr(str, "quote")
    str = UbbStr(str, "color")
    str = UbbStr(str, "size")
    str = UbbStr(str, "face")
    
    If showEmot Then
        Dim i
        For i = 1 To 16
            str = Replace(str, ":em" & i & ":", "", 1, 6, 1)
            str = Replace(str, ":em" & i & ":", "", 1, -1, 1)
        Next
    End If
    
    If showImg Then
        str = UbbStr(str, "img")
        str = UbbStr(str, "swf")
        str = UbbStr(str, "dir")
        str = UbbStr(str, "rm")
        str = UbbStr(str, "mp")
        str = UbbStr(str, "qt")
    End If
    
    str = UbbStr(str, "frame")
    
    ' 簡單標籤直接替換
    str = Replace(str, "[b]", "", 1, -1, 1)
    str = Replace(str, "[/b]", "", 1, -1, 1)
    str = Replace(str, "[u]", "", 1, -1, 1)
    str = Replace(str, "[/u]", "", 1, -1, 1)
    str = Replace(str, "[br]", "
", 1, -1, 1) str = Replace(str, "[center]", "
", 1, -1, 1) str = Replace(str, "[/center]", "
", 1, -1, 1) str = Replace(str, "[fly]", "", 1, -1, 1) str = Replace(str, "[/fly]", "", 1, -1, 1) ' 還原佔位符 str = Replace(str, "[" & tmpStr, "[", 1, -1, 1) str = Replace(str, tmpStr & "]", "]", 1, -1, 1) str = Replace(str, "/" & tmpStr, "/", 1, -1, 1) Ubb2Html = str End Function

缺失與風險

  • UbbStr 函數未在片段中定義,無法完整執行。
  • 標籤巢狀、屬性值含引號、XSS 過濾(如 [url=javascript:alert(1)])未處理。
  • Replace 參數 1, -1, 1 表示從第 1 字元開始、全部替換、二進位比較 (vbBinaryCompare)。
  • 表情符號雙重 Replace 邏輯不明(先換圖再清空?)。

建議

採用成熟解析器(如 BBCodeParser 移植版)或前端編輯器(CKEditor、TinyMCE)輸出 HTML 再經伺服端白名單清理(HtmlSanitizer)。

copyright © 萬盛學電腦網 all rights reserved