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 while (false !== ($filename = readdir($dh))) {
101 if (empty($filename)) {
104 if ($filename[0] === '.') {
107 unlink($dir . '/' . $filename);
112 * @param HTMLPurifier_Config $config
115 public function cleanup($config)
117 if (!$this->_prepareDir($config)) {
120 $dir = $this->generateDirectoryPath($config);
122 while (false !== ($filename = readdir($dh))) {
123 if (empty($filename)) {
126 if ($filename[0] === '.') {
129 $key = substr($filename, 0, strlen($filename) - 4);
130 if ($this->isOld($key, $config)) {
131 unlink($dir . '/' . $filename);
137 * Generates the file path to the serial file corresponding to
138 * the configuration and definition name
139 * @param HTMLPurifier_Config $config
141 * @todo Make protected
143 public function generateFilePath($config)
145 $key = $this->generateKey($config);
146 return $this->generateDirectoryPath($config) . '/' . $key . '.ser';
150 * Generates the path to the directory contain this cache's serial files
151 * @param HTMLPurifier_Config $config
153 * @note No trailing slash
154 * @todo Make protected
156 public function generateDirectoryPath($config)
158 $base = $this->generateBaseDirectoryPath($config);
159 return $base . '/' . $this->type;
163 * Generates path to base directory that contains all definition type
165 * @param HTMLPurifier_Config $config
166 * @return mixed|string
167 * @todo Make protected
169 public function generateBaseDirectoryPath($config)
171 $base = $config->get('Cache.SerializerPath');
172 $base = is_null($base) ? HTMLPURIFIER_PREFIX . '/HTMLPurifier/DefinitionCache/Serializer' : $base;
177 * Convenience wrapper function for file_put_contents
178 * @param string $file File name to write to
179 * @param string $data Data to write into file
180 * @param HTMLPurifier_Config $config
181 * @return int|bool Number of bytes written if success, or false if failure.
183 private function _write($file, $data, $config)
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');
190 $chmod = 0644; // invalid config or simpletest
192 $chmod = $chmod & 0666;
193 chmod($file, $chmod);
199 * Prepares the directory that this type stores the serials in
200 * @param HTMLPurifier_Config $config
201 * @return bool True if successful
203 private function _prepareDir($config)
205 $directory = $this->generateDirectoryPath($config);
206 $chmod = $config->get('Cache.SerializerPermissions');
208 $chmod = 0755; // invalid config or simpletest
210 if (!is_dir($directory)) {
211 $base = $this->generateBaseDirectoryPath($config);
212 if (!is_dir($base)) {
214 'Base directory ' . $base . ' does not exist,
215 please create or change using %Cache.SerializerPath',
219 } elseif (!$this->_testPermissions($base, $chmod)) {
222 mkdir($directory, $chmod);
223 if (!$this->_testPermissions($directory, $chmod)) {
225 'Base directory ' . $base . ' does not exist,
226 please create or change using %Cache.SerializerPath',
231 } elseif (!$this->_testPermissions($directory, $chmod)) {
238 * Tests permissions on a directory and throws out friendly
239 * error messages and attempts to chmod it itself if possible
240 * @param string $dir Directory path
241 * @param int $chmod Permissions
242 * @return bool True if directory is writable
244 private function _testPermissions($dir, $chmod)
246 // early abort, if it is writable, everything is hunky-dory
247 if (is_writable($dir)) {
251 // generally, you'll want to handle this beforehand
252 // so a more specific error message can be given
254 'Directory ' . $dir . ' does not exist',
259 if (function_exists('posix_getuid')) {
260 // POSIX system, we can give more specific advice
261 if (fileowner($dir) === posix_getuid()) {
262 // we can chmod it ourselves
263 $chmod = $chmod | 0700;
264 if (chmod($dir, $chmod)) {
267 } elseif (filegroup($dir) === posix_getgid()) {
268 $chmod = $chmod | 0070;
270 // PHP's probably running as nobody, so we'll
271 // need to give global permissions
272 $chmod = $chmod | 0777;
275 'Directory ' . $dir . ' not writable, ' .
276 'please chmod to ' . decoct($chmod),
280 // generic error message
282 'Directory ' . $dir . ' not writable, ' .
283 'please alter file permissions',
291 // vim: et sw=4 sts=4