parent
bd1c35d830
commit
1604390e01
@ -0,0 +1,73 @@ |
||||
<?php |
||||
|
||||
declare(strict_types=1); |
||||
|
||||
namespace CodeHydrater\services; |
||||
|
||||
use \CodeHydrater\bootstrap\registry as Reg; |
||||
|
||||
if (!defined('HTMLPURIFIER_PREFIX')) { |
||||
define('HTMLPURIFIER_PREFIX', CodeHydrater_PROJECT . 'vendor/ezyang/htmlpurifier/library'); |
||||
} |
||||
|
||||
final class html_filter { |
||||
private $engine = false; |
||||
private $config = null; |
||||
|
||||
public function __construct(private $enable_file_caching = false) { |
||||
if (Reg::get('html_filter') === null) { |
||||
spl_autoload_register(function ($class) { |
||||
// HTMLPurifier uses underscores to indicate subfolders |
||||
$prefix = 'HTMLPurifier'; |
||||
|
||||
if (strpos($class, $prefix) === 0) { |
||||
$path = HTMLPURIFIER_PREFIX . "/" . str_replace('_', '/', $class) . '.php'; |
||||
if (file_exists($path)) { |
||||
require_once $path; |
||||
} |
||||
} |
||||
}); |
||||
Reg::set('html_filter', true); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Keep this as its used by safer_io.php in the p() method |
||||
* @return bool |
||||
*/ |
||||
public function has_loaded(): bool { |
||||
return ($this->engine === false) ? false : true; |
||||
} |
||||
|
||||
public function set_defaults(): void { |
||||
$cache_dir = BaseDir . "/protected/runtime/html_purifier_filter_cache"; |
||||
|
||||
$this->config = \HTMLPurifier_Config::createDefault(); |
||||
$this->config->set('Core.Encoding', 'UTF-8'); |
||||
|
||||
if ($this->enable_file_caching) { |
||||
// Set the path for the serialized cache files |
||||
$this->config->set('Cache.SerializerPath', $cache_dir); |
||||
|
||||
// Optionally create the directory if it doesn't exist |
||||
if (!file_exists($cache_dir)) { |
||||
mkdir($cache_dir, 0775, true); |
||||
} |
||||
} |
||||
} |
||||
|
||||
public function set_config(string $key, $value, $a = null): void { |
||||
$this->config->set($key, $value, $a); |
||||
} |
||||
|
||||
public function init(): void { |
||||
$this->engine = new \HTMLPurifier($this->config); |
||||
} |
||||
|
||||
public function purify(string $html): string { |
||||
if ($this->engine === false) { |
||||
$this->init(); |
||||
} |
||||
return $this->engine->purify($html); |
||||
} |
||||
} |
||||
Loading…
Reference in new issue