chromabits/purifier

Laravel 5 的 HTMLPurifier 包

安装: 24 183

依赖者: 0

建议者: 0

安全: 0

星星: 13

关注者: 5

分支: 230

2.1.1 2015-02-24 20:24 UTC

This package is not auto-updated.

Last update: 2024-09-17 10:04:06 UTC


README

Laravel 5 的 HTMLPurifier 服务

安装

注意:此包仅适用于 Laravel 5。它不包含 Facade,并且仅需要 Laravel 5 中可用的某些“Contracts”接口。

您可以通过在项目的 composer.json 中要求 chromabits/purifier 包来使用 Composer 安装此包。

{
    "require": {
        "laravel/framework": "~5.0",
        "chromabits/purifier": "~2.1"
    }
}

使用 composer update 更新您的包或使用 composer install 安装。

使用方法

要使用 HTMLPurifier 服务,您必须在启动 Laravel 应用程序时注册服务提供者。

config/app.php 中找到 providers 键并注册 HTMLPurifier 服务提供者

    'providers' => [
        // ...
        'Chromabits\Purifier\PurifierServiceProvider',
    ]

注册提供者后,需要 Chromabits\Purifier\Contracts\Purifier 合约的类将通过依赖注入获得净化器服务实例(以下提供示例)。

配置

要使用自己的设置,请将此包中的 config/purifier.php 文件复制到您的应用程序的 config 目录中,并根据需要进行修改。

您可以通过在 settings 数组键中指定新条目来定义多个配置集。

return [
    "settings" => [
        "default" => [
            "HTML.SafeIframe" => 'true',
            "URI.SafeIframeRegexp" => "%^(http://|https://|//)(www.youtube.com/embed/|player.vimeo.com/video/)%",
        ],
        "titles" => [
            'AutoFormat.AutoParagraph' => false,
            'AutoFormat.Linkify' => false,
        ]
    ],
];

服务将使用 default 键作为默认配置集,任何其他配置都将扩展此配置。如果没有提供配置文件,服务将使用安全默认值。

示例

在 Laravel 5 控制器中使用默认设置的完整使用示例

<?php

namespace Http\Controllers;

use Chromabits\Purifier\Contracts\Purifier;
use HTMLPurifier_Config;
use Illuminate\Http\Request;

/**
 * Class IndexController
 *
 * @package App\Http\Controllers;
 */
class IndexController {
	/**
	 * @var Purifier
	 */
	protected $purifier;
	
	/**
	 * Construct an instance of MyClass
	 *
	 * @param Purifier $purifier
	 */
	public function __construct(Purifier $purifier) {
		// Inject dependencies
		$this->purifier = $purifier;
	}
	
	/**
	 * Get index page
	 *
	 * @param Request $request
	 */
	public function getIndex(Request $request)
	{
		return $this->purifier->clean($request->input('first_name'));
	}
}

使用动态配置

	// Using config entries from purifier.php
	$this->purifier->clean('This is my H1 title', 'titles');
	
	// Passing configuration from an array (inherits default config)
	$this->purifier->clean(
		'This is my H1 title',
		[ 'Attr.EnableID' => true ]
	);

使用自定义服务提供者和闭包直接与 HTMLPurifier_Config 对象交互

<?php

namespace App\Providers;

use Chromabits\Purifier\Purifier;
use Illuminate\Support\ServiceProvider;

/**
 * ...
 */
class CustomPurifierServiceProvider extends ServiceProvider
{
    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register()
    {
        // Bind contract with concrete implementation
        $this->app->bind(
        	'Chromabits\Purifier\Contracts\Purifier',
        	function ($app) {
          		new Purifier($app, $app['config'], function (HTMLPurifier_Config $config) {
                		// Do stuff with $config here
                		return $config;
                }
            }
        );
    }
}

许可证

基于 Laravel 4 Purifier 服务

有关许可证信息,请参阅 LICENSE.md