| 1 |
<?php |
| 2 |
// If this file is called directly, abort. |
| 3 |
if ( ! defined( 'ABSPATH' ) ) { |
| 4 |
exit; |
| 5 |
} |
| 6 |
|
| 7 |
/* Sanitize SVG during uploading */ |
| 8 |
class BORDERLESS_SvgSanitizer { |
| 9 |
|
| 10 |
private $document; |
| 11 |
private static $whitelist_elems = array(); |
| 12 |
private static $whitelist_attrs = array(); |
| 13 |
|
| 14 |
function __construct() { |
| 15 |
global $whitelist_elems; |
| 16 |
global $whitelist_attrs; |
| 17 |
|
| 18 |
$this->document = new DOMDocument(); |
| 19 |
$this->document->preserveWhiteSpace = FALSE; |
| 20 |
|
| 21 |
require_once 'whitelist.php'; |
| 22 |
|
| 23 |
self::$whitelist_elems = $whitelist_elems; |
| 24 |
self::$whitelist_attrs = $whitelist_attrs; |
| 25 |
} |
| 26 |
|
| 27 |
function load_svg( $file ) { |
| 28 |
$this->document->load( $file ); |
| 29 |
} |
| 30 |
|
| 31 |
function borderless_sanitize_svg() { |
| 32 |
$elems = $this->document->getElementsByTagName( "*" ); |
| 33 |
|
| 34 |
for( $i = 0; $i < $elems->length; $i++ ) { |
| 35 |
$node = $elems->item($i); |
| 36 |
|
| 37 |
$tag_name = $node->tagName; |
| 38 |
if( in_array( $tag_name, self::$whitelist_elems ) ) { |
| 39 |
for( $j = 0; $j < $node->attributes->length; $j++ ) { |
| 40 |
$attr_name = $node->attributes->item($j)->name; |
| 41 |
if( !in_array( $attr_name, self::$whitelist_attrs ) ) { |
| 42 |
$node->removeAttribute( $attr_name ); |
| 43 |
} |
| 44 |
} |
| 45 |
} else { |
| 46 |
$node->parentNode->removeChild( $node ); |
| 47 |
} |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
function save_svg() { |
| 52 |
$this->document->formatOutput = TRUE; |
| 53 |
|
| 54 |
return $this->document->saveXML(); |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
?> |