1
0
mirror of https://github.com/ezyang/htmlpurifier.git synced 2025-07-31 11:20:13 +02:00

[1.7.0] DefinitionCache->flush() now requires configuration object. DefinitionCache_Serializer now will create directories for new types on the fly, and can accept custom directories to save serials into.

git-svn-id: http://htmlpurifier.org/svnroot/htmlpurifier/trunk@1147 48356398-32a2-884e-a903-53898d9a118a
This commit is contained in:
Edward Z. Yang
2007-06-16 20:46:44 +00:00
parent 9c7483166c
commit d1f43636e5
6 changed files with 58 additions and 12 deletions

View File

@@ -2,6 +2,17 @@
require_once 'HTMLPurifier/DefinitionCache.php';
HTMLPurifier_ConfigSchema::define(
'Cache', 'SerializerPath', null, 'string/null', '
<p>
Absolute path with no trailing slash to store serialized definitions in.
Default is within the
HTML Purifier library inside DefinitionCache/Serializer. This
path must be writable by the webserver. This directive has been
available since 1.7.0.
</p>
');
class HTMLPurifier_DefinitionCache_Serializer extends
HTMLPurifier_DefinitionCache
{
@@ -10,12 +21,14 @@ class HTMLPurifier_DefinitionCache_Serializer extends
if (!$this->checkDefType($def)) return;
$file = $this->generateFilePath($config);
if (file_exists($file)) return false;
$this->_prepareDir($config);
return $this->_write($file, serialize($def));
}
function set($def, $config) {
if (!$this->checkDefType($def)) return;
$file = $this->generateFilePath($config);
$this->_prepareDir($config);
return $this->_write($file, serialize($def));
}
@@ -23,6 +36,7 @@ class HTMLPurifier_DefinitionCache_Serializer extends
if (!$this->checkDefType($def)) return;
$file = $this->generateFilePath($config);
if (!file_exists($file)) return false;
$this->_prepareDir($config);
return $this->_write($file, serialize($def));
}
@@ -38,8 +52,8 @@ class HTMLPurifier_DefinitionCache_Serializer extends
return unlink($file);
}
function flush() {
$dir = $this->generateDirectoryPath();
function flush($config) {
$dir = $this->generateDirectoryPath($config);
$dh = opendir($dir);
while (false !== ($filename = readdir($dh))) {
if (empty($filename)) continue;
@@ -49,7 +63,8 @@ class HTMLPurifier_DefinitionCache_Serializer extends
}
function cleanup($config) {
$dir = $this->generateDirectoryPath();
$this->_prepareDir($config);
$dir = $this->generateDirectoryPath($config);
$dh = opendir($dir);
while (false !== ($filename = readdir($dh))) {
if (empty($filename)) continue;
@@ -65,15 +80,17 @@ class HTMLPurifier_DefinitionCache_Serializer extends
*/
function generateFilePath($config) {
$key = $this->generateKey($config);
return $this->generateDirectoryPath() . '/' . $key . '.ser';
return $this->generateDirectoryPath($config) . '/' . $key . '.ser';
}
/**
* Generates the path to the directory contain this cache's serial files
* @note No trailing slash
*/
function generateDirectoryPath() {
return dirname(__FILE__) . '/Serializer/' . $this->type;
function generateDirectoryPath($config) {
$base = $config->get('Cache', 'SerializerPath');
$base = is_null($base) ? dirname(__FILE__) . '/Serializer' : $base;
return $base . '/' . $this->type;
}
/**
@@ -97,6 +114,16 @@ class HTMLPurifier_DefinitionCache_Serializer extends
return $status;
}
/**
* Prepares the directory that this type stores the serials in
*/
function _prepareDir($config) {
$directory = $this->generateDirectoryPath($config);
if (!is_dir($directory)) {
mkdir($directory);
}
}
}
?>