2006-07-23 00:11:03 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
// HTMLPurifier_ChildDef and inheritance have three types of output:
|
|
|
|
// true = leave nodes as is
|
|
|
|
// false = delete parent node and all children
|
|
|
|
// array(...) = replace children nodes with these
|
|
|
|
|
|
|
|
// this is the hardest one to implement. We'll use fancy regexp tricks
|
|
|
|
// right now, we only expect it to return TRUE or FALSE (it won't attempt
|
|
|
|
// to fix the tree)
|
|
|
|
|
|
|
|
// we may end up writing custom code for each HTML case
|
|
|
|
// in order to make it self correcting
|
2006-07-31 03:04:57 +00:00
|
|
|
|
2006-08-14 02:46:34 +00:00
|
|
|
HTMLPurifier_ConfigDef::define(
|
|
|
|
'Core', 'EscapeInvalidChildren', false,
|
|
|
|
'When true, a child is found that is not allowed in the context of the '.
|
|
|
|
'parent element will be transformed into text as if it were ASCII. When '.
|
2006-08-15 23:58:18 +00:00
|
|
|
'false, that element and all internal tags will be dropped, though text '.
|
|
|
|
'will be preserved. There is no option for dropping the element but '.
|
|
|
|
'preserving child nodes.'
|
2006-08-14 02:46:34 +00:00
|
|
|
);
|
|
|
|
|
2006-08-17 20:29:34 +00:00
|
|
|
/**
|
|
|
|
* Class that defines allowed child nodes and validates tokens against this.
|
|
|
|
*/
|
2006-07-23 00:11:03 +00:00
|
|
|
class HTMLPurifier_ChildDef
|
2006-07-31 03:04:57 +00:00
|
|
|
{
|
|
|
|
var $type;
|
|
|
|
var $allow_empty;
|
|
|
|
function validateChildren($tokens_of_children) {
|
|
|
|
trigger_error('Call to abstract function', E_USER_ERROR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class HTMLPurifier_ChildDef_Custom extends HTMLPurifier_ChildDef
|
2006-07-23 00:11:03 +00:00
|
|
|
{
|
|
|
|
var $type = 'custom';
|
2006-07-31 03:04:57 +00:00
|
|
|
var $allow_empty = false;
|
2006-07-23 00:11:03 +00:00
|
|
|
var $dtd_regex;
|
|
|
|
var $_pcre_regex;
|
2006-07-31 03:04:57 +00:00
|
|
|
function HTMLPurifier_ChildDef_Custom($dtd_regex) {
|
2006-07-23 00:11:03 +00:00
|
|
|
$this->dtd_regex = $dtd_regex;
|
|
|
|
$this->_compileRegex();
|
|
|
|
}
|
|
|
|
function _compileRegex() {
|
|
|
|
$raw = str_replace(' ', '', $this->dtd_regex);
|
|
|
|
if ($raw{0} != '(') {
|
|
|
|
$raw = "($raw)";
|
|
|
|
}
|
|
|
|
$reg = str_replace(',', ',?', $raw);
|
|
|
|
$reg = preg_replace('/([#a-zA-Z0-9_.-]+)/', '(,?\\0)', $reg);
|
|
|
|
$this->_pcre_regex = $reg;
|
|
|
|
}
|
2006-08-14 02:46:34 +00:00
|
|
|
function validateChildren($tokens_of_children, $config, $context) {
|
2006-07-23 00:11:03 +00:00
|
|
|
$list_of_children = '';
|
|
|
|
$nesting = 0; // depth into the nest
|
|
|
|
foreach ($tokens_of_children as $token) {
|
|
|
|
if (!empty($token->is_whitespace)) continue;
|
|
|
|
|
|
|
|
$is_child = ($nesting == 0); // direct
|
|
|
|
|
|
|
|
if ($token->type == 'start') {
|
|
|
|
$nesting++;
|
|
|
|
} elseif ($token->type == 'end') {
|
|
|
|
$nesting--;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($is_child) {
|
|
|
|
$list_of_children .= $token->name . ',';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$list_of_children = rtrim($list_of_children, ',');
|
|
|
|
|
|
|
|
$okay =
|
|
|
|
preg_match(
|
|
|
|
'/^'.$this->_pcre_regex.'$/',
|
|
|
|
$list_of_children
|
|
|
|
);
|
|
|
|
|
|
|
|
return (bool) $okay;
|
|
|
|
}
|
|
|
|
}
|
2006-07-31 03:04:57 +00:00
|
|
|
|
|
|
|
class HTMLPurifier_ChildDef_Required extends HTMLPurifier_ChildDef
|
2006-07-23 00:11:03 +00:00
|
|
|
{
|
|
|
|
var $elements = array();
|
2006-07-31 03:04:57 +00:00
|
|
|
function HTMLPurifier_ChildDef_Required($elements) {
|
2006-07-23 00:11:03 +00:00
|
|
|
if (is_string($elements)) {
|
|
|
|
$elements = str_replace(' ', '', $elements);
|
|
|
|
$elements = explode('|', $elements);
|
|
|
|
}
|
|
|
|
$elements = array_flip($elements);
|
|
|
|
foreach ($elements as $i => $x) $elements[$i] = true;
|
|
|
|
$this->elements = $elements;
|
|
|
|
$this->gen = new HTMLPurifier_Generator();
|
|
|
|
}
|
2006-07-31 03:04:57 +00:00
|
|
|
var $allow_empty = false;
|
2006-07-23 00:11:03 +00:00
|
|
|
var $type = 'required';
|
2006-08-14 02:46:34 +00:00
|
|
|
function validateChildren($tokens_of_children, $config, $context) {
|
2006-07-23 00:11:03 +00:00
|
|
|
// if there are no tokens, delete parent node
|
|
|
|
if (empty($tokens_of_children)) return false;
|
|
|
|
|
|
|
|
// the new set of children
|
|
|
|
$result = array();
|
|
|
|
|
|
|
|
// current depth into the nest
|
|
|
|
$nesting = 0;
|
|
|
|
|
|
|
|
// whether or not we're deleting a node
|
|
|
|
$is_deleting = false;
|
|
|
|
|
|
|
|
// whether or not parsed character data is allowed
|
|
|
|
// this controls whether or not we silently drop a tag
|
|
|
|
// or generate escaped HTML from it
|
|
|
|
$pcdata_allowed = isset($this->elements['#PCDATA']);
|
|
|
|
|
|
|
|
// a little sanity check to make sure it's not ALL whitespace
|
|
|
|
$all_whitespace = true;
|
|
|
|
|
2006-08-14 02:46:34 +00:00
|
|
|
// some configuration
|
|
|
|
$escape_invalid_children = $config->get('Core', 'EscapeInvalidChildren');
|
|
|
|
|
2006-07-23 00:11:03 +00:00
|
|
|
foreach ($tokens_of_children as $token) {
|
|
|
|
if (!empty($token->is_whitespace)) {
|
|
|
|
$result[] = $token;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$all_whitespace = false; // phew, we're not talking about whitespace
|
|
|
|
|
|
|
|
$is_child = ($nesting == 0);
|
|
|
|
|
|
|
|
if ($token->type == 'start') {
|
|
|
|
$nesting++;
|
|
|
|
} elseif ($token->type == 'end') {
|
|
|
|
$nesting--;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($is_child) {
|
|
|
|
$is_deleting = false;
|
|
|
|
if (!isset($this->elements[$token->name])) {
|
|
|
|
$is_deleting = true;
|
2006-08-15 23:58:18 +00:00
|
|
|
if ($pcdata_allowed && $token->type == 'text') {
|
|
|
|
$result[] = $token;
|
|
|
|
} elseif ($pcdata_allowed && $escape_invalid_children) {
|
2006-08-14 02:46:34 +00:00
|
|
|
$result[] = new HTMLPurifier_Token_Text(
|
2006-08-15 00:31:12 +00:00
|
|
|
$this->gen->generateFromToken($token, $config)
|
2006-08-14 02:46:34 +00:00
|
|
|
);
|
2006-07-23 00:11:03 +00:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2006-08-15 23:58:18 +00:00
|
|
|
if (!$is_deleting || ($pcdata_allowed && $token->type == 'text')) {
|
2006-07-23 00:11:03 +00:00
|
|
|
$result[] = $token;
|
2006-08-14 02:46:34 +00:00
|
|
|
} elseif ($pcdata_allowed && $escape_invalid_children) {
|
|
|
|
$result[] =
|
|
|
|
new HTMLPurifier_Token_Text(
|
2006-08-15 00:31:12 +00:00
|
|
|
$this->gen->generateFromToken( $token, $config )
|
2006-08-14 02:46:34 +00:00
|
|
|
);
|
2006-07-23 00:11:03 +00:00
|
|
|
} else {
|
|
|
|
// drop silently
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (empty($result)) return false;
|
|
|
|
if ($all_whitespace) return false;
|
|
|
|
if ($tokens_of_children == $result) return true;
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// only altered behavior is that it returns an empty array
|
|
|
|
// instead of a false (to delete the node)
|
|
|
|
class HTMLPurifier_ChildDef_Optional extends HTMLPurifier_ChildDef_Required
|
|
|
|
{
|
2006-07-31 03:04:57 +00:00
|
|
|
var $allow_empty = true;
|
2006-07-23 00:11:03 +00:00
|
|
|
var $type = 'optional';
|
2006-08-14 02:46:34 +00:00
|
|
|
function validateChildren($tokens_of_children, $config, $context) {
|
|
|
|
$result = parent::validateChildren($tokens_of_children, $config, $context);
|
2006-07-23 00:11:03 +00:00
|
|
|
if ($result === false) return array();
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// placeholder
|
|
|
|
class HTMLPurifier_ChildDef_Empty extends HTMLPurifier_ChildDef
|
|
|
|
{
|
2006-07-31 03:04:57 +00:00
|
|
|
var $allow_empty = true;
|
2006-07-23 00:11:03 +00:00
|
|
|
var $type = 'empty';
|
|
|
|
function HTMLPurifier_ChildDef_Empty() {}
|
2006-08-14 02:46:34 +00:00
|
|
|
function validateChildren($tokens_of_children, $config, $context) {
|
2006-07-23 00:11:03 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-08-03 01:03:23 +00:00
|
|
|
class HTMLPurifier_ChildDef_Chameleon extends HTMLPurifier_ChildDef
|
|
|
|
{
|
|
|
|
|
|
|
|
var $inline;
|
|
|
|
var $block;
|
|
|
|
|
|
|
|
function HTMLPurifier_ChildDef_Chameleon($inline, $block) {
|
|
|
|
$this->inline = new HTMLPurifier_ChildDef_Optional($inline);
|
|
|
|
$this->block = new HTMLPurifier_ChildDef_Optional($block);
|
|
|
|
}
|
|
|
|
|
2006-08-14 02:46:34 +00:00
|
|
|
function validateChildren($tokens_of_children, $config, $context) {
|
2006-08-03 01:03:23 +00:00
|
|
|
switch ($context) {
|
|
|
|
case 'unknown':
|
|
|
|
case 'inline':
|
2006-08-14 02:46:34 +00:00
|
|
|
$result = $this->inline->validateChildren(
|
|
|
|
$tokens_of_children, $config, $context);
|
2006-08-03 01:03:23 +00:00
|
|
|
break;
|
|
|
|
case 'block':
|
2006-08-14 02:46:34 +00:00
|
|
|
$result = $this->block->validateChildren(
|
|
|
|
$tokens_of_children, $config, $context);
|
2006-08-03 01:03:23 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
trigger_error('Invalid context', E_USER_ERROR);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-21 23:58:12 +00:00
|
|
|
?>
|