]> git.immae.eu Git - github/wallabag/wallabag.git/blob - inc/3rdparty/htmlpurifier/HTMLPurifier/DefinitionCache/Serializer.php
[add] HTML Purifier added to clean code
[github/wallabag/wallabag.git] / inc / 3rdparty / htmlpurifier / HTMLPurifier / DefinitionCache / Serializer.php
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 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 }
110
111 /**
112 * @param HTMLPurifier_Config $config
113 * @return bool
114 */
115 public function cleanup($config)
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 }
135
136 /**
137 * Generates the file path to the serial file corresponding to
138 * the configuration and definition name
139 * @param HTMLPurifier_Config $config
140 * @return string
141 * @todo Make protected
142 */
143 public function generateFilePath($config)
144 {
145 $key = $this->generateKey($config);
146 return $this->generateDirectoryPath($config) . '/' . $key . '.ser';
147 }
148
149 /**
150 * Generates the path to the directory contain this cache's serial files
151 * @param HTMLPurifier_Config $config
152 * @return string
153 * @note No trailing slash
154 * @todo Make protected
155 */
156 public function generateDirectoryPath($config)
157 {
158 $base = $this->generateBaseDirectoryPath($config);
159 return $base . '/' . $this->type;
160 }
161
162 /**
163 * Generates path to base directory that contains all definition type
164 * serials
165 * @param HTMLPurifier_Config $config
166 * @return mixed|string
167 * @todo Make protected
168 */
169 public function generateBaseDirectoryPath($config)
170 {
171 $base = $config->get('Cache.SerializerPath');
172 $base = is_null($base) ? HTMLPURIFIER_PREFIX . '/HTMLPurifier/DefinitionCache/Serializer' : $base;
173 return $base;
174 }
175
176 /**
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.
182 */
183 private function _write($file, $data, $config)
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 }
197
198 /**
199 * Prepares the directory that this type stores the serials in
200 * @param HTMLPurifier_Config $config
201 * @return bool True if successful
202 */
203 private function _prepareDir($config)
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 }
230
231 /**
232 * Tests permissions on a directory and throws out friendly
233 * error messages and attempts to chmod it itself if possible
234 * @param string $dir Directory path
235 * @param int $chmod Permissions
236 * @return bool True if directory is writable
237 */
238 private function _testPermissions($dir, $chmod)
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 }
283 }
284
285 // vim: et sw=4 sts=4