Retrieves a definition
public getDefinition ( string $type, boolean $raw = false, boolean $optimized = false ) : HTMLPurifier_Definition | ||
$type | string | Type of definition: HTML, CSS, etc |
$raw | boolean | Whether or not definition should be returned raw |
$optimized | boolean | Only has an effect when $raw is true. Whether or not to return null if the result is already present in the cache. This is off by default for backwards compatibility reasons, but you need to do things this way in order to ensure that caching is done properly. Check out enduser-customize.html for more details. We probably won't ever change this default, as much as the maybe semantics is the "right thing to do." |
Результат | HTMLPurifier_Definition |
/** * @param HTMLPurifier_Config $config * @return void */ public function prepare($config) { $our_host = $config->getDefinition('URI')->host; if ($our_host !== null) { $this->ourHostParts = array_reverse(explode('.', $our_host)); } }
/** * @param string $string * @param HTMLPurifier_Config $config * @param HTMLPurifier_Context $context * @return bool|string */ protected function split($string, $config, $context) { // really, this twiddle should be lazy loaded $name = $config->getDefinition('HTML')->doctype->name; if ($name == "XHTML 1.1" || $name == "XHTML 2.0") { return parent::split($string, $config, $context); } else { return preg_split('/\\s+/', $string); } }
/** * @param HTMLPurifier_Config $config * @return bool */ public function prepare($config) { $def = $config->getDefinition('URI'); $this->base = $def->base; if (is_null($this->base)) { trigger_error('URI.MakeAbsolute is being ignored due to lack of ' . 'value for URI.Base configuration', E_USER_WARNING); return false; } $this->base->fragment = null; // fragment is invalid for base URI $stack = explode('/', $this->base->path); array_pop($stack); // discard last segment $stack = $this->_collapseStack($stack); // do pre-parsing $this->basePathStack = $stack; return true; }
/** * Takes CSS (the stuff found in <style>) and cleans it. * @warning Requires CSSTidy <http://csstidy.sourceforge.net/> * @param string $css CSS styling to clean * @param HTMLPurifier_Config $config * @param HTMLPurifier_Context $context * @throws HTMLPurifier_Exception * @return string Cleaned CSS */ public function cleanCSS($css, $config, $context) { // prepare scope $scope = $config->get('Filter.ExtractStyleBlocks.Scope'); if ($scope !== null) { $scopes = array_map('trim', explode(',', $scope)); } else { $scopes = array(); } // remove comments from CSS $css = trim($css); if (strncmp('<!--', $css, 4) === 0) { $css = substr($css, 4); } if (strlen($css) > 3 && substr($css, -3) == '-->') { $css = substr($css, 0, -3); } $css = trim($css); set_error_handler('htmlpurifier_filter_extractstyleblocks_muteerrorhandler'); $this->_tidy->parse($css); restore_error_handler(); $css_definition = $config->getDefinition('CSS'); $html_definition = $config->getDefinition('HTML'); $new_css = array(); foreach ($this->_tidy->css as $k => $decls) { // $decls are all CSS declarations inside an @ selector $new_decls = array(); foreach ($decls as $selector => $style) { $selector = trim($selector); if ($selector === '') { continue; } // should not happen // Parse the selector // Here is the relevant part of the CSS grammar: // // ruleset // : selector [ ',' S* selector ]* '{' ... // selector // : simple_selector [ combinator selector | S+ [ combinator? selector ]? ]? // combinator // : '+' S* // : '>' S* // simple_selector // : element_name [ HASH | class | attrib | pseudo ]* // | [ HASH | class | attrib | pseudo ]+ // element_name // : IDENT | '*' // ; // class // : '.' IDENT // ; // attrib // : '[' S* IDENT S* [ [ '=' | INCLUDES | DASHMATCH ] S* // [ IDENT | STRING ] S* ]? ']' // ; // pseudo // : ':' [ IDENT | FUNCTION S* [IDENT S*]? ')' ] // ; // // For reference, here are the relevant tokens: // // HASH #{name} // IDENT {ident} // INCLUDES == // DASHMATCH |= // STRING {string} // FUNCTION {ident}\( // // And the lexical scanner tokens // // name {nmchar}+ // nmchar [_a-z0-9-]|{nonascii}|{escape} // nonascii [\240-\377] // escape {unicode}|\\[^\r\n\f0-9a-f] // unicode \\{h}}{1,6}(\r\n|[ \t\r\n\f])? // ident -?{nmstart}{nmchar*} // nmstart [_a-z]|{nonascii}|{escape} // string {string1}|{string2} // string1 \"([^\n\r\f\\"]|\\{nl}|{escape})*\" // string2 \'([^\n\r\f\\"]|\\{nl}|{escape})*\' // // We'll implement a subset (in order to reduce attack // surface); in particular: // // - No Unicode support // - No escapes support // - No string support (by proxy no attrib support) // - element_name is matched against allowed // elements (some people might find this // annoying...) // - Pseudo-elements one of :first-child, :link, // :visited, :active, :hover, :focus // handle ruleset $selectors = array_map('trim', explode(',', $selector)); $new_selectors = array(); foreach ($selectors as $sel) { // split on +, > and spaces $basic_selectors = preg_split('/\\s*([+> ])\\s*/', $sel, -1, PREG_SPLIT_DELIM_CAPTURE); // even indices are chunks, odd indices are // delimiters $nsel = null; $delim = null; // guaranteed to be non-null after // two loop iterations for ($i = 0, $c = count($basic_selectors); $i < $c; $i++) { $x = $basic_selectors[$i]; if ($i % 2) { // delimiter if ($x === ' ') { $delim = ' '; } else { $delim = ' ' . $x . ' '; } } else { // simple selector $components = preg_split('/([#.:])/', $x, -1, PREG_SPLIT_DELIM_CAPTURE); $sdelim = null; $nx = null; for ($j = 0, $cc = count($components); $j < $cc; $j++) { $y = $components[$j]; if ($j === 0) { if ($y === '*' || isset($html_definition->info[$y = strtolower($y)])) { $nx = $y; } else { // $nx stays null; this matters // if we don't manage to find // any valid selector content, // in which case we ignore the // outer $delim } } elseif ($j % 2) { // set delimiter $sdelim = $y; } else { $attrdef = null; if ($sdelim === '#') { $attrdef = $this->_id_attrdef; } elseif ($sdelim === '.') { $attrdef = $this->_class_attrdef; } elseif ($sdelim === ':') { $attrdef = $this->_enum_attrdef; } else { throw new HTMLPurifier_Exception('broken invariant sdelim and preg_split'); } $r = $attrdef->validate($y, $config, $context); if ($r !== false) { if ($r !== true) { $y = $r; } if ($nx === null) { $nx = ''; } $nx .= $sdelim . $y; } } } if ($nx !== null) { if ($nsel === null) { $nsel = $nx; } else { $nsel .= $delim . $nx; } } else { // delimiters to the left of invalid // basic selector ignored } } } if ($nsel !== null) { if (!empty($scopes)) { foreach ($scopes as $s) { $new_selectors[] = "{$s} {$nsel}"; } } else { $new_selectors[] = $nsel; } } } if (empty($new_selectors)) { continue; } $selector = implode(', ', $new_selectors); foreach ($style as $name => $value) { if (!isset($css_definition->info[$name])) { unset($style[$name]); continue; } $def = $css_definition->info[$name]; $ret = $def->validate($value, $config, $context); if ($ret === false) { unset($style[$name]); } else { $style[$name] = $ret; } } $new_decls[$selector] = $style; } $new_css[$k] = $new_decls; } // remove stuff that shouldn't be used, could be reenabled // after security risks are analyzed $this->_tidy->css = $new_css; $this->_tidy->import = array(); $this->_tidy->charset = null; $this->_tidy->namespace = null; $css = $this->_tidy->print->plain(); // we are going to escape any special characters <>& to ensure // that no funny business occurs (i.e. </style> in a font-family prop). if ($config->get('Filter.ExtractStyleBlocks.Escaping')) { $css = str_replace(array('<', '>', '&'), array('\\3C ', '\\3E ', '\\26 '), $css); } return $css; }
/** * Wraps an HTML fragment in the necessary HTML * @param string $html * @param HTMLPurifier_Config $config * @param HTMLPurifier_Context $context * @return string */ protected function wrapHTML($html, $config, $context) { $def = $config->getDefinition('HTML'); $ret = ''; if (!empty($def->doctype->dtdPublic) || !empty($def->doctype->dtdSystem)) { $ret .= '<!DOCTYPE html '; if (!empty($def->doctype->dtdPublic)) { $ret .= 'PUBLIC "' . $def->doctype->dtdPublic . '" '; } if (!empty($def->doctype->dtdSystem)) { $ret .= '"' . $def->doctype->dtdSystem . '" '; } $ret .= '>'; } $ret .= '<html><head>'; $ret .= '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />'; // No protection if $html contains a stray </div>! $ret .= '</head><body>' . $html . '</body></html>'; return $ret; }
public function test_getDefinition() { $this->schema->add('Cache.DefinitionImpl', null, 'string', true); $config = new HTMLPurifier_Config($this->schema); $this->expectException(new HTMLPurifier_Exception("Definition of Crust type not supported")); $config->getDefinition('Crust'); }
/** * Returns true if this URL might be considered a 'local' URL given * the current context. This is true when the host is null, or * when it matches the host supplied to the configuration. * * Note that this does not do any scheme checking, so it is mostly * only appropriate for metadata that doesn't care about protocol * security. isBenign is probably what you actually want. * @param HTMLPurifier_Config $config * @param HTMLPurifier_Context $context * @return bool */ public function isLocal($config, $context) { if ($this->host === null) { return true; } $uri_def = $config->getDefinition('URI'); if ($uri_def->host === $this->host) { return true; } return false; }
function test_getDefinition() { $this->schema->addNamespace('Cache', 'Cache stuff'); $this->schema->add('Cache', 'DefinitionImpl', null, 'string', true); $this->schema->addNamespace('Crust', 'Krusty Krabs'); $config = new HTMLPurifier_Config($this->schema); $this->expectException(new HTMLPurifier_Exception("Definition of Crust type not supported")); $config->getDefinition('Crust'); }