@@ -60,12 +60,10 @@ discard block |
||
60 | 60 | if($use_context) |
61 | 61 | { |
62 | 62 | $var = Context::get($varName0); |
63 | - } |
|
64 | - elseif($varName0) |
|
63 | + } elseif($varName0) |
|
65 | 64 | { |
66 | 65 | $var = $is_object ? $this->_targetVar->{$varName0} : $this->_targetVar[$varName0]; |
67 | - } |
|
68 | - else |
|
66 | + } else |
|
69 | 67 | { |
70 | 68 | $var = $this->_targetVar; |
71 | 69 | } |
@@ -79,19 +77,16 @@ discard block |
||
79 | 77 | if($use_context) |
80 | 78 | { |
81 | 79 | Context::set($varName0, $var); |
82 | - } |
|
83 | - elseif($varName0) |
|
80 | + } elseif($varName0) |
|
84 | 81 | { |
85 | 82 | if($is_object) |
86 | 83 | { |
87 | 84 | $this->_targetVar->{$varName0} = $var; |
88 | - } |
|
89 | - else |
|
85 | + } else |
|
90 | 86 | { |
91 | 87 | $this->_targetVar[$varName0] = $var; |
92 | 88 | } |
93 | - } |
|
94 | - else |
|
89 | + } else |
|
95 | 90 | { |
96 | 91 | $this->_targetVar = $var; |
97 | 92 | } |
@@ -142,8 +137,7 @@ discard block |
||
142 | 137 | if($is_object) |
143 | 138 | { |
144 | 139 | $var->{$name0} = $target; |
145 | - } |
|
146 | - else |
|
140 | + } else |
|
147 | 141 | { |
148 | 142 | $var[$name0] = $target; |
149 | 143 | } |
@@ -165,8 +159,7 @@ discard block |
||
165 | 159 | if($is_object) |
166 | 160 | { |
167 | 161 | $var->{$key} = $target; |
168 | - } |
|
169 | - else |
|
162 | + } else |
|
170 | 163 | { |
171 | 164 | $var[$key] = $target; |
172 | 165 | } |
@@ -185,7 +178,9 @@ discard block |
||
185 | 178 | */ |
186 | 179 | static function detectingXEE($xml) |
187 | 180 | { |
188 | - if(!$xml) return FALSE; |
|
181 | + if(!$xml) { |
|
182 | + return FALSE; |
|
183 | + } |
|
189 | 184 | |
190 | 185 | if(strpos($xml, '<!ENTITY') !== FALSE) |
191 | 186 | { |
@@ -37,15 +37,15 @@ discard block |
||
37 | 37 | function encodeHTML(/* , $varName1, $varName2, ... */) |
38 | 38 | { |
39 | 39 | $varNames = func_get_args(); |
40 | - if(count($varNames) < 0) |
|
40 | + if (count($varNames) < 0) |
|
41 | 41 | { |
42 | 42 | return FALSE; |
43 | 43 | } |
44 | 44 | |
45 | 45 | $use_context = is_null($this->_targetVar); |
46 | - if(!$use_context) |
|
46 | + if (!$use_context) |
|
47 | 47 | { |
48 | - if(!count($varNames) || (!is_object($this->_targetVar) && !is_array($this->_targetVar))) |
|
48 | + if (!count($varNames) || (!is_object($this->_targetVar) && !is_array($this->_targetVar))) |
|
49 | 49 | { |
50 | 50 | return $this->_encodeHTML($this->_targetVar); |
51 | 51 | } |
@@ -53,15 +53,15 @@ discard block |
||
53 | 53 | $is_object = is_object($this->_targetVar); |
54 | 54 | } |
55 | 55 | |
56 | - foreach($varNames as $varName) |
|
56 | + foreach ($varNames as $varName) |
|
57 | 57 | { |
58 | 58 | $varName = explode('.', $varName); |
59 | 59 | $varName0 = array_shift($varName); |
60 | - if($use_context) |
|
60 | + if ($use_context) |
|
61 | 61 | { |
62 | 62 | $var = Context::get($varName0); |
63 | 63 | } |
64 | - elseif($varName0) |
|
64 | + elseif ($varName0) |
|
65 | 65 | { |
66 | 66 | $var = $is_object ? $this->_targetVar->{$varName0} : $this->_targetVar[$varName0]; |
67 | 67 | } |
@@ -71,18 +71,18 @@ discard block |
||
71 | 71 | } |
72 | 72 | $var = $this->_encodeHTML($var, $varName); |
73 | 73 | |
74 | - if($var === FALSE) |
|
74 | + if ($var === FALSE) |
|
75 | 75 | { |
76 | 76 | continue; |
77 | 77 | } |
78 | 78 | |
79 | - if($use_context) |
|
79 | + if ($use_context) |
|
80 | 80 | { |
81 | 81 | Context::set($varName0, $var); |
82 | 82 | } |
83 | - elseif($varName0) |
|
83 | + elseif ($varName0) |
|
84 | 84 | { |
85 | - if($is_object) |
|
85 | + if ($is_object) |
|
86 | 86 | { |
87 | 87 | $this->_targetVar->{$varName0} = $var; |
88 | 88 | } |
@@ -97,7 +97,7 @@ discard block |
||
97 | 97 | } |
98 | 98 | } |
99 | 99 | |
100 | - if(!$use_context) |
|
100 | + if (!$use_context) |
|
101 | 101 | { |
102 | 102 | return $this->_targetVar; |
103 | 103 | } |
@@ -111,9 +111,9 @@ discard block |
||
111 | 111 | */ |
112 | 112 | function _encodeHTML($var, $name = array()) |
113 | 113 | { |
114 | - if(is_string($var)) |
|
114 | + if (is_string($var)) |
|
115 | 115 | { |
116 | - if(strncmp('$user_lang->', $var, 12) !== 0) |
|
116 | + if (strncmp('$user_lang->', $var, 12) !== 0) |
|
117 | 117 | { |
118 | 118 | $var = htmlspecialchars($var, ENT_COMPAT | ENT_HTML401, 'UTF-8', false); |
119 | 119 | } |
@@ -121,7 +121,7 @@ discard block |
||
121 | 121 | return $var; |
122 | 122 | } |
123 | 123 | |
124 | - if(!count($name) || (!is_array($var) && !is_object($var))) |
|
124 | + if (!count($name) || (!is_array($var) && !is_object($var))) |
|
125 | 125 | { |
126 | 126 | return false; |
127 | 127 | } |
@@ -129,17 +129,17 @@ discard block |
||
129 | 129 | $is_object = is_object($var); |
130 | 130 | $name0 = array_shift($name); |
131 | 131 | |
132 | - if(strlen($name0)) |
|
132 | + if (strlen($name0)) |
|
133 | 133 | { |
134 | 134 | $target = $is_object ? $var->{$name0} : $var[$name0]; |
135 | 135 | $target = $this->_encodeHTML($target, $name); |
136 | 136 | |
137 | - if($target === false) |
|
137 | + if ($target === false) |
|
138 | 138 | { |
139 | 139 | return $var; |
140 | 140 | } |
141 | 141 | |
142 | - if($is_object) |
|
142 | + if ($is_object) |
|
143 | 143 | { |
144 | 144 | $var->{$name0} = $target; |
145 | 145 | } |
@@ -151,18 +151,18 @@ discard block |
||
151 | 151 | return $var; |
152 | 152 | } |
153 | 153 | |
154 | - foreach($var as $key => $target) |
|
154 | + foreach ($var as $key => $target) |
|
155 | 155 | { |
156 | 156 | $cloned_name = array_slice($name, 0); |
157 | 157 | $target = $this->_encodeHTML($target, $name); |
158 | 158 | $name = $cloned_name; |
159 | 159 | |
160 | - if($target === false) |
|
160 | + if ($target === false) |
|
161 | 161 | { |
162 | 162 | continue; |
163 | 163 | } |
164 | 164 | |
165 | - if($is_object) |
|
165 | + if ($is_object) |
|
166 | 166 | { |
167 | 167 | $var->{$key} = $target; |
168 | 168 | } |
@@ -185,9 +185,9 @@ discard block |
||
185 | 185 | */ |
186 | 186 | static function detectingXEE($xml) |
187 | 187 | { |
188 | - if(!$xml) return FALSE; |
|
188 | + if (!$xml) return FALSE; |
|
189 | 189 | |
190 | - if(strpos($xml, '<!ENTITY') !== FALSE) |
|
190 | + if (strpos($xml, '<!ENTITY') !== FALSE) |
|
191 | 191 | { |
192 | 192 | return TRUE; |
193 | 193 | } |
@@ -195,7 +195,7 @@ discard block |
||
195 | 195 | // Strip XML declaration. |
196 | 196 | $header = preg_replace('/<\?xml.*?\?'.'>/s', '', substr($xml, 0, 100), 1); |
197 | 197 | $xml = trim(substr_replace($xml, $header, 0, 100)); |
198 | - if($xml == '') |
|
198 | + if ($xml == '') |
|
199 | 199 | { |
200 | 200 | return TRUE; |
201 | 201 | } |
@@ -203,7 +203,7 @@ discard block |
||
203 | 203 | // Strip DTD. |
204 | 204 | $header = preg_replace('/^<!DOCTYPE[^>]*+>/i', '', substr($xml, 0, 200), 1); |
205 | 205 | $xml = trim(substr_replace($xml, $header, 0, 200)); |
206 | - if($xml == '') |
|
206 | + if ($xml == '') |
|
207 | 207 | { |
208 | 208 | return TRUE; |
209 | 209 | } |
@@ -212,12 +212,12 @@ discard block |
||
212 | 212 | $root_tag = substr($xml, 0, strcspn(substr($xml, 0, 20), "> \t\r\n")); |
213 | 213 | |
214 | 214 | // Reject a second DTD. |
215 | - if(strtoupper($root_tag) == '<!DOCTYPE') |
|
215 | + if (strtoupper($root_tag) == '<!DOCTYPE') |
|
216 | 216 | { |
217 | 217 | return TRUE; |
218 | 218 | } |
219 | 219 | |
220 | - if(!in_array($root_tag, array('<methodCall', '<methodResponse', '<fault'))) |
|
220 | + if (!in_array($root_tag, array('<methodCall', '<methodResponse', '<fault'))) |
|
221 | 221 | { |
222 | 222 | return TRUE; |
223 | 223 | } |
@@ -7,20 +7,20 @@ |
||
7 | 7 | */ |
8 | 8 | |
9 | 9 | if (function_exists('spl_autoload_register') && function_exists('spl_autoload_unregister')) { |
10 | - // We need unregister for our pre-registering functionality |
|
11 | - HTMLPurifier_Bootstrap::registerAutoload(); |
|
12 | - if (function_exists('__autoload')) { |
|
13 | - // Be polite and ensure that userland autoload gets retained |
|
14 | - spl_autoload_register('__autoload'); |
|
15 | - } |
|
10 | + // We need unregister for our pre-registering functionality |
|
11 | + HTMLPurifier_Bootstrap::registerAutoload(); |
|
12 | + if (function_exists('__autoload')) { |
|
13 | + // Be polite and ensure that userland autoload gets retained |
|
14 | + spl_autoload_register('__autoload'); |
|
15 | + } |
|
16 | 16 | } elseif (!function_exists('__autoload')) { |
17 | - function __autoload($class) { |
|
18 | - return HTMLPurifier_Bootstrap::autoload($class); |
|
19 | - } |
|
17 | + function __autoload($class) { |
|
18 | + return HTMLPurifier_Bootstrap::autoload($class); |
|
19 | + } |
|
20 | 20 | } |
21 | 21 | |
22 | 22 | if (ini_get('zend.ze1_compatibility_mode')) { |
23 | - trigger_error("HTML Purifier is not compatible with zend.ze1_compatibility_mode; please turn it off", E_USER_ERROR); |
|
23 | + trigger_error("HTML Purifier is not compatible with zend.ze1_compatibility_mode; please turn it off", E_USER_ERROR); |
|
24 | 24 | } |
25 | 25 | |
26 | 26 | // vim: et sw=4 sts=4 |
@@ -13,11 +13,11 @@ |
||
13 | 13 | * HTMLPurifier_Config::create() |
14 | 14 | */ |
15 | 15 | function HTMLPurifier($html, $config = null) { |
16 | - static $purifier = false; |
|
17 | - if (!$purifier) { |
|
18 | - $purifier = new HTMLPurifier(); |
|
19 | - } |
|
20 | - return $purifier->purify($html, $config); |
|
16 | + static $purifier = false; |
|
17 | + if (!$purifier) { |
|
18 | + $purifier = new HTMLPurifier(); |
|
19 | + } |
|
20 | + return $purifier->purify($html, $config); |
|
21 | 21 | } |
22 | 22 | |
23 | 23 | // vim: et sw=4 sts=4 |
@@ -8,23 +8,23 @@ |
||
8 | 8 | require_once dirname(__FILE__) . '/HTMLPurifier.auto.php'; |
9 | 9 | |
10 | 10 | function kses($string, $allowed_html, $allowed_protocols = null) { |
11 | - $config = HTMLPurifier_Config::createDefault(); |
|
12 | - $allowed_elements = array(); |
|
13 | - $allowed_attributes = array(); |
|
14 | - foreach ($allowed_html as $element => $attributes) { |
|
15 | - $allowed_elements[$element] = true; |
|
16 | - foreach ($attributes as $attribute => $x) { |
|
17 | - $allowed_attributes["$element.$attribute"] = true; |
|
18 | - } |
|
19 | - } |
|
20 | - $config->set('HTML.AllowedElements', $allowed_elements); |
|
21 | - $config->set('HTML.AllowedAttributes', $allowed_attributes); |
|
22 | - $allowed_schemes = array(); |
|
23 | - if ($allowed_protocols !== null) { |
|
24 | - $config->set('URI.AllowedSchemes', $allowed_protocols); |
|
25 | - } |
|
26 | - $purifier = new HTMLPurifier($config); |
|
27 | - return $purifier->purify($string); |
|
11 | + $config = HTMLPurifier_Config::createDefault(); |
|
12 | + $allowed_elements = array(); |
|
13 | + $allowed_attributes = array(); |
|
14 | + foreach ($allowed_html as $element => $attributes) { |
|
15 | + $allowed_elements[$element] = true; |
|
16 | + foreach ($attributes as $attribute => $x) { |
|
17 | + $allowed_attributes["$element.$attribute"] = true; |
|
18 | + } |
|
19 | + } |
|
20 | + $config->set('HTML.AllowedElements', $allowed_elements); |
|
21 | + $config->set('HTML.AllowedAttributes', $allowed_attributes); |
|
22 | + $allowed_schemes = array(); |
|
23 | + if ($allowed_protocols !== null) { |
|
24 | + $config->set('URI.AllowedSchemes', $allowed_protocols); |
|
25 | + } |
|
26 | + $purifier = new HTMLPurifier($config); |
|
27 | + return $purifier->purify($string); |
|
28 | 28 | } |
29 | 29 | |
30 | 30 | // vim: et sw=4 sts=4 |
@@ -5,7 +5,7 @@ |
||
5 | 5 | * Emulation layer for code that used kses(), substituting in HTML Purifier. |
6 | 6 | */ |
7 | 7 | |
8 | -require_once dirname(__FILE__) . '/HTMLPurifier.auto.php'; |
|
8 | +require_once dirname(__FILE__).'/HTMLPurifier.auto.php'; |
|
9 | 9 | |
10 | 10 | function kses($string, $allowed_html, $allowed_protocols = null) { |
11 | 11 | $config = HTMLPurifier_Config::createDefault(); |
@@ -54,183 +54,183 @@ |
||
54 | 54 | class HTMLPurifier |
55 | 55 | { |
56 | 56 | |
57 | - /** Version of HTML Purifier */ |
|
58 | - public $version = '4.4.0'; |
|
59 | - |
|
60 | - /** Constant with version of HTML Purifier */ |
|
61 | - const VERSION = '4.4.0'; |
|
62 | - |
|
63 | - /** Global configuration object */ |
|
64 | - public $config; |
|
65 | - |
|
66 | - /** Array of extra HTMLPurifier_Filter objects to run on HTML, for backwards compatibility */ |
|
67 | - private $filters = array(); |
|
68 | - |
|
69 | - /** Single instance of HTML Purifier */ |
|
70 | - private static $instance; |
|
71 | - |
|
72 | - protected $strategy, $generator; |
|
73 | - |
|
74 | - /** |
|
75 | - * Resultant HTMLPurifier_Context of last run purification. Is an array |
|
76 | - * of contexts if the last called method was purifyArray(). |
|
77 | - */ |
|
78 | - public $context; |
|
79 | - |
|
80 | - /** |
|
81 | - * Initializes the purifier. |
|
82 | - * @param $config Optional HTMLPurifier_Config object for all instances of |
|
83 | - * the purifier, if omitted, a default configuration is |
|
84 | - * supplied (which can be overridden on a per-use basis). |
|
85 | - * The parameter can also be any type that |
|
86 | - * HTMLPurifier_Config::create() supports. |
|
87 | - */ |
|
88 | - public function __construct($config = null) { |
|
89 | - |
|
90 | - $this->config = HTMLPurifier_Config::create($config); |
|
91 | - |
|
92 | - $this->strategy = new HTMLPurifier_Strategy_Core(); |
|
93 | - |
|
94 | - } |
|
95 | - |
|
96 | - /** |
|
97 | - * Adds a filter to process the output. First come first serve |
|
98 | - * @param $filter HTMLPurifier_Filter object |
|
99 | - */ |
|
100 | - public function addFilter($filter) { |
|
101 | - trigger_error('HTMLPurifier->addFilter() is deprecated, use configuration directives in the Filter namespace or Filter.Custom', E_USER_WARNING); |
|
102 | - $this->filters[] = $filter; |
|
103 | - } |
|
104 | - |
|
105 | - /** |
|
106 | - * Filters an HTML snippet/document to be XSS-free and standards-compliant. |
|
107 | - * |
|
108 | - * @param $html String of HTML to purify |
|
109 | - * @param $config HTMLPurifier_Config object for this operation, if omitted, |
|
110 | - * defaults to the config object specified during this |
|
111 | - * object's construction. The parameter can also be any type |
|
112 | - * that HTMLPurifier_Config::create() supports. |
|
113 | - * @return Purified HTML |
|
114 | - */ |
|
115 | - public function purify($html, $config = null) { |
|
116 | - |
|
117 | - // :TODO: make the config merge in, instead of replace |
|
118 | - $config = $config ? HTMLPurifier_Config::create($config) : $this->config; |
|
119 | - |
|
120 | - // implementation is partially environment dependant, partially |
|
121 | - // configuration dependant |
|
122 | - $lexer = HTMLPurifier_Lexer::create($config); |
|
123 | - |
|
124 | - $context = new HTMLPurifier_Context(); |
|
125 | - |
|
126 | - // setup HTML generator |
|
127 | - $this->generator = new HTMLPurifier_Generator($config, $context); |
|
128 | - $context->register('Generator', $this->generator); |
|
129 | - |
|
130 | - // set up global context variables |
|
131 | - if ($config->get('Core.CollectErrors')) { |
|
132 | - // may get moved out if other facilities use it |
|
133 | - $language_factory = HTMLPurifier_LanguageFactory::instance(); |
|
134 | - $language = $language_factory->create($config, $context); |
|
135 | - $context->register('Locale', $language); |
|
136 | - |
|
137 | - $error_collector = new HTMLPurifier_ErrorCollector($context); |
|
138 | - $context->register('ErrorCollector', $error_collector); |
|
139 | - } |
|
140 | - |
|
141 | - // setup id_accumulator context, necessary due to the fact that |
|
142 | - // AttrValidator can be called from many places |
|
143 | - $id_accumulator = HTMLPurifier_IDAccumulator::build($config, $context); |
|
144 | - $context->register('IDAccumulator', $id_accumulator); |
|
145 | - |
|
146 | - $html = HTMLPurifier_Encoder::convertToUTF8($html, $config, $context); |
|
147 | - |
|
148 | - // setup filters |
|
149 | - $filter_flags = $config->getBatch('Filter'); |
|
150 | - $custom_filters = $filter_flags['Custom']; |
|
151 | - unset($filter_flags['Custom']); |
|
152 | - $filters = array(); |
|
153 | - foreach ($filter_flags as $filter => $flag) { |
|
154 | - if (!$flag) continue; |
|
155 | - if (strpos($filter, '.') !== false) continue; |
|
156 | - $class = "HTMLPurifier_Filter_$filter"; |
|
157 | - $filters[] = new $class; |
|
158 | - } |
|
159 | - foreach ($custom_filters as $filter) { |
|
160 | - // maybe "HTMLPurifier_Filter_$filter", but be consistent with AutoFormat |
|
161 | - $filters[] = $filter; |
|
162 | - } |
|
163 | - $filters = array_merge($filters, $this->filters); |
|
164 | - // maybe prepare(), but later |
|
165 | - |
|
166 | - for ($i = 0, $filter_size = count($filters); $i < $filter_size; $i++) { |
|
167 | - $html = $filters[$i]->preFilter($html, $config, $context); |
|
168 | - } |
|
169 | - |
|
170 | - // purified HTML |
|
171 | - $html = |
|
172 | - $this->generator->generateFromTokens( |
|
173 | - // list of tokens |
|
174 | - $this->strategy->execute( |
|
175 | - // list of un-purified tokens |
|
176 | - $lexer->tokenizeHTML( |
|
177 | - // un-purified HTML |
|
178 | - $html, $config, $context |
|
179 | - ), |
|
180 | - $config, $context |
|
181 | - ) |
|
182 | - ); |
|
183 | - |
|
184 | - for ($i = $filter_size - 1; $i >= 0; $i--) { |
|
185 | - $html = $filters[$i]->postFilter($html, $config, $context); |
|
186 | - } |
|
187 | - |
|
188 | - $html = HTMLPurifier_Encoder::convertFromUTF8($html, $config, $context); |
|
189 | - $this->context =& $context; |
|
190 | - return $html; |
|
191 | - } |
|
192 | - |
|
193 | - /** |
|
194 | - * Filters an array of HTML snippets |
|
195 | - * @param $config Optional HTMLPurifier_Config object for this operation. |
|
196 | - * See HTMLPurifier::purify() for more details. |
|
197 | - * @return Array of purified HTML |
|
198 | - */ |
|
199 | - public function purifyArray($array_of_html, $config = null) { |
|
200 | - $context_array = array(); |
|
201 | - foreach ($array_of_html as $key => $html) { |
|
202 | - $array_of_html[$key] = $this->purify($html, $config); |
|
203 | - $context_array[$key] = $this->context; |
|
204 | - } |
|
205 | - $this->context = $context_array; |
|
206 | - return $array_of_html; |
|
207 | - } |
|
208 | - |
|
209 | - /** |
|
210 | - * Singleton for enforcing just one HTML Purifier in your system |
|
211 | - * @param $prototype Optional prototype HTMLPurifier instance to |
|
212 | - * overload singleton with, or HTMLPurifier_Config |
|
213 | - * instance to configure the generated version with. |
|
214 | - */ |
|
215 | - public static function instance($prototype = null) { |
|
216 | - if (!self::$instance || $prototype) { |
|
217 | - if ($prototype instanceof HTMLPurifier) { |
|
218 | - self::$instance = $prototype; |
|
219 | - } elseif ($prototype) { |
|
220 | - self::$instance = new HTMLPurifier($prototype); |
|
221 | - } else { |
|
222 | - self::$instance = new HTMLPurifier(); |
|
223 | - } |
|
224 | - } |
|
225 | - return self::$instance; |
|
226 | - } |
|
227 | - |
|
228 | - /** |
|
229 | - * @note Backwards compatibility, see instance() |
|
230 | - */ |
|
231 | - public static function getInstance($prototype = null) { |
|
232 | - return HTMLPurifier::instance($prototype); |
|
233 | - } |
|
57 | + /** Version of HTML Purifier */ |
|
58 | + public $version = '4.4.0'; |
|
59 | + |
|
60 | + /** Constant with version of HTML Purifier */ |
|
61 | + const VERSION = '4.4.0'; |
|
62 | + |
|
63 | + /** Global configuration object */ |
|
64 | + public $config; |
|
65 | + |
|
66 | + /** Array of extra HTMLPurifier_Filter objects to run on HTML, for backwards compatibility */ |
|
67 | + private $filters = array(); |
|
68 | + |
|
69 | + /** Single instance of HTML Purifier */ |
|
70 | + private static $instance; |
|
71 | + |
|
72 | + protected $strategy, $generator; |
|
73 | + |
|
74 | + /** |
|
75 | + * Resultant HTMLPurifier_Context of last run purification. Is an array |
|
76 | + * of contexts if the last called method was purifyArray(). |
|
77 | + */ |
|
78 | + public $context; |
|
79 | + |
|
80 | + /** |
|
81 | + * Initializes the purifier. |
|
82 | + * @param $config Optional HTMLPurifier_Config object for all instances of |
|
83 | + * the purifier, if omitted, a default configuration is |
|
84 | + * supplied (which can be overridden on a per-use basis). |
|
85 | + * The parameter can also be any type that |
|
86 | + * HTMLPurifier_Config::create() supports. |
|
87 | + */ |
|
88 | + public function __construct($config = null) { |
|
89 | + |
|
90 | + $this->config = HTMLPurifier_Config::create($config); |
|
91 | + |
|
92 | + $this->strategy = new HTMLPurifier_Strategy_Core(); |
|
93 | + |
|
94 | + } |
|
95 | + |
|
96 | + /** |
|
97 | + * Adds a filter to process the output. First come first serve |
|
98 | + * @param $filter HTMLPurifier_Filter object |
|
99 | + */ |
|
100 | + public function addFilter($filter) { |
|
101 | + trigger_error('HTMLPurifier->addFilter() is deprecated, use configuration directives in the Filter namespace or Filter.Custom', E_USER_WARNING); |
|
102 | + $this->filters[] = $filter; |
|
103 | + } |
|
104 | + |
|
105 | + /** |
|
106 | + * Filters an HTML snippet/document to be XSS-free and standards-compliant. |
|
107 | + * |
|
108 | + * @param $html String of HTML to purify |
|
109 | + * @param $config HTMLPurifier_Config object for this operation, if omitted, |
|
110 | + * defaults to the config object specified during this |
|
111 | + * object's construction. The parameter can also be any type |
|
112 | + * that HTMLPurifier_Config::create() supports. |
|
113 | + * @return Purified HTML |
|
114 | + */ |
|
115 | + public function purify($html, $config = null) { |
|
116 | + |
|
117 | + // :TODO: make the config merge in, instead of replace |
|
118 | + $config = $config ? HTMLPurifier_Config::create($config) : $this->config; |
|
119 | + |
|
120 | + // implementation is partially environment dependant, partially |
|
121 | + // configuration dependant |
|
122 | + $lexer = HTMLPurifier_Lexer::create($config); |
|
123 | + |
|
124 | + $context = new HTMLPurifier_Context(); |
|
125 | + |
|
126 | + // setup HTML generator |
|
127 | + $this->generator = new HTMLPurifier_Generator($config, $context); |
|
128 | + $context->register('Generator', $this->generator); |
|
129 | + |
|
130 | + // set up global context variables |
|
131 | + if ($config->get('Core.CollectErrors')) { |
|
132 | + // may get moved out if other facilities use it |
|
133 | + $language_factory = HTMLPurifier_LanguageFactory::instance(); |
|
134 | + $language = $language_factory->create($config, $context); |
|
135 | + $context->register('Locale', $language); |
|
136 | + |
|
137 | + $error_collector = new HTMLPurifier_ErrorCollector($context); |
|
138 | + $context->register('ErrorCollector', $error_collector); |
|
139 | + } |
|
140 | + |
|
141 | + // setup id_accumulator context, necessary due to the fact that |
|
142 | + // AttrValidator can be called from many places |
|
143 | + $id_accumulator = HTMLPurifier_IDAccumulator::build($config, $context); |
|
144 | + $context->register('IDAccumulator', $id_accumulator); |
|
145 | + |
|
146 | + $html = HTMLPurifier_Encoder::convertToUTF8($html, $config, $context); |
|
147 | + |
|
148 | + // setup filters |
|
149 | + $filter_flags = $config->getBatch('Filter'); |
|
150 | + $custom_filters = $filter_flags['Custom']; |
|
151 | + unset($filter_flags['Custom']); |
|
152 | + $filters = array(); |
|
153 | + foreach ($filter_flags as $filter => $flag) { |
|
154 | + if (!$flag) continue; |
|
155 | + if (strpos($filter, '.') !== false) continue; |
|
156 | + $class = "HTMLPurifier_Filter_$filter"; |
|
157 | + $filters[] = new $class; |
|
158 | + } |
|
159 | + foreach ($custom_filters as $filter) { |
|
160 | + // maybe "HTMLPurifier_Filter_$filter", but be consistent with AutoFormat |
|
161 | + $filters[] = $filter; |
|
162 | + } |
|
163 | + $filters = array_merge($filters, $this->filters); |
|
164 | + // maybe prepare(), but later |
|
165 | + |
|
166 | + for ($i = 0, $filter_size = count($filters); $i < $filter_size; $i++) { |
|
167 | + $html = $filters[$i]->preFilter($html, $config, $context); |
|
168 | + } |
|
169 | + |
|
170 | + // purified HTML |
|
171 | + $html = |
|
172 | + $this->generator->generateFromTokens( |
|
173 | + // list of tokens |
|
174 | + $this->strategy->execute( |
|
175 | + // list of un-purified tokens |
|
176 | + $lexer->tokenizeHTML( |
|
177 | + // un-purified HTML |
|
178 | + $html, $config, $context |
|
179 | + ), |
|
180 | + $config, $context |
|
181 | + ) |
|
182 | + ); |
|
183 | + |
|
184 | + for ($i = $filter_size - 1; $i >= 0; $i--) { |
|
185 | + $html = $filters[$i]->postFilter($html, $config, $context); |
|
186 | + } |
|
187 | + |
|
188 | + $html = HTMLPurifier_Encoder::convertFromUTF8($html, $config, $context); |
|
189 | + $this->context =& $context; |
|
190 | + return $html; |
|
191 | + } |
|
192 | + |
|
193 | + /** |
|
194 | + * Filters an array of HTML snippets |
|
195 | + * @param $config Optional HTMLPurifier_Config object for this operation. |
|
196 | + * See HTMLPurifier::purify() for more details. |
|
197 | + * @return Array of purified HTML |
|
198 | + */ |
|
199 | + public function purifyArray($array_of_html, $config = null) { |
|
200 | + $context_array = array(); |
|
201 | + foreach ($array_of_html as $key => $html) { |
|
202 | + $array_of_html[$key] = $this->purify($html, $config); |
|
203 | + $context_array[$key] = $this->context; |
|
204 | + } |
|
205 | + $this->context = $context_array; |
|
206 | + return $array_of_html; |
|
207 | + } |
|
208 | + |
|
209 | + /** |
|
210 | + * Singleton for enforcing just one HTML Purifier in your system |
|
211 | + * @param $prototype Optional prototype HTMLPurifier instance to |
|
212 | + * overload singleton with, or HTMLPurifier_Config |
|
213 | + * instance to configure the generated version with. |
|
214 | + */ |
|
215 | + public static function instance($prototype = null) { |
|
216 | + if (!self::$instance || $prototype) { |
|
217 | + if ($prototype instanceof HTMLPurifier) { |
|
218 | + self::$instance = $prototype; |
|
219 | + } elseif ($prototype) { |
|
220 | + self::$instance = new HTMLPurifier($prototype); |
|
221 | + } else { |
|
222 | + self::$instance = new HTMLPurifier(); |
|
223 | + } |
|
224 | + } |
|
225 | + return self::$instance; |
|
226 | + } |
|
227 | + |
|
228 | + /** |
|
229 | + * @note Backwards compatibility, see instance() |
|
230 | + */ |
|
231 | + public static function getInstance($prototype = null) { |
|
232 | + return HTMLPurifier::instance($prototype); |
|
233 | + } |
|
234 | 234 | |
235 | 235 | } |
236 | 236 |
@@ -89,7 +89,7 @@ discard block |
||
89 | 89 | |
90 | 90 | $this->config = HTMLPurifier_Config::create($config); |
91 | 91 | |
92 | - $this->strategy = new HTMLPurifier_Strategy_Core(); |
|
92 | + $this->strategy = new HTMLPurifier_Strategy_Core(); |
|
93 | 93 | |
94 | 94 | } |
95 | 95 | |
@@ -186,7 +186,7 @@ discard block |
||
186 | 186 | } |
187 | 187 | |
188 | 188 | $html = HTMLPurifier_Encoder::convertFromUTF8($html, $config, $context); |
189 | - $this->context =& $context; |
|
189 | + $this->context = & $context; |
|
190 | 190 | return $html; |
191 | 191 | } |
192 | 192 |
@@ -151,8 +151,12 @@ |
||
151 | 151 | unset($filter_flags['Custom']); |
152 | 152 | $filters = array(); |
153 | 153 | foreach ($filter_flags as $filter => $flag) { |
154 | - if (!$flag) continue; |
|
155 | - if (strpos($filter, '.') !== false) continue; |
|
154 | + if (!$flag) { |
|
155 | + continue; |
|
156 | + } |
|
157 | + if (strpos($filter, '.') !== false) { |
|
158 | + continue; |
|
159 | + } |
|
156 | 160 | $class = "HTMLPurifier_Filter_$filter"; |
157 | 161 | $filters[] = new $class; |
158 | 162 | } |
@@ -7,121 +7,121 @@ |
||
7 | 7 | class HTMLPurifier_AttrCollections |
8 | 8 | { |
9 | 9 | |
10 | - /** |
|
11 | - * Associative array of attribute collections, indexed by name |
|
12 | - */ |
|
13 | - public $info = array(); |
|
14 | - |
|
15 | - /** |
|
16 | - * Performs all expansions on internal data for use by other inclusions |
|
17 | - * It also collects all attribute collection extensions from |
|
18 | - * modules |
|
19 | - * @param $attr_types HTMLPurifier_AttrTypes instance |
|
20 | - * @param $modules Hash array of HTMLPurifier_HTMLModule members |
|
21 | - */ |
|
22 | - public function __construct($attr_types, $modules) { |
|
23 | - // load extensions from the modules |
|
24 | - foreach ($modules as $module) { |
|
25 | - foreach ($module->attr_collections as $coll_i => $coll) { |
|
26 | - if (!isset($this->info[$coll_i])) { |
|
27 | - $this->info[$coll_i] = array(); |
|
28 | - } |
|
29 | - foreach ($coll as $attr_i => $attr) { |
|
30 | - if ($attr_i === 0 && isset($this->info[$coll_i][$attr_i])) { |
|
31 | - // merge in includes |
|
32 | - $this->info[$coll_i][$attr_i] = array_merge( |
|
33 | - $this->info[$coll_i][$attr_i], $attr); |
|
34 | - continue; |
|
35 | - } |
|
36 | - $this->info[$coll_i][$attr_i] = $attr; |
|
37 | - } |
|
38 | - } |
|
39 | - } |
|
40 | - // perform internal expansions and inclusions |
|
41 | - foreach ($this->info as $name => $attr) { |
|
42 | - // merge attribute collections that include others |
|
43 | - $this->performInclusions($this->info[$name]); |
|
44 | - // replace string identifiers with actual attribute objects |
|
45 | - $this->expandIdentifiers($this->info[$name], $attr_types); |
|
46 | - } |
|
47 | - } |
|
48 | - |
|
49 | - /** |
|
50 | - * Takes a reference to an attribute associative array and performs |
|
51 | - * all inclusions specified by the zero index. |
|
52 | - * @param &$attr Reference to attribute array |
|
53 | - */ |
|
54 | - public function performInclusions(&$attr) { |
|
55 | - if (!isset($attr[0])) return; |
|
56 | - $merge = $attr[0]; |
|
57 | - $seen = array(); // recursion guard |
|
58 | - // loop through all the inclusions |
|
59 | - for ($i = 0; isset($merge[$i]); $i++) { |
|
60 | - if (isset($seen[$merge[$i]])) continue; |
|
61 | - $seen[$merge[$i]] = true; |
|
62 | - // foreach attribute of the inclusion, copy it over |
|
63 | - if (!isset($this->info[$merge[$i]])) continue; |
|
64 | - foreach ($this->info[$merge[$i]] as $key => $value) { |
|
65 | - if (isset($attr[$key])) continue; // also catches more inclusions |
|
66 | - $attr[$key] = $value; |
|
67 | - } |
|
68 | - if (isset($this->info[$merge[$i]][0])) { |
|
69 | - // recursion |
|
70 | - $merge = array_merge($merge, $this->info[$merge[$i]][0]); |
|
71 | - } |
|
72 | - } |
|
73 | - unset($attr[0]); |
|
74 | - } |
|
75 | - |
|
76 | - /** |
|
77 | - * Expands all string identifiers in an attribute array by replacing |
|
78 | - * them with the appropriate values inside HTMLPurifier_AttrTypes |
|
79 | - * @param &$attr Reference to attribute array |
|
80 | - * @param $attr_types HTMLPurifier_AttrTypes instance |
|
81 | - */ |
|
82 | - public function expandIdentifiers(&$attr, $attr_types) { |
|
83 | - |
|
84 | - // because foreach will process new elements we add, make sure we |
|
85 | - // skip duplicates |
|
86 | - $processed = array(); |
|
87 | - |
|
88 | - foreach ($attr as $def_i => $def) { |
|
89 | - // skip inclusions |
|
90 | - if ($def_i === 0) continue; |
|
91 | - |
|
92 | - if (isset($processed[$def_i])) continue; |
|
93 | - |
|
94 | - // determine whether or not attribute is required |
|
95 | - if ($required = (strpos($def_i, '*') !== false)) { |
|
96 | - // rename the definition |
|
97 | - unset($attr[$def_i]); |
|
98 | - $def_i = trim($def_i, '*'); |
|
99 | - $attr[$def_i] = $def; |
|
100 | - } |
|
101 | - |
|
102 | - $processed[$def_i] = true; |
|
103 | - |
|
104 | - // if we've already got a literal object, move on |
|
105 | - if (is_object($def)) { |
|
106 | - // preserve previous required |
|
107 | - $attr[$def_i]->required = ($required || $attr[$def_i]->required); |
|
108 | - continue; |
|
109 | - } |
|
110 | - |
|
111 | - if ($def === false) { |
|
112 | - unset($attr[$def_i]); |
|
113 | - continue; |
|
114 | - } |
|
115 | - |
|
116 | - if ($t = $attr_types->get($def)) { |
|
117 | - $attr[$def_i] = $t; |
|
118 | - $attr[$def_i]->required = $required; |
|
119 | - } else { |
|
120 | - unset($attr[$def_i]); |
|
121 | - } |
|
122 | - } |
|
123 | - |
|
124 | - } |
|
10 | + /** |
|
11 | + * Associative array of attribute collections, indexed by name |
|
12 | + */ |
|
13 | + public $info = array(); |
|
14 | + |
|
15 | + /** |
|
16 | + * Performs all expansions on internal data for use by other inclusions |
|
17 | + * It also collects all attribute collection extensions from |
|
18 | + * modules |
|
19 | + * @param $attr_types HTMLPurifier_AttrTypes instance |
|
20 | + * @param $modules Hash array of HTMLPurifier_HTMLModule members |
|
21 | + */ |
|
22 | + public function __construct($attr_types, $modules) { |
|
23 | + // load extensions from the modules |
|
24 | + foreach ($modules as $module) { |
|
25 | + foreach ($module->attr_collections as $coll_i => $coll) { |
|
26 | + if (!isset($this->info[$coll_i])) { |
|
27 | + $this->info[$coll_i] = array(); |
|
28 | + } |
|
29 | + foreach ($coll as $attr_i => $attr) { |
|
30 | + if ($attr_i === 0 && isset($this->info[$coll_i][$attr_i])) { |
|
31 | + // merge in includes |
|
32 | + $this->info[$coll_i][$attr_i] = array_merge( |
|
33 | + $this->info[$coll_i][$attr_i], $attr); |
|
34 | + continue; |
|
35 | + } |
|
36 | + $this->info[$coll_i][$attr_i] = $attr; |
|
37 | + } |
|
38 | + } |
|
39 | + } |
|
40 | + // perform internal expansions and inclusions |
|
41 | + foreach ($this->info as $name => $attr) { |
|
42 | + // merge attribute collections that include others |
|
43 | + $this->performInclusions($this->info[$name]); |
|
44 | + // replace string identifiers with actual attribute objects |
|
45 | + $this->expandIdentifiers($this->info[$name], $attr_types); |
|
46 | + } |
|
47 | + } |
|
48 | + |
|
49 | + /** |
|
50 | + * Takes a reference to an attribute associative array and performs |
|
51 | + * all inclusions specified by the zero index. |
|
52 | + * @param &$attr Reference to attribute array |
|
53 | + */ |
|
54 | + public function performInclusions(&$attr) { |
|
55 | + if (!isset($attr[0])) return; |
|
56 | + $merge = $attr[0]; |
|
57 | + $seen = array(); // recursion guard |
|
58 | + // loop through all the inclusions |
|
59 | + for ($i = 0; isset($merge[$i]); $i++) { |
|
60 | + if (isset($seen[$merge[$i]])) continue; |
|
61 | + $seen[$merge[$i]] = true; |
|
62 | + // foreach attribute of the inclusion, copy it over |
|
63 | + if (!isset($this->info[$merge[$i]])) continue; |
|
64 | + foreach ($this->info[$merge[$i]] as $key => $value) { |
|
65 | + if (isset($attr[$key])) continue; // also catches more inclusions |
|
66 | + $attr[$key] = $value; |
|
67 | + } |
|
68 | + if (isset($this->info[$merge[$i]][0])) { |
|
69 | + // recursion |
|
70 | + $merge = array_merge($merge, $this->info[$merge[$i]][0]); |
|
71 | + } |
|
72 | + } |
|
73 | + unset($attr[0]); |
|
74 | + } |
|
75 | + |
|
76 | + /** |
|
77 | + * Expands all string identifiers in an attribute array by replacing |
|
78 | + * them with the appropriate values inside HTMLPurifier_AttrTypes |
|
79 | + * @param &$attr Reference to attribute array |
|
80 | + * @param $attr_types HTMLPurifier_AttrTypes instance |
|
81 | + */ |
|
82 | + public function expandIdentifiers(&$attr, $attr_types) { |
|
83 | + |
|
84 | + // because foreach will process new elements we add, make sure we |
|
85 | + // skip duplicates |
|
86 | + $processed = array(); |
|
87 | + |
|
88 | + foreach ($attr as $def_i => $def) { |
|
89 | + // skip inclusions |
|
90 | + if ($def_i === 0) continue; |
|
91 | + |
|
92 | + if (isset($processed[$def_i])) continue; |
|
93 | + |
|
94 | + // determine whether or not attribute is required |
|
95 | + if ($required = (strpos($def_i, '*') !== false)) { |
|
96 | + // rename the definition |
|
97 | + unset($attr[$def_i]); |
|
98 | + $def_i = trim($def_i, '*'); |
|
99 | + $attr[$def_i] = $def; |
|
100 | + } |
|
101 | + |
|
102 | + $processed[$def_i] = true; |
|
103 | + |
|
104 | + // if we've already got a literal object, move on |
|
105 | + if (is_object($def)) { |
|
106 | + // preserve previous required |
|
107 | + $attr[$def_i]->required = ($required || $attr[$def_i]->required); |
|
108 | + continue; |
|
109 | + } |
|
110 | + |
|
111 | + if ($def === false) { |
|
112 | + unset($attr[$def_i]); |
|
113 | + continue; |
|
114 | + } |
|
115 | + |
|
116 | + if ($t = $attr_types->get($def)) { |
|
117 | + $attr[$def_i] = $t; |
|
118 | + $attr[$def_i]->required = $required; |
|
119 | + } else { |
|
120 | + unset($attr[$def_i]); |
|
121 | + } |
|
122 | + } |
|
123 | + |
|
124 | + } |
|
125 | 125 | |
126 | 126 | } |
127 | 127 |
@@ -52,17 +52,26 @@ discard block |
||
52 | 52 | * @param &$attr Reference to attribute array |
53 | 53 | */ |
54 | 54 | public function performInclusions(&$attr) { |
55 | - if (!isset($attr[0])) return; |
|
55 | + if (!isset($attr[0])) { |
|
56 | + return; |
|
57 | + } |
|
56 | 58 | $merge = $attr[0]; |
57 | 59 | $seen = array(); // recursion guard |
58 | 60 | // loop through all the inclusions |
59 | 61 | for ($i = 0; isset($merge[$i]); $i++) { |
60 | - if (isset($seen[$merge[$i]])) continue; |
|
62 | + if (isset($seen[$merge[$i]])) { |
|
63 | + continue; |
|
64 | + } |
|
61 | 65 | $seen[$merge[$i]] = true; |
62 | 66 | // foreach attribute of the inclusion, copy it over |
63 | - if (!isset($this->info[$merge[$i]])) continue; |
|
67 | + if (!isset($this->info[$merge[$i]])) { |
|
68 | + continue; |
|
69 | + } |
|
64 | 70 | foreach ($this->info[$merge[$i]] as $key => $value) { |
65 | - if (isset($attr[$key])) continue; // also catches more inclusions |
|
71 | + if (isset($attr[$key])) { |
|
72 | + continue; |
|
73 | + } |
|
74 | + // also catches more inclusions |
|
66 | 75 | $attr[$key] = $value; |
67 | 76 | } |
68 | 77 | if (isset($this->info[$merge[$i]][0])) { |
@@ -87,9 +96,13 @@ discard block |
||
87 | 96 | |
88 | 97 | foreach ($attr as $def_i => $def) { |
89 | 98 | // skip inclusions |
90 | - if ($def_i === 0) continue; |
|
99 | + if ($def_i === 0) { |
|
100 | + continue; |
|
101 | + } |
|
91 | 102 | |
92 | - if (isset($processed[$def_i])) continue; |
|
103 | + if (isset($processed[$def_i])) { |
|
104 | + continue; |
|
105 | + } |
|
93 | 106 | |
94 | 107 | // determine whether or not attribute is required |
95 | 108 | if ($required = (strpos($def_i, '*') !== false)) { |
@@ -13,110 +13,110 @@ |
||
13 | 13 | abstract class HTMLPurifier_AttrDef |
14 | 14 | { |
15 | 15 | |
16 | - /** |
|
17 | - * Tells us whether or not an HTML attribute is minimized. Has no |
|
18 | - * meaning in other contexts. |
|
19 | - */ |
|
20 | - public $minimized = false; |
|
16 | + /** |
|
17 | + * Tells us whether or not an HTML attribute is minimized. Has no |
|
18 | + * meaning in other contexts. |
|
19 | + */ |
|
20 | + public $minimized = false; |
|
21 | 21 | |
22 | - /** |
|
23 | - * Tells us whether or not an HTML attribute is required. Has no |
|
24 | - * meaning in other contexts |
|
25 | - */ |
|
26 | - public $required = false; |
|
22 | + /** |
|
23 | + * Tells us whether or not an HTML attribute is required. Has no |
|
24 | + * meaning in other contexts |
|
25 | + */ |
|
26 | + public $required = false; |
|
27 | 27 | |
28 | - /** |
|
29 | - * Validates and cleans passed string according to a definition. |
|
30 | - * |
|
31 | - * @param $string String to be validated and cleaned. |
|
32 | - * @param $config Mandatory HTMLPurifier_Config object. |
|
33 | - * @param $context Mandatory HTMLPurifier_AttrContext object. |
|
34 | - */ |
|
35 | - abstract public function validate($string, $config, $context); |
|
28 | + /** |
|
29 | + * Validates and cleans passed string according to a definition. |
|
30 | + * |
|
31 | + * @param $string String to be validated and cleaned. |
|
32 | + * @param $config Mandatory HTMLPurifier_Config object. |
|
33 | + * @param $context Mandatory HTMLPurifier_AttrContext object. |
|
34 | + */ |
|
35 | + abstract public function validate($string, $config, $context); |
|
36 | 36 | |
37 | - /** |
|
38 | - * Convenience method that parses a string as if it were CDATA. |
|
39 | - * |
|
40 | - * This method process a string in the manner specified at |
|
41 | - * <http://www.w3.org/TR/html4/types.html#h-6.2> by removing |
|
42 | - * leading and trailing whitespace, ignoring line feeds, and replacing |
|
43 | - * carriage returns and tabs with spaces. While most useful for HTML |
|
44 | - * attributes specified as CDATA, it can also be applied to most CSS |
|
45 | - * values. |
|
46 | - * |
|
47 | - * @note This method is not entirely standards compliant, as trim() removes |
|
48 | - * more types of whitespace than specified in the spec. In practice, |
|
49 | - * this is rarely a problem, as those extra characters usually have |
|
50 | - * already been removed by HTMLPurifier_Encoder. |
|
51 | - * |
|
52 | - * @warning This processing is inconsistent with XML's whitespace handling |
|
53 | - * as specified by section 3.3.3 and referenced XHTML 1.0 section |
|
54 | - * 4.7. However, note that we are NOT necessarily |
|
55 | - * parsing XML, thus, this behavior may still be correct. We |
|
56 | - * assume that newlines have been normalized. |
|
57 | - */ |
|
58 | - public function parseCDATA($string) { |
|
59 | - $string = trim($string); |
|
60 | - $string = str_replace(array("\n", "\t", "\r"), ' ', $string); |
|
61 | - return $string; |
|
62 | - } |
|
37 | + /** |
|
38 | + * Convenience method that parses a string as if it were CDATA. |
|
39 | + * |
|
40 | + * This method process a string in the manner specified at |
|
41 | + * <http://www.w3.org/TR/html4/types.html#h-6.2> by removing |
|
42 | + * leading and trailing whitespace, ignoring line feeds, and replacing |
|
43 | + * carriage returns and tabs with spaces. While most useful for HTML |
|
44 | + * attributes specified as CDATA, it can also be applied to most CSS |
|
45 | + * values. |
|
46 | + * |
|
47 | + * @note This method is not entirely standards compliant, as trim() removes |
|
48 | + * more types of whitespace than specified in the spec. In practice, |
|
49 | + * this is rarely a problem, as those extra characters usually have |
|
50 | + * already been removed by HTMLPurifier_Encoder. |
|
51 | + * |
|
52 | + * @warning This processing is inconsistent with XML's whitespace handling |
|
53 | + * as specified by section 3.3.3 and referenced XHTML 1.0 section |
|
54 | + * 4.7. However, note that we are NOT necessarily |
|
55 | + * parsing XML, thus, this behavior may still be correct. We |
|
56 | + * assume that newlines have been normalized. |
|
57 | + */ |
|
58 | + public function parseCDATA($string) { |
|
59 | + $string = trim($string); |
|
60 | + $string = str_replace(array("\n", "\t", "\r"), ' ', $string); |
|
61 | + return $string; |
|
62 | + } |
|
63 | 63 | |
64 | - /** |
|
65 | - * Factory method for creating this class from a string. |
|
66 | - * @param $string String construction info |
|
67 | - * @return Created AttrDef object corresponding to $string |
|
68 | - */ |
|
69 | - public function make($string) { |
|
70 | - // default implementation, return a flyweight of this object. |
|
71 | - // If $string has an effect on the returned object (i.e. you |
|
72 | - // need to overload this method), it is best |
|
73 | - // to clone or instantiate new copies. (Instantiation is safer.) |
|
74 | - return $this; |
|
75 | - } |
|
64 | + /** |
|
65 | + * Factory method for creating this class from a string. |
|
66 | + * @param $string String construction info |
|
67 | + * @return Created AttrDef object corresponding to $string |
|
68 | + */ |
|
69 | + public function make($string) { |
|
70 | + // default implementation, return a flyweight of this object. |
|
71 | + // If $string has an effect on the returned object (i.e. you |
|
72 | + // need to overload this method), it is best |
|
73 | + // to clone or instantiate new copies. (Instantiation is safer.) |
|
74 | + return $this; |
|
75 | + } |
|
76 | 76 | |
77 | - /** |
|
78 | - * Removes spaces from rgb(0, 0, 0) so that shorthand CSS properties work |
|
79 | - * properly. THIS IS A HACK! |
|
80 | - */ |
|
81 | - protected function mungeRgb($string) { |
|
82 | - return preg_replace('/rgb\((\d+)\s*,\s*(\d+)\s*,\s*(\d+)\)/', 'rgb(\1,\2,\3)', $string); |
|
83 | - } |
|
77 | + /** |
|
78 | + * Removes spaces from rgb(0, 0, 0) so that shorthand CSS properties work |
|
79 | + * properly. THIS IS A HACK! |
|
80 | + */ |
|
81 | + protected function mungeRgb($string) { |
|
82 | + return preg_replace('/rgb\((\d+)\s*,\s*(\d+)\s*,\s*(\d+)\)/', 'rgb(\1,\2,\3)', $string); |
|
83 | + } |
|
84 | 84 | |
85 | - /** |
|
86 | - * Parses a possibly escaped CSS string and returns the "pure" |
|
87 | - * version of it. |
|
88 | - */ |
|
89 | - protected function expandCSSEscape($string) { |
|
90 | - // flexibly parse it |
|
91 | - $ret = ''; |
|
92 | - for ($i = 0, $c = strlen($string); $i < $c; $i++) { |
|
93 | - if ($string[$i] === '\\') { |
|
94 | - $i++; |
|
95 | - if ($i >= $c) { |
|
96 | - $ret .= '\\'; |
|
97 | - break; |
|
98 | - } |
|
99 | - if (ctype_xdigit($string[$i])) { |
|
100 | - $code = $string[$i]; |
|
101 | - for ($a = 1, $i++; $i < $c && $a < 6; $i++, $a++) { |
|
102 | - if (!ctype_xdigit($string[$i])) break; |
|
103 | - $code .= $string[$i]; |
|
104 | - } |
|
105 | - // We have to be extremely careful when adding |
|
106 | - // new characters, to make sure we're not breaking |
|
107 | - // the encoding. |
|
108 | - $char = HTMLPurifier_Encoder::unichr(hexdec($code)); |
|
109 | - if (HTMLPurifier_Encoder::cleanUTF8($char) === '') continue; |
|
110 | - $ret .= $char; |
|
111 | - if ($i < $c && trim($string[$i]) !== '') $i--; |
|
112 | - continue; |
|
113 | - } |
|
114 | - if ($string[$i] === "\n") continue; |
|
115 | - } |
|
116 | - $ret .= $string[$i]; |
|
117 | - } |
|
118 | - return $ret; |
|
119 | - } |
|
85 | + /** |
|
86 | + * Parses a possibly escaped CSS string and returns the "pure" |
|
87 | + * version of it. |
|
88 | + */ |
|
89 | + protected function expandCSSEscape($string) { |
|
90 | + // flexibly parse it |
|
91 | + $ret = ''; |
|
92 | + for ($i = 0, $c = strlen($string); $i < $c; $i++) { |
|
93 | + if ($string[$i] === '\\') { |
|
94 | + $i++; |
|
95 | + if ($i >= $c) { |
|
96 | + $ret .= '\\'; |
|
97 | + break; |
|
98 | + } |
|
99 | + if (ctype_xdigit($string[$i])) { |
|
100 | + $code = $string[$i]; |
|
101 | + for ($a = 1, $i++; $i < $c && $a < 6; $i++, $a++) { |
|
102 | + if (!ctype_xdigit($string[$i])) break; |
|
103 | + $code .= $string[$i]; |
|
104 | + } |
|
105 | + // We have to be extremely careful when adding |
|
106 | + // new characters, to make sure we're not breaking |
|
107 | + // the encoding. |
|
108 | + $char = HTMLPurifier_Encoder::unichr(hexdec($code)); |
|
109 | + if (HTMLPurifier_Encoder::cleanUTF8($char) === '') continue; |
|
110 | + $ret .= $char; |
|
111 | + if ($i < $c && trim($string[$i]) !== '') $i--; |
|
112 | + continue; |
|
113 | + } |
|
114 | + if ($string[$i] === "\n") continue; |
|
115 | + } |
|
116 | + $ret .= $string[$i]; |
|
117 | + } |
|
118 | + return $ret; |
|
119 | + } |
|
120 | 120 | |
121 | 121 | } |
122 | 122 |
@@ -99,19 +99,27 @@ |
||
99 | 99 | if (ctype_xdigit($string[$i])) { |
100 | 100 | $code = $string[$i]; |
101 | 101 | for ($a = 1, $i++; $i < $c && $a < 6; $i++, $a++) { |
102 | - if (!ctype_xdigit($string[$i])) break; |
|
102 | + if (!ctype_xdigit($string[$i])) { |
|
103 | + break; |
|
104 | + } |
|
103 | 105 | $code .= $string[$i]; |
104 | 106 | } |
105 | 107 | // We have to be extremely careful when adding |
106 | 108 | // new characters, to make sure we're not breaking |
107 | 109 | // the encoding. |
108 | 110 | $char = HTMLPurifier_Encoder::unichr(hexdec($code)); |
109 | - if (HTMLPurifier_Encoder::cleanUTF8($char) === '') continue; |
|
111 | + if (HTMLPurifier_Encoder::cleanUTF8($char) === '') { |
|
112 | + continue; |
|
113 | + } |
|
110 | 114 | $ret .= $char; |
111 | - if ($i < $c && trim($string[$i]) !== '') $i--; |
|
115 | + if ($i < $c && trim($string[$i]) !== '') { |
|
116 | + $i--; |
|
117 | + } |
|
112 | 118 | continue; |
113 | 119 | } |
114 | - if ($string[$i] === "\n") continue; |
|
120 | + if ($string[$i] === "\n") { |
|
121 | + continue; |
|
122 | + } |
|
115 | 123 | } |
116 | 124 | $ret .= $string[$i]; |
117 | 125 | } |
@@ -14,73 +14,73 @@ |
||
14 | 14 | class HTMLPurifier_AttrDef_CSS extends HTMLPurifier_AttrDef |
15 | 15 | { |
16 | 16 | |
17 | - public function validate($css, $config, $context) { |
|
17 | + public function validate($css, $config, $context) { |
|
18 | 18 | |
19 | - $css = $this->parseCDATA($css); |
|
19 | + $css = $this->parseCDATA($css); |
|
20 | 20 | |
21 | - $definition = $config->getCSSDefinition(); |
|
21 | + $definition = $config->getCSSDefinition(); |
|
22 | 22 | |
23 | - // we're going to break the spec and explode by semicolons. |
|
24 | - // This is because semicolon rarely appears in escaped form |
|
25 | - // Doing this is generally flaky but fast |
|
26 | - // IT MIGHT APPEAR IN URIs, see HTMLPurifier_AttrDef_CSSURI |
|
27 | - // for details |
|
23 | + // we're going to break the spec and explode by semicolons. |
|
24 | + // This is because semicolon rarely appears in escaped form |
|
25 | + // Doing this is generally flaky but fast |
|
26 | + // IT MIGHT APPEAR IN URIs, see HTMLPurifier_AttrDef_CSSURI |
|
27 | + // for details |
|
28 | 28 | |
29 | - $declarations = explode(';', $css); |
|
30 | - $propvalues = array(); |
|
29 | + $declarations = explode(';', $css); |
|
30 | + $propvalues = array(); |
|
31 | 31 | |
32 | - /** |
|
33 | - * Name of the current CSS property being validated. |
|
34 | - */ |
|
35 | - $property = false; |
|
36 | - $context->register('CurrentCSSProperty', $property); |
|
32 | + /** |
|
33 | + * Name of the current CSS property being validated. |
|
34 | + */ |
|
35 | + $property = false; |
|
36 | + $context->register('CurrentCSSProperty', $property); |
|
37 | 37 | |
38 | - foreach ($declarations as $declaration) { |
|
39 | - if (!$declaration) continue; |
|
40 | - if (!strpos($declaration, ':')) continue; |
|
41 | - list($property, $value) = explode(':', $declaration, 2); |
|
42 | - $property = trim($property); |
|
43 | - $value = trim($value); |
|
44 | - $ok = false; |
|
45 | - do { |
|
46 | - if (isset($definition->info[$property])) { |
|
47 | - $ok = true; |
|
48 | - break; |
|
49 | - } |
|
50 | - if (ctype_lower($property)) break; |
|
51 | - $property = strtolower($property); |
|
52 | - if (isset($definition->info[$property])) { |
|
53 | - $ok = true; |
|
54 | - break; |
|
55 | - } |
|
56 | - } while(0); |
|
57 | - if (!$ok) continue; |
|
58 | - // inefficient call, since the validator will do this again |
|
59 | - if (strtolower(trim($value)) !== 'inherit') { |
|
60 | - // inherit works for everything (but only on the base property) |
|
61 | - $result = $definition->info[$property]->validate( |
|
62 | - $value, $config, $context ); |
|
63 | - } else { |
|
64 | - $result = 'inherit'; |
|
65 | - } |
|
66 | - if ($result === false) continue; |
|
67 | - $propvalues[$property] = $result; |
|
68 | - } |
|
38 | + foreach ($declarations as $declaration) { |
|
39 | + if (!$declaration) continue; |
|
40 | + if (!strpos($declaration, ':')) continue; |
|
41 | + list($property, $value) = explode(':', $declaration, 2); |
|
42 | + $property = trim($property); |
|
43 | + $value = trim($value); |
|
44 | + $ok = false; |
|
45 | + do { |
|
46 | + if (isset($definition->info[$property])) { |
|
47 | + $ok = true; |
|
48 | + break; |
|
49 | + } |
|
50 | + if (ctype_lower($property)) break; |
|
51 | + $property = strtolower($property); |
|
52 | + if (isset($definition->info[$property])) { |
|
53 | + $ok = true; |
|
54 | + break; |
|
55 | + } |
|
56 | + } while(0); |
|
57 | + if (!$ok) continue; |
|
58 | + // inefficient call, since the validator will do this again |
|
59 | + if (strtolower(trim($value)) !== 'inherit') { |
|
60 | + // inherit works for everything (but only on the base property) |
|
61 | + $result = $definition->info[$property]->validate( |
|
62 | + $value, $config, $context ); |
|
63 | + } else { |
|
64 | + $result = 'inherit'; |
|
65 | + } |
|
66 | + if ($result === false) continue; |
|
67 | + $propvalues[$property] = $result; |
|
68 | + } |
|
69 | 69 | |
70 | - $context->destroy('CurrentCSSProperty'); |
|
70 | + $context->destroy('CurrentCSSProperty'); |
|
71 | 71 | |
72 | - // procedure does not write the new CSS simultaneously, so it's |
|
73 | - // slightly inefficient, but it's the only way of getting rid of |
|
74 | - // duplicates. Perhaps config to optimize it, but not now. |
|
72 | + // procedure does not write the new CSS simultaneously, so it's |
|
73 | + // slightly inefficient, but it's the only way of getting rid of |
|
74 | + // duplicates. Perhaps config to optimize it, but not now. |
|
75 | 75 | |
76 | - $new_declarations = ''; |
|
77 | - foreach ($propvalues as $prop => $value) { |
|
78 | - $new_declarations .= "$prop:$value;"; |
|
79 | - } |
|
76 | + $new_declarations = ''; |
|
77 | + foreach ($propvalues as $prop => $value) { |
|
78 | + $new_declarations .= "$prop:$value;"; |
|
79 | + } |
|
80 | 80 | |
81 | - return $new_declarations ? $new_declarations : false; |
|
81 | + return $new_declarations ? $new_declarations : false; |
|
82 | 82 | |
83 | - } |
|
83 | + } |
|
84 | 84 | |
85 | 85 | } |
86 | 86 |
@@ -53,7 +53,7 @@ |
||
53 | 53 | $ok = true; |
54 | 54 | break; |
55 | 55 | } |
56 | - } while(0); |
|
56 | + } while (0); |
|
57 | 57 | if (!$ok) continue; |
58 | 58 | // inefficient call, since the validator will do this again |
59 | 59 | if (strtolower(trim($value)) !== 'inherit') { |
@@ -36,8 +36,12 @@ discard block |
||
36 | 36 | $context->register('CurrentCSSProperty', $property); |
37 | 37 | |
38 | 38 | foreach ($declarations as $declaration) { |
39 | - if (!$declaration) continue; |
|
40 | - if (!strpos($declaration, ':')) continue; |
|
39 | + if (!$declaration) { |
|
40 | + continue; |
|
41 | + } |
|
42 | + if (!strpos($declaration, ':')) { |
|
43 | + continue; |
|
44 | + } |
|
41 | 45 | list($property, $value) = explode(':', $declaration, 2); |
42 | 46 | $property = trim($property); |
43 | 47 | $value = trim($value); |
@@ -47,14 +51,18 @@ discard block |
||
47 | 51 | $ok = true; |
48 | 52 | break; |
49 | 53 | } |
50 | - if (ctype_lower($property)) break; |
|
54 | + if (ctype_lower($property)) { |
|
55 | + break; |
|
56 | + } |
|
51 | 57 | $property = strtolower($property); |
52 | 58 | if (isset($definition->info[$property])) { |
53 | 59 | $ok = true; |
54 | 60 | break; |
55 | 61 | } |
56 | 62 | } while(0); |
57 | - if (!$ok) continue; |
|
63 | + if (!$ok) { |
|
64 | + continue; |
|
65 | + } |
|
58 | 66 | // inefficient call, since the validator will do this again |
59 | 67 | if (strtolower(trim($value)) !== 'inherit') { |
60 | 68 | // inherit works for everything (but only on the base property) |
@@ -63,7 +71,9 @@ discard block |
||
63 | 71 | } else { |
64 | 72 | $result = 'inherit'; |
65 | 73 | } |
66 | - if ($result === false) continue; |
|
74 | + if ($result === false) { |
|
75 | + continue; |
|
76 | + } |
|
67 | 77 | $propvalues[$property] = $result; |
68 | 78 | } |
69 | 79 |
@@ -3,18 +3,18 @@ |
||
3 | 3 | class HTMLPurifier_AttrDef_CSS_AlphaValue extends HTMLPurifier_AttrDef_CSS_Number |
4 | 4 | { |
5 | 5 | |
6 | - public function __construct() { |
|
7 | - parent::__construct(false); // opacity is non-negative, but we will clamp it |
|
8 | - } |
|
6 | + public function __construct() { |
|
7 | + parent::__construct(false); // opacity is non-negative, but we will clamp it |
|
8 | + } |
|
9 | 9 | |
10 | - public function validate($number, $config, $context) { |
|
11 | - $result = parent::validate($number, $config, $context); |
|
12 | - if ($result === false) return $result; |
|
13 | - $float = (float) $result; |
|
14 | - if ($float < 0.0) $result = '0'; |
|
15 | - if ($float > 1.0) $result = '1'; |
|
16 | - return $result; |
|
17 | - } |
|
10 | + public function validate($number, $config, $context) { |
|
11 | + $result = parent::validate($number, $config, $context); |
|
12 | + if ($result === false) return $result; |
|
13 | + $float = (float) $result; |
|
14 | + if ($float < 0.0) $result = '0'; |
|
15 | + if ($float > 1.0) $result = '1'; |
|
16 | + return $result; |
|
17 | + } |
|
18 | 18 | |
19 | 19 | } |
20 | 20 |
@@ -9,10 +9,16 @@ |
||
9 | 9 | |
10 | 10 | public function validate($number, $config, $context) { |
11 | 11 | $result = parent::validate($number, $config, $context); |
12 | - if ($result === false) return $result; |
|
12 | + if ($result === false) { |
|
13 | + return $result; |
|
14 | + } |
|
13 | 15 | $float = (float) $result; |
14 | - if ($float < 0.0) $result = '0'; |
|
15 | - if ($float > 1.0) $result = '1'; |
|
16 | + if ($float < 0.0) { |
|
17 | + $result = '0'; |
|
18 | + } |
|
19 | + if ($float > 1.0) { |
|
20 | + $result = '1'; |
|
21 | + } |
|
16 | 22 | return $result; |
17 | 23 | } |
18 | 24 |