Developed with love by KnpLabs Hire us for your project!
130

HTMLPurifierBundle

by Exercise

HTML Purifier is a standards-compliant HTML filter library written in PHP.

ExerciseHTMLPurifierBundle

This bundle integrates HTMLPurifier into Symfony2.

Installation

Symfony 2.1 and above (using Composer)

Require the bundle in your composer.json file:

{
    "require": {
        "exercise/htmlpurifier-bundle": "*",
    }
}

Install the bundle:

$ composer update exercise/htmlpurifier-bundle

Register the bundle:

// app/AppKernel.php

public function registerBundles()
{
    return array(
        new Exercise\HTMLPurifierBundle\ExerciseHTMLPurifierBundle(),
        // ...
    );
}

Symfony 2.0.*

Submodule Creation

Add HTMLPurifier and this bundle to your vendor/ directory:

$ git submodule add git://github.com/Exercise/HTMLPurifierBundle.git vendor/bundles/Exercise/HTMLPurifierBundle
$ git submodule add git://github.com/ezyang/htmlpurifier.git vendor/htmlpurifier

Class Autoloading

Register "HTMLPurifier" and the "Exercise" namespace prefix in your project's
autoload.php:

# app/autoload.php

$loader->registerNamespaces(array(
    'Exercise' => __DIR__ . '/../vendor/bundles',
));

$loader->registerPrefixes(array(
    'HTMLPurifier' => __DIR__ . '/../vendor//htmlpurifier/library',
));

Application Kernel

Add HTMLPurifierBundle to the registerBundles() method of your application
kernel:

# app/AppKernel.php

public function registerBundles()
{
    return array(
        // ...
        new Exercise\HTMLPurifierBundle\ExerciseHTMLPurifierBundle(),
        // ...
    );
}

Configuration

If you do not explicitly configure this bundle, an HTMLPurifier service will be
defined as exercise_html_purifier.default. This behavior is the same as if you
had specified the following configuration:

# app/config.yml

exercise_html_purifier:
    default:
        Cache.SerializerPath: '%kernel.cache_dir%/htmlpurifier'

The default profile is special in that it is used as the configuration for the
exercise_html_purifier.default service as well as the base configuration for
other profiles you might define.

# app/config.yml

exercise_html_purifier:
    default:
        Cache.SerializerPath: '%kernel.cache_dir%/htmlpurifier'
    custom:
        Core.Encoding: 'ISO-8859-1'

In this example, a exercise_html_purifier.custom service will also be defined,
which includes both the cache and encoding options. Available configuration
options may be found in HTMLPurifier's configuration documentation.

Note: If you define a default profile but omit Cache.SerializerPath, it
will still default to the path above. You can specify a value of null for the
option to suppress the default path.

Cache Warming

When a path is supplied for HTMLPurifier's Cache.SerializerPath configuration
option, an error is raised if the directory is not writable. This bundle defines
a cache warmer service that will collect all Cache.SerializerPath options and
ensure those directories exist and are writeable.

Form Data Transformer

This bundles provides a data transformer class for filtering form fields with
HTMLPurifier. Purification is done during the reverseTransform() method, which
means that client data will be filtered during binding to the form.

The following example demonstrates one possible way to integrate an HTMLPurifier
transformer into a form by way of a custom field type:

<?php

namespace Acme\MainBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class PurifiedTextareaType extends AbstractType
{
    private $purifierTransformer;

    public function __construct(DataTransformerInterface $purifierTransformer)
    {
        $this->purifierTransformer = $purifierTransformer;
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->addViewTransformer($this->purifierTransformer);
    }

    public function getParent()
    {
        return 'textarea';
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'compound' => false,
        ));
    }

    public function getName()
    {
        return 'purified_textarea';
    }
}

Then define both the field type and transformer in the service container:

<services>
    <service id="acme.form.type.purified_textarea" class="Acme\MainBundle\Form\Type\PurifiedTextareaType">
        <argument type="service" id="acme.form.transformer.html_purifier" />
        <tag name="form.type" alias="purified_textarea" />
    </service>

    <service id="acme.form.transformer.html_purifier" class="Exercise\HTMLPurifierBundle\Form\HTMLPurifierTransformer">
        <argument type="service" id="exercise_html_purifier.default" />
    </service>
</services>

Additional documentation on data transformers may be found in the
Symfony2 documentation.

Twig Filter

This bundles registers a purify filter with Twig. Output from this filter is
marked safe for HTML, much like Twig's built-in escapers. The filter may be used
as follows:

{# Filters text's value through the "default" HTMLPurifier service #}
{{ text|purify }}

{# Filters text's value through the "custom" HTMLPurifier service #}
{{ text|purify('custom') }}
exercise_html_purifier:

# Prototype
name: []
  • Merge pull request #18 from shieldo/use_composer_for_tests
    By cystbear, 3 years ago
  • Merge pull request #19 from rjmunro/feature/improve-readme
    By AlexKovalevych, 3 years ago
  • Use more definite language to explain steps
    By rjmunro, 3 years ago
  • Add up to date installation instructions
    By rjmunro, 3 years ago
  • add symfony/twig to composer.json, and make test suite boot using composer autoload
    By shieldo, 3 years ago
  • Merge pull request #15 from vjnrv/patch-1
    By cystbear, 3 years ago
  • Merge pull request #16 from stof/patch-1
    By cystbear, 3 years ago
  • Added the branch alias for master and fixed requirements
    By stof, 3 years ago
  • Remove deprecated method from field type example
    By vjnrv, 4 years ago
  • Merge pull request #14 from khepin/patch-1
    By jmikola, 4 years ago
  • Update composer.json
    By khepin, 4 years ago
  • Merge pull request #12 from vjnrv/2.1
    By cystbear, 4 years ago
  • Update example of purified_textarea to be compatible with Symfony 2.1
    By vjnrv, 4 years ago
  • Merge pull request #10 from MDrollette/patch-1
    By jseverson, 5 years ago
  • change htmlpurifier version requirement in composer to one that works.
    By MDrollette, 5 years ago
  • Merge pull request #9 from IamPersistent/master
    By jmikola, 5 years ago
  • take hyphen out of name to better match the composer name of HTMLPurifier
    By IamPersistent, 5 years ago
  • fixed htmlpurifier version to less than sign
    By IamPersistent, 5 years ago
  • add composer.json
    By IamPersistent, 5 years ago
  • Merge pull request #8 from vytautasgimbutas/master
    By cystbear, 5 years ago
  • several other fixes in readme.md
    By vytautasgimbutas, 5 years ago
  • Fix in README.md
    By vytautasgimbutas, 5 years ago
  • Merge pull request #7 from arnaud-lb/resolve-services
    By jmikola, 5 years ago
  • test that services are resolved
    By arnaud-lb, 5 years ago
  • resolve services in config
    By arnaud-lb, 5 years ago
  • Add a class to HTMLPurifier_Config definitions
    By jmikola, 5 years ago
  • Implement a "purify" filter for Twig
    By jmikola, 5 years ago
  • Clean up use statements and doc blocks
    By jmikola, 5 years ago
  • Mock HTMLPurifier in data transformer test
    By jmikola, 5 years ago
  • Handle dynamic paths in SerializerCacheWarmer
    By jmikola, 5 years ago