Un proveedor de servicios Laravel simple para usar fácilmente HTMLPurifier dentro de Laravel. Desde su sitio web:
HTML Purifier es una biblioteca de filtros HTML compatible con estándares escrita en PHP. HTML Purifier no solo eliminará todo el código malicioso (mejor conocido como XSS) con una lista blanca completamente auditada, segura pero permisiva, sino que también se asegurará de que sus documentos cumplan con los estándares, algo que solo se puede lograr con un conocimiento integral de las especificaciones del W3C. ¿Estás cansado de usar BBCode debido al panorama actual de filtros HTML deficientes o inseguros? ¿Tiene un editor WYSIWYG pero nunca ha podido utilizarlo? ¿Busca componentes de código abierto, de alta calidad y que cumplan con los estándares para la aplicación que está creando? ¡El Purificador HTML es para ti!
Requiere este paquete con el compositor:
composer require mews/purifier
El proveedor de servicios será descubierto automáticamente. No es necesario agregar el proveedor en ningún lugar.
Requiere este paquete con el compositor:
composer require mews/purifier
Busque la clave providers en config/app.php y registre el proveedor de servicios HTMLPurifier.
' providers ' => [
// ...
Mews Purifier PurifierServiceProvider::class,
] Busque la clave aliases en config/app.php y registre el alias del Purificador.
' aliases ' => [
// ...
' Purifier ' => Mews Purifier Facades Purifier::class,
]Consulte HTMLPurifier para Laravel 4
Utilice estos métodos dentro de sus solicitudes o middleware, siempre que necesite limpiar el HTML:
clean (Input:: get ( ' inputname ' ));o
Purifier:: clean (Input:: get ( ' inputname ' ));configuración dinámica
clean ( ' This is my H1 title ' , ' titles ' );
clean ( ' This is my H1 title ' , array ( ' Attr.EnableID ' => true ));o
Purifier:: clean ( ' This is my H1 title ' , ' titles ' );
Purifier:: clean ( ' This is my H1 title ' , array ( ' Attr.EnableID ' => true ));usar filtro URI
Purifier:: clean ( ' This is my H1 title ' , ' titles ' , function ( HTMLPurifier_Config $ config ) {
$ uri = $ config -> getDefinition ( ' URI ' );
$ uri -> addFilter ( new HTMLPurifier_URIFilter_NameOfFilter (), $ config );
});Alternativamente, en Laravel 7+, si buscas limpiar tu HTML dentro de tus modelos Eloquent, puedes usar nuestras conversiones personalizadas:
<?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
];
}Para usar su propia configuración, publique config.
php artisan vendor:publish --provider="MewsPurifierPurifierServiceProvider"
El archivo de configuración config/purifier.php debería tener este aspecto
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 ' ],
],
],
];Consulte la pestaña Lanzamientos de Github para obtener más información sobre los cambios recientes.
Si descubre algún problema relacionado con la seguridad, envíe un correo electrónico al autor en lugar de utilizar el rastreador de problemas.
MIT. Consulte el archivo de licencia para obtener más información.