HtmlPurifier
- Full name:
\Qubus\Security\HtmlPurifier
- This class implements:
\Qubus\Security\Purifier
Properties
neverAllowedStr
neverAllowedRegex
List of never allowed regex replacement
xssDisalowedAttibutes
Remove bad attributes such as style, onclick and xmlns
xssNaughtyHtml
If a tag containing any of the words in the list below is found, the tag gets converted to entities.
xssNaughtyScripts
Similar to $this->xssNaughtyHtml, but instead of looking for tags it looks for PHP and JavaScript commands that are disallowed. Rather than removing the code, it simply converts the parenthesis to entities rendering the code un-executable.
filenameBadChars
List of sanitize filename strings.
mbencoding
Your mb_string encoding, default is 'utf-8'. Do not change, if not sure.
Methods
__construct
purify
Escaping for rich text.
This method should only be used on output. With the exception of uploading images, never use this method on input. All inputted data should be accepted and then purified on output for optimal results. For output of images, make sure to escape with esc_url().
Parameters:
Parameter | Type | Description |
---|---|---|
$string | string|string[] | The string to purify. |
$isImage | bool | Is the string an image? |
Return Value:
Escaped rich text.
entityDecode
HTML Entities Decode
This function is a replacement for html_entity_decode()
The reason we are not using html_entity_decode() by itself is because while it is not technically correct to leave out the semicolon at the end of an entity most browsers will still interpret the entity correctly. html_entity_decode() does not convert entities without semicolons, so we are left with our own little solution here. Bummer.
Parameters:
Parameter | Type | Description |
---|---|---|
$string | string | |
$charset | string |
Return Value:
The decoded string.
urlDecodeSpaces
URL-decode taking spaces into account
Parameters:
Parameter | Type | Description |
---|---|---|
$matches | array |
compactExplodedWords
Compact Exploded Words
Callback function for $this->purify() to remove whitespace from things like j a v a s c r i p t.
Parameters:
Parameter | Type | Description |
---|---|---|
$matches | array |
removeEvilAttributes
Remove evil HTML Attributes (like evenhandlers and style)
It removes the evil attribute and either:
-
Everything up until a space For example, everything between the pipes:
-
Everything inside the quotes For example, everything between the pipes:
Parameters:
Parameter | Type | Description |
---|---|---|
$string | string | The string to check |
$isImage | bool | true if this is an image |
Return Value:
The string with the evil attributes removed
sanitizeNaughtyHtml
Sanitize Naughty HTML
Callback function for $this->purify() to sanitize naughty HTML elements.
Parameters:
Parameter | Type | Description |
---|---|---|
$matches | array |
jsLinkRemoval
JS Link Removal
Callback function for $this->purify() to sanitize links. This limits the PCRE backtracks, making it more performant friendly.
Parameters:
Parameter | Type | Description |
---|---|---|
$match | array |
jsImgRemoval
JS Image Removal
Callback function for $this->purify() to sanitize image tags. This limits the PCRE backtracks, making it more performance friendly.
Parameters:
Parameter | Type | Description |
---|---|---|
$match | array |
convertAttribute
Attribute Conversion
Used as a callback for Purify.
Parameters:
Parameter | Type | Description |
---|---|---|
$match | array |
filterAttributes
Filter Attributes
Filters tag attributes for consistency and safety.
Parameters:
Parameter | Type | Description |
---|---|---|
$string | string |
decodeEntity
HTML Entity Decode Callback
Used as a callback for Purify.
Parameters:
Parameter | Type | Description |
---|---|---|
$match | array |
validateEntities
Validate URL entities
Called by $this->purify().
Parameters:
Parameter | Type | Description |
---|---|---|
$string | string |
neverAllowed
Never Allowed
A utility function for $this->purify().
Parameters:
Parameter | Type | Description |
---|---|---|
$string | string |
removeInvisibleCharacters
Removes invisible characters.
Parameters:
Parameter | Type | Description |
---|---|---|
$string | string | |
$urlEncoded | bool |
sanitizeFilename
Sanitize Filename
Tries to sanitize filenames in order to prevent directory traversal attempts and other security threats, which is particularly useful for files that were supplied via user input.
If it is acceptable for the user input to include relative paths, e.g. file/in/some/approved/folder.txt, you can set the second optional parameter, $relativePath to true.
Parameters:
Parameter | Type | Description |
---|---|---|
$string | string | Input file name |
$relativePath | bool | Whether to preserve paths |
Automatically generated on 2025-10-13