Laravel 내부에서 HTMLPurifier를 쉽게 사용하기 위한 간단한 Laravel 서비스 제공자입니다. 웹사이트에서:
HTML Purifier는 PHP로 작성된 표준을 준수하는 HTML 필터 라이브러리입니다. HTML Purifier는 철저하게 감사되고 안전하지만 허용되는 화이트리스트를 통해 모든 악성 코드(XSS로 더 잘 알려져 있음)를 제거할 뿐만 아니라 문서가 표준을 준수하는지 확인합니다. 이는 W3C 사양에 대한 포괄적인 지식을 통해서만 달성할 수 있습니다. 현재 HTML 필터가 부족하거나 안전하지 않기 때문에 BBCode를 사용하는 데 지치셨나요? WYSIWYG 편집기가 있지만 사용해 본 적이 없습니까? 귀하가 구축 중인 애플리케이션을 위한 고품질의 표준 준수 오픈 소스 구성 요소를 찾고 계십니까? 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 릴리스 탭을 참조하세요.
보안 관련 문제를 발견한 경우 문제 추적기를 사용하는 대신 작성자에게 이메일을 보내주세요.
MIT. 자세한 내용은 라이센스 파일을 참조하십시오.