Changeset 47733 for trunk/src/wp-includes/SimplePie/Sanitize.php
- Timestamp:
- 05/01/2020 02:24:42 PM (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/SimplePie/Sanitize.php
r25342 r47733 6 6 * Takes the hard work out of managing a complete RSS/Atom solution. 7 7 * 8 * Copyright (c) 2004-201 2, Ryan Parman, GeoffreySneddon, Ryan McCue, and contributors8 * Copyright (c) 2004-2016, Ryan Parman, Sam Sneddon, Ryan McCue, and contributors 9 9 * All rights reserved. 10 10 * … … 34 34 * 35 35 * @package SimplePie 36 * @version 1.3.1 37 * @copyright 2004-2012 Ryan Parman, Geoffrey Sneddon, Ryan McCue 36 * @copyright 2004-2016 Ryan Parman, Sam Sneddon, Ryan McCue 38 37 * @author Ryan Parman 39 * @author GeoffreySneddon38 * @author Sam Sneddon 40 39 * @author Ryan McCue 41 40 * @link http://simplepie.org/ SimplePie … … 62 61 var $strip_htmltags = array('base', 'blink', 'body', 'doctype', 'embed', 'font', 'form', 'frame', 'frameset', 'html', 'iframe', 'input', 'marquee', 'meta', 'noscript', 'object', 'param', 'script', 'style'); 63 62 var $encode_instead_of_strip = false; 64 var $strip_attributes = array('bgsound', 'class', 'expr', 'id', 'style', 'onclick', 'onerror', 'onfinish', 'onmouseover', 'onmouseout', 'onfocus', 'onblur', 'lowsrc', 'dynsrc'); 63 var $strip_attributes = array('bgsound', 'expr', 'id', 'style', 'onclick', 'onerror', 'onfinish', 'onmouseover', 'onmouseout', 'onfocus', 'onblur', 'lowsrc', 'dynsrc'); 64 var $add_attributes = array('audio' => array('preload' => 'none'), 'iframe' => array('sandbox' => 'allow-scripts allow-same-origin'), 'video' => array('preload' => 'none')); 65 65 var $strip_comments = false; 66 66 var $output_encoding = 'UTF-8'; … … 161 161 } 162 162 163 public function strip_attributes($attribs = array('bgsound', ' class', 'expr', 'id', 'style', 'onclick', 'onerror', 'onfinish', 'onmouseover', 'onmouseout', 'onfocus', 'onblur', 'lowsrc', 'dynsrc'))163 public function strip_attributes($attribs = array('bgsound', 'expr', 'id', 'style', 'onclick', 'onerror', 'onfinish', 'onmouseover', 'onmouseout', 'onfocus', 'onblur', 'lowsrc', 'dynsrc')) 164 164 { 165 165 if ($attribs) … … 177 177 { 178 178 $this->strip_attributes = false; 179 } 180 } 181 182 public function add_attributes($attribs = array('audio' => array('preload' => 'none'), 'iframe' => array('sandbox' => 'allow-scripts allow-same-origin'), 'video' => array('preload' => 'none'))) 183 { 184 if ($attribs) 185 { 186 if (is_array($attribs)) 187 { 188 $this->add_attributes = $attribs; 189 } 190 else 191 { 192 $this->add_attributes = explode(',', $attribs); 193 } 194 } 195 else 196 { 197 $this->add_attributes = false; 179 198 } 180 199 } … … 250 269 if (!class_exists('DOMDocument')) 251 270 { 252 $this->registry->call('Misc', 'error', array('DOMDocument not found, unable to use sanitizer', E_USER_WARNING, __FILE__, __LINE__)); 253 return ''; 271 throw new SimplePie_Exception('DOMDocument not found, unable to use sanitizer'); 254 272 } 255 273 $document = new DOMDocument(); 256 274 $document->encoding = 'UTF-8'; 275 257 276 $data = $this->preprocess($data, $type); 258 277 … … 261 280 restore_error_handler(); 262 281 282 $xpath = new DOMXPath($document); 283 263 284 // Strip comments 264 285 if ($this->strip_comments) 265 286 { 266 $xpath = new DOMXPath($document);267 287 $comments = $xpath->query('//comment()'); 268 288 … … 280 300 foreach ($this->strip_htmltags as $tag) 281 301 { 282 $this->strip_tag($tag, $document, $ type);302 $this->strip_tag($tag, $document, $xpath, $type); 283 303 } 284 304 } … … 288 308 foreach ($this->strip_attributes as $attrib) 289 309 { 290 $this->strip_attr($attrib, $document); 310 $this->strip_attr($attrib, $xpath); 311 } 312 } 313 314 if ($this->add_attributes) 315 { 316 foreach ($this->add_attributes as $tag => $valuePairs) 317 { 318 $this->add_attr($tag, $valuePairs, $document); 291 319 } 292 320 } … … 327 355 else 328 356 { 329 trigger_error("$this->cache_location is not writ eable. Make sure you've set the correct relative or absolute path, and that the location is server-writable.", E_USER_WARNING);357 trigger_error("$this->cache_location is not writable. Make sure you've set the correct relative or absolute path, and that the location is server-writable.", E_USER_WARNING); 330 358 } 331 359 } … … 335 363 } 336 364 337 // Remove the DOCTYPE 338 // Seems to cause segfaulting if we don't do this 339 if ($document->firstChild instanceof DOMDocumentType) 340 { 341 $document->removeChild($document->firstChild); 342 } 343 344 // Move everything from the body to the root 345 $real_body = $document->getElementsByTagName('body')->item(0)->childNodes->item(0); 346 $document->replaceChild($real_body, $document->firstChild); 347 365 // Get content node 366 $div = $document->getElementsByTagName('body')->item(0)->firstChild; 348 367 // Finally, convert to a HTML string 349 $data = trim($document->saveHTML( ));368 $data = trim($document->saveHTML($div)); 350 369 351 370 if ($this->remove_div) … … 385 404 { 386 405 $ret = ''; 406 $html = preg_replace('%</?(?:html|body)[^>]*?'.'>%is', '', $html); 387 407 if ($type & ~SIMPLEPIE_CONSTRUCT_XHTML) 388 408 { … … 457 477 } 458 478 459 protected function strip_tag($tag, $document, $type) 460 { 461 $xpath = new DOMXPath($document); 479 protected function strip_tag($tag, $document, $xpath, $type) 480 { 462 481 $elements = $xpath->query('body//' . $tag); 463 482 if ($this->encode_instead_of_strip) … … 542 561 } 543 562 544 protected function strip_attr($attrib, $document) 545 { 546 $xpath = new DOMXPath($document); 563 protected function strip_attr($attrib, $xpath) 564 { 547 565 $elements = $xpath->query('//*[@' . $attrib . ']'); 548 566 … … 552 570 } 553 571 } 572 573 protected function add_attr($tag, $valuePairs, $document) 574 { 575 $elements = $document->getElementsByTagName($tag); 576 foreach ($elements as $element) 577 { 578 foreach ($valuePairs as $attrib => $value) 579 { 580 $element->setAttribute($attrib, $value); 581 } 582 } 583 } 554 584 }
Note: See TracChangeset
for help on using the changeset viewer.