ILIAS  release_5-0 Revision 5.0.0-1144-gc4397b1f870
All Data Structures Namespaces Files Functions Variables Modules Pages
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 203 of file Serializer.php.

References _testPermissions(), generateBaseDirectoryPath(), and generateDirectoryPath().

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

204  {
205  $directory = $this->generateDirectoryPath($config);
206  $chmod = $config->get('Cache.SerializerPermissions');
207  if (!$chmod) {
208  $chmod = 0755; // invalid config or simpletest
209  }
210  if (!is_dir($directory)) {
211  $base = $this->generateBaseDirectoryPath($config);
212  if (!is_dir($base)) {
213  trigger_error(
214  'Base directory ' . $base . ' does not exist,
215  please create or change using %Cache.SerializerPath',
216  E_USER_WARNING
217  );
218  return false;
219  } elseif (!$this->_testPermissions($base, $chmod)) {
220  return false;
221  }
222  $old = umask(0000);
223  mkdir($directory, $chmod);
224  umask($old);
225  } elseif (!$this->_testPermissions($directory, $chmod)) {
226  return false;
227  }
228  return true;
229  }
generateBaseDirectoryPath($config)
Generates path to base directory that contains all definition type serials.
Definition: Serializer.php:169
generateDirectoryPath($config)
Generates the path to the directory contain this cache's serial files.
Definition: Serializer.php:156
_testPermissions($dir, $chmod)
Tests permissions on a directory and throws out friendly error messages and attempts to chmod it itse...
Definition: Serializer.php:238
+ 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 238 of file Serializer.php.

Referenced by _prepareDir().

239  {
240  // early abort, if it is writable, everything is hunky-dory
241  if (is_writable($dir)) {
242  return true;
243  }
244  if (!is_dir($dir)) {
245  // generally, you'll want to handle this beforehand
246  // so a more specific error message can be given
247  trigger_error(
248  'Directory ' . $dir . ' does not exist',
249  E_USER_WARNING
250  );
251  return false;
252  }
253  if (function_exists('posix_getuid')) {
254  // POSIX system, we can give more specific advice
255  if (fileowner($dir) === posix_getuid()) {
256  // we can chmod it ourselves
257  $chmod = $chmod | 0700;
258  if (chmod($dir, $chmod)) {
259  return true;
260  }
261  } elseif (filegroup($dir) === posix_getgid()) {
262  $chmod = $chmod | 0070;
263  } else {
264  // PHP's probably running as nobody, so we'll
265  // need to give global permissions
266  $chmod = $chmod | 0777;
267  }
268  trigger_error(
269  'Directory ' . $dir . ' not writable, ' .
270  'please chmod to ' . decoct($chmod),
271  E_USER_WARNING
272  );
273  } else {
274  // generic error message
275  trigger_error(
276  'Directory ' . $dir . ' not writable, ' .
277  'please alter file permissions',
278  E_USER_WARNING
279  );
280  }
281  return false;
282  }
+ 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 183 of file Serializer.php.

References $data, $file, and $result.

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

184  {
185  $result = file_put_contents($file, $data);
186  if ($result !== false) {
187  // set permissions of the new file (no execute)
188  $chmod = $config->get('Cache.SerializerPermissions');
189  if (!$chmod) {
190  $chmod = 0644; // invalid config or simpletest
191  }
192  $chmod = $chmod & 0666;
193  chmod($file, $chmod);
194  }
195  return $result;
196  }
print $file
$result
+ 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 $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  }
print $file
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:203
_write($file, $data, $config)
Convenience wrapper function for file_put_contents.
Definition: Serializer.php:183
generateFilePath($config)
Generates the file path to the serial file corresponding to the configuration and definition name...
Definition: Serializer.php:143
+ Here is the call graph for this function:

◆ cleanup()

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

Definition at line 115 of file Serializer.php.

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

