Purifier
vel 5
簡單的 Laravel 服務提供者,可在 Laravel 中輕鬆使用 HTMLPurifier。從他們的網站:
HTML Purifier 是一個用 PHP 寫的符合標準的 HTML 過濾器函式庫。 HTML Purifier 不僅會透過經過徹底審核、安全且寬鬆的白名單刪除所有惡意程式碼(更廣為人知的 XSS),還將確保您的文件符合標準,只有全面了解 W3C 規範才能實現這一目標。由於目前 HTML 過濾器有缺陷或不安全,您是否厭倦了使用 BBCode?有所見即所得編輯器但從未使用過?正在為您正在建立的應用程式尋找高品質、符合標準的開源元件? HTML 淨化器適合您!
需要此包與作曲家:
composer require mews/purifier
將自動發現服務提供者。您不需要在任何地方新增提供者。
需要此包與作曲家:
composer require mews/purifier
在config/app.php中找到providers鍵並註冊HTMLPurifier服務提供者。
' providers ' => [
// ...
Mews Purifier PurifierServiceProvider::class,
]在config/app.php中找到aliases鍵並註冊 Purifier 別名。
' aliases ' => [
// ...
' Purifier ' => Mews Purifier Facades Purifier::class,
]查看 Laravel 4 的 HTMLPurifier
在您的請求或中間件中,只要您需要清理 HTML,就可以使用這些方法:
clean (Input:: get ( ' inputname ' ));或者
Purifier:: clean (Input:: get ( ' inputname ' ));動態配置
clean ( ' This is my H1 title ' , ' titles ' );
clean ( ' This is my H1 title ' , array ( ' Attr.EnableID ' => true ));或者
Purifier:: clean ( ' This is my H1 title ' , ' titles ' );
Purifier:: clean ( ' This is my H1 title ' , array ( ' Attr.EnableID ' => true ));使用 URI 過濾器
Purifier:: clean ( ' This is my H1 title ' , ' titles ' , function ( HTMLPurifier_Config $ config ) {
$ uri = $ config -> getDefinition ( ' URI ' );
$ uri -> addFilter ( new HTMLPurifier_URIFilter_NameOfFilter (), $ config );
});或者,在 Laravel 7+ 中,如果您希望清理 Eloquent 模型中的 HTML,您可以使用我們的自訂轉換:
<?php
namespace App Models ;
use Illuminate Database Eloquent Model ;
use Mews Purifier Casts CleanHtml ;
use Mews Purifier Casts CleanHtmlInput ;
use Mews Purifier Casts CleanHtmlOutput ;
class Monster extends Model
{
protected $ casts = [
' bio ' => CleanHtml::class, // cleans both when getting and setting the value
' description ' => CleanHtmlInput::class, // cleans when setting the value
' history ' => CleanHtmlOutput::class, // cleans when getting the value
];
}若要使用您自己的設置,請發布配置。
php artisan vendor:publish --provider="MewsPurifierPurifierServiceProvider"
設定檔config/purifier.php應該像這樣
return [
' encoding ' => ' UTF-8 ' ,
' finalize ' => true ,
' ignoreNonStrings ' => false ,
' cachePath ' => storage_path ( ' app/purifier ' ),
' cacheFileMode ' => 0755 ,
' settings ' => [
' default ' => [
' HTML.Doctype ' => ' HTML 4.01 Transitional ' ,
' HTML.Allowed ' => ' div,b,strong,i,em,u,a[href|title],ul,ol,li,p[style],br,span[style],img[width|height|alt|src] ' ,
' CSS.AllowedProperties ' => ' font,font-size,font-weight,font-style,font-family,text-decoration,padding-left,color,background-color,text-align ' ,
' AutoFormat.AutoParagraph ' => true ,
' AutoFormat.RemoveEmpty ' => true ,
],
' test ' => [
' Attr.EnableID ' => ' true ' ,
],
" youtube " => [
" HTML.SafeIframe " => ' true ' ,
" URI.SafeIframeRegexp " => " %^(http://|https://|//)(www.youtube.com/embed/|player.vimeo.com/video/)% " ,
],
' custom_definition ' => [
' id ' => ' html5-definitions ' ,
' rev ' => 1 ,
' debug ' => false ,
' elements ' => [
// http://developers.whatwg.org/sections.html
[ ' section ' , ' Block ' , ' Flow ' , ' Common ' ],
[ ' nav ' , ' Block ' , ' Flow ' , ' Common ' ],
[ ' article ' , ' Block ' , ' Flow ' , ' Common ' ],
[ ' aside ' , ' Block ' , ' Flow ' , ' Common ' ],
[ ' header ' , ' Block ' , ' Flow ' , ' Common ' ],
[ ' footer ' , ' Block ' , ' Flow ' , ' Common ' ],
// Content model actually excludes several tags, not modelled here
[ ' address ' , ' Block ' , ' Flow ' , ' Common ' ],
[ ' hgroup ' , ' Block ' , ' Required: h1 | h2 | h3 | h4 | h5 | h6 ' , ' Common ' ],
// http://developers.whatwg.org/grouping-content.html
[ ' figure ' , ' Block ' , ' Optional: (figcaption, Flow) | (Flow, figcaption) | Flow ' , ' Common ' ],
[ ' figcaption ' , ' Inline ' , ' Flow ' , ' Common ' ],
// http://developers.whatwg.org/the-video-element.html#the-video-element
[ ' video ' , ' Block ' , ' Optional: (source, Flow) | (Flow, source) | Flow ' , ' Common ' , [
' src ' => ' URI ' ,
' type ' => ' Text ' ,
' width ' => ' Length ' ,
' height ' => ' Length ' ,
' poster ' => ' URI ' ,
' preload ' => ' Enum#auto,metadata,none ' ,
' controls ' => ' Bool ' ,
]],
[ ' source ' , ' Block ' , ' Flow ' , ' Common ' , [
' src ' => ' URI ' ,
' type ' => ' Text ' ,
]],
// http://developers.whatwg.org/text-level-semantics.html
[ ' s ' , ' Inline ' , ' Inline ' , ' Common ' ],
[ ' var ' , ' Inline ' , ' Inline ' , ' Common ' ],
[ ' sub ' , ' Inline ' , ' Inline ' , ' Common ' ],
[ ' sup ' , ' Inline ' , ' Inline ' , ' Common ' ],
[ ' mark ' , ' Inline ' , ' Inline ' , ' Common ' ],
[ ' wbr ' , ' Inline ' , ' Empty ' , ' Core ' ],
// http://developers.whatwg.org/edits.html
[ ' ins ' , ' Block ' , ' Flow ' , ' Common ' , [ ' cite ' => ' URI ' , ' datetime ' => ' CDATA ' ]],
[ ' del ' , ' Block ' , ' Flow ' , ' Common ' , [ ' cite ' => ' URI ' , ' datetime ' => ' CDATA ' ]],
],
' attributes ' => [
[ ' iframe ' , ' allowfullscreen ' , ' Bool ' ],
[ ' table ' , ' height ' , ' Text ' ],
[ ' td ' , ' border ' , ' Text ' ],
[ ' th ' , ' border ' , ' Text ' ],
[ ' tr ' , ' width ' , ' Text ' ],
[ ' tr ' , ' height ' , ' Text ' ],
[ ' tr ' , ' border ' , ' Text ' ],
],
],
' custom_attributes ' => [
[ ' a ' , ' target ' , ' Enum#_blank,_self,_target,_top ' ],
],
' custom_elements ' => [
[ ' u ' , ' Inline ' , ' Inline ' , ' Common ' ],
],
],
];請參閱 Github 發布選項卡,以了解有關最近更改內容的更多資訊。
如果您發現任何與安全相關的問題,請向作者發送電子郵件,而不是使用問題追蹤器。
麻省理工學院。請參閱許可證文件以獲取更多資訊。