ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
HTMLPurifier_DefinitionCache_Serializer Class Reference
+ Inheritance diagram for HTMLPurifier_DefinitionCache_Serializer:
+ Collaboration diagram for HTMLPurifier_DefinitionCache_Serializer:

Public Member Functions

 add ($def, $config)
 
 set ($def, $config)
 
 replace ($def, $config)
 
 get ($config)
 
 remove ($config)
 
 flush ($config)
 
 cleanup ($config)
 
 generateFilePath ($config)
 Generates the file path to the serial file corresponding to the configuration and definition name. More...
 
 generateDirectoryPath ($config)
 Generates the path to the directory contain this cache's serial files. More...
 
 generateBaseDirectoryPath ($config)
 Generates path to base directory that contains all definition type serials. More...
 
- Public Member Functions inherited from HTMLPurifier_DefinitionCache
 __construct ($type)
 
 generateKey ($config)
 Generates a unique identifier for a particular configuration. More...
 
 isOld ($key, $config)
 Tests whether or not a key is old with respect to the configuration's version and revision number. More...
 
 checkDefType ($def)
 Checks if a definition's type jives with the cache's type. More...
 
 add ($def, $config)
 Adds a definition object to the cache. More...
 
 set ($def, $config)
 Unconditionally saves a definition object to the cache. More...
 
 replace ($def, $config)
 Replace an object in the cache. More...
 
 get ($config)
 Retrieves a definition object from the cache. More...
 
 remove ($config)
 Removes a definition object to the cache. More...
 
 flush ($config)
 Clears all objects from cache. More...
 
 cleanup ($config)
 Clears all expired (older version or revision) objects from cache. More...
 

Private Member Functions

 _write ($file, $data, $config)
 Convenience wrapper function for file_put_contents. More...
 
 _prepareDir ($config)
 Prepares the directory that this type stores the serials in. More...
 
 _testPermissions ($dir, $chmod)
 Tests permissions on a directory and throws out friendly error messages and attempts to chmod it itself if possible. More...
 

Additional Inherited Members

- Data Fields inherited from HTMLPurifier_DefinitionCache
 $type
 string More...
 

Detailed Description

Definition at line 3 of file Serializer.php.

Member Function Documentation

◆ _prepareDir()

HTMLPurifier_DefinitionCache_Serializer::_prepareDir (   $config)
private

Prepares the directory that this type stores the serials in.

Parameters
HTMLPurifier_Config$config
Returns
bool True if successful

Definition at line 215 of file Serializer.php.

References $base, $config, _testPermissions(), generateBaseDirectoryPath(), and generateDirectoryPath().

Referenced by add(), cleanup(), flush(), replace(), and set().

216  {
217  $directory = $this->generateDirectoryPath($config);
218  $chmod = $config->get('Cache.SerializerPermissions');
219  if ($chmod === null) {
220  // TODO: This races
221  if (is_dir($directory)) return true;
222  return mkdir($directory);
223  }
224  if (!is_dir($directory)) {
226  if (!is_dir($base)) {
227  trigger_error(
228  'Base directory ' . $base . ' does not exist,
229  please create or change using %Cache.SerializerPath',
230  E_USER_WARNING
231  );
232  return false;
233  } elseif (!$this->_testPermissions($base, $chmod)) {
234  return false;
235  }
236  if (!mkdir($directory, $chmod)) {
237  trigger_error(
238  'Could not create directory ' . $directory . '',
239  E_USER_WARNING
240  );
241  return false;
242  }
243  if (!$this->_testPermissions($directory, $chmod)) {
244  return false;
245  }
246  } elseif (!$this->_testPermissions($directory, $chmod)) {
247  return false;
248  }
249  return true;
250  }
generateBaseDirectoryPath($config)
Generates path to base directory that contains all definition type serials.
Definition: Serializer.php:183
generateDirectoryPath($config)
Generates the path to the directory contain this cache's serial files.
Definition: Serializer.php:170
$base
Definition: index.php:4
_testPermissions($dir, $chmod)
Tests permissions on a directory and throws out friendly error messages and attempts to chmod it itse...
Definition: Serializer.php:259
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ _testPermissions()

HTMLPurifier_DefinitionCache_Serializer::_testPermissions (   $dir,
  $chmod 
)
private

