Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,7 @@ public function getFilterConfig()
{
return array(
'factories' => array(
'htmlpurifier' => function($sl) {
$purifier = $sl->getServiceLocator()->get('HTMLPurifier');
return new Filter\Purifier($purifier);
},
'htmlpurifier' => 'Soflomo\Purifier\Factory\PurifierFilterFactory'
),
);
}
Expand Down
4 changes: 4 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
"zendframework/zendframework": "2.*",
"ezyang/htmlpurifier": ">=4.5.0"
},
"require-dev":
{
"phpunit/phpunit": "~3.7"
},
"autoload": {
"psr-0": {
"Soflomo\\Purifier": "src/"
Expand Down
3 changes: 1 addition & 2 deletions src/Soflomo/Purifier/Factory/HtmlPurifierFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ public function createService(ServiceLocatorInterface $sl)
$config->set($key, $value);
}

$purifier = new HTMLPurifier($config);
return $purifier;
return new HTMLPurifier($config);
}
}
41 changes: 41 additions & 0 deletions src/Soflomo/Purifier/Factory/PurifierFilterFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
namespace Soflomo\Purifier\Factory;

use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Soflomo\Purifier\Filter\Purifier;

class PurifierFilterFactory implements FactoryInterface
{
/**
* array of options for htmlpurify
*
* @var array
*/
protected $options;

/**
* @param mixed $options
*/
public function __construct($options = null)
{
$this->options = $options;
}

/**
* (non-PHPdoc)
* @see \Zend\ServiceManager\FactoryInterface::createService()
*/
public function createService(ServiceLocatorInterface $serviceLocator)
{
$purifier = $serviceLocator->getServiceLocator()->get('HTMLPurifier');

$filter = new Purifier($purifier);

if ($this->options === null || empty($this->options)) {
return $filter;
} else {
return $filter->setOptions($this->options);
}
}
}
43 changes: 40 additions & 3 deletions src/Soflomo/Purifier/Filter/Purifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,63 @@

use HTMLPurifier;
use Zend\Filter\FilterInterface;
use Zend\Filter\AbstractFilter;

class Purifier implements FilterInterface
class Purifier extends AbstractFilter implements FilterInterface
{
protected $purifier;

/**
* Comma seperated values as string
*
* @var string
*/
protected $allowedElements;

/**
* @param HTMLPurifier $purifier
*/
public function __construct(HTMLPurifier $purifier)
{
$this->purifier = $purifier;
}

/**
* Returns the purifier with config allowed elements added if specified
*
* @return HTMLPurifier
*/
protected function getPurifier()
{
return $this->purifier;
}

/**
* {@inheritdocs}
*/
public function filter($value)
{
return $this->getPurifier()->purify($value);
$purifier = $this->getPurifier();

if ($this->allowedElements !== null) {
$config = \HTMLPurifier_Config::createDefault();

$config = $config->inherit($purifier->config);
$config->set('HTML.AllowedElements', $this->allowedElements);

return $purifier->purify($value, $config);
}

return $purifier->purify($value);
}

/**
* Array of values to be provided to HTMLPurifier_Config Attr.AllowedClasses
*
* @param array $allowedClasses
*/
public function setAllowedElements($allowedElements)
{
$this->allowedElements = $allowedElements;
}
}
54 changes: 54 additions & 0 deletions src/Soflomo/Purifier/Test/Form/PurifierTestForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
namespace Soflomo\Purifier\Test\Form;

use Zend\Form\Element;
use Zend\Form\Form;
use Zend\InputFilter\InputFilterProviderInterface;

class PurifierTestForm extends Form implements InputFilterProviderInterface
{
/**
* @var string
*/
protected $name;

public function __construct()
{
$this->name = 'test';
parent::__construct($this->name);
}

/**
* Provide default input rules for this element
*
* Attaches strip tags filter
*
* @return array
*/
public function getInputFilterSpecification()
{
return [
'test' => [
'required' => false,
'filters' => [
array('name' => 'htmlpurifier',
'options' => [
'allowed_elements' => 'p'
]
),
],
]
];
}



public function init()
{
$this->add(array(
'name' => 'test',
'type' => 'Textarea',

));
}
}
20 changes: 20 additions & 0 deletions src/Soflomo/Purifier/View/Helper/Purifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,33 @@

