2006-07-23 23:29:12 +00:00
|
|
|
<?php
|
|
|
|
|
2006-07-24 01:50:41 +00:00
|
|
|
require_once 'HTMLPurifier/Strategy.php';
|
2006-08-14 21:22:49 +00:00
|
|
|
require_once 'HTMLPurifier/HTMLDefinition.php';
|
2006-07-24 01:50:41 +00:00
|
|
|
require_once 'HTMLPurifier/Generator.php';
|
2006-07-23 23:29:12 +00:00
|
|
|
|
2007-06-23 18:50:41 +00:00
|
|
|
require_once 'HTMLPurifier/Injector/AutoParagraph.php';
|
2007-06-24 02:45:38 +00:00
|
|
|
require_once 'HTMLPurifier/Injector/Linkify.php';
|
2007-06-23 18:50:41 +00:00
|
|
|
|
2007-06-22 21:32:56 +00:00
|
|
|
HTMLPurifier_ConfigSchema::define(
|
|
|
|
'Core', 'AutoParagraph', false, 'bool', '
|
|
|
|
<p>
|
|
|
|
This directive will cause HTML Purifier to automatically paragraph text
|
|
|
|
in the document fragment root based on two newlines and block tags.
|
|
|
|
This directive has been available since 2.0.1.
|
|
|
|
</p>
|
|
|
|
'
|
|
|
|
);
|
|
|
|
|
2007-06-24 02:45:38 +00:00
|
|
|
HTMLPurifier_ConfigSchema::define(
|
|
|
|
'Core', 'AutoLinkify', false, 'bool', '
|
|
|
|
<p>
|
|
|
|
This directive will cause HTML Purifier to automatically linkify
|
|
|
|
text that looks like URLs. This directive has been available since
|
|
|
|
2.0.1.
|
|
|
|
</p>
|
|
|
|
'
|
|
|
|
);
|
|
|
|
|
2006-08-20 21:59:41 +00:00
|
|
|
/**
|
|
|
|
* Takes tokens makes them well-formed (balance end tags, etc.)
|
|
|
|
*/
|
2006-07-23 23:29:12 +00:00
|
|
|
class HTMLPurifier_Strategy_MakeWellFormed extends HTMLPurifier_Strategy
|
|
|
|
{
|
|
|
|
|
2007-06-24 17:44:27 +00:00
|
|
|
/**
|
|
|
|
* Locally shared variable references
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
var $inputTokens, $inputIndex, $outputTokens, $currentNesting,
|
|
|
|
$currentInjector, $injectors;
|
|
|
|
|
2006-10-01 20:47:07 +00:00
|
|
|
function execute($tokens, $config, &$context) {
|
2007-06-23 17:11:05 +00:00
|
|
|
|
2006-08-31 20:33:07 +00:00
|
|
|
$definition = $config->getHTMLDefinition();
|
2007-06-23 17:11:05 +00:00
|
|
|
|
2007-06-24 17:44:27 +00:00
|
|
|
// CurrentNesting
|
|
|
|
$this->currentNesting = array();
|
|
|
|
$context->register('CurrentNesting', $this->currentNesting);
|
2007-06-23 17:11:05 +00:00
|
|
|
|
2007-06-24 17:44:27 +00:00
|
|
|
// InputIndex
|
|
|
|
$this->inputIndex = false;
|
|
|
|
$context->register('InputIndex', $this->inputIndex);
|
|
|
|
|
|
|
|
// InputTokens
|
2007-06-23 17:11:05 +00:00
|
|
|
$context->register('InputTokens', $tokens);
|
2007-06-24 17:44:27 +00:00
|
|
|
$this->inputTokens =& $tokens;
|
2007-06-23 17:11:05 +00:00
|
|
|
|
2007-06-24 17:44:27 +00:00
|
|
|
// OutputTokens
|
2007-06-23 17:11:05 +00:00
|
|
|
$result = array();
|
2007-06-24 17:44:27 +00:00
|
|
|
$this->outputTokens =& $result;
|
2007-06-22 21:32:56 +00:00
|
|
|
|
2007-06-24 17:44:27 +00:00
|
|
|
// %Core.EscapeInvalidTags
|
2006-08-15 23:58:18 +00:00
|
|
|
$escape_invalid_tags = $config->get('Core', 'EscapeInvalidTags');
|
2007-06-24 17:44:27 +00:00
|
|
|
$generator = new HTMLPurifier_Generator();
|
2007-06-22 21:32:56 +00:00
|
|
|
|
2007-06-24 04:22:28 +00:00
|
|
|
// -- begin INJECTOR --
|
|
|
|
|
2007-06-24 17:44:27 +00:00
|
|
|
$this->injectors = array();
|
2007-06-23 17:44:28 +00:00
|
|
|
|
2007-06-24 17:44:27 +00:00
|
|
|
// we need a generic way of adding injectors, and also its own
|
|
|
|
// configuration namespace
|
2007-06-24 02:27:57 +00:00
|
|
|
if ($config->get('Core', 'AutoParagraph')) {
|
2007-06-24 17:44:27 +00:00
|
|
|
$this->injectors[] = new HTMLPurifier_Injector_AutoParagraph();
|
2007-06-24 02:27:57 +00:00
|
|
|
}
|
|
|
|
|
2007-06-24 02:45:38 +00:00
|
|
|
if ($config->get('Core', 'AutoLinkify')) {
|
2007-06-24 17:44:27 +00:00
|
|
|
$this->injectors[] = new HTMLPurifier_Injector_Linkify();
|
2007-06-24 02:45:38 +00:00
|
|
|
}
|
|
|
|
|
2007-06-24 04:22:28 +00:00
|
|
|
// array index of the injector that resulted in an array
|
|
|
|
// substitution. This enables processTokens() to know which
|
|
|
|
// injectors are affected by the added tokens and which are
|
|
|
|
// not (namely, the ones after the current injector are not
|
|
|
|
// affected)
|
2007-06-24 17:44:27 +00:00
|
|
|
$this->currentInjector = false;
|
2007-06-24 02:27:57 +00:00
|
|
|
|
2007-06-24 17:44:27 +00:00
|
|
|
// give the injectors references to the definition and context
|
|
|
|
// variables for performance reasons
|
|
|
|
foreach ($this->injectors as $i => $x) {
|
|
|
|
$this->injectors[$i]->prepare($config, $context);
|
|
|
|
}
|
2007-06-24 04:22:28 +00:00
|
|
|
|
|
|
|
// -- end INJECTOR --
|
|
|
|
|
2007-06-24 17:44:27 +00:00
|
|
|
for ($this->inputIndex = 0; isset($tokens[$this->inputIndex]); $this->inputIndex++) {
|
2007-06-23 17:11:05 +00:00
|
|
|
|
|
|
|
// if all goes well, this token will be passed through unharmed
|
2007-06-24 17:44:27 +00:00
|
|
|
$token = $tokens[$this->inputIndex];
|
2007-06-23 17:11:05 +00:00
|
|
|
|
2007-06-24 17:44:27 +00:00
|
|
|
foreach ($this->injectors as $i => $x) {
|
|
|
|
if ($x->skip > 0) $this->injectors[$i]->skip--;
|
2007-06-23 17:44:28 +00:00
|
|
|
}
|
|
|
|
|
2007-06-23 17:11:05 +00:00
|
|
|
// quick-check: if it's not a tag, no need to process
|
2006-07-23 23:29:12 +00:00
|
|
|
if (empty( $token->is_tag )) {
|
2007-06-23 17:44:28 +00:00
|
|
|
if ($token->type === 'text') {
|
2007-06-24 17:44:27 +00:00
|
|
|
// injector handler code; duplicated for performance reasons
|
|
|
|
foreach ($this->injectors as $i => $x) {
|
|
|
|
if (!$x->skip) $x->handleText($token, $config, $context);
|
2007-06-24 02:45:38 +00:00
|
|
|
if (is_array($token)) {
|
2007-06-24 17:44:27 +00:00
|
|
|
$this->currentInjector = $i;
|
2007-06-24 02:45:38 +00:00
|
|
|
break;
|
|
|
|
}
|
2007-06-23 17:44:28 +00:00
|
|
|
}
|
2007-06-22 21:32:56 +00:00
|
|
|
}
|
2007-06-23 17:44:28 +00:00
|
|
|
$this->processToken($token, $config, $context);
|
2006-07-23 23:29:12 +00:00
|
|
|
continue;
|
|
|
|
}
|
2006-07-30 19:11:18 +00:00
|
|
|
|
2006-08-31 20:33:07 +00:00
|
|
|
$info = $definition->info[$token->name]->child;
|
2006-07-23 23:29:12 +00:00
|
|
|
|
2007-06-24 17:44:27 +00:00
|
|
|
// quick checks:
|
2006-07-23 23:29:12 +00:00
|
|
|
// test if it claims to be a start tag but is empty
|
2007-06-23 17:11:05 +00:00
|
|
|
if ($info->type == 'empty' && $token->type == 'start') {
|
|
|
|
$result[] = new HTMLPurifier_Token_Empty($token->name, $token->attr);
|
2006-07-23 23:29:12 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// test if it claims to be empty but really is a start tag
|
2007-06-23 17:11:05 +00:00
|
|
|
if ($info->type != 'empty' && $token->type == 'empty' ) {
|
|
|
|
$result[] = new HTMLPurifier_Token_Start($token->name, $token->attr);
|
2006-07-23 23:29:12 +00:00
|
|
|
$result[] = new HTMLPurifier_Token_End($token->name);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// automatically insert empty tags
|
|
|
|
if ($token->type == 'empty') {
|
|
|
|
$result[] = $token;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2007-06-23 17:11:05 +00:00
|
|
|
// start tags have precedence, so they get passed through...
|
2006-07-23 23:29:12 +00:00
|
|
|
if ($token->type == 'start') {
|
|
|
|
|
2007-06-23 17:11:05 +00:00
|
|
|
// ...unless they also have to close their parent
|
2007-06-24 17:44:27 +00:00
|
|
|
if (!empty($this->currentNesting)) {
|
2006-07-23 23:29:12 +00:00
|
|
|
|
2007-06-24 17:44:27 +00:00
|
|
|
$parent = array_pop($this->currentNesting);
|
2007-06-23 17:11:05 +00:00
|
|
|
$parent_info = $definition->info[$parent->name];
|
2006-07-30 22:57:54 +00:00
|
|
|
|
2007-06-23 17:11:05 +00:00
|
|
|
// this can be replaced with a more general algorithm:
|
|
|
|
// if the token is not allowed by the parent, auto-close
|
|
|
|
// the parent
|
2007-06-23 20:52:57 +00:00
|
|
|
if (!isset($parent_info->child->elements[$token->name])) {
|
|
|
|
// close the parent, then append the token
|
2007-06-23 17:11:05 +00:00
|
|
|
$result[] = new HTMLPurifier_Token_End($parent->name);
|
2006-07-23 23:29:12 +00:00
|
|
|
$result[] = $token;
|
2007-06-24 17:44:27 +00:00
|
|
|
$this->currentNesting[] = $token;
|
2006-07-23 23:29:12 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2007-06-24 17:44:27 +00:00
|
|
|
$this->currentNesting[] = $parent; // undo the pop
|
2006-07-23 23:29:12 +00:00
|
|
|
}
|
|
|
|
|
2007-06-24 17:44:27 +00:00
|
|
|
// injector handler code; duplicated for performance reasons
|
|
|
|
foreach ($this->injectors as $i => $x) {
|
|
|
|
if (!$x->skip[$i]) $x->handleStart($token, $config, $context);
|
2007-06-24 02:45:38 +00:00
|
|
|
if (is_array($token)) {
|
2007-06-24 17:44:27 +00:00
|
|
|
$this->currentInjector = $i;
|
2007-06-24 02:45:38 +00:00
|
|
|
break;
|
|
|
|
}
|
2007-06-23 17:44:28 +00:00
|
|
|
}
|
2007-06-22 21:32:56 +00:00
|
|
|
|
2007-06-23 17:44:28 +00:00
|
|
|
$this->processToken($token, $config, $context);
|
2006-07-23 23:29:12 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2007-06-23 17:11:05 +00:00
|
|
|
// sanity check: we should be dealing with a closing tag
|
2006-07-23 23:29:12 +00:00
|
|
|
if ($token->type != 'end') continue;
|
|
|
|
|
|
|
|
// make sure that we have something open
|
2007-06-24 17:44:27 +00:00
|
|
|
if (empty($this->currentNesting)) {
|
2006-08-15 23:58:18 +00:00
|
|
|
if ($escape_invalid_tags) {
|
|
|
|
$result[] = new HTMLPurifier_Token_Text(
|
2006-10-01 20:47:07 +00:00
|
|
|
$generator->generateFromToken($token, $config, $context)
|
2006-08-15 23:58:18 +00:00
|
|
|
);
|
|
|
|
}
|
2006-07-23 23:29:12 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// first, check for the simplest case: everything closes neatly
|
2007-06-24 17:44:27 +00:00
|
|
|
$current_parent = array_pop($this->currentNesting);
|
2006-07-23 23:29:12 +00:00
|
|
|
if ($current_parent->name == $token->name) {
|
|
|
|
$result[] = $token;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// okay, so we're trying to close the wrong tag
|
|
|
|
|
2007-06-23 17:11:05 +00:00
|
|
|
// undo the pop previous pop
|
2007-06-24 17:44:27 +00:00
|
|
|
$this->currentNesting[] = $current_parent;
|
2007-06-23 17:11:05 +00:00
|
|
|
|
|
|
|
// scroll back the entire nest, trying to find our tag.
|
|
|
|
// (feature could be to specify how far you'd like to go)
|
2007-06-24 17:44:27 +00:00
|
|
|
$size = count($this->currentNesting);
|
2006-07-23 23:29:12 +00:00
|
|
|
// -2 because -1 is the last element, but we already checked that
|
|
|
|
$skipped_tags = false;
|
|
|
|
for ($i = $size - 2; $i >= 0; $i--) {
|
2007-06-24 17:44:27 +00:00
|
|
|
if ($this->currentNesting[$i]->name == $token->name) {
|
2006-07-23 23:29:12 +00:00
|
|
|
// current nesting is modified
|
2007-06-24 17:44:27 +00:00
|
|
|
$skipped_tags = array_splice($this->currentNesting, $i);
|
2006-07-23 23:29:12 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-23 17:11:05 +00:00
|
|
|
// we still didn't find the tag, so remove
|
2006-07-23 23:29:12 +00:00
|
|
|
if ($skipped_tags === false) {
|
2006-08-15 23:58:18 +00:00
|
|
|
if ($escape_invalid_tags) {
|
|
|
|
$result[] = new HTMLPurifier_Token_Text(
|
2006-10-01 20:47:07 +00:00
|
|
|
$generator->generateFromToken($token, $config, $context)
|
2006-08-15 23:58:18 +00:00
|
|
|
);
|
|
|
|
}
|
2006-07-23 23:29:12 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// okay, we found it, close all the skipped tags
|
|
|
|
// note that skipped tags contains the element we need closed
|
|
|
|
$size = count($skipped_tags);
|
|
|
|
for ($i = $size - 1; $i >= 0; $i--) {
|
|
|
|
$result[] = new HTMLPurifier_Token_End($skipped_tags[$i]->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// we're at the end now, fix all still unclosed tags
|
2007-06-23 17:44:28 +00:00
|
|
|
// not using processToken() because at this point we don't
|
|
|
|
// care about current nesting
|
2007-06-24 17:44:27 +00:00
|
|
|
if (!empty($this->currentNesting)) {
|
|
|
|
$size = count($this->currentNesting);
|
2006-07-23 23:29:12 +00:00
|
|
|
for ($i = $size - 1; $i >= 0; $i--) {
|
|
|
|
$result[] =
|
2007-06-24 17:44:27 +00:00
|
|
|
new HTMLPurifier_Token_End($this->currentNesting[$i]->name);
|
2006-07-23 23:29:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-23 17:11:05 +00:00
|
|
|
$context->destroy('CurrentNesting');
|
|
|
|
$context->destroy('InputTokens');
|
|
|
|
$context->destroy('InputIndex');
|
|
|
|
|
2007-06-24 17:44:27 +00:00
|
|
|
unset($this->outputTokens, $this->injectors, $this->currentInjector,
|
|
|
|
$this->currentNesting, $this->inputTokens, $this->inputIndex);
|
2007-06-24 04:22:28 +00:00
|
|
|
|
2006-07-23 23:29:12 +00:00
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2007-06-23 17:44:28 +00:00
|
|
|
function processToken($token, $config, &$context) {
|
|
|
|
if (is_array($token)) {
|
2007-06-24 04:22:28 +00:00
|
|
|
// the original token was overloaded by an injector, time
|
2007-06-23 17:44:28 +00:00
|
|
|
// to some fancy acrobatics
|
|
|
|
|
2007-06-24 17:44:27 +00:00
|
|
|
// $this->inputIndex is decremented so that the entire set gets
|
2007-06-23 17:44:28 +00:00
|
|
|
// re-processed
|
2007-06-24 17:44:27 +00:00
|
|
|
array_splice($this->inputTokens, $this->inputIndex--, 1, $token);
|
2007-06-23 17:44:28 +00:00
|
|
|
|
2007-06-24 04:22:28 +00:00
|
|
|
// adjust the injector skips based on the array substitution
|
|
|
|
$offset = count($token) + 1;
|
2007-06-24 17:44:27 +00:00
|
|
|
for ($i = 0; $i <= $this->currentInjector; $i++) {
|
|
|
|
$this->injectors[$i]->skip += $offset;
|
2007-06-24 02:27:57 +00:00
|
|
|
}
|
2007-06-23 17:44:28 +00:00
|
|
|
} elseif ($token) {
|
|
|
|
// regular case
|
2007-06-24 17:44:27 +00:00
|
|
|
$this->outputTokens[] = $token;
|
2007-06-23 17:44:28 +00:00
|
|
|
if ($token->type == 'start') {
|
2007-06-24 17:44:27 +00:00
|
|
|
$this->currentNesting[] = $token;
|
2007-06-23 17:44:28 +00:00
|
|
|
} elseif ($token->type == 'end') {
|
2007-06-24 17:44:27 +00:00
|
|
|
array_pop($this->currentNesting); // not actually used
|
2007-06-23 17:44:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-23 23:29:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
?>
|