2016-08-19 00:51:35 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace SilverStripe\View\Parsers;
|
|
|
|
|
2017-05-17 07:40:13 +02:00
|
|
|
use SilverStripe\Core\Config\Configurable;
|
|
|
|
use SilverStripe\Core\Injector\Injectable;
|
2016-08-19 00:51:35 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Base class for HTML cleaning implementations.
|
|
|
|
*/
|
2017-05-17 07:40:13 +02:00
|
|
|
abstract class HTMLCleaner
|
2016-11-29 00:31:16 +01:00
|
|
|
{
|
2017-05-17 07:40:13 +02:00
|
|
|
use Configurable;
|
|
|
|
use Injectable;
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $defaultConfig = array();
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
/**
|
|
|
|
* Configuration variables for HTMLCleaners that support configuration (like Tidy)
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public $config;
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
/**
|
|
|
|
* @param array $config The configuration for the cleaner, if necessary
|
|
|
|
*/
|
|
|
|
public function __construct($config = null)
|
|
|
|
{
|
|
|
|
if ($config) {
|
|
|
|
$config = array_merge($this->defaultConfig, $config);
|
|
|
|
} else {
|
|
|
|
$config = $this->defaultConfig;
|
|
|
|
}
|
|
|
|
$this->setConfig($config);
|
|
|
|
}
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
/**
|
|
|
|
* @param array
|
|
|
|
*/
|
|
|
|
public function setConfig($config)
|
|
|
|
{
|
|
|
|
$this->config = $config;
|
|
|
|
}
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getConfig()
|
|
|
|
{
|
|
|
|
return $this->config;
|
|
|
|
}
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
/**
|
|
|
|
* Passed a string, return HTML that has been tidied.
|
|
|
|
*
|
|
|
|
* @param string $content
|
|
|
|
* @return string HTML, tidied
|
|
|
|
*/
|
|
|
|
abstract public function cleanHTML($content);
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2016-11-29 00:31:16 +01:00
|
|
|
/**
|
|
|
|
* Experimental inst class to create a default html cleaner class
|
|
|
|
*
|
|
|
|
* @return static
|
|
|
|
*/
|
|
|
|
public static function inst()
|
|
|
|
{
|
|
|
|
if (class_exists('HTMLPurifier')) {
|
|
|
|
return new PurifierHTMLCleaner();
|
|
|
|
} elseif (class_exists('tidy')) {
|
|
|
|
return new TidyHTMLCleaner();
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2016-08-19 00:51:35 +02:00
|
|
|
}
|