dilneiss/purify
Composer 安装命令:
composer require dilneiss/purify
包简介
An HTML Purifier / Sanitizer for Laravel
README 文档
README
Purify is a Laravel wrapper around HTMLPurifier by ezyang.
Requirements
- PHP >= 7.1
- Laravel >= 5.5
Installation
To install Purify, run the following in the root of your project:
composer require stevebauman/purify
Then, publish the configuration file using:
php artisan vendor:publish --provider="Stevebauman\Purify\PurifyServiceProvider"
If you are using Lumen, you should copy the config file purify.php by hand, and add this line to your bootstrap/app.php:
$app->register(Stevebauman\Purify\PurifyServiceProvider::class);
Usage
Cleaning a String
To clean a users input, simply use the clean method:
$input = '<script>alert("Harmful Script");</script> <p style="border:1px solid black" class="text-gray-700">Test</p>'; // Returns '<p class="text-gray-700">Test</p>' $cleaned = Purify::clean($input);
Cleaning an Array
Need to purify an array of user input? Just pass in an array:
$array = [ '<script>alert("Harmful Script");</script> <p style="border:1px solid black" class="text-gray-700">Test</p>', '<script>alert("Harmful Script");</script> <p style="border:1px solid black" class="text-gray-700">Test</p>', ]; $cleaned = Purify::clean($array); // array [ // '<p class="text-gray-700">Test</p>', // '<p class="text-gray-700">Test</p>', // ] var_dump($cleaned);
Dynamic Configuration
Need a different configuration for a single input? Pass in a configuration array into the second parameter:
$config = ['HTML.Allowed' => 'div,b,a[href]']; $cleaned = Purify::clean($input, $config);
Note: Configuration passed into the second parameter is not merged with your current configuration.
$config = ['HTML.Allowed' => 'div,b,a[href]']; $cleaned = Purify::clean($input, $config);
Replacing the HTML Purifier instance
Need to replace the HTML Purifier instance with your own? Call the setPurifier() method:
$purifier = new HTMLPurifier(); Purify::setPurifier($purifier);
Practices
If you're looking into sanitization, you're likely wanting to sanitize inputted user HTML content that is then stored in your database to be rendered onto your application.
In this scenario, it's likely best practice to sanitize on the way out instead of the on the way in. Remember, the database doesn't care what text it contains.
This way you can allow anything to be inserted in the database, and have strong sanization rules on the way out.
This helps tremendously if you change your sanization requirements later down the line, then all rendered content will follow these sanization rules.
Configuration
Inside the configuration file, the entire settings array is passed directly to the HTML Purifier configuration, so feel free to customize it however you wish. For the configuration documentation, please visit the HTML Purifier Website:
http://htmlpurifier.org/live/configdoc/plain.html
Custom Configuration Rules
There's multiple ways of creating custom rules on the HTML Purifier instance.
Below is an example service provider you can use as a starting point to add rules to the instance. This provider gives compatibility with Basecamp's Trix WYSIWYG editor:
Credit to Antonio Primera for resolving some HTML Purifier configuration issues with trix.
<?php namespace App\Providers; use HTMLPurifier_HTMLDefinition; use Stevebauman\Purify\Facades\Purify; use Illuminate\Support\ServiceProvider; class PurifySetupProvider extends ServiceProvider { const DEFINITION_ID = 'trix-editor'; const DEFINITION_REV = 1; /** * Bootstrap the application services. * * @return void */ public function boot() { /** @var \HTMLPurifier $purifier */ $purifier = Purify::getPurifier(); /** @var \HTMLPurifier_Config $config */ $config = $purifier->config; $config->set('HTML.DefinitionID', static::DEFINITION_ID); $config->set('HTML.DefinitionRev', static::DEFINITION_REV); if ($def = $config->maybeGetRawHTMLDefinition()) { $this->setupDefinitions($def); } $purifier->config = $config; } /** * Register the application services. * * @return void */ public function register() { // } /** * Adds elements and attributes to the HTML purifier * definition required by the trix editor. * * @param HTMLPurifier_HTMLDefinition $def */ protected function setupDefinitions(HTMLPurifier_HTMLDefinition $def) { $def->addElement('figure', 'Inline', 'Inline', 'Common'); $def->addAttribute('figure', 'class', 'Text'); $def->addElement('figcaption', 'Inline', 'Inline', 'Common'); $def->addAttribute('figcaption', 'class', 'Text'); $def->addAttribute('figcaption', 'data-trix-placeholder', 'Text'); $def->addAttribute('a', 'rel', 'Text'); $def->addAttribute('a', 'tabindex', 'Text'); $def->addAttribute('a', 'contenteditable', 'Enum#true,false'); $def->addAttribute('a', 'data-trix-attachment', 'Text'); $def->addAttribute('a', 'data-trix-content-type', 'Text'); $def->addAttribute('a', 'data-trix-id', 'Number'); $def->addElement('span', 'Block', 'Flow', 'Common'); $def->addAttribute('span', 'data-trix-cursor-target', 'Enum#right,left'); $def->addAttribute('span', 'data-trix-serialize', 'Enum#true,false'); $def->addAttribute('img', 'data-trix-mutable', 'Enum#true,false'); $def->addAttribute('img', 'data-trix-store-key', 'Text'); } }
After this service provider is created, make sure you insert it into your providers array in the config/app.php
file, and update your HTML.Allowed string in the config/purify.php file.
Note: Remember that after this definition is created, and you have ran
Purify::clean(), the definition will be cached, and you will have to clear it from yourstorage/app/purifyfolder if you want to make changes to the definition.Otherwise, you will have to change the definition version number or ID for it to be re-cached.
dilneiss/purify 适用场景与选型建议
dilneiss/purify 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 231 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 02 月 09 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「html」 「laravel」 「clean」 「Purifier」 「purify」 「cleaner」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 dilneiss/purify 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 dilneiss/purify 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 dilneiss/purify 相关的其它包
同方向 / 同关键字的高下载量 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
Laravel console command for deleting temp files and associated model instances.
Removes invalid UTF-8 characters from the given text
Jobilla's general code styling rules for different tools
HTML and form generation
A modern HTML DOM parser for PHP using DOMDocument and Symfony CssSelector.
统计信息
- 总下载量: 231
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 2
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-02-09