From 6ea447283147644bbf2a4f91e2385363c4efd201 Mon Sep 17 00:00:00 2001 From: helpfulrobot Date: Fri, 1 Jan 2016 00:53:13 +1300 Subject: [PATCH] Converted to PSR-2 --- code/Purifier.php | 153 +++++++++++++++++++++++----------------------- 1 file changed, 76 insertions(+), 77 deletions(-) diff --git a/code/Purifier.php b/code/Purifier.php index 3eb94c0..d51d14f 100644 --- a/code/Purifier.php +++ b/code/Purifier.php @@ -3,88 +3,87 @@ /** * Standard HTML purifier */ -class Purifier { +class Purifier +{ - /** - * Standard HTML purifier - * @param String $html HTML to purify - * @param String $encoding - * @return String Valid HTML string - */ - public static function Purify($html = null, $encoding = 'UTF-8') { + /** + * Standard HTML purifier + * @param String $html HTML to purify + * @param String $encoding + * @return String Valid HTML string + */ + public static function Purify($html = null, $encoding = 'UTF-8') + { + $config = HTMLPurifier_Config::createDefault(); + $config->set('Core.Encoding', $encoding); + $config->set('HTML.Allowed', 'span,p,br,a,h1,h2,h3,h4,h5,strong,em,u,ul,li,ol,hr,blockquote,sub,sup,p[class],img'); + $config->set('HTML.AllowedElements', array('span', 'p', 'br', 'a', 'h1', 'h2', 'h3', 'h4', 'h5', 'strong', 'em', 'u', 'ul', 'li', 'ol', 'hr', 'blockquote', 'sub', 'sup', 'img')); + $config->set('HTML.AllowedAttributes', 'style,target,title,href,class,src,border,alt,width,height,title,name,id'); + $config->set('CSS.AllowedProperties', 'text-align,font-weight,text-decoration'); + $config->set('AutoFormat.RemoveEmpty', true); + $config->set('Attr.ForbiddenClasses', array('MsoNormal')); - $config = HTMLPurifier_Config::createDefault(); - $config->set('Core.Encoding', $encoding); - $config->set('HTML.Allowed', 'span,p,br,a,h1,h2,h3,h4,h5,strong,em,u,ul,li,ol,hr,blockquote,sub,sup,p[class],img'); - $config->set('HTML.AllowedElements', array('span', 'p', 'br', 'a', 'h1', 'h2', 'h3', 'h4', 'h5', 'strong', 'em', 'u', 'ul', 'li', 'ol', 'hr', 'blockquote', 'sub', 'sup', 'img')); - $config->set('HTML.AllowedAttributes', 'style,target,title,href,class,src,border,alt,width,height,title,name,id'); - $config->set('CSS.AllowedProperties', 'text-align,font-weight,text-decoration'); - $config->set('AutoFormat.RemoveEmpty', true); - $config->set('Attr.ForbiddenClasses', array('MsoNormal')); + $purifier = new HTMLPurifier($config); + return $cleanCode = $purifier->purify($html); + } - $purifier = new HTMLPurifier($config); - return $cleanCode = $purifier->purify($html); - } + /** + * XHTML 1.0 Strict purifier + * @param String $html HTML to purify + * @param String $encoding + * @return String XTML 1.0 Strict validating string + */ + public static function PurifyXHTML($html = null, $encoding = 'UTF-8') + { + $config = HTMLPurifier_Config::createDefault(); + $config->set('Core.Encoding', $encoding); + $config->set('HTML.Doctype', 'XHTML 1.0 Strict'); + $config->set('HTML.TidyLevel', 'heavy'); // all changes, minus... + $config->set('CSS.AllowedProperties', array()); + $config->set('Attr.AllowedClasses', array()); + $config->set('HTML.Allowed', null); + $config->set('AutoFormat.RemoveEmpty', true); // Remove empty tags + $config->set('AutoFormat.Linkify', true); // add to links + $config->set('AutoFormat.AutoParagraph', true); + $config->set('HTML.ForbiddenElements', array('span', 'center')); + $config->set('Core.EscapeNonASCIICharacters', true); + $config->set('Output.TidyFormat', true); - /** - * XHTML 1.0 Strict purifier - * @param String $html HTML to purify - * @param String $encoding - * @return String XTML 1.0 Strict validating string - */ - public static function PurifyXHTML($html = null, $encoding = 'UTF-8') { + $purifier = new HTMLPurifier($config); + $html = $purifier->purify($html); - $config = HTMLPurifier_Config::createDefault(); - $config->set('Core.Encoding', $encoding); - $config->set('HTML.Doctype', 'XHTML 1.0 Strict'); - $config->set('HTML.TidyLevel', 'heavy'); // all changes, minus... - $config->set('CSS.AllowedProperties', array()); - $config->set('Attr.AllowedClasses', array()); - $config->set('HTML.Allowed', null); - $config->set('AutoFormat.RemoveEmpty', true); // Remove empty tags - $config->set('AutoFormat.Linkify', true); // add to links - $config->set('AutoFormat.AutoParagraph', true); - $config->set('HTML.ForbiddenElements', array('span', 'center')); - $config->set('Core.EscapeNonASCIICharacters', true); - $config->set('Output.TidyFormat', true); + // Rimpiazzo le parentesi quadre + $html = str_ireplace("%5B", "[", $html); + $html = str_ireplace("%5D", "]", $html); - $purifier = new HTMLPurifier($config); - $html = $purifier->purify($html); + return $html; + } + + /** + * Strip all HTML tags, like strip_tags, but encoding safe + * @param String $html HTML to purify + * @param String $encoding + * @return String The input string without any html tags + */ + public static function PurifyTXT($html = null, $encoding = 'UTF-8') + { + $config = HTMLPurifier_Config::createDefault(); + $config->set('Core.Encoding', $encoding); + $config->set('HTML.Allowed', null); // Allow Nothing + $config->set('HTML.AllowedElements', array()); + $purifier = new HTMLPurifier($config); + return $purifier->purify($html); + } - // Rimpiazzo le parentesi quadre - $html = str_ireplace("%5B", "[", $html); - $html = str_ireplace("%5D", "]", $html); - - return $html; - } - - /** - * Strip all HTML tags, like strip_tags, but encoding safe - * @param String $html HTML to purify - * @param String $encoding - * @return String The input string without any html tags - */ - public static function PurifyTXT($html = null, $encoding = 'UTF-8') { - - $config = HTMLPurifier_Config::createDefault(); - $config->set('Core.Encoding', $encoding); - $config->set('HTML.Allowed', null); // Allow Nothing - $config->set('HTML.AllowedElements', array()); - $purifier = new HTMLPurifier($config); - return $purifier->purify($html); - } - - /** - * Removes Silverstripe [embed] tags - * @param String $html - * @param String $encoding - * @return String - */ - public static function RemoveEmbed($html = null, $encoding = 'UTF-8') { - - $pattern = '/\[embed.*\].*\[\/embed\]/'; - return preg_replace($pattern, '', $html); - } - + /** + * Removes Silverstripe [embed] tags + * @param String $html + * @param String $encoding + * @return String + */ + public static function RemoveEmbed($html = null, $encoding = 'UTF-8') + { + $pattern = '/\[embed.*\].*\[\/embed\]/'; + return preg_replace($pattern, '', $html); + } } -