|
1 | 1 | # laravel-html-purifier
|
2 |
| - An HTML Purifier for Laravel |
| 2 | +An HTML Purifier for Laravel |
| 3 | + |
| 4 | +[ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square |
| 5 | +[![Software License][ico-license]](LICENSE) |
| 6 | + |
| 7 | +A simple [Laravel](http://www.laravel.com/) service provider for easily using [HTMLPurifier](http://htmlpurifier.org/) inside Laravel. From their website: |
| 8 | + |
| 9 | +> HTML Purifier is a standards-compliant HTML filter library written in PHP. HTML Purifier will not only remove all malicious code (better known as XSS) with a thoroughly audited, secure yet permissive whitelist, it will also make sure your documents are standards compliant, something only achievable with a comprehensive knowledge of W3C's specifications. Tired of using BBCode due to the current landscape of deficient or insecure HTML filters? Have a WYSIWYG editor but never been able to use it? Looking for high-quality, standards-compliant, open-source components for that application you're building? HTML Purifier is for you! |
| 10 | +
|
| 11 | +## Installation |
| 12 | + |
| 13 | +### For Laravel 5.5+ |
| 14 | + |
| 15 | +Require this package with composer: |
| 16 | +``` |
| 17 | +composer require hdvinnie/laravel-html-purifier |
| 18 | +``` |
| 19 | + |
| 20 | +The service provider will be auto-discovered. You do not need to add the provider anywhere. |
| 21 | + |
| 22 | +### For Laravel 5.0 to 5.4 |
| 23 | + |
| 24 | +Require this package with composer: |
| 25 | +``` |
| 26 | +composer require hdvinnie/laravel-html-purifier |
| 27 | +``` |
| 28 | + |
| 29 | +Find the `providers` key in `config/app.php` and register the HTMLPurifier Service Provider. |
| 30 | + |
| 31 | +```php |
| 32 | + 'providers' => [ |
| 33 | + // ... |
| 34 | + HDVinnie\Purifier\PurifierServiceProvider::class, |
| 35 | + ] |
| 36 | +``` |
| 37 | + |
| 38 | +Find the `aliases` key in `config/app.php` and register the Purifier alias. |
| 39 | + |
| 40 | +```php |
| 41 | + 'aliases' => [ |
| 42 | + // ... |
| 43 | + 'Purifier' => HDVinnie\Purifier\Facades\Purifier::class, |
| 44 | + ] |
| 45 | +``` |
| 46 | + |
| 47 | +## Usage |
| 48 | + |
| 49 | + |
| 50 | +Use these methods inside your requests or middleware, wherever you need the HTML cleaned up: |
| 51 | + |
| 52 | +```php |
| 53 | +\clean(Input::get('inputname')); |
| 54 | +``` |
| 55 | +or |
| 56 | + |
| 57 | +```php |
| 58 | +Purifier::clean(Input::get('inputname')); |
| 59 | +``` |
| 60 | + |
| 61 | +dynamic config |
| 62 | +```php |
| 63 | +\clean('This is my H1 title', 'titles'); |
| 64 | +\clean('This is my H1 title', array('Attr.EnableID' => true)); |
| 65 | +``` |
| 66 | +or |
| 67 | + |
| 68 | +```php |
| 69 | +Purifier::clean('This is my H1 title', 'titles'); |
| 70 | +Purifier::clean('This is my H1 title', array('Attr.EnableID' => true)); |
| 71 | +``` |
| 72 | + |
| 73 | +use [URI filter](http://htmlpurifier.org/docs/enduser-uri-filter.html) |
| 74 | + |
| 75 | +```php |
| 76 | +Purifier::clean('This is my H1 title', 'titles', function (HTMLPurifier_Config $config) { |
| 77 | + $uri = $config->getDefinition('URI'); |
| 78 | + $uri->addFilter(new HTMLPurifier_URIFilter_NameOfFilter(), $config); |
| 79 | +}); |
| 80 | +``` |
| 81 | + |
| 82 | +Alternatively, in Laravel 7+, if you're looking to clean your HTML inside your Eloquent models, you can use our custom casts: |
| 83 | + |
| 84 | +```php |
| 85 | +<?php |
| 86 | + |
| 87 | +namespace App\Models; |
| 88 | + |
| 89 | +use Illuminate\Database\Eloquent\Model; |
| 90 | +use HDVinnie\Purifier\Casts\CleanHtml; |
| 91 | +use HDVinnie\Purifier\Casts\CleanHtmlInput; |
| 92 | +use HDVinnie\Purifier\Casts\CleanHtmlOutput; |
| 93 | + |
| 94 | +class User extends Model |
| 95 | +{ |
| 96 | + protected $casts = [ |
| 97 | + 'bio' => CleanHtml::class, // cleans both when getting and setting the value |
| 98 | + 'description' => CleanHtmlInput::class, // cleans when setting the value |
| 99 | + 'history' => CleanHtmlOutput::class, // cleans when getting the value |
| 100 | + ]; |
| 101 | +} |
| 102 | +``` |
| 103 | + |
| 104 | +## Configuration |
| 105 | + |
| 106 | +To use your own settings, publish config. |
| 107 | + |
| 108 | +``` |
| 109 | +php artisan vendor:publish --provider="HDVinnie\Purifier\PurifierServiceProvider" |
| 110 | +``` |
| 111 | + |
| 112 | +Config file `config/purifier.php` should like this |
| 113 | + |
| 114 | +```php |
| 115 | + |
| 116 | +return [ |
| 117 | + 'encoding' => 'UTF-8', |
| 118 | + 'finalize' => true, |
| 119 | + 'ignoreNonStrings' => false, |
| 120 | + 'cachePath' => storage_path('app/purifier'), |
| 121 | + 'cacheFileMode' => 0755, |
| 122 | + 'settings' => [ |
| 123 | + 'default' => [ |
| 124 | + 'HTML.Doctype' => 'HTML 4.01 Transitional', |
| 125 | + 'HTML.Allowed' => 'div,b,strong,i,em,u,a[href|title],ul,ol,li,p[style],br,span[style],img[width|height|alt|src]', |
| 126 | + 'CSS.AllowedProperties' => 'font,font-size,font-weight,font-style,font-family,text-decoration,padding-left,color,background-color,text-align', |
| 127 | + 'AutoFormat.AutoParagraph' => true, |
| 128 | + 'AutoFormat.RemoveEmpty' => true, |
| 129 | + ], |
| 130 | + 'test' => [ |
| 131 | + 'Attr.EnableID' => 'true', |
| 132 | + ], |
| 133 | + "youtube" => [ |
| 134 | + "HTML.SafeIframe" => 'true', |
| 135 | + "URI.SafeIframeRegexp" => "%^(http://|https://|//)(www.youtube.com/embed/|player.vimeo.com/video/)%", |
| 136 | + ], |
| 137 | + 'custom_definition' => [ |
| 138 | + 'id' => 'html5-definitions', |
| 139 | + 'rev' => 1, |
| 140 | + 'debug' => false, |
| 141 | + 'elements' => [ |
| 142 | + // http://developers.whatwg.org/sections.html |
| 143 | + ['section', 'Block', 'Flow', 'Common'], |
| 144 | + ['nav', 'Block', 'Flow', 'Common'], |
| 145 | + ['article', 'Block', 'Flow', 'Common'], |
| 146 | + ['aside', 'Block', 'Flow', 'Common'], |
| 147 | + ['header', 'Block', 'Flow', 'Common'], |
| 148 | + ['footer', 'Block', 'Flow', 'Common'], |
| 149 | + |
| 150 | + // Content model actually excludes several tags, not modelled here |
| 151 | + ['address', 'Block', 'Flow', 'Common'], |
| 152 | + ['hgroup', 'Block', 'Required: h1 | h2 | h3 | h4 | h5 | h6', 'Common'], |
| 153 | + |
| 154 | + // http://developers.whatwg.org/grouping-content.html |
| 155 | + ['figure', 'Block', 'Optional: (figcaption, Flow) | (Flow, figcaption) | Flow', 'Common'], |
| 156 | + ['figcaption', 'Inline', 'Flow', 'Common'], |
| 157 | + |
| 158 | + // http://developers.whatwg.org/the-video-element.html#the-video-element |
| 159 | + ['video', 'Block', 'Optional: (source, Flow) | (Flow, source) | Flow', 'Common', [ |
| 160 | + 'src' => 'URI', |
| 161 | + 'type' => 'Text', |
| 162 | + 'width' => 'Length', |
| 163 | + 'height' => 'Length', |
| 164 | + 'poster' => 'URI', |
| 165 | + 'preload' => 'Enum#auto,metadata,none', |
| 166 | + 'controls' => 'Bool', |
| 167 | + ]], |
| 168 | + ['source', 'Block', 'Flow', 'Common', [ |
| 169 | + 'src' => 'URI', |
| 170 | + 'type' => 'Text', |
| 171 | + ]], |
| 172 | + |
| 173 | + // http://developers.whatwg.org/text-level-semantics.html |
| 174 | + ['s', 'Inline', 'Inline', 'Common'], |
| 175 | + ['var', 'Inline', 'Inline', 'Common'], |
| 176 | + ['sub', 'Inline', 'Inline', 'Common'], |
| 177 | + ['sup', 'Inline', 'Inline', 'Common'], |
| 178 | + ['mark', 'Inline', 'Inline', 'Common'], |
| 179 | + ['wbr', 'Inline', 'Empty', 'Core'], |
| 180 | + |
| 181 | + // http://developers.whatwg.org/edits.html |
| 182 | + ['ins', 'Block', 'Flow', 'Common', ['cite' => 'URI', 'datetime' => 'CDATA']], |
| 183 | + ['del', 'Block', 'Flow', 'Common', ['cite' => 'URI', 'datetime' => 'CDATA']], |
| 184 | + ], |
| 185 | + 'attributes' => [ |
| 186 | + ['iframe', 'allowfullscreen', 'Bool'], |
| 187 | + ['table', 'height', 'Text'], |
| 188 | + ['td', 'border', 'Text'], |
| 189 | + ['th', 'border', 'Text'], |
| 190 | + ['tr', 'width', 'Text'], |
| 191 | + ['tr', 'height', 'Text'], |
| 192 | + ['tr', 'border', 'Text'], |
| 193 | + ], |
| 194 | + ], |
| 195 | + 'custom_attributes' => [ |
| 196 | + ['a', 'target', 'Enum#_blank,_self,_target,_top'], |
| 197 | + ], |
| 198 | + 'custom_elements' => [ |
| 199 | + ['u', 'Inline', 'Inline', 'Common'], |
| 200 | + ], |
| 201 | + ], |
| 202 | + |
| 203 | +]; |
| 204 | +``` |
0 commit comments