Tests permissions on a directory and throws out friendly error messages and attempts to chmod it itself if possible.

Parameters
string$dirDirectory path
int$chmodPermissions
Returns
bool True if directory is writable

Definition at line 259 of file Serializer.php.

Referenced by _prepareDir().

260  {
261  // early abort, if it is writable, everything is hunky-dory
262  if (is_writable($dir)) {
263  return true;
264  }
265  if (!is_dir($dir)) {
266  // generally, you'll want to handle this beforehand
267  // so a more specific error message can be given
268  trigger_error(
269  'Directory ' . $dir . ' does not exist',
270  E_USER_WARNING
271  );
272  return false;
273  }
274  if (function_exists('posix_getuid') && $chmod !== null) {
275  // POSIX system, we can give more specific advice
276  if (fileowner($dir) === posix_getuid()) {
277  // we can chmod it ourselves
278  $chmod = $chmod | 0700;
279  if (chmod($dir, $chmod)) {
280  return true;
281  }
282  } elseif (filegroup($dir) === posix_getgid()) {
283  $chmod = $chmod | 0070;
284  } else {
285  // PHP's probably running as nobody, so we'll
286  // need to give global permissions
287  $chmod = $chmod | 0777;
288  }
289  trigger_error(
290  'Directory ' . $dir . ' not writable, ' .
291  'please chmod to ' . decoct($chmod),
292  E_USER_WARNING
293  );
294  } else {
295  // generic error message
296  trigger_error(
297  'Directory ' . $dir . ' not writable, ' .
298  'please alter file permissions',
299  E_USER_WARNING
300  );
301  }
302  return false;
303  }
+ Here is the caller graph for this function:

◆ _write()

HTMLPurifier_DefinitionCache_Serializer::_write (   $file,
  $data,
  $config 
)
private

Convenience wrapper function for file_put_contents.

Parameters
string$fileFile name to write to
string$dataData to write into file
HTMLPurifier_Config$config
Returns
int|bool Number of bytes written if success, or false if failure.

Definition at line 197 of file Serializer.php.

References $config, $data, $file, and $result.

Referenced by add(), replace(), and set().

198  {
199  $result = file_put_contents($file, $data);
200  if ($result !== false) {
201  // set permissions of the new file (no execute)
202  $chmod = $config->get('Cache.SerializerPermissions');
203  if ($chmod !== null) {
204  chmod($file, $chmod & 0666);
205  }
206  }
207  return $result;
208  }
$result
if(!file_exists("$old.txt")) if($old===$new) if(file_exists("$new.txt")) $file
+ Here is the caller graph for this function:

◆ add()

HTMLPurifier_DefinitionCache_Serializer::add (   $def,
  $config 
)
Parameters
HTMLPurifier_Definition$def
HTMLPurifier_Config$config
Returns
int|bool

Definition at line 11 of file Serializer.php.

References $config, $def, $file, _prepareDir(), _write(), HTMLPurifier_DefinitionCache\checkDefType(), and generateFilePath().

12  {
13  if (!$this->checkDefType($def)) {
14  return;
15  }
16  $file = $this->generateFilePath($config);
17  if (file_exists($file)) {
18  return false;
19  }
20  if (!$this->_prepareDir($config)) {
21  return false;
22  }
23  return $this->_write($file, serialize($def), $config);
24  }
checkDefType($def)
Checks if a definition's type jives with the cache's type.
_prepareDir($config)
Prepares the directory that this type stores the serials in.
Definition: Serializer.php:215
_write($file, $data, $config)
Convenience wrapper function for file_put_contents.
Definition: Serializer.php:197
generateFilePath($config)
Generates the file path to the serial file corresponding to the configuration and definition name...
Definition: Serializer.php:157
$def
Definition: croninfo.php:21
if(!file_exists("$old.txt")) if($old===$new) if(file_exists("$new.txt")) $file
+ Here is the call graph for this function:

◆ cleanup()

HTMLPurifier_DefinitionCache_Serializer::cleanup (   $config)
Parameters
HTMLPurifier_Config$config
Returns
bool

Definition at line 123 of file Serializer.php.

References $config, $filename, $key, _prepareDir(), generateDirectoryPath(), and HTMLPurifier_DefinitionCache\isOld().

