2006-07-24 01:50:02 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
require_once 'HTMLPurifier/Strategy.php';
|
2006-08-14 21:22:49 +00:00
|
|
|
require_once 'HTMLPurifier/HTMLDefinition.php';
|
2006-07-24 01:50:02 +00:00
|
|
|
require_once 'HTMLPurifier/Generator.php';
|
2006-08-02 02:24:03 +00:00
|
|
|
require_once 'HTMLPurifier/TagTransform.php';
|
2006-07-24 01:50:02 +00:00
|
|
|
|
2006-11-23 23:59:20 +00:00
|
|
|
HTMLPurifier_ConfigSchema::define(
|
|
|
|
'Core', 'RemoveInvalidImg', true, 'bool',
|
|
|
|
'This directive enables pre-emptive URI checking in <code>img</code> '.
|
|
|
|
'tags, as the attribute validation strategy is not authorized to '.
|
|
|
|
'remove elements from the document. This directive has been available '.
|
|
|
|
'since 1.3.0, revert to pre-1.3.0 behavior by setting to false.'
|
|
|
|
);
|
|
|
|
|
2006-07-30 19:11:18 +00:00
|
|
|
/**
|
|
|
|
* Removes all unrecognized tags from the list of tokens.
|
|
|
|
*
|
|
|
|
* This strategy iterates through all the tokens and removes unrecognized
|
2006-08-02 02:26:01 +00:00
|
|
|
* tokens. If a token is not recognized but a TagTransform is defined for
|
|
|
|
* that element, the element will be transformed accordingly.
|
2006-07-30 19:11:18 +00:00
|
|
|
*/
|
|
|
|
|
2006-07-24 01:50:02 +00:00
|
|
|
class HTMLPurifier_Strategy_RemoveForeignElements extends HTMLPurifier_Strategy
|
|
|
|
{
|
|
|
|
|
2006-10-01 20:47:07 +00:00
|
|
|
function execute($tokens, $config, &$context) {
|
2006-08-31 20:33:07 +00:00
|
|
|
$definition = $config->getHTMLDefinition();
|
|
|
|
$generator = new HTMLPurifier_Generator();
|
2006-07-24 01:50:02 +00:00
|
|
|
$result = array();
|
2006-08-15 23:58:18 +00:00
|
|
|
$escape_invalid_tags = $config->get('Core', 'EscapeInvalidTags');
|
2007-04-30 00:53:13 +00:00
|
|
|
$remove_invalid_img = $config->get('Core', 'RemoveInvalidImg');
|
2006-07-24 01:50:02 +00:00
|
|
|
foreach($tokens as $token) {
|
|
|
|
if (!empty( $token->is_tag )) {
|
2006-07-30 19:11:18 +00:00
|
|
|
// DEFINITION CALL
|
2006-08-31 20:33:07 +00:00
|
|
|
if (isset($definition->info[$token->name])) {
|
2006-11-23 23:59:20 +00:00
|
|
|
// leave untouched, except for a few special cases:
|
|
|
|
|
|
|
|
// hard-coded image special case, pre-emptively drop
|
|
|
|
// if not available. Probably not abstract-able
|
2007-04-30 00:53:13 +00:00
|
|
|
if ( $token->name == 'img' && $remove_invalid_img ) {
|
2006-12-06 22:29:08 +00:00
|
|
|
if (!isset($token->attr['src'])) {
|
2006-12-06 22:12:44 +00:00
|
|
|
continue;
|
|
|
|
}
|
2006-11-23 23:59:20 +00:00
|
|
|
if (!isset($definition->info['img']->attr['src'])) {
|
|
|
|
continue;
|
|
|
|
}
|
2006-12-06 22:29:08 +00:00
|
|
|
$token->attr['src'] =
|
2006-11-23 23:59:20 +00:00
|
|
|
$definition->
|
|
|
|
info['img']->
|
|
|
|
attr['src']->
|
2006-12-06 22:29:08 +00:00
|
|
|
validate($token->attr['src'],
|
2006-12-06 22:12:44 +00:00
|
|
|
$config, $context);
|
2006-12-06 22:29:08 +00:00
|
|
|
if ($token->attr['src'] === false) continue;
|
2006-11-23 23:59:20 +00:00
|
|
|
}
|
|
|
|
|
2006-08-02 02:24:03 +00:00
|
|
|
} elseif (
|
2006-08-31 20:33:07 +00:00
|
|
|
isset($definition->info_tag_transform[$token->name])
|
2006-08-02 02:24:03 +00:00
|
|
|
) {
|
|
|
|
// there is a transformation for this tag
|
|
|
|
// DEFINITION CALL
|
2006-08-31 20:33:07 +00:00
|
|
|
$token = $definition->
|
|
|
|
info_tag_transform[$token->name]->
|
2006-10-22 15:56:38 +00:00
|
|
|
transform($token, $config, $context);
|
2006-08-15 23:58:18 +00:00
|
|
|
} elseif ($escape_invalid_tags) {
|
2006-07-24 01:50:02 +00:00
|
|
|
// invalid tag, generate HTML and insert in
|
|
|
|
$token = new HTMLPurifier_Token_Text(
|
2006-10-01 20:47:07 +00:00
|
|
|
$generator->generateFromToken($token, $config, $context)
|
2006-07-24 01:50:02 +00:00
|
|
|
);
|
2006-08-15 23:58:18 +00:00
|
|
|
} else {
|
|
|
|
continue;
|
2006-07-24 01:50:02 +00:00
|
|
|
}
|
|
|
|
} elseif ($token->type == 'comment') {
|
|
|
|
// strip comments
|
|
|
|
continue;
|
|
|
|
} elseif ($token->type == 'text') {
|
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$result[] = $token;
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
?>
|