Caching: Altered purifier cache folder to be server-created

Moved from a static folder to a dynamically created folder in the
framework/cache directory, to increase the chance that it's created with
server-writable permissions.
This is due to an issue where users had permission issues, since adding
a new folder means it's created by the git user and often
non-web-writable.
This commit is contained in:
Dan Brown
2026-02-17 18:22:13 +00:00
parent 9646339933
commit a2017ffa55
2 changed files with 15 additions and 5 deletions

View File

@@ -22,8 +22,13 @@ class ConfiguredHtmlPurifier
public function __construct()
{
// This is done by the web-server at run-time, with the existing
// storage/framework/cache folder to ensure we're using a server-writable folder.
$cachePath = storage_path('framework/cache/purifier');
$this->createCacheFolderIfNeeded($cachePath);
$config = HTMLPurifier_HTML5Config::createDefault();
$this->setConfig($config);
$this->setConfig($config, $cachePath);
$this->resetCacheIfNeeded($config);
$htmlDef = $config->getDefinition('HTML', true, true);
@@ -34,6 +39,13 @@ class ConfiguredHtmlPurifier
$this->purifier = new HTMLPurifier($config);
}
protected function createCacheFolderIfNeeded(string $cachePath): void
{
if (!file_exists($cachePath)) {
mkdir($cachePath, 0777, true);
}
}
protected function resetCacheIfNeeded(HTMLPurifier_Config $config): void
{
if (self::$cachedChecked) {
@@ -53,9 +65,9 @@ class ConfiguredHtmlPurifier
self::$cachedChecked = true;
}
protected function setConfig(HTMLPurifier_Config $config): void
protected function setConfig(HTMLPurifier_Config $config, string $cachePath): void
{
$config->set('Cache.SerializerPath', storage_path('framework/purifier'));
$config->set('Cache.SerializerPath', $cachePath);
$config->set('Core.AllowHostnameUnderscore', true);
$config->set('CSS.AllowTricky', true);
$config->set('HTML.SafeIframe', true);