lab1521/neaty-html
Composer 安装命令:
composer require lab1521/neaty-html
包简介
Cleans up HTML markup with Tidy
README 文档
README
Cleans up your HTML. Useful for writing content or blogs that accepts HTML markup. Requires the PHP tidy class. Please install this extension first.
Accepting HTML contents (with markups/tags included etc.) on your website is somewhat a concern regarding the security of your website and its users. The dreaded XSS or Cross Site Scripting is always a threat. However with the power of open source we can keep up with common threats and mitigate the issues for safekeeping our websites or blogs.
Installation
composer require lab1521/neaty-html
How to use
<?php require('../vendor/autoload.php'); use Lab1521\NeatyHTML\NeatyHTML; //Goal: Remove onerror attribute which prevents eval to alert $badImage = '<img src=x:alert(window) onerror=eval(src) alt="bad image">'; $goodImage = '<img src="images/good.gif" alt="good image">'; $neaty = new NeatyHTML($badImage . $goodImage); //Outputs <img src="x:alert(window)" alt="bad image"><img src="images/good.gif" alt="good image"> echo $neaty->tidyUp(); //Goal: Remove unrecognized images and keep local sources only $neaty->blockedTags(['img']); $neaty->tagOverrides([ 'img' => [ [ 'attribute' => 'src', 'values' => ['images/'] //restricts to local folder ], ] ]); //Goal: Remove $badImage $neaty->loadHtml($badImage . $goodImage); //Outputs $goodImage only echo $neaty->tidyUp();
Limitations
Current PHP's DomDocument class does not support HTML5 tags/attributes. For this reason a NeatyDOMException exception is thrown.
Laravel Specific Usage
Service provider
For your Laravel app, open config/app.php and, within the providers array, append:
/* * Package Service Providers... */ Lab1521\NeatyHTML\NeatyHTMLServiceProvider::class,
Using Facades
Add a new item in the 'aliases' array on the same file, config/app.php
'NeatyHTML' => Lab1521\NeatyHTML\Facades\NeatyHTML::class,
Example Usage
<?php Route::get('/', function () { //Goal: Remove onerror attribute which prevents eval to alert $badImage = '<img src=x:alert(window) onerror=eval(src) alt="bad image">'; $goodImage = '<img src="images/good.gif" alt="good image">'; $neaty = NeatyHTML::loadHtml($badImage . $goodImage); //Goal: Remove unrecognized images and keep local sources only $neaty->blockedTags(['img']); $neaty->tagOverrides([ 'img' => [ [ 'attribute' => 'src', 'values' => ['images/'] //restricts to local folder ], ] ]); //Outputs $goodImage only return $neaty->tidyUp(); // return view('welcome'); });
Custom Validation Rule
NeatyHTML provides a custom validation rule called 'html' which will check your POST input containing markups and passes the error message when validation fails. Please note that NeatyHTMLServiceProvider is deferred by default so we need to inject this class in the controller method in order to use the custom validation feature. In this way we only use NeatyHTML when we wanted which is nice to have.
<?php use Lab1521\NeatyHTML\NeatyHTML; class PostController extends Controller { public function store(Request $request, NeatyHTML $neaty) { $this->validate($request, [ 'title' => 'required', 'body' => 'required|html' // <-- custom rule ]); // Your awesome code here ... $body = $neaty->tidyUp($request['body']); } }
lab1521/neaty-html 适用场景与选型建议
lab1521/neaty-html 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 7.15k 次下载、GitHub Stars 达 23, 最近一次更新时间为 2016 年 07 月 31 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「html」 「clean」 「tidy」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 lab1521/neaty-html 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 lab1521/neaty-html 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 lab1521/neaty-html 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
CSSTidy is a CSS minifier
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
PHP-Tidy wrapper Bundle for Symfony 2 Projects
Simplify markup of DOCX XML by tidying successive elements w/ redundant properties / types.
HTML5 support for HTML Purifier
HTML Tidy middleware for Laravel
统计信息
- 总下载量: 7.15k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 24
- 点击次数: 8
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2016-07-31