116  {
117  if (!$this->_prepareDir($config)) {
118  return false;
119  }
120  $dir = $this->generateDirectoryPath($config);
121  $dh = opendir($dir);
122  while (false !== ($filename = readdir($dh))) {
123  if (empty($filename)) {
124  continue;
125  }
126  if ($filename[0] === '.') {
127  continue;
128  }
129  $key = substr($filename, 0, strlen($filename) - 4);
130  if ($this->isOld($key, $config)) {
131  unlink($dir . '/' . $filename);
132  }
133  }
134  }
generateDirectoryPath($config)
Generates the path to the directory contain this cache's serial files.
Definition: Serializer.php:156
_prepareDir($config)
Prepares the directory that this type stores the serials in.
Definition: Serializer.php:203
$filename
Definition: buildRTE.php:89
isOld($key, $config)
Tests whether or not a key is old with respect to the configuration's version and revision number...
+ 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 $filename, _prepareDir(), and generateDirectoryPath().

94  {
95  if (!$this->_prepareDir($config)) {
96  return false;
97  }
98  $dir = $this->generateDirectoryPath($config);
99  $dh = opendir($dir);
100  while (false !== ($filename = readdir($dh))) {
101  if (empty($filename)) {
102  continue;
103  }
104  if ($filename[0] === '.') {
105  continue;
106  }
107  unlink($dir . '/' . $filename);
108  }
109  }
generateDirectoryPath($config)
Generates the path to the directory contain this cache's serial files.
Definition: Serializer.php:156
_prepareDir($config)
Prepares the directory that this type stores the serials in.
Definition: Serializer.php:203
$filename
Definition: buildRTE.php:89
+ 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 169 of file Serializer.php.

Referenced by _prepareDir(), and generateDirectoryPath().

170  {
171  $base = $config->get('Cache.SerializerPath');
172  $base = is_null($base) ? HTMLPURIFIER_PREFIX . '/HTMLPurifier/DefinitionCache/Serializer' : $base;
173  return $base;
174  }
+ 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 156 of file Serializer.php.

References HTMLPurifier_DefinitionCache\$type, and generateBaseDirectoryPath().

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

157  {
158  $base = $this->generateBaseDirectoryPath($config);
159  return $base . '/' . $this->type;
160  }
generateBaseDirectoryPath($config)
Generates path to base directory that contains all definition type serials.
Definition: Serializer.php:169
+ 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 143 of file Serializer.php.

References generateDirectoryPath(), and HTMLPurifier_DefinitionCache\generateKey().

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

144  {
145  $key = $this->generateKey($config);
146  return $this->generateDirectoryPath($config) . '/' . $key . '.ser';
147  }
generateDirectoryPath($config)
Generates the path to the directory contain this cache's serial files.
Definition: Serializer.php:156
generateKey($config)
Generates a unique identifier for a particular configuration.
+ 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 $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  }
print $file
generateFilePath($config)
Generates the file path to the serial file corresponding to the configuration and definition name...
Definition: Serializer.php:143
+ 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 $file, and generateFilePath().

81  {
82  $file = $this->generateFilePath($config);
83  if (!file_exists($file)) {
84  return false;
85  }
86  return unlink($file);
87  }
print $file
generateFilePath($config)
Generates the file path to the serial file corresponding to the configuration and definition name...
Definition: Serializer.php:143
+ 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 $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  }
print $file
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:203
_write($file, $data, $config)
Convenience wrapper function for file_put_contents.
Definition: Serializer.php:183
generateFilePath($config)
Generates the file path to the serial file corresponding to the configuration and definition name...
Definition: Serializer.php:143
+ 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 $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  }
print $file
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:203
_write($file, $data, $config)
Convenience wrapper function for file_put_contents.
Definition: Serializer.php:183
generateFilePath($config)
Generates the file path to the serial file corresponding to the configuration and definition name...
Definition: Serializer.php:143
+ Here is the call graph for this function:

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