124  {
125  if (!$this->_prepareDir($config)) {
126  return false;
127  }
128  $dir = $this->generateDirectoryPath($config);
129  $dh = opendir($dir);
130  // See #49 (and above).
131  if (false === $dh) {
132  return false;
133  }
134  while (false !== ($filename = readdir($dh))) {
135  if (empty($filename)) {
136  continue;
137  }
138  if ($filename[0] === '.') {
139  continue;
140  }
141  $key = substr($filename, 0, strlen($filename) - 4);
142  if ($this->isOld($key, $config)) {
143  unlink($dir . '/' . $filename);
144  }
145  }
146  closedir($dh);
147  return true;
148  }
generateDirectoryPath($config)
Generates the path to the directory contain this cache's serial files.
Definition: Serializer.php:170
_prepareDir($config)
Prepares the directory that this type stores the serials in.
Definition: Serializer.php:215
isOld($key, $config)
Tests whether or not a key is old with respect to the configuration's version and revision number...
$key
Definition: croninfo.php:18
+ Here is the call graph for this function:

◆ flush()

HTMLPurifier_DefinitionCache_Serializer::flush (   $config)
Parameters
HTMLPurifier_Config$config
Returns
bool

Definition at line 93 of file Serializer.php.

References $config, $filename, _prepareDir(), and generateDirectoryPath().

94  {
95  if (!$this->_prepareDir($config)) {
96  return false;
97  }
98  $dir = $this->generateDirectoryPath($config);
99  $dh = opendir($dir);
100  // Apparently, on some versions of PHP, readdir will return
101  // an empty string if you pass an invalid argument to readdir.
102  // So you need this test. See #49.
103  if (false === $dh) {
104  return false;
105  }
106  while (false !== ($filename = readdir($dh))) {
107  if (empty($filename)) {
108  continue;
109  }
110  if ($filename[0] === '.') {
111  continue;
112  }
113  unlink($dir . '/' . $filename);
114  }
115  closedir($dh);
116  return true;
117  }
generateDirectoryPath($config)
Generates the path to the directory contain this cache's serial files.
Definition: Serializer.php:170
_prepareDir($config)
Prepares the directory that this type stores the serials in.
Definition: Serializer.php:215
+ Here is the call graph for this function:

◆ generateBaseDirectoryPath()

HTMLPurifier_DefinitionCache_Serializer::generateBaseDirectoryPath (   $config)

Generates path to base directory that contains all definition type serials.

Parameters
HTMLPurifier_Config$config
Returns
mixed|string
Todo:
Make protected

Definition at line 183 of file Serializer.php.

References $base, and $config.

Referenced by _prepareDir(), and generateDirectoryPath().

184  {
185  $base = $config->get('Cache.SerializerPath');
186  $base = is_null($base) ? HTMLPURIFIER_PREFIX . '/HTMLPurifier/DefinitionCache/Serializer' : $base;
187  return $base;
188  }
$base
Definition: index.php:4
+ Here is the caller graph for this function:

◆ generateDirectoryPath()

HTMLPurifier_DefinitionCache_Serializer::generateDirectoryPath (   $config)

Generates the path to the directory contain this cache's serial files.

Parameters
HTMLPurifier_Config$config
Returns
string
Note
No trailing slash
Todo:
Make protected

Definition at line 170 of file Serializer.php.

References $base, $config, HTMLPurifier_DefinitionCache\$type, and generateBaseDirectoryPath().

Referenced by _prepareDir(), cleanup(), flush(), and generateFilePath().

171  {
173  return $base . '/' . $this->type;
174  }
generateBaseDirectoryPath($config)
Generates path to base directory that contains all definition type serials.
Definition: Serializer.php:183
$base
Definition: index.php:4
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ generateFilePath()

HTMLPurifier_DefinitionCache_Serializer::generateFilePath (   $config)

Generates the file path to the serial file corresponding to the configuration and definition name.

Parameters
HTMLPurifier_Config$config
Returns
string
Todo:
Make protected

Definition at line 157 of file Serializer.php.

References $config, $key, generateDirectoryPath(), and HTMLPurifier_DefinitionCache\generateKey().

Referenced by add(), get(), remove(), replace(), and set().

