Un simple fournisseur de services Laravel pour utiliser facilement HTMLPurifier dans Laravel. Depuis leur site internet :
HTML Purifier est une bibliothèque de filtres HTML conforme aux normes écrite en PHP. HTML Purifier supprimera non seulement tous les codes malveillants (mieux connus sous le nom de XSS) grâce à une liste blanche soigneusement auditée, sécurisée mais permissive, mais il garantira également que vos documents sont conformes aux normes, ce qui n'est possible qu'avec une connaissance approfondie des spécifications du W3C. Vous en avez assez d'utiliser BBCode en raison du paysage actuel de filtres HTML déficients ou non sécurisés ? Vous disposez d'un éditeur WYSIWYG mais vous n'avez jamais pu l'utiliser ? Vous recherchez des composants open source de haute qualité, conformes aux normes, pour l'application que vous créez ? HTML Purifier est fait pour vous !
Exiger ce package avec composer :
composer require mews/purifier
Le fournisseur de services sera découvert automatiquement. Vous n’avez pas besoin d’ajouter le fournisseur n’importe où.
Exiger ce package avec composer :
composer require mews/purifier
Recherchez la clé providers
dans config/app.php
et enregistrez le fournisseur de services HTMLPurifier.
' providers ' => [
// ...
Mews Purifier PurifierServiceProvider::class,
]
Recherchez la clé aliases
dans config/app.php
et enregistrez l'alias Purifier.
' aliases ' => [
// ...
' Purifier ' => Mews Purifier Facades Purifier::class,
]
Découvrez HTMLPurifier pour Laravel 4
Utilisez ces méthodes dans vos requêtes ou middleware, partout où vous avez besoin de nettoyer le code HTML :
clean (Input:: get ( ' inputname ' ));
ou
Purifier:: clean (Input:: get ( ' inputname ' ));
configuration dynamique
clean ( ' This is my H1 title ' , ' titles ' );
clean ( ' This is my H1 title ' , array ( ' Attr.EnableID ' => true ));
ou
Purifier:: clean ( ' This is my H1 title ' , ' titles ' );
Purifier:: clean ( ' This is my H1 title ' , array ( ' Attr.EnableID ' => true ));
utiliser le filtre URI
Purifier:: clean ( ' This is my H1 title ' , ' titles ' , function ( HTMLPurifier_Config $ config ) {
$ uri = $ config -> getDefinition ( ' URI ' );
$ uri -> addFilter ( new HTMLPurifier_URIFilter_NameOfFilter (), $ config );
});
Alternativement, dans Laravel 7+, si vous cherchez à nettoyer votre HTML dans vos modèles Eloquent, vous pouvez utiliser nos conversions personnalisées :
<?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
];
}
Pour utiliser vos propres paramètres, publiez config.
php artisan vendor:publish --provider="MewsPurifierPurifierServiceProvider"
Le fichier de configuration config/purifier.php
devrait ressembler à ceci
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 ' ],
],
],
];
Veuillez consulter l'onglet Versions de Github pour plus d'informations sur ce qui a changé récemment.
Si vous découvrez des problèmes liés à la sécurité, veuillez envoyer un e-mail à l'auteur au lieu d'utiliser le suivi des problèmes.
MIT. Veuillez consulter le fichier de licence pour plus d'informations.