aboutsummaryrefslogtreecommitdiffhomepage
path: root/inc/3rdparty/htmlpurifier/HTMLPurifier/DefinitionCache
diff options
context:
space:
mode:
authorNicolas LÅ“uillet <nicolas.loeuillet@gmail.com>2014-02-21 15:43:14 +0100
committerNicolas LÅ“uillet <nicolas.loeuillet@gmail.com>2014-02-21 15:43:14 +0100
commitd4949327efa15b492cab1bef3fe074290a328a17 (patch)
treee89e0322bb1f1b06d663fd10fdded21bac867e5d /inc/3rdparty/htmlpurifier/HTMLPurifier/DefinitionCache
parentc9bd17a1007bb78e5de0775efca01df0fb515031 (diff)
downloadwallabag-d4949327efa15b492cab1bef3fe074290a328a17.tar.gz
wallabag-d4949327efa15b492cab1bef3fe074290a328a17.tar.zst
wallabag-d4949327efa15b492cab1bef3fe074290a328a17.zip
[add] HTML Purifier added to clean code
Diffstat (limited to 'inc/3rdparty/htmlpurifier/HTMLPurifier/DefinitionCache')
-rw-r--r--inc/3rdparty/htmlpurifier/HTMLPurifier/DefinitionCache/Decorator.php112
-rw-r--r--inc/3rdparty/htmlpurifier/HTMLPurifier/DefinitionCache/Decorator/Cleanup.php78
-rw-r--r--inc/3rdparty/htmlpurifier/HTMLPurifier/DefinitionCache/Decorator/Memory.php85
-rw-r--r--inc/3rdparty/htmlpurifier/HTMLPurifier/DefinitionCache/Decorator/Template.php.in82
-rw-r--r--inc/3rdparty/htmlpurifier/HTMLPurifier/DefinitionCache/Null.php76
-rw-r--r--inc/3rdparty/htmlpurifier/HTMLPurifier/DefinitionCache/Serializer.php285
-rwxr-xr-xinc/3rdparty/htmlpurifier/HTMLPurifier/DefinitionCache/Serializer/README3
7 files changed, 721 insertions, 0 deletions
diff --git a/inc/3rdparty/htmlpurifier/HTMLPurifier/DefinitionCache/Decorator.php b/inc/3rdparty/htmlpurifier/HTMLPurifier/DefinitionCache/Decorator.php
new file mode 100644
index 00000000..09cdc1c8
--- /dev/null
+++ b/inc/3rdparty/htmlpurifier/HTMLPurifier/DefinitionCache/Decorator.php
@@ -0,0 +1,112 @@
1<?php
2
3class HTMLPurifier_DefinitionCache_Decorator extends HTMLPurifier_DefinitionCache
4{
5
6 /**
7 * Cache object we are decorating
8 * @type HTMLPurifier_DefinitionCache
9 */
10 public $cache;
11
12 /**
13 * The name of the decorator
14 * @var string
15 */
16 public $name;
17
18 public function __construct()
19 {
20 }
21
22 /**
23 * Lazy decorator function
24 * @param HTMLPurifier_DefinitionCache $cache Reference to cache object to decorate
25 * @return HTMLPurifier_DefinitionCache_Decorator
26 */
27 public function decorate(&$cache)
28 {
29 $decorator = $this->copy();
30 // reference is necessary for mocks in PHP 4
31 $decorator->cache =& $cache;
32 $decorator->type = $cache->type;
33 return $decorator;
34 }
35
36 /**
37 * Cross-compatible clone substitute
38 * @return HTMLPurifier_DefinitionCache_Decorator
39 */
40 public function copy()
41 {
42 return new HTMLPurifier_DefinitionCache_Decorator();
43 }
44
45 /**
46 * @param HTMLPurifier_Definition $def
47 * @param HTMLPurifier_Config $config
48 * @return mixed
49 */
50 public function add($def, $config)
51 {
52 return $this->cache->add($def, $config);
53 }
54
55 /**
56 * @param HTMLPurifier_Definition $def
57 * @param HTMLPurifier_Config $config
58 * @return mixed
59 */
60 public function set($def, $config)
61 {
62 return $this->cache->set($def, $config);
63 }
64
65 /**
66 * @param HTMLPurifier_Definition $def
67 * @param HTMLPurifier_Config $config
68 * @return mixed
69 */
70 public function replace($def, $config)
71 {
72 return $this->cache->replace($def, $config);
73 }
74
75 /**
76 * @param HTMLPurifier_Config $config
77 * @return mixed
78 */
79 public function get($config)
80 {
81 return $this->cache->get($config);
82 }
83
84 /**
85 * @param HTMLPurifier_Config $config
86 * @return mixed
87 */
88 public function remove($config)
89 {
90 return $this->cache->remove($config);
91 }
92
93 /**
94 * @param HTMLPurifier_Config $config
95 * @return mixed
96 */
97 public function flush($config)
98 {
99 return $this->cache->flush($config);
100 }
101
102 /**
103 * @param HTMLPurifier_Config $config
104 * @return mixed
105 */
106 public function cleanup($config)
107 {
108 return $this->cache->cleanup($config);
109 }
110}
111
112// vim: et sw=4 sts=4
diff --git a/inc/3rdparty/htmlpurifier/HTMLPurifier/DefinitionCache/Decorator/Cleanup.php b/inc/3rdparty/htmlpurifier/HTMLPurifier/DefinitionCache/Decorator/Cleanup.php
new file mode 100644
index 00000000..53dca67d
--- /dev/null
+++ b/inc/3rdparty/htmlpurifier/HTMLPurifier/DefinitionCache/Decorator/Cleanup.php
@@ -0,0 +1,78 @@
1<?php
2
3/**
4 * Definition cache decorator class that cleans up the cache
5 * whenever there is a cache miss.
6 */
7class HTMLPurifier_DefinitionCache_Decorator_Cleanup extends HTMLPurifier_DefinitionCache_Decorator
8{
9 /**
10 * @type string
11 */
12 public $name = 'Cleanup';
13
14 /**
15 * @return HTMLPurifier_DefinitionCache_Decorator_Cleanup
16 */
17 public function copy()
18 {
19 return new HTMLPurifier_DefinitionCache_Decorator_Cleanup();
20 }
21
22 /**
23 * @param HTMLPurifier_Definition $def
24 * @param HTMLPurifier_Config $config
25 * @return mixed
26 */
27 public function add($def, $config)
28 {
29 $status = parent::add($def, $config);
30 if (!$status) {
31 parent::cleanup($config);
32 }
33 return $status;
34 }
35
36 /**
37 * @param HTMLPurifier_Definition $def
38 * @param HTMLPurifier_Config $config
39 * @return mixed
40 */
41 public function set($def, $config)
42 {
43 $status = parent::set($def, $config);
44 if (!$status) {
45 parent::cleanup($config);
46 }
47 return $status;
48 }
49
50 /**
51 * @param HTMLPurifier_Definition $def
52 * @param HTMLPurifier_Config $config
53 * @return mixed
54 */
55 public function replace($def, $config)
56 {
57 $status = parent::replace($def, $config);
58 if (!$status) {
59 parent::cleanup($config);
60 }
61 return $status;
62 }
63
64 /**
65 * @param HTMLPurifier_Config $config
66 * @return mixed
67 */
68 public function get($config)
69 {
70 $ret = parent::get($config);
71 if (!$ret) {
72 parent::cleanup($config);
73 }
74 return $ret;
75 }
76}
77
78// vim: et sw=4 sts=4
diff --git a/inc/3rdparty/htmlpurifier/HTMLPurifier/DefinitionCache/Decorator/Memory.php b/inc/3rdparty/htmlpurifier/HTMLPurifier/DefinitionCache/Decorator/Memory.php
new file mode 100644
index 00000000..1725d830
--- /dev/null
+++ b/inc/3rdparty/htmlpurifier/HTMLPurifier/DefinitionCache/Decorator/Memory.php
@@ -0,0 +1,85 @@
1<?php
2
3/**
4 * Definition cache decorator class that saves all cache retrievals
5 * to PHP's memory; good for unit tests or circumstances where
6 * there are lots of configuration objects floating around.
7 */
8class HTMLPurifier_DefinitionCache_Decorator_Memory extends HTMLPurifier_DefinitionCache_Decorator
9{
10 /**
11 * @type array
12 */
13 protected $definitions;
14
15 /**
16 * @type string
17 */
18 public $name = 'Memory';
19
20 /**
21 * @return HTMLPurifier_DefinitionCache_Decorator_Memory
22 */
23 public function copy()
24 {
25 return new HTMLPurifier_DefinitionCache_Decorator_Memory();
26 }
27
28 /**
29 * @param HTMLPurifier_Definition $def
30 * @param HTMLPurifier_Config $config
31 * @return mixed
32 */
33 public function add($def, $config)
34 {
35 $status = parent::add($def, $config);
36 if ($status) {
37 $this->definitions[$this->generateKey($config)] = $def;
38 }
39 return $status;
40 }
41
42 /**
43 * @param HTMLPurifier_Definition $def
44 * @param HTMLPurifier_Config $config
45 * @return mixed
46 */
47 public function set($def, $config)
48 {
49 $status = parent::set($def, $config);
50 if ($status) {
51 $this->definitions[$this->generateKey($config)] = $def;
52 }
53 return $status;
54 }
55
56 /**
57 * @param HTMLPurifier_Definition $def
58 * @param HTMLPurifier_Config $config
59 * @return mixed
60 */
61 public function replace($def, $config)
62 {
63 $status = parent::replace($def, $config);
64 if ($status) {
65 $this->definitions[$this->generateKey($config)] = $def;
66 }
67 return $status;
68 }
69
70 /**
71 * @param HTMLPurifier_Config $config
72 * @return mixed
73 */
74 public function get($config)
75 {
76 $key = $this->generateKey($config);
77 if (isset($this->definitions[$key])) {
78 return $this->definitions[$key];
79 }
80 $this->definitions[$key] = parent::get($config);
81 return $this->definitions[$key];
82 }
83}
84
85// vim: et sw=4 sts=4
diff --git a/inc/3rdparty/htmlpurifier/HTMLPurifier/DefinitionCache/Decorator/Template.php.in b/inc/3rdparty/htmlpurifier/HTMLPurifier/DefinitionCache/Decorator/Template.php.in
new file mode 100644
index 00000000..c586890f
--- /dev/null
+++ b/inc/3rdparty/htmlpurifier/HTMLPurifier/DefinitionCache/Decorator/Template.php.in
@@ -0,0 +1,82 @@
1<?php
2
3require_once 'HTMLPurifier/DefinitionCache/Decorator.php';
4
5/**
6 * Definition cache decorator template.
7 */
8class HTMLPurifier_DefinitionCache_Decorator_Template extends HTMLPurifier_DefinitionCache_Decorator
9{
10
11 /**
12 * @type string
13 */
14 public $name = 'Template'; // replace this
15
16 public function copy()
17 {
18 // replace class name with yours
19 return new HTMLPurifier_DefinitionCache_Decorator_Template();
20 }
21
22 // remove methods you don't need
23
24 /**
25 * @param HTMLPurifier_Definition $def
26 * @param HTMLPurifier_Config $config
27 * @return mixed
28 */
29 public function add($def, $config)
30 {
31 return parent::add($def, $config);
32 }
33
34 /**
35 * @param HTMLPurifier_Definition $def
36 * @param HTMLPurifier_Config $config
37 * @return mixed
38 */
39 public function set($def, $config)
40 {
41 return parent::set($def, $config);
42 }
43
44 /**
45 * @param HTMLPurifier_Definition $def
46 * @param HTMLPurifier_Config $config
47 * @return mixed
48 */
49 public function replace($def, $config)
50 {
51 return parent::replace($def, $config);
52 }
53
54 /**
55 * @param HTMLPurifier_Config $config
56 * @return mixed
57 */
58 public function get($config)
59 {
60 return parent::get($config);
61 }
62
63 /**
64 * @param HTMLPurifier_Config $config
65 * @return mixed
66 */
67 public function flush($config)
68 {
69 return parent::flush($config);
70 }
71
72 /**
73 * @param HTMLPurifier_Config $config
74 * @return mixed
75 */
76 public function cleanup($config)
77 {
78 return parent::cleanup($config);
79 }
80}
81
82// vim: et sw=4 sts=4
diff --git a/inc/3rdparty/htmlpurifier/HTMLPurifier/DefinitionCache/Null.php b/inc/3rdparty/htmlpurifier/HTMLPurifier/DefinitionCache/Null.php
new file mode 100644
index 00000000..55eff9e0
--- /dev/null
+++ b/inc/3rdparty/htmlpurifier/HTMLPurifier/DefinitionCache/Null.php
@@ -0,0 +1,76 @@
1<?php
2
3/**
4 * Null cache object to use when no caching is on.
5 */
6class HTMLPurifier_DefinitionCache_Null extends HTMLPurifier_DefinitionCache
7{
8
9 /**
10 * @param HTMLPurifier_Definition $def
11 * @param HTMLPurifier_Config $config
12 * @return bool
13 */
14 public function add($def, $config)
15 {
16 return false;
17 }
18
19 /**
20 * @param HTMLPurifier_Definition $def
21 * @param HTMLPurifier_Config $config
22 * @return bool
23 */
24 public function set($def, $config)
25 {
26 return false;
27 }
28
29 /**
30 * @param HTMLPurifier_Definition $def
31 * @param HTMLPurifier_Config $config
32 * @return bool
33 */
34 public function replace($def, $config)
35 {
36 return false;
37 }
38
39 /**
40 * @param HTMLPurifier_Config $config
41 * @return bool
42 */
43 public function remove($config)
44 {
45 return false;
46 }
47
48 /**
49 * @param HTMLPurifier_Config $config
50 * @return bool
51 */
52 public function get($config)
53 {
54 return false;
55 }
56
57 /**
58 * @param HTMLPurifier_Config $config
59 * @return bool
60 */
61 public function flush($config)
62 {
63 return false;
64 }
65
66 /**
67 * @param HTMLPurifier_Config $config
68 * @return bool
69 */
70 public function cleanup($config)
71 {
72 return false;
73 }
74}
75
76// vim: et sw=4 sts=4
diff --git a/inc/3rdparty/htmlpurifier/HTMLPurifier/DefinitionCache/Serializer.php b/inc/3rdparty/htmlpurifier/HTMLPurifier/DefinitionCache/Serializer.php
new file mode 100644
index 00000000..7e8539ad
--- /dev/null
+++ b/inc/3rdparty/htmlpurifier/HTMLPurifier/DefinitionCache/Serializer.php
@@ -0,0 +1,285 @@
1<?php
2
3class 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
diff --git a/inc/3rdparty/htmlpurifier/HTMLPurifier/DefinitionCache/Serializer/README b/inc/3rdparty/htmlpurifier/HTMLPurifier/DefinitionCache/Serializer/README
new file mode 100755
index 00000000..ba005de7
--- /dev/null
+++ b/inc/3rdparty/htmlpurifier/HTMLPurifier/DefinitionCache/Serializer/README
@@ -0,0 +1,3 @@
1This is a dummy file to prevent Git from ignoring this empty directory.
2
3 vim: et sw=4 sts=4