3 class HTMLPurifier_DefinitionCache_Serializer
extends HTMLPurifier_DefinitionCache
7 * @param HTMLPurifier_Definition $def
8 * @param HTMLPurifier_Config $config
11 public function add($def, $config)
13 if (!$this->checkDefType($def)) {
16 $file = $this->generateFilePath($config);
17 if (file_exists($file)) {
20 if (!$this->_prepareDir($config)) {
23 return $this->_write($file, serialize($def), $config);
27 * @param HTMLPurifier_Definition $def
28 * @param HTMLPurifier_Config $config
31 public function set($def, $config)
33 if (!$this->checkDefType($def)) {
36 $file = $this->generateFilePath($config);
37 if (!$this->_prepareDir($config)) {
40 return $this->_write($file, serialize($def), $config);
44 * @param HTMLPurifier_Definition $def
45 * @param HTMLPurifier_Config $config
48 public function replace($def, $config)
50 if (!$this->checkDefType($def)) {
53 $file = $this->generateFilePath($config);
54 if (!file_exists($file)) {
57 if (!$this->_prepareDir($config)) {
60 return $this->_write($file, serialize($def), $config);
64 * @param HTMLPurifier_Config $config
65 * @return bool|HTMLPurifier_Config
67 public function get($config)
69 $file = $this->generateFilePath($config);
70 if (!file_exists($file)) {
73 return unserialize(file_get_contents($file));
77 * @param HTMLPurifier_Config $config
80 public function remove($config)
82 $file = $this->generateFilePath($config);
83 if (!file_exists($file)) {
90 * @param HTMLPurifier_Config $config
93 public function flush($config)
95 if (!$this->_prepareDir($config)) {
98 $dir = $this->generateDirectoryPath($config);
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.
106 while (false !== ($filename = readdir($dh))) {
107 if (empty($filename)) {
110 if ($filename[0] === '.') {
113 unlink($dir . '/' . $filename);
119 * @param HTMLPurifier_Config $config
122 public function cleanup($config)
124 if (!$this->_prepareDir($config)) {
127 $dir = $this->generateDirectoryPath($config);
129 // See #49 (and above).
133 while (false !== ($filename = readdir($dh))) {
134 if (empty($filename)) {
137 if ($filename[0] === '.') {
140 $key = substr($filename, 0, strlen($filename) - 4);
141 if ($this->isOld($key, $config)) {
142 unlink($dir . '/' . $filename);
149 * Generates the file path to the serial file corresponding to
150 * the configuration and definition name
151 * @param HTMLPurifier_Config $config
153 * @todo Make protected
155 public function generateFilePath($config)
157 $key = $this->generateKey($config);
158 return $this->generateDirectoryPath($config) . '/' . $key . '.ser';
162 * Generates the path to the directory contain this cache's serial files
163 * @param HTMLPurifier_Config $config
165 * @note No trailing slash
166 * @todo Make protected
168 public function generateDirectoryPath($config)
170 $base = $this->generateBaseDirectoryPath($config);
171 return $base . '/' . $this->type
;
175 * Generates path to base directory that contains all definition type
177 * @param HTMLPurifier_Config $config
178 * @return mixed|string
179 * @todo Make protected
181 public function generateBaseDirectoryPath($config)
183 $base = $config->get('Cache.SerializerPath');
184 $base = is_null($base) ? HTMLPURIFIER_PREFIX
. '/HTMLPurifier/DefinitionCache/Serializer' : $base;
189 * Convenience wrapper function for file_put_contents
190 * @param string $file File name to write to
191 * @param string $data Data to write into file
192 * @param HTMLPurifier_Config $config
193 * @return int|bool Number of bytes written if success, or false if failure.
195 private function _write($file, $data, $config)
197 $result = file_put_contents($file, $data);
198 if ($result !== false) {
199 // set permissions of the new file (no execute)
200 $chmod = $config->get('Cache.SerializerPermissions');
201 if ($chmod === null) {
204 $chmod = $chmod & 0666;
205 chmod($file, $chmod);
212 * Prepares the directory that this type stores the serials in
213 * @param HTMLPurifier_Config $config
214 * @return bool True if successful
216 private function _prepareDir($config)
218 $directory = $this->generateDirectoryPath($config);
219 $chmod = $config->get('Cache.SerializerPermissions');
220 if (!is_dir($directory)) {
221 $base = $this->generateBaseDirectoryPath($config);
222 if (!is_dir($base)) {
224 'Base directory ' . $base . ' does not exist,
225 please create or change using %Cache.SerializerPath',
229 } elseif (!$this->_testPermissions($base, $chmod)) {
232 if ($chmod === null) {
234 'Base directory ' . $base . ' does not exist,
235 please create or change using %Cache.SerializerPath',
240 if ($chmod !== null) {
241 mkdir($directory, $chmod);
245 if (!$this->_testPermissions($directory, $chmod)) {
247 'Base directory ' . $base . ' does not exist,
248 please create or change using %Cache.SerializerPath',
253 } elseif (!$this->_testPermissions($directory, $chmod)) {
260 * Tests permissions on a directory and throws out friendly
261 * error messages and attempts to chmod it itself if possible
262 * @param string $dir Directory path
263 * @param int $chmod Permissions
264 * @return bool True if directory is writable
266 private function _testPermissions($dir, $chmod)
268 // early abort, if it is writable, everything is hunky-dory
269 if (is_writable($dir)) {
273 // generally, you'll want to handle this beforehand
274 // so a more specific error message can be given
276 'Directory ' . $dir . ' does not exist',
281 if (function_exists('posix_getuid') && $chmod !== null) {
282 // POSIX system, we can give more specific advice
283 if (fileowner($dir) === posix_getuid()) {
284 // we can chmod it ourselves
285 $chmod = $chmod |
0700;
286 if (chmod($dir, $chmod)) {
289 } elseif (filegroup($dir) === posix_getgid()) {
290 $chmod = $chmod |
0070;
292 // PHP's probably running as nobody, so we'll
293 // need to give global permissions
294 $chmod = $chmod |
0777;
297 'Directory ' . $dir . ' not writable, ' .
298 'please chmod to ' . decoct($chmod),
302 // generic error message
304 'Directory ' . $dir . ' not writable, ' .
305 'please alter file permissions',
313 // vim: et sw=4 sts=4