Skip to content

Commit ed7bad8

Browse files
Initial commit
0 parents  commit ed7bad8

File tree

10 files changed

+645
-0
lines changed

10 files changed

+645
-0
lines changed

Classes/Service/PurifyService.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace ElementareTeilchen\HtmlPurify\Service;
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+
/**
14+
* Service for purifying HTML content
15+
*/
16+
class PurifyService
17+
{
18+
/**
19+
* @param string $htmlContent HTML content which should be purified by only keeping allowed HTML tags
20+
* @param string|null $allowedHtmlTags Comma separated list of allowed HTML tags
21+
*
22+
* @return string
23+
*/
24+
public static function purify(string $htmlContent, ?string $allowedHtmlTags = null) : string
25+
{
26+
// if there is no content, we do not proceed
27+
if (\trim($htmlContent) === '') {
28+
return $htmlContent;
29+
}
30+
31+
// if there are no allowed HTML tags provided, use default
32+
if (\is_null($allowedHtmlTags) || $allowedHtmlTags === '') {
33+
$allowedHtmlTags = 'h1,h2,h3,p,strong,br,i,a[href],ol,ul,li';
34+
}
35+
36+
// create Purifier config
37+
$config = \HTMLPurifier_Config::createDefault();
38+
$config->loadArray(
39+
[
40+
'HTML' => [
41+
'Allowed' => $allowedHtmlTags,
42+
// we set targets to blank
43+
'TargetBlank' => true,
44+
],
45+
]
46+
);
47+
48+
// create Purifier and assign config to it
49+
$purifier = new \HTMLPurifier($config);
50+
51+
// purify html content and return it
52+
return $purifier->purify($htmlContent);
53+
}
54+
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
defined('TYPO3_MODE') or die();
3+
4+
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addStaticFile('html_purify', 'Configuration/TypoScript', 'HTML purify');
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
plugin.tx_html_purify {
2+
# ====================================
3+
# Settings used by purify viewhelper for default values
4+
# ====================================
5+
settings {
6+
allowedHtmlTags = h1,h2,h3,p,strong,br,em,ol,ul,li
7+
}
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
plugin.tx_html_purify {
2+
# ====================================
3+
# Settings used by purify viewhelper for default values
4+
# ====================================
5+
settings {
6+
allowedHtmlTags = {$plugin.tx_html_purify.settings.allowedHtmlTags}
7+
}
8+
}

0 commit comments

Comments
 (0)