mc-cms-namelessmc/core/classes/Core/Output.php

105 lines
4.5 KiB
PHP
Raw Permalink Normal View History

<?php
/**
* Provides static methods for cleansing user input before storing in the database.
*
* @package NamelessMC\Core
* @author Samerton
* @version 2.0.0-pr8
* @license MIT
2018-08-19 13:13:58 +01:00
*/
class Output
{
/**
* @var HTMLPurifier Static purifier instance.
*/
private static HTMLPurifier $_purifier;
2018-08-19 13:13:58 +01:00
/**
* Returns a clean version of an inputted string.
2021-12-05 08:17:24 -08:00
* Will remove HTML, convert HTML entities, and strip slashes.
*
* @param ?string $input The string which will be cleaned
* @return ?string Cleaned version of string.
*/
public static function getClean(?string $input): ?string
{
return $input === null ? null : htmlspecialchars($input, ENT_QUOTES);
2020-12-13 19:38:04 +01:00
}
2020-12-13 20:42:04 -08:00
/**
* Returns a decoded version of a clean string.
2021-10-29 22:09:38 -07:00
*
* @param ?string $input Contains the clean string which will be decoded.
* @return ?string Decoded string.
*/
public static function getDecoded(?string $input): ?string
{
2022-07-03 20:56:15 +02:00
return $input === null ? null : htmlspecialchars_decode($input, ENT_QUOTES);
2020-12-13 19:38:04 +01:00
}
2018-08-05 19:47:01 +01:00
/**
* Returns a purified version of an inputted string with HTMLPurifier.
2021-12-05 08:17:24 -08:00
* Will not remove any HTML tags.
*
* @param string|null $input String which will be purified.
* @param bool $escape_invalid Should invalid HTML be escaped instead of fully removed?
* @param bool $for_editor Whether the purification is for use in the WYSIWYG editor or not, default true
2021-10-29 22:09:38 -07:00
*
* @return string Purified string.
*/
public static function getPurified(?string $input, bool $escape_invalid = false, bool $for_editor = true): string
{
if (!isset(self::$_purifier)) {
2020-12-13 19:38:04 +01:00
$purifierConfig = HTMLPurifier_Config::createDefault();
2018-08-19 13:13:58 +01:00
2020-12-13 19:38:04 +01:00
// Config settings
$purifierConfig->set('HTML.Doctype', 'XHTML 1.0 Transitional');
$purifierConfig->set('URI.DisableExternalResources', false);
$purifierConfig->set('URI.DisableResources', false);
$purifierConfig->set('HTML.Allowed', 'u,a,p,p[style],b,i,small,blockquote,span[style],span[class],p,strong,em,li,ul,ol,div[align],br,img,figure,figcaption');
$purifierConfig->set('CSS.AllowedProperties', ['text-align', 'display', 'float', 'color', 'background-color', 'background', 'font-size', 'font-family', 'margin', 'margin-bottom', 'margin-left', 'margin-right', 'margin-top', 'padding', 'padding-bottom', 'padding-left', 'padding-right', 'padding-top', 'text-decoration', 'font-weight', 'font-style', 'font-size', 'vertical-align', 'width', 'border-color', 'border-style']);
2020-12-13 19:38:04 +01:00
$purifierConfig->set('CSS.AllowTricky', true);
$purifierConfig->set('HTML.AllowedAttributes', 'target, rel, href, id, src, height, width, alt, class, *.style, dir');
$purifierConfig->set('HTML.ForbiddenAttributes', 'iframe@width,iframe@height');
2021-10-29 22:00:05 -07:00
$purifierConfig->set('Attr.AllowedFrameTargets', ['_blank', '_self', '_parent', '_top']);
$purifierConfig->set('Attr.AllowedRel', ['noopener', 'nofollow']);
2020-12-13 19:38:04 +01:00
$purifierConfig->set('HTML.SafeIframe', true);
$purifierConfig->set('URI.SafeIframeRegexp', '%^(https?:)?//(www\.youtube(?:-nocookie)?\.com/embed/|player\.vimeo\.com/video/)%');
$purifierConfig->set('Core.EscapeInvalidTags', $escape_invalid);
2020-12-13 19:38:04 +01:00
$purifierConfig->set('AutoFormat.Linkify', true);
2018-08-19 13:13:58 +01:00
2020-12-13 19:38:04 +01:00
$purifierConfig->set('HTML.DefinitionID', 'namelessmc');
$purifierConfig->set('HTML.DefinitionRev', 1);
$purifierConfig->set('Cache.DefinitionImpl', null);
2020-12-13 20:42:04 -08:00
if ($def = $purifierConfig->maybeGetRawHTMLDefinition()) {
2020-12-13 19:38:04 +01:00
$def->addElement('figure', 'Block', 'Optional: (figcaption, Flow) | (Flow, figcaption) | Flow', 'Common');
$def->addElement('figcaption', 'Inline', 'Flow', 'Common');
}
2020-12-13 19:38:04 +01:00
self::$_purifier = new HTMLPurifier($purifierConfig);
}
2018-08-19 13:13:58 +01:00
$purified = self::$_purifier->purify($input);
if ($for_editor) {
// Double encode &lt; and &gt; to prevent editor from parsing them
return str_replace(['&lt;', '&gt;'], ['&amp;lt;', '&amp;gt;'], $purified);
}
return $purified;
2020-12-13 19:38:04 +01:00
}
/**
* urlencode() a string without encoding slashes.
*
* @param string $input String to encode
* @return string Encoded string
*/
public static function urlEncodeAllowSlashes(string $input): string
{
return str_replace('%2F', '/', urlencode($input));
}
2020-12-13 19:38:04 +01:00
}