PHPackages                             saeven/zf3-purifier - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Search &amp; Filtering](/categories/search)
4. /
5. saeven/zf3-purifier

ActiveLibrary[Search &amp; Filtering](/categories/search)

saeven/zf3-purifier
===================

HTML Purifier integration module for Zend Framework 3

1.1.0(4y ago)038.6k—5.7%[1 PRs](https://github.com/Saeven/Soflomo-Purifier/pulls)BSD-3-ClausePHPPHP &gt;=7.4

Since Apr 23Pushed 4y ago1 watchersCompare

[ Source](https://github.com/Saeven/Soflomo-Purifier)[ Packagist](https://packagist.org/packages/saeven/zf3-purifier)[ Docs](https://github.com/juriansluiman/Soflomo-Purifier)[ RSS](/packages/saeven-zf3-purifier/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (2)Dependencies (14)Versions (12)Used By (0)

Soflomo\\Purifier
=================

[](#soflomopurifier)

[![Mantainer](https://camo.githubusercontent.com/be09e2ada99809a70e99af070dbcf4bb837a2467a7b25ff6018990f70403081f/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6d616e7461696e65722d53746566616e6f253230546f72726573692d677265656e2e737667)](https://github.com/stefanotorresi)[![Build Status](https://camo.githubusercontent.com/d2d457470f586bdf1bfa8c389b4d0e16512d8949ffb31b758dd877ded978dc87/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f6a757269616e736c75696d616e2f536f666c6f6d6f2d50757269666965722f6d61737465722e737667)](https://travis-ci.org/juriansluiman/Soflomo-Purifier)[![Latest Stable Version](https://camo.githubusercontent.com/fcf2385279bff92a660a09eab424351b34e282fa732e6c49bfe57184695e6d64/68747470733a2f2f706f7365722e707567782e6f72672f736f666c6f6d6f2f70757269666965722f762f737461626c65)](https://packagist.org/packages/soflomo/purifier)[![License](https://camo.githubusercontent.com/25b4b5c60827f2bb0ba9fab0d49ee7dc16c55458d19074ef0bcba78fe9adc952/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f736f666c6f6d6f2f70757269666965722e737667)](LICENSE)[![Total Downloads](https://camo.githubusercontent.com/ab362ab8135ad41b2e423a45b04f13883a4c3a5710ee474d8c7059fd1b184f95/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f736f666c6f6d6f2f70757269666965722e737667)](https://packagist.org/packages/soflomo/purifier)

Soflomo\\Purifier is the [HTMLPurifier](http://htmlpurifier.org/) integration for Zend Framework 2.

It provides a `Laminas\Filter\FilterInterface` implementation so you can use HTMLPurifier within your `Laminas\InputFilter` classes. Furthermore, a view helper is provided to help purifying html on the fly in view scripts.

Installation
------------

[](#installation)

You can install `Soflomo\Purifier` through Composer

```
$: composer require soflomo/purifier
```

To enable the module in your ZF2 application, add an entry `Soflomo\Purifier` to the list of enabled modules in `config/application.config.php`.

Usage
-----

[](#usage)

In your input filter configuration, use the `htmlpurifier` as `name` in your filter spec.

An example `Form`:

```
class MyForm extends Laminas\Form\Form implements Laminas\InputFilter\InputFilterProviderInterface
{
    public function init()
    {
        $this->add([
            'name'    => 'text',
            'options' => [
                'label' => 'Text'
            ],
            'attributes' => [
                'type' => 'textarea',
            ],
        ]);
    }

    public function getInputFilterSpecification()
    {
        return [
            'text'  => [
                'required' => true,
                'filters'  => [
                    [ 'name' => 'stringtrim' ],
                    [ 'name' => 'htmlpurifier' ],
                ],
            ],
        ];
    }
}
```

or an `InputFilter`:

```
class MyInputFilter extends Laminas\InputFilter\InputFilter
{
    public function init()
    {
        $this->add([
            'name'     => 'text',
            'required' => true,
            'filters'  => [
                [ 'name' => 'stringtrim' ],
                [ 'name' => 'htmlpurifier' ],
            ],
        ]);
    }
}
```

Alternatively, you can use the FQCN `Soflomo\Purifier\PurifierFilter` in place of the `htmlpurifier` alias.

If you're pulling the consumers from their respective plugin managers, this should work out-of-the-box. If not, please read [how to inject the filter plugin manager](#injecting-the-filtermanager).

If for some reason you want to use the filter in your view templates, you also have a view helper available. Please be aware that 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`:

```

```

And there is a shorthand available too:

```

```

### Configuring HTMLPurifier

[](#configuring-htmlpurifier)

HTMLPurifier use the class `HTMLPurifier_Config` to configure its rules. Most configuration rules are based on a key/value pair: `$config->set('HTML.Doctype', 'HTML 4.01 Transitional')`.

#### Global configuration

[](#global-configuration)

The `HTMLPurifier_Config` key/value storege is exposed by **Soflomo\\Purifier** as an associative array into the ZF2 configuration, so you can customize the default `HTMLPurifier_Config` instance like this:

```
return [
    'soflomo_purifier' => [
        'config' => [
            'HTML.Doctype' => 'HTML 4.01 Transitional'
        ],
    ],
];
```

The configuration factory also handles a `definitions` sub array to add custom definitions to the purifier, as [documented here](http://htmlpurifier.org/docs/enduser-customize.html).

For example:

```
return [
    'soflomo_purifier' => [
        'config' => [
            'HTML.DefinitionID' => 'my custom definitions',
        ],
        'definitions' => [
            'HTML' => [
                'addAttribute' => [
                    [ 'a', 'target', 'Enum#_blank,_self,_target,_top' ]
                ],
            ],
        ],
    ],
];
```

This will add a `HTMLPurifier_AttrDef_Enum` definition for the `target` attribute of the `a` element. Note that an arbitrary value for the `HTML.DefinitionID` config key is required to correctly load the definition.

Definitions can also be set under the `definitions` key in the `config` array. These will override the key in the `soflomo_purifier` array.

#### Per-instance configuration

[](#per-instance-configuration)

You can also set a different configuration each time you add the filter with a spec using the usual `options` key:

```
class MyInputFilter extends Laminas\InputFilter\InputFilter
{
    public function init()
    {
        $this->add([
            'name'     => 'text',
            'required' => true,
            'filters'  => [
                [ 'name' => 'stringtrim' ],
                [
                    'name' => 'htmlpurifier',
                    'options' => [
                        'purifier_config' => [
                            'HTML.AllowedElements' => 'a, span'
                        ],
                    ],
                ],
            ],
        ]);
    }
}
```

### Injecting the FilterManager

[](#injecting-the-filtermanager)

If you instantiate your forms or your input filters manually with the `new` keyword, rather than pulling them from their respective plugin managers (i.e. `FormElementManager` and `InputFilterManager`), the `FilterManager` is not injected automatically into their factories, and these will resort to use a default one.

As such, you get a `ServiceNotFoundException: Laminas\Filter\FilterPluginManager::get was unable to fetch or create an instance for htmlpurifier`. This means the filter plugin manager was lazily instantiated, and does not know about the `htmlpurifier` plugin.

You can hack your way through this by executing the initializers manually:

```
$form = new MyForm();
$formElementManager = $serviceManager->get('FormElementManager');
$formElementManager->injectFactory($form);

// same goes for input filters
$inputFilter = new MyInputFilter();
$inputFilterManager = $serviceManager->get('InputFilterManager');
$inputFilterManager->populateFactory($inputFilter);
```

It is however strongly advised to pull forms and input filters from their respective plugin managers, and use the `init()` method (which will be invoked after all the factories are injected) when applicable.

### Performance optimization

[](#performance-optimization)

HTMLPurifier is not the fastest library. It uses a large number of classes and files so autoloading can be cumbersome.

Luckily, you can [create a standalone version](http://htmlpurifier.org/live/INSTALL) 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` so make sure you can write in that directory.

**Soflomo\\Purifier** helps you using this standalone version with the configuration option `soflomo_purifier.standalone`.

For example, you could add this in your `config/autoload/local.php`:

```
return [
    'soflomo_purifier' => [
        'standalone' => true,
    ],
];
```

If you want to place the standalone file somewhere else, you can set its path too:

```
return [
    'soflomo_purifier' => [
        'standalone'      => true,
        'standalone_path' => 'path/to/HTMLPurifier.standalone.php',
    ],
];
```

**Note:** The standalone generator script requires HTMLPurifier to be installed either with version `
