-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Require PHP 5.5 or 7.0 and Symfony 3.4 minimum - Refactored extension and enabled autowiring - [BC Break] Removed classes parameters - [BC Break] Removed the form data transformer - added a new text form type extension with a purifier listener to purify submitted data in all text based fields, using opt-in and custom profile thanks to dedicated options - added a new "exercise.html_purifier" tag to make custom purifier implementations available as profile through form options and Twig filter - added a purifiers registry to lazy load purifiers everywhere - added a Twig HTMLPurifierRuntime for better performances - upgraded the LICENSE and README files
- Loading branch information
Showing
23 changed files
with
961 additions
and
416 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
namespace Exercise\HTMLPurifierBundle\DependencyInjection\Compiler; | ||
|
||
use Exercise\HTMLPurifierBundle\HTMLPurifiersRegistryInterface; | ||
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; | ||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
|
||
class HTMLPurifierPass implements CompilerPassInterface | ||
{ | ||
const PURIFIER_TAG = 'exercise.html_purifier'; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function process(ContainerBuilder $container) | ||
{ | ||
if (!$container->hasAlias(HTMLPurifiersRegistryInterface::class)) { | ||
return; | ||
} | ||
|
||
try { | ||
$registry = $container->findDefinition(HTMLPurifiersRegistryInterface::class); | ||
} catch (ServiceNotFoundException $e) { | ||
return; | ||
} | ||
|
||
$purifiers = []; | ||
|
||
foreach ($container->findTaggedServiceIds(self::PURIFIER_TAG) as $id => $tags) { | ||
if (empty($tags[0]['profile'])) { | ||
throw new InvalidConfigurationException(sprintf('Tag "%s" must define a "profile" attribute.', self::PURIFIER_TAG)); | ||
} | ||
|
||
$profile = $tags[0]['profile']; | ||
$purifier = $container->getDefinition($id); | ||
|
||
if (empty($purifier->getArguments())) { | ||
$configId = "exercise_html_purifier.config.$profile"; | ||
$config = $container->hasDefinition($configId) ? $configId : 'exercise_html_purifier.config.default'; | ||
|
||
$purifier->addArgument(new Reference($config)); | ||
} | ||
|
||
$purifiers[$profile] = new Reference($id); | ||
} | ||
|
||
$registry->setArguments([ | ||
ServiceLocatorTagPass::register($container, $purifiers), | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
namespace Exercise\HTMLPurifierBundle\Form\Listener; | ||
|
||
use Exercise\HTMLPurifierBundle\HTMLPurifiersRegistryInterface; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
use Symfony\Component\Form\FormEvent; | ||
use Symfony\Component\Form\FormEvents; | ||
|
||
class HTMLPurifierListener implements EventSubscriberInterface | ||
{ | ||
private $registry; | ||
private $profile; | ||
|
||
/** | ||
* @param HTMLPurifiersRegistryInterface $registry | ||
* @param string $profile | ||
*/ | ||
public function __construct(HTMLPurifiersRegistryInterface $registry, $profile) | ||
{ | ||
$this->registry = $registry; | ||
$this->profile = $profile; | ||
} | ||
|
||
public function purifySubmittedData(FormEvent $event) | ||
{ | ||
if (!is_scalar($data = $event->getData())) { | ||
// Hope there is a view transformer, otherwise an error might happen | ||
return; // because we don't want to handle it here | ||
} | ||
|
||
if (0 === strlen($submittedData = trim($data))) { | ||
return; | ||
} | ||
|
||
$event->setData($this->getPurifier()->purify($submittedData)); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function getSubscribedEvents() | ||
{ | ||
return [ | ||
FormEvents::PRE_SUBMIT => ['purifySubmittedData', /* as soon as possible */ 1000000], | ||
]; | ||
} | ||
|
||
/** | ||
* @return \HTMLPurifier | ||
*/ | ||
private function getPurifier() | ||
{ | ||
return $this->registry->get($this->profile); | ||
} | ||
} |
Oops, something went wrong.