saeven/zf2-purifier
Composer 安装命令:
composer require saeven/zf2-purifier
包简介
HTML Purifier as filter and view helper for Zend Framework 2.5
README 文档
README
Soflomo\Purifier is the HTMLPurifier integration for Zend Framework 2. It provides a Zend\Filter\FilterInterface instance so you can use Soflomo\Purifier directly to filter your html input with your Zend\InputFilter classes. Furthermore, a view helper is provided to help purifying html on the fly in view scripts.
Installation
Soflomo\Purifier is available through composer. Add "soflomo/purifier" to your composer.json list. You can specify the latest stable version of Soflomo\Purifier:
"soflomo/purifier": ">=0.1.0"
To enable the module in your config/application.config.php file, add an entry Soflomo\Purifier to the list of enabled modules.
Usage
In your input filter configuration, add the htmlpurifier filter to filter the result. An example form:
namespace Application\Form; use Zend\InputFilter; use Zend\Form\Form; class Foo extends Form implements InputFilter\InputFilterProviderInterface { public function __construct($name = null) { parent::__construct($name); $this->add(array( 'name' => 'title', 'options' => array( 'label' => 'Title' ), )); $this->add(array( 'name' => 'text', 'options' => array( 'label' => 'Text' ), 'attributes' => array( 'type' => 'textarea', ), )); } public function getInputFilterSpecification() { return array( 'title' => array( 'required' => true, 'filters' => array( array('name' => 'stringtrim'), ), ), 'text' => array( 'required' => true, 'filters' => array( array('name' => 'stringtrim'), array('name' => 'htmlpurifier'), ), ), ); } }
If your form has the filter plugin manager injected, this should work out-of-the-box. If not, please read how to inject the filter plugin manager.
When you are not able to filter the html when it is inserted into the database, you might want to filter the html in your view. Please be aware 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())?>
Inject Filter Manager
If you instantiate your form with new FormClass, the plugin manager for filters is not injected. As such, you get a ServiceNotFoundException: "Zend\Filter\FilterPluginManager::get was unable to fetch or create an instance for htmlpurifier". This means the filter plugin manager does not know about the htmlpurifier plugin.
You can fix this by grabbing the filter plugin manager from the Service Manager and inject it into the form manually:
use Zend\Filter\FilterChain; $form = new MyForm; // $sl is the service locator $plugins = $sl->get('FilterManager'); $chain = new FilterChain; $chain->setPluginManager($plugins); $form->getFormFactory()->getInputFilterFactory()->setDefaultFilterChain($chain);
The library Soflomo\Common has a utility class to inject the filter manager. If you require soflomo\common via composer, you can replace above code with this line:
use Soflomo\Common\Form\FormUtils; $form = new MyForm; // $sl is the service locator FormUtils::injectFilterPluginManager($form, $sl);
Speed up performance
HTMLPurifier is not the fastest library, but it is the most secure one to filter html in php. By default, HTMLPurifier uses a large number of classes and inclusion of all the files slows down performance during autoloading. 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 (there is nothing to configure about that) so make sure you can write in that directory. The Soflomo\Purifier library helps you to use this standalone version by the configuration option soflomo_prototype.standalone. In your config/autoload/local.php:
return array( 'soflomo_purifier' => array( 'standalone' => true, ), );
If your composer does not load the libraries into vendor/, you can update the path to the standalone version too:
return array( 'soflomo_purifier' => array( 'standalone' => true, 'standalone_path' => 'something-else/ezyang/htmlpurifier/library/HTMLPurifier.standalone.php', ), );
Configure HTMLPurifier
The HTMLPurifier has a class HTMLPurifier_Config where it is possible to configure the purifier rules. Most configuration rules are based on a key/value pair: $config->set('HTML.Doctype', 'HTML 4.01 Transitional');. This mapping is copied to Zend Framework 2 configuration files, so you can easily modify HTMLPurifiers configation:
return array( 'soflomo_purifier' => array( 'config' => array( 'HTML.Doctype' => 'HTML 4.01 Transitional' ), ), );
saeven/zf2-purifier 适用场景与选型建议
saeven/zf2-purifier 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 141 次下载、GitHub Stars 达 0, 最近一次更新时间为 2015 年 07 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「zf2」 「filter」 「html」 「Purifier」 「purify」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 saeven/zf2-purifier 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 saeven/zf2-purifier 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 saeven/zf2-purifier 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Texy converts plain text in easy to read Texy syntax into structurally valid (X)HTML. It supports adding of images, links, nested lists, tables and has full support for CSS. Texy supports hyphenation of long words (which reflects language rules), clickable emails and URL (emails are obfuscated again
LosLog provides some log utility
Nova searchable filter for belongsTo relationships.
EdpModuleLayouts is very simple Laminas module for making module-specific layouts insanely easy.
Symfony DataGridBundle
Zend Framework module to leverage the symfony dependency injection container
统计信息
- 总下载量: 141
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 21
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2015-07-13