158  {
159  $key = $this->generateKey($config);
160  return $this->generateDirectoryPath($config) . '/' . $key . '.ser';
161  }
generateDirectoryPath($config)
Generates the path to the directory contain this cache's serial files.
Definition: Serializer.php:170
generateKey($config)
Generates a unique identifier for a particular configuration.
$key
Definition: croninfo.php:18
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ get()

HTMLPurifier_DefinitionCache_Serializer::get (   $config)
Parameters
HTMLPurifier_Config$config
Returns
bool|HTMLPurifier_Config

Definition at line 67 of file Serializer.php.

References $config, $file, and generateFilePath().

68  {
69  $file = $this->generateFilePath($config);
70  if (!file_exists($file)) {
71  return false;
72  }
73  return unserialize(file_get_contents($file));
74  }
generateFilePath($config)
Generates the file path to the serial file corresponding to the configuration and definition name...
Definition: Serializer.php:157
if(!file_exists("$old.txt")) if($old===$new) if(file_exists("$new.txt")) $file
+ Here is the call graph for this function:

◆ remove()

HTMLPurifier_DefinitionCache_Serializer::remove (   $config)
Parameters
HTMLPurifier_Config$config
Returns
bool

Definition at line 80 of file Serializer.php.

References $config, $file, and generateFilePath().

81  {
82  $file = $this->generateFilePath($config);
83  if (!file_exists($file)) {
84  return false;
85  }
86  return unlink($file);
87  }
generateFilePath($config)
Generates the file path to the serial file corresponding to the configuration and definition name...
Definition: Serializer.php:157
if(!file_exists("$old.txt")) if($old===$new) if(file_exists("$new.txt")) $file
+ Here is the call graph for this function:

◆ replace()

HTMLPurifier_DefinitionCache_Serializer::replace (   $def,
  $config 
)
Parameters
HTMLPurifier_Definition$def
HTMLPurifier_Config$config
Returns
int|bool

Definition at line 48 of file Serializer.php.

References $config, $def, $file, _prepareDir(), _write(), HTMLPurifier_DefinitionCache\checkDefType(), and generateFilePath().

49  {
50  if (!$this->checkDefType($def)) {
51  return;
52  }
53  $file = $this->generateFilePath($config);
54  if (!file_exists($file)) {
55  return false;
56  }
57  if (!$this->_prepareDir($config)) {
58  return false;
59  }
60  return $this->_write($file, serialize($def), $config);
61  }
checkDefType($def)
Checks if a definition's type jives with the cache's type.
_prepareDir($config)
Prepares the directory that this type stores the serials in.
Definition: Serializer.php:215
_write($file, $data, $config)
Convenience wrapper function for file_put_contents.
Definition: Serializer.php:197
generateFilePath($config)
Generates the file path to the serial file corresponding to the configuration and definition name...
Definition: Serializer.php:157
$def
Definition: croninfo.php:21
if(!file_exists("$old.txt")) if($old===$new) if(file_exists("$new.txt")) $file
+ Here is the call graph for this function:

◆ set()

HTMLPurifier_DefinitionCache_Serializer::set (   $def,
  $config 
)
Parameters
HTMLPurifier_Definition$def
HTMLPurifier_Config$config
Returns
int|bool

Definition at line 31 of file Serializer.php.

References $config, $def, $file, _prepareDir(), _write(), HTMLPurifier_DefinitionCache\checkDefType(), and generateFilePath().

32  {
33  if (!$this->checkDefType($def)) {
34  return;
35  }
36  $file = $this->generateFilePath($config);
37  if (!$this->_prepareDir($config)) {
38  return false;
39  }
40  return $this->_write($file, serialize($def), $config);
41  }
checkDefType($def)
Checks if a definition's type jives with the cache's type.
_prepareDir($config)
Prepares the directory that this type stores the serials in.
Definition: Serializer.php:215
_write($file, $data, $config)
Convenience wrapper function for file_put_contents.
Definition: Serializer.php:197
generateFilePath($config)
Generates the file path to the serial file corresponding to the configuration and definition name...
Definition: Serializer.php:157
$def
Definition: croninfo.php:21
if(!file_exists("$old.txt")) if($old===$new) if(file_exists("$new.txt")) $file
+ Here is the call graph for this function:

The documentation for this class was generated from the following file: