diff options
author | Aaron Parecki <aaron@parecki.com> | 2017-02-02 21:19:35 -0800 |
---|---|---|
committer | Aaron Parecki <aaron@parecki.com> | 2017-02-02 21:19:35 -0800 |
commit | d00c336a55d1dd047cdb778cb44ae441ea6b842e (patch) | |
tree | 95411b6ae52c1166fe60ecfc6eb19ef6c21d1f87 /lib | |
parent | 61a80c5a2c1cd3ee6fb53602e20d6c0c832c9138 (diff) |
clean up editor HTML when posting to Micropub endpoint
closes #67
Diffstat (limited to 'lib')
-rw-r--r-- | lib/helpers.php | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/lib/helpers.php b/lib/helpers.php index 4a4da11..26faf49 100644 --- a/lib/helpers.php +++ b/lib/helpers.php @@ -380,3 +380,50 @@ function correct_photo_rotation($filename) { $image->writeImage($filename); } } + +function sanitize_editor_html($html) { + $config = HTMLPurifier_Config::createDefault(); + $config->set('Cache.DefinitionImpl', null); + $config->set('HTML.AllowedElements', [ + 'a', + 'abbr', + 'b', + 'code', + 'del', + 'em', + 'i', + 'img', + 'q', + 'strike', + 'strong', + 'blockquote', + 'pre', + 'p', + 'h1', + 'h2', + 'h3', + 'h4', + 'h5', + 'h6', + 'ul', + 'li', + 'ol' + ]); + + // Allow data: URIs + $config->set('URI.AllowedSchemes', array('data' => true, 'http' => true, 'https' => true)); + + // Strip all classes from elements + $config->set('Attr.AllowedClasses', ''); + + // $def = $config->getHTMLDefinition(true); + $purifier = new HTMLPurifier($config); + $sanitized = $purifier->purify($html); + $sanitized = str_replace("
","\r",$sanitized); + + # Remove empty paragraphs + $sanitized = str_replace('<p><br /></p>','',$sanitized); + $sanitized = str_replace('<p></p>','',$sanitized); + + return $sanitized; +} |