|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +namespace ElementareTeilchen\HtmlPurify\ViewHelpers; |
| 5 | + |
| 6 | +/** |
| 7 | + * This file is part of the "html_purify" Extension for TYPO3 CMS. |
| 8 | + * |
| 9 | + * For the full copyright and license information, please read the |
| 10 | + * LICENSE.txt file that was distributed with this source code. |
| 11 | + */ |
| 12 | + |
| 13 | +use ElementareTeilchen\HtmlPurify\Service\PurifyService; |
| 14 | +use TYPO3\CMS\Core\Utility\GeneralUtility; |
| 15 | +use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface; |
| 16 | +use TYPO3Fluid\Fluid\Core\Rendering\RenderingContextInterface; |
| 17 | +use TYPO3Fluid\Fluid\Core\ViewHelper\AbstractViewHelper; |
| 18 | +use TYPO3Fluid\Fluid\Core\ViewHelper\Traits\CompileWithContentArgumentAndRenderStatic; |
| 19 | + |
| 20 | +/** |
| 21 | + * Purifies HTML content by keeping only allowed HTML tags. |
| 22 | + * |
| 23 | + * Examples |
| 24 | + * ======== |
| 25 | + * |
| 26 | + * Default |
| 27 | + * ------- |
| 28 | + * |
| 29 | + * :: |
| 30 | + * |
| 31 | + * <hp:purify allowedHtmlTags="strong,em">{someVariableWithUnwantedHTMLTagsOrPotentialXSS -> f:format.raw()}</hp:purify> |
| 32 | + * |
| 33 | + * Output in frontend:: |
| 34 | + * |
| 35 | + * String without html tags beside strong or em |
| 36 | + * |
| 37 | + * |
| 38 | + * Inline notation |
| 39 | + * --------------- |
| 40 | + * |
| 41 | + * :: |
| 42 | + * |
| 43 | + * {someVariableWithUnwantedHTMLTagsOrPotentialXSS -> f:format.raw() -> hp:purify(allowedHtmlTags: 'strong,em')} |
| 44 | + * |
| 45 | + * Output:: |
| 46 | + * |
| 47 | + * String without html tags beside strong or em |
| 48 | + * |
| 49 | + * |
| 50 | + * Inline notation without variable |
| 51 | + * ------------------ |
| 52 | + * |
| 53 | + * :: |
| 54 | + * |
| 55 | + * {hp:purify(allowedHtmlTags: 'p,strong', htmlContent: '<p>Text with <strong>HTML</strong> but <em>EM tag will be removed</em></p>')} |
| 56 | + * |
| 57 | + * Output:: |
| 58 | + * |
| 59 | + * String without html tags beside p and strong |
| 60 | + * <p>Text with <strong>HTML</strong> but EM tag will be removed</p> |
| 61 | + */ |
| 62 | +class PurifyViewHelper extends AbstractViewHelper |
| 63 | +{ |
| 64 | + use CompileWithContentArgumentAndRenderStatic; |
| 65 | + |
| 66 | + protected $escapeOutput = false; |
| 67 | + |
| 68 | + public function initializeArguments() |
| 69 | + { |
| 70 | + $this->registerArgument('htmlContent', 'string', 'The html content to purify'); |
| 71 | + $this->registerArgument('allowedHtmlTags', 'string', 'The allowed HTML tags'); |
| 72 | + } |
| 73 | + |
| 74 | + public static function renderStatic( |
| 75 | + array $arguments, |
| 76 | + \Closure $renderChildrenClosure, |
| 77 | + RenderingContextInterface $renderingContext |
| 78 | + ) { |
| 79 | + $htmlContent = $renderChildrenClosure(); |
| 80 | + |
| 81 | + // if there is no string content, we do not proceed |
| 82 | + if (!is_string($htmlContent) || \trim($htmlContent) === '') { |
| 83 | + return ''; |
| 84 | + } |
| 85 | + |
| 86 | + // allowed HTML tags |
| 87 | + $allowedHtmlTags = null; |
| 88 | + // use viewhelper argument if set |
| 89 | + if ($arguments['allowedHtmlTags']) { |
| 90 | + $allowedHtmlTags = $arguments['allowedHtmlTags']; |
| 91 | + |
| 92 | + // use typoscript default values if configured |
| 93 | + } else { |
| 94 | + $configurationManager = GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Configuration\\ConfigurationManager'); |
| 95 | + $typoScript = $configurationManager->getConfiguration( |
| 96 | + ConfigurationManagerInterface::CONFIGURATION_TYPE_SETTINGS, |
| 97 | + 'html_purify' |
| 98 | + ); |
| 99 | + if (isset($typoScript['allowedHtmlTags'])) { |
| 100 | + $allowedHtmlTags = $typoScript['allowedHtmlTags']; |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + // purify and return HTML content |
| 105 | + return PurifyService::purify($htmlContent, $allowedHtmlTags); |
| 106 | + } |
| 107 | +} |
0 commit comments