Debian Codegraph @

vendor/ezyang/htmlpurifier/library/HTMLPurifier/DefinitionCache/Serializer.php

raw
1 <?php
2
3 class HTMLPurifier_DefinitionCache_Serializer extends HTMLPurifier_DefinitionCache
4 {
5
6 /**
7 * @param HTMLPurifier_Definition $def
8 * @param HTMLPurifier_Config $config
9 * @return int|bool
10 */
11 public function add($def, $config)
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 }
25
26 /**
27 * @param HTMLPurifier_Definition $def
28 * @param HTMLPurifier_Config $config
29 * @return int|bool
30 */
31 public function set($def, $config)
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 }
42
43 /**
44 * @param HTMLPurifier_Definition $def
45 * @param HTMLPurifier_Config $config
46 * @return int|bool
47 */
48 public function replace($def, $config)
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 }
62
63 /**
64 * @param HTMLPurifier_Config $config
65 * @return bool|HTMLPurifier_Config
66 */
67 public function get($config)
68 {
69 $file = $this->generateFilePath($config);
70 if (!file_exists($file)) {
71 return false;
72 }
73 return unserialize(file_get_contents($file));
74 }
75
76 /**
77 * @param HTMLPurifier_Config $config
78 * @return bool
79 */
80 public function remove($config)
81 {
82 $file = $this->generateFilePath($config);
83 if (!file_exists($file)) {
84 return false;
85 }
86 return unlink($file);
87 }
88
89 /**
90 * @param HTMLPurifier_Config $config
91 * @return bool
92 */
93 public function flush($config)
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 }
118
119 /**
120 * @param HTMLPurifier_Config $config
121 * @return bool
122 */
123 public function cleanup($config)
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 $file = $dir . '/' . $filename;
143 if ($this->isOld($key, $config) && file_exists($file)) {
144 unlink($file);
145 }
146 }
147 closedir($dh);
148 return true;
149 }
150
151 /**
152 * Generates the file path to the serial file corresponding to
153 * the configuration and definition name
154 * @param HTMLPurifier_Config $config
155 * @return string
156 * @todo Make protected
157 */
158 public function generateFilePath($config)
159 {
160 $key = $this->generateKey($config);
161 return $this->generateDirectoryPath($config) . '/' . $key . '.ser';
162 }
163
164 /**
165 * Generates the path to the directory contain this cache's serial files
166 * @param HTMLPurifier_Config $config
167 * @return string
168 * @note No trailing slash
169 * @todo Make protected
170 */
171 public function generateDirectoryPath($config)
172 {
173 $base = $this->generateBaseDirectoryPath($config);
174 return $base . '/' . $this->type;
175 }
176
177 /**
178 * Generates path to base directory that contains all definition type
179 * serials
180 * @param HTMLPurifier_Config $config
181 * @return mixed|string
182 * @todo Make protected
183 */
184 public function generateBaseDirectoryPath($config)
185 {
186 $base = $config->get('Cache.SerializerPath');
187 $base = is_null($base) ? HTMLPURIFIER_PREFIX . '/HTMLPurifier/DefinitionCache/Serializer' : $base;
188 return $base;
189 }
190
191 /**
192 * Convenience wrapper function for file_put_contents
193 * @param string $file File name to write to
194 * @param string $data Data to write into file
195 * @param HTMLPurifier_Config $config
196 * @return int|bool Number of bytes written if success, or false if failure.
197 */
198 private function _write($file, $data, $config)
199 {
200 $result = file_put_contents($file, $data);
201 if ($result !== false) {
202 // set permissions of the new file (no execute)
203 $chmod = $config->get('Cache.SerializerPermissions');
204 if ($chmod !== null) {
205 chmod($file, $chmod & 0666);
206 }
207 }
208 return $result;
209 }
210
211 /**
212 * Prepares the directory that this type stores the serials in
213 * @param HTMLPurifier_Config $config
214 * @return bool True if successful
215 */
216 private function _prepareDir($config)
217 {
218 $directory = $this->generateDirectoryPath($config);
219 $chmod = $config->get('Cache.SerializerPermissions');
220 if ($chmod === null) {
221 if (!@mkdir($directory) && !is_dir($directory)) {
222 trigger_error(
223 'Could not create directory ' . $directory . '',
224 E_USER_WARNING
225 );
226 return false;
227 }
228 return true;
229 }
230 if (!is_dir($directory)) {
231 $base = $this->generateBaseDirectoryPath($config);
232 if (!is_dir($base)) {
233 trigger_error(
234 'Base directory ' . $base . ' does not exist,
235 please create or change using %Cache.SerializerPath',
236 E_USER_WARNING
237 );
238 return false;
239 } elseif (!$this->_testPermissions($base, $chmod)) {
240 return false;
241 }
242 if (!@mkdir($directory, $chmod) && !is_dir($directory)) {
243 trigger_error(
244 'Could not create directory ' . $directory . '',
245 E_USER_WARNING
246 );
247 return false;
248 }
249 if (!$this->_testPermissions($directory, $chmod)) {
250 return false;
251 }
252 } elseif (!$this->_testPermissions($directory, $chmod)) {
253 return false;
254 }
255 return true;
256 }
257
258 /**
259 * Tests permissions on a directory and throws out friendly
260 * error messages and attempts to chmod it itself if possible
261 * @param string $dir Directory path
262 * @param int $chmod Permissions
263 * @return bool True if directory is writable
264 */
265 private function _testPermissions($dir, $chmod)
266 {
267 // early abort, if it is writable, everything is hunky-dory
268 if (is_writable($dir)) {
269 return true;
270 }
271 if (!is_dir($dir)) {
272 // generally, you'll want to handle this beforehand
273 // so a more specific error message can be given
274 trigger_error(
275 'Directory ' . $dir . ' does not exist',
276 E_USER_WARNING
277 );
278 return false;
279 }
280 if (function_exists('posix_getuid') && $chmod !== null) {
281 // POSIX system, we can give more specific advice
282 if (fileowner($dir) === posix_getuid()) {
283 // we can chmod it ourselves
284 $chmod = $chmod | 0700;
285 if (chmod($dir, $chmod)) {
286 return true;
287 }
288 } elseif (filegroup($dir) === posix_getgid()) {
289 $chmod = $chmod | 0070;
290 } else {
291 // PHP's probably running as nobody, it is
292 // not obvious how to fix this (777 is probably
293 // bad if you are multi-user), let the user figure it out
294 $chmod = null;
295 }
296 trigger_error(
297 'Directory ' . $dir . ' not writable. ' .
298 ($chmod === null ? '' : 'Please chmod to ' . decoct($chmod)),
299 E_USER_WARNING
300 );
301 } else {
302 // generic error message
303 trigger_error(
304 'Directory ' . $dir . ' not writable, ' .
305 'please alter file permissions',
306 E_USER_WARNING
307 );
308 }
309 return false;
310 }
311 }
312
313 // vim: et sw=4 sts=4