2011-03-30 06:46:13 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2011-03-30 06:46:13 +02:00
|
|
|
* @subpackage misc
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Base class for HTML cleaning implementations.
|
|
|
|
*/
|
2017-11-30 18:49:46 +01:00
|
|
|
abstract class HTMLCleaner extends SS_Object {
|
2011-03-30 06:46:13 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $defaultConfig = array();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-03-30 06:46:13 +02:00
|
|
|
/**
|
|
|
|
* @var $config Array configuration variables for HTMLCleaners that support configuration (like Tidy)
|
|
|
|
*/
|
|
|
|
public $config;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Array The configuration for the cleaner, if necessary
|
|
|
|
*/
|
|
|
|
public function __construct($config = null) {
|
|
|
|
if ($config) $this->config = array_merge($this->defaultConfig, $config);
|
|
|
|
else $this->config = $this->defaultConfig;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-03-30 06:46:13 +02:00
|
|
|
/**
|
|
|
|
* @param Array
|
|
|
|
*/
|
|
|
|
public function setConfig($config) {
|
|
|
|
$this->config = $config;
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-03-30 06:46:13 +02:00
|
|
|
/**
|
|
|
|
* @return Array
|
|
|
|
*/
|
|
|
|
public function getConfig() {
|
|
|
|
return $this->config;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Passed a string, return HTML that has been tidied.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2011-03-30 06:46:13 +02:00
|
|
|
* @param String HTML
|
|
|
|
* @return String HTML, tidied
|
|
|
|
*/
|
2012-09-19 12:07:39 +02:00
|
|
|
abstract public function cleanHTML($content);
|
2011-03-30 06:46:13 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Experimental inst class to create a default html cleaner class
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2011-03-30 06:46:13 +02:00
|
|
|
* @return PurifierHTMLCleaner|TidyHTMLCleaner
|
|
|
|
*/
|
|
|
|
public static function inst() {
|
|
|
|
if (class_exists('HTMLPurifier')) return new PurifierHTMLCleaner();
|
|
|
|
elseif (class_exists('tidy')) return new TidyHTMLCleaner();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cleans HTML using the HTMLPurifier package
|
|
|
|
* http://htmlpurifier.org/
|
|
|
|
*/
|
|
|
|
class PurifierHTMLCleaner extends HTMLCleaner {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-03-30 06:46:13 +02:00
|
|
|
public function cleanHTML($content) {
|
|
|
|
$html = new HTMLPurifier();
|
2013-03-11 21:06:20 +01:00
|
|
|
$doc = Injector::inst()->create('HTMLValue', $html->purify($content));
|
2011-03-30 06:46:13 +02:00
|
|
|
return $doc->getContent();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cleans HTML using the Tidy package
|
|
|
|
* http://php.net/manual/en/book.tidy.php
|
|
|
|
*/
|
|
|
|
class TidyHTMLCleaner extends HTMLCleaner {
|
|
|
|
|
|
|
|
protected $defaultConfig = array(
|
|
|
|
'clean' => true,
|
|
|
|
'output-xhtml' => true,
|
|
|
|
'show-body-only' => true,
|
|
|
|
'wrap' => 0,
|
|
|
|
'doctype' => 'omit',
|
|
|
|
'input-encoding' => 'utf8',
|
|
|
|
'output-encoding' => 'utf8'
|
|
|
|
);
|
|
|
|
|
|
|
|
public function cleanHTML($content) {
|
|
|
|
$tidy = new tidy();
|
|
|
|
$output = $tidy->repairString($content, $this->config);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-10-16 06:10:54 +02:00
|
|
|
// Clean leading/trailing whitespace
|
|
|
|
return preg_replace('/(^\s+)|(\s+$)/', '', $output);
|
2011-03-30 06:46:13 +02:00
|
|
|
}
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|