定制 saeven/zf3-purifier 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

saeven/zf3-purifier

Composer 安装命令:

composer require saeven/zf3-purifier

包简介

HTML Purifier integration module for Zend Framework 3

README 文档

README

Mantainer Build Status Latest Stable Version License Total Downloads

Soflomo\Purifier is the HTMLPurifier integration for Zend Framework 2.

It provides a Laminas\Filter\FilterInterface implementation so you can use HTMLPurifier within your Laminas\InputFilter classes. Furthermore, a view helper is provided to help purifying html on the fly in view scripts.

Installation

You can install Soflomo\Purifier through Composer

$: composer require soflomo/purifier

To enable the module in your ZF2 application, add an entry Soflomo\Purifier to the list of enabled modules in config/application.config.php.

Usage

In your input filter configuration, use the htmlpurifier as name in your filter spec.

An example Form:

class MyForm extends Laminas\Form\Form implements Laminas\InputFilter\InputFilterProviderInterface
{
    public function init()
    {
        $this->add([
            'name'    => 'text',
            'options' => [
                'label' => 'Text'
            ],
            'attributes' => [
                'type' => 'textarea',
            ],
        ]);
    }

    public function getInputFilterSpecification()
    {
        return [
            'text'  => [
                'required' => true,
                'filters'  => [
                    [ 'name' => 'stringtrim' ],
                    [ 'name' => 'htmlpurifier' ],
                ],
            ],
        ];
    }
}

or an InputFilter:

class MyInputFilter extends Laminas\InputFilter\InputFilter
{
    public function init()
    {
        $this->add([
            'name'     => 'text',
            'required' => true,
            'filters'  => [
                [ 'name' => 'stringtrim' ],
                [ 'name' => 'htmlpurifier' ],
            ],
        ]);
    }
}

Alternatively, you can use the FQCN Soflomo\Purifier\PurifierFilter in place of the htmlpurifier alias.

If you're pulling the consumers from their respective plugin managers, this should work out-of-the-box. If not, please read how to inject the filter plugin manager.

If for some reason you want to use the filter in your view templates, you also have a view helper available. Please be aware that HTMLPurifier is not a very fast library and as such, filtering on every request can be a significant performance bottleneck. Be advised to use a caching mechanism to cache the output of the filtered html. The view helper is available under the key htmlPurifier:

<?php echo $this->htmlPurifier()->purify($foo->getText()) ?>

And there is a shorthand available too:

<?php echo $this->htmlPurifier($foo->getText()) ?>

Configuring HTMLPurifier

HTMLPurifier use the class HTMLPurifier_Config to configure its rules. Most configuration rules are based on a key/value pair: $config->set('HTML.Doctype', 'HTML 4.01 Transitional').

Global configuration

The HTMLPurifier_Config key/value storege is exposed by Soflomo\Purifier as an associative array into the ZF2 configuration, so you can customize the default HTMLPurifier_Config instance like this:

return [
    'soflomo_purifier' => [
        'config' => [
            'HTML.Doctype' => 'HTML 4.01 Transitional'
        ],
    ],
];

The configuration factory also handles a definitions sub array to add custom definitions to the purifier, as documented here.

For example:

return [
    'soflomo_purifier' => [
        'config' => [
            'HTML.DefinitionID' => 'my custom definitions',
        ], 
        'definitions' => [
            'HTML' => [
                'addAttribute' => [
                    [ 'a', 'target', 'Enum#_blank,_self,_target,_top' ]
                ],
            ],
        ],
    ],
];

This will add a HTMLPurifier_AttrDef_Enum definition for the target attribute of the a element. Note that an arbitrary value for the HTML.DefinitionID config key is required to correctly load the definition.

Definitions can also be set under the definitions key in the config array. These will override the key in the soflomo_purifier array.

Per-instance configuration

You can also set a different configuration each time you add the filter with a spec using the usual options key:

class MyInputFilter extends Laminas\InputFilter\InputFilter
{
    public function init()
    {
        $this->add([
            'name'     => 'text',
            'required' => true,
            'filters'  => [
                [ 'name' => 'stringtrim' ],
                [
                    'name' => 'htmlpurifier',
                    'options' => [
                        'purifier_config' => [
                            'HTML.AllowedElements' => 'a, span'
                        ],
                    ],
                ],
            ],
        ]);
    }
}

Injecting the FilterManager

If you instantiate your forms or your input filters manually with the new keyword, rather than pulling them from their respective plugin managers (i.e. FormElementManager and InputFilterManager), the FilterManager is not injected automatically into their factories, and these will resort to use a default one.

As such, you get a ServiceNotFoundException: Laminas\Filter\FilterPluginManager::get was unable to fetch or create an instance for htmlpurifier. This means the filter plugin manager was lazily instantiated, and does not know about the htmlpurifier plugin.

You can hack your way through this by executing the initializers manually:

$form = new MyForm();
$formElementManager = $serviceManager->get('FormElementManager');
$formElementManager->injectFactory($form);

// same goes for input filters
$inputFilter = new MyInputFilter();
$inputFilterManager = $serviceManager->get('InputFilterManager');
$inputFilterManager->populateFactory($inputFilter);

It is however strongly advised to pull forms and input filters from their respective plugin managers, and use the init() method (which will be invoked after all the factories are injected) when applicable.

Performance optimization

HTMLPurifier is not the fastest library. It uses a large number of classes and files so autoloading can be cumbersome.

Luckily, you can create a standalone version of the HTMLPurifier class, where a single file contains most of the classes.

The script in vendor/bin/purifier-generate-standalone generates this file for you. The standalone file is created inside vendor/ezyang/htmlpurifier/library so make sure you can write in that directory.

Soflomo\Purifier helps you using this standalone version with the configuration option soflomo_purifier.standalone.

For example, you could add this in your config/autoload/local.php:

return [
    'soflomo_purifier' => [
        'standalone' => true,
    ],
];

If you want to place the standalone file somewhere else, you can set its path too:

return [
    'soflomo_purifier' => [
        'standalone'      => true,
        'standalone_path' => 'path/to/HTMLPurifier.standalone.php',
    ],
];

Note: The standalone generator script requires HTMLPurifier to be installed either with version <4.7.0 or with Composer --prefer-source flag, because since that version the maintenance tools were removed from the archive (see htmlpurifier #65).

saeven/zf3-purifier 适用场景与选型建议

saeven/zf3-purifier 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 43.04k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2016 年 10 月 10 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「zf2」 「filter」 「html」 「Purifier」 「purify」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 saeven/zf3-purifier 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 saeven/zf3-purifier 我们能提供哪些服务?
定制开发 / 二次开发

基于 saeven/zf3-purifier 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 43.04k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 3
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 12
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2016-10-10