class Purifier extends AbstractHelper
{
/**
* @var HTMLPurifier
*/
protected $purifier;

/**
* @param HTMLPurifier $purifier
*/
public function __construct(HTMLPurifier $purifier)
{
$this->purifier = $purifier;
}

/**
* Getter for purifier
*
* @return HTMLPurifier
*/
protected function getPurifier()
{
return $this->purifier;
}

/**
* @param string $html
* @return \Soflomo\Purifier\View\Helper\Purifier|Ambigous <string, boolean>
*/
public function __invoke($html = null)
{
if (null === $html) {
Expand All @@ -66,6 +81,11 @@ public function __invoke($html = null)
return $this->purify($html);
}

/**
* Purifies the html string
*
* @param string $html
*/
public function purify($html)
{
return $this->getPurifier()->purify($html);
Expand Down
108 changes: 108 additions & 0 deletions test/Bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php
namespace PurifierTest;

putenv('APPLICATION_ENV=test');

use Zend\Loader\AutoloaderFactory;
use Zend\Mvc\Service\ServiceManagerConfig;
use Zend\ServiceManager\ServiceManager;
use Zend\Stdlib\ArrayUtils;
use RuntimeException;

error_reporting(-1);
chdir(__DIR__.'/../');

class Bootstrap
{
protected static $serviceManager;
protected static $config;
protected static $bootstrap;

//Zend_Test_PHPUnit_DatabaseTestCase

public static function init()
{
// Load the user-defined test configuration file, if it exists; otherwise, load
if (is_readable(__DIR__ . '/config/TestConfig.php')) {
$testConfig = include __DIR__ . '/config/TestConfig.php';
} else {
$testConfig = include __DIR__ . '/config/TestConfig.php.dist';
}

$zf2ModulePaths = array();

if (isset($testConfig['module_listener_options']['module_paths'])) {
$modulePaths = $testConfig['module_listener_options']['module_paths'];
foreach ($modulePaths as $modulePath) {
if (($path = static::findParentPath($modulePath)) ) {
$zf2ModulePaths[] = $path;
}
}
}

$zf2ModulePaths = implode(PATH_SEPARATOR, $zf2ModulePaths) . PATH_SEPARATOR;
$zf2ModulePaths .= getenv('ZF2_MODULES_TEST_PATHS') ?: (defined('ZF2_MODULES_TEST_PATHS') ? ZF2_MODULES_TEST_PATHS : '');

static::initAutoloader();

// use ModuleManager to load this module and it's dependencies
$baseConfig = array(
'module_listener_options' => array(
'module_paths' => explode(PATH_SEPARATOR, $zf2ModulePaths),
),
);

$config = ArrayUtils::merge($baseConfig, $testConfig);
//exit(var_dump($config));
$serviceManager = new ServiceManager(new ServiceManagerConfig());
$serviceManager->setService('ApplicationConfig', $config);
$serviceManager->get('ModuleManager')->loadModules();

static::$serviceManager = $serviceManager;
static::$config = $config;
}

public static function getServiceManager()
{
return static::$serviceManager;
}

public static function getConfig()
{
return static::$config;
}

protected static function initAutoloader()
{
$vendorPath = static::findParentPath('vendor');

if (is_readable($vendorPath . '/autoload.php')) {
$loader = include $vendorPath . '/autoload.php';
} else {
$zf2Path = getenv('ZF2_PATH') ?: (defined('ZF2_PATH') ? ZF2_PATH : (is_dir($vendorPath . '/ZF2/library') ? $vendorPath . '/ZF2/library' : false));

if (!$zf2Path) {
throw new RuntimeException('Unable to load ZF2. Run `php composer.phar install` or define a ZF2_PATH environment variable.');
}

include $zf2Path . '/Zend/Loader/AutoloaderFactory.php';

}

}

protected static function findParentPath($path)
{
$dir = __DIR__;
$previousDir = '.';
while (!is_dir($dir . '/' . $path)) {
$dir = dirname($dir);
if ($previousDir === $dir) return false;
$previousDir = $dir;
}
return $dir . '/' . $path;
}

}

Bootstrap::init();
Loading