Skip to content

Instantly share code, notes, and snippets.

@bwaidelich
Created July 20, 2015 14:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bwaidelich/8933a3ac65451e8b897d to your computer and use it in GitHub Desktop.
Save bwaidelich/8933a3ac65451e8b897d to your computer and use it in GitHub Desktop.
Fluid ViewHelper integrating HTMLPurifier to sanitize/tidy HTML output
{
"name": "your/package",
"type": "typo3-flow-package",
"description": "<some description>",
"require": {
"typo3/flow": "~2.3",
"ezyang/htmlpurifier": "~4.6"
},
"autoload": {
"psr-0": {
"Your\\Package": "Classes"
}
}
}
<?php
namespace Your\Package\ViewHelpers\Format;
use TYPO3\Flow\Annotations as Flow;
use TYPO3\Fluid\Core\ViewHelper\AbstractViewHelper;
/**
* ViewHelper rendering the given HTML string through HTMLPurifier
*/
class PurifyViewHelper extends AbstractViewHelper {
/**
* @var boolean
*/
protected $escapeChildren = FALSE;
/**
* @var boolean
*/
protected $escapeOutput = FALSE;
/**
* @param string $value The HTML string to purify. If NULL the child nodes will be used as value
* @return string The purified HTML string
*/
public function render($value = NULL) {
if ($value === NULL) {
$value = $this->renderChildren();
}
$purifierConfiguration = \HTMLPurifier_Config::createDefault();
// TODO adjust purifier configuration (possibly from settings, to make configurable)
$purifier = new \HTMLPurifier($purifierConfiguration);
return $purifier->purify($value);
}
}
{someHtml -> your.package:format.purify()}
@bwaidelich
Copy link
Author

NOTE: The Flow ClassLoader seems to have issues with certain 3rd party packages. If you get an error execute composer install -o to optimize the composer autoload files

@bwaidelich
Copy link
Author

NOTE2: If you use Flow 2.x you'll have to manually exclude HTMLPurifier classes from being proxied:

TYPO3:
  Flow:
    object:
      excludeClasses:
        'ezyang.htmlpurifier' : ['.*']

With Flow 3.0+ this is the default for non-flow packages

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment