Skip to content

Instantly share code, notes, and snippets.

@tomshaw
Created April 25, 2013 03:19
Show Gist options
  • Save tomshaw/5457269 to your computer and use it in GitHub Desktop.
Save tomshaw/5457269 to your computer and use it in GitHub Desktop.
ZF2 HTMLPurifier Controller Plugin.
<?php
namespace HTMLPurifier\Controller\Plugin;
use Zend\Mvc\Controller\Plugin\AbstractPlugin;
class Purifier extends AbstractPlugin
{
public function __invoke($data)
{
$purifier = $this->getController()->getServiceLocator()->get('HTMLPurifier');
if (is_string($data)) {
$data = $purifier->purify($data);
} else if (is_array($data)) {
array_walk_recursive($data, function(&$value, $key) use ($purifier) {
$value = $purifier->purify($value);
});
}
return $data;
}
}
// Simple usage scenario.
if ($this->request->isPost()) {
$post = $this->purify($request->getPost());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment