mews/purifier

Laravel 5/6/7/8/9/10 HtmlPurifier 包

安装次数: 10,504,158

依赖: 113

建议者: 0

安全: 0

星标: 1,864

关注者: 37

分支: 229

开放问题: 73

类型:package

3.4.2 2024-03-20 16:18 UTC

README

Build Status codecov Latest Stable Version Latest Unstable Version License Total Downloads

一个简单的 Laravel 服务提供者,可以轻松地在 Laravel 中使用 HTMLPurifier。从他们的网站

HTML Purifier 是一个遵循标准的 HTML 过滤库,用 PHP 编写。HTML Purifier 不仅能彻底审计、安全且宽容的白名单移除所有恶意代码(更广为人知的是 XSS),还能确保您的文档遵循标准,这是只有全面了解 W3C 规范才能实现的事情。厌倦了使用 BBCode,因为当前缺乏或不可靠的 HTML 过滤器?拥有 WYSIWYG 编辑器但从未能够使用它?正在寻找高质量、遵循标准、开源的应用程序组件?HTML Purifier 就是您需要的!

安装

对于 Laravel 5.5+

使用 composer 安装此包

composer require mews/purifier

服务提供者将自动发现。您不需要将其添加到任何地方。

对于 Laravel 5.0 到 5.4

使用 composer 安装此包

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

查看 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="Mews\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'],
        ],
    ],

];

变更日志

请参阅 Github 发布标签 了解最近更改的信息。

安全

如果您发现任何安全相关的问题,请通过电子邮件 联系作者 而不是使用问题跟踪器。

致谢

许可证

MIT。有关更多信息,请参阅 许可证文件