slouyakuza / laravel-html-purifier
Laravel 的 HTML 清理器
v2.0.0
2023-03-04 09:08 UTC
Requires
- php: ^7.2|^8.0
- ezyang/htmlpurifier: 4.13.*
- illuminate/config: ^5.8|^6.0|^7.0|^8.0|^9.0|^10.0
- illuminate/filesystem: ^5.8|^6.0|^7.0|^8.0|^9.0|^10.0
- illuminate/support: ^5.8|^6.0|^7.0|^8.0|^9.0|^10.0
Requires (Dev)
- graham-campbell/testbench: ^3.2|^5.5.1
- mockery/mockery: ^1.3.3
- phpunit/phpunit: ^8.0|^9.0
Suggests
- laravel/framework: To test the Laravel bindings
- laravel/lumen-framework: To test the Lumen bindings
README
HTML清理器预处理器
一个简单的 Laravel 服务提供者,用于在 Laravel 中轻松使用 HTMLPurifier。从他们的网站
HTML Purifier 是一个标准的 HTML 过滤库,用 PHP 编写。HTML Purifier 不仅会移除所有恶意代码(更广为人知的是 XSS),而且会通过一个彻底审查的、安全的、允许的白色列表,同时确保您的文档符合标准,这是通过全面了解 W3C 规范的一部分来实现的。您是否因为当前环境中缺乏或不可靠的 HTML 过滤器而厌倦了 BBCode?您有一个 WYSIWYG 编辑器,但它从未真正有效?您正在寻找高质量、标准兼容、开源的组件来构建您正在构建的应用程序?HTML 清理器是您的选择!
安装
适用于 Laravel 5.5+
使用 composer 安装此包
composer require sloyakuza/laravel-html-purifier
服务提供者将自动被发现。无需在任何地方添加提供者。
适用于 Laravel 5.0 至 5.4
使用 composer 安装此包
composer require sloyakuza/laravel-html-purifier
在 config/app.php
中查找 providers
键并注册 HTMLPurifier 服务提供者。
'providers' => [ // ... SLOYakuza\Purifier\PurifierServiceProvider::class, ]
在 config/app.php
中查找 aliases
键并注册别名。
'aliases' => [ // ... 'Purifier' => SLOYakuza\Purifier\Facades\Purifier::class, ]
常规
在您的请求或中间件中使用这些方法,无论您需要在何处进行 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 SLOYakuza\Purifier\Casts\CleanHtml; use SLOYakuza\Purifier\Casts\CleanHtmlInput; use SLOYakuza\Purifier\Casts\CleanHtmlOutput; class User 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="SLOYakuza\Purifier\PurifierServiceProvider"
配置文件 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'], ], ], ];