2006-07-23 00:11:03 +00:00
|
|
|
<?php
|
|
|
|
|
2006-08-19 17:53:59 +00:00
|
|
|
require_once 'HTMLPurifier/Lexer.php';
|
|
|
|
|
2006-09-16 22:36:58 +00:00
|
|
|
HTMLPurifier_ConfigSchema::define(
|
2006-08-27 18:49:16 +00:00
|
|
|
'Core', 'CleanUTF8DuringGeneration', false, 'bool',
|
2006-08-19 17:53:59 +00:00
|
|
|
'When true, HTMLPurifier_Generator will also check all strings it '.
|
|
|
|
'escapes for UTF-8 well-formedness as a defense in depth measure. '.
|
|
|
|
'This could cause a considerable performance impact, and is not '.
|
|
|
|
'strictly necessary due to the fact that the Lexers should have '.
|
|
|
|
'ensured that all the UTF-8 strings were well-formed. Note that '.
|
|
|
|
'the configuration value is only read at the beginning of '.
|
|
|
|
'generateFromTokens.'
|
|
|
|
);
|
|
|
|
|
2006-09-16 22:36:58 +00:00
|
|
|
HTMLPurifier_ConfigSchema::define(
|
2006-09-16 00:37:33 +00:00
|
|
|
'Core', 'XHTML', true, 'bool',
|
|
|
|
'Determines whether or not output is XHTML or not. When disabled, HTML '.
|
|
|
|
'Purifier goes into HTML 4.01 removes XHTML-specific markup constructs, '.
|
|
|
|
'such as boolean attribute expansion and trailing slashes in empty tags. '.
|
|
|
|
'This directive was available since 1.1.'
|
|
|
|
);
|
|
|
|
|
2007-05-15 01:17:10 +00:00
|
|
|
HTMLPurifier_ConfigSchema::define(
|
|
|
|
'Core', 'CommentScriptContents', true, 'bool',
|
|
|
|
'Determines whether or not HTML Purifier should attempt to fix up '.
|
|
|
|
'the contents of script tags for legacy browsers with comments. This '.
|
|
|
|
'directive was available since 1.7.'
|
|
|
|
);
|
|
|
|
|
2006-09-24 21:23:54 +00:00
|
|
|
// extension constraints could be factored into ConfigSchema
|
|
|
|
HTMLPurifier_ConfigSchema::define(
|
|
|
|
'Core', 'TidyFormat', false, 'bool',
|
2006-09-24 21:58:14 +00:00
|
|
|
'<p>Determines whether or not to run Tidy on the final output for pretty '.
|
|
|
|
'formatting reasons, such as indentation and wrap.</p><p>This can greatly '.
|
2006-09-24 21:23:54 +00:00
|
|
|
'improve readability for editors who are hand-editing the HTML, but is '.
|
|
|
|
'by no means necessary as HTML Purifier has already fixed all major '.
|
2006-09-24 21:58:14 +00:00
|
|
|
'errors the HTML may have had. Tidy is a non-default extension, and this directive '.
|
|
|
|
'will silently fail if Tidy is not available.</p><p>If you are looking to make '.
|
|
|
|
'the overall look of your page\'s source better, I recommend running Tidy '.
|
|
|
|
'on the entire page rather than just user-content (after all, the '.
|
|
|
|
'indentation relative to the containing blocks will be incorrect).</p><p>This '.
|
|
|
|
'directive was available since 1.1.1.</p>'
|
2006-09-24 21:23:54 +00:00
|
|
|
);
|
|
|
|
|
2006-08-20 20:59:13 +00:00
|
|
|
/**
|
|
|
|
* Generates HTML from tokens.
|
|
|
|
*/
|
2006-07-23 00:11:03 +00:00
|
|
|
class HTMLPurifier_Generator
|
|
|
|
{
|
|
|
|
|
2006-08-20 20:59:13 +00:00
|
|
|
/**
|
2006-09-16 00:37:33 +00:00
|
|
|
* Bool cache of %Core.CleanUTF8DuringGeneration
|
2006-08-20 20:59:13 +00:00
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
var $_clean_utf8 = false;
|
2006-08-19 17:53:59 +00:00
|
|
|
|
2006-09-16 00:37:33 +00:00
|
|
|
/**
|
|
|
|
* Bool cache of %Core.XHTML
|
2006-10-09 16:07:35 +00:00
|
|
|
* @private
|
2006-09-16 00:37:33 +00:00
|
|
|
*/
|
|
|
|
var $_xhtml = true;
|
|
|
|
|
2007-05-15 01:17:10 +00:00
|
|
|
/**
|
|
|
|
* Bool cache of %Core.CommentScriptContents
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
var $_scriptFix = false;
|
|
|
|
|
2006-08-20 20:59:13 +00:00
|
|
|
/**
|
|
|
|
* Generates HTML from an array of tokens.
|
|
|
|
* @param $tokens Array of HTMLPurifier_Token
|
|
|
|
* @param $config HTMLPurifier_Config object
|
|
|
|
* @return Generated HTML
|
|
|
|
*/
|
2006-10-01 21:55:13 +00:00
|
|
|
function generateFromTokens($tokens, $config, &$context) {
|
2006-07-23 00:11:03 +00:00
|
|
|
$html = '';
|
2006-08-15 00:31:12 +00:00
|
|
|
if (!$config) $config = HTMLPurifier_Config::createDefault();
|
2007-05-15 01:17:10 +00:00
|
|
|
$this->_clean_utf8 = $config->get('Core', 'CleanUTF8DuringGeneration');
|
|
|
|
$this->_xhtml = $config->get('Core', 'XHTML');
|
|
|
|
$this->_scriptFix = $config->get('Core', 'CommentScriptContents');
|
2006-07-30 18:37:42 +00:00
|
|
|
if (!$tokens) return '';
|
2007-05-15 01:17:10 +00:00
|
|
|
for ($i = 0, $size = count($tokens); $i < $size; $i++) {
|
|
|
|
if ($this->_scriptFix && $tokens[$i]->name === 'script') {
|
|
|
|
// script special case
|
|
|
|
$html .= $this->generateFromToken($tokens[$i++]);
|
|
|
|
$html .= $this->generateScriptFromToken($tokens[$i++]);
|
|
|
|
while ($tokens[$i]->name != 'script') {
|
|
|
|
$html .= $this->generateScriptFromToken($tokens[$i++]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$html .= $this->generateFromToken($tokens[$i]);
|
2006-07-23 00:11:03 +00:00
|
|
|
}
|
2006-09-24 21:23:54 +00:00
|
|
|
if ($config->get('Core', 'TidyFormat') && extension_loaded('tidy')) {
|
|
|
|
|
|
|
|
$tidy_options = array(
|
|
|
|
'indent'=> true,
|
|
|
|
'output-xhtml' => $this->_xhtml,
|
|
|
|
'show-body-only' => true,
|
|
|
|
'indent-spaces' => 2,
|
|
|
|
'wrap' => 68,
|
|
|
|
);
|
|
|
|
if (version_compare(PHP_VERSION, '5', '<')) {
|
|
|
|
tidy_set_encoding('utf8');
|
|
|
|
foreach ($tidy_options as $key => $value) {
|
|
|
|
tidy_setopt($key, $value);
|
|
|
|
}
|
|
|
|
tidy_parse_string($html);
|
|
|
|
tidy_clean_repair();
|
|
|
|
$html = tidy_get_output();
|
|
|
|
} else {
|
|
|
|
$tidy = new Tidy;
|
|
|
|
$tidy->parseString($html, $tidy_options, 'utf8');
|
|
|
|
$tidy->cleanRepair();
|
|
|
|
$html = (string) $tidy;
|
|
|
|
}
|
|
|
|
}
|
2006-07-23 00:11:03 +00:00
|
|
|
return $html;
|
|
|
|
}
|
|
|
|
|
2006-08-20 20:59:13 +00:00
|
|
|
/**
|
|
|
|
* Generates HTML from a single token.
|
|
|
|
* @param $token HTMLPurifier_Token object.
|
|
|
|
* @return Generated HTML
|
|
|
|
*/
|
2006-08-19 17:53:59 +00:00
|
|
|
function generateFromToken($token) {
|
2006-08-01 00:29:38 +00:00
|
|
|
if (!isset($token->type)) return '';
|
2006-07-23 00:11:03 +00:00
|
|
|
if ($token->type == 'start') {
|
2006-12-06 22:29:08 +00:00
|
|
|
$attr = $this->generateAttributes($token->attr);
|
2006-07-23 00:11:03 +00:00
|
|
|
return '<' . $token->name . ($attr ? ' ' : '') . $attr . '>';
|
|
|
|
|
|
|
|
} elseif ($token->type == 'end') {
|
|
|
|
return '</' . $token->name . '>';
|
|
|
|
|
|
|
|
} elseif ($token->type == 'empty') {
|
2006-12-06 22:29:08 +00:00
|
|
|
$attr = $this->generateAttributes($token->attr);
|
2006-09-16 00:37:33 +00:00
|
|
|
return '<' . $token->name . ($attr ? ' ' : '') . $attr .
|
|
|
|
( $this->_xhtml ? ' /': '' )
|
|
|
|
. '>';
|
2006-07-23 00:11:03 +00:00
|
|
|
|
|
|
|
} elseif ($token->type == 'text') {
|
2006-08-19 17:53:59 +00:00
|
|
|
return $this->escape($token->data);
|
2006-07-23 00:11:03 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
return '';
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-15 01:17:10 +00:00
|
|
|
/**
|
|
|
|
* Special case processor for the contents of script tags
|
|
|
|
* @warning This runs into problems if there's already a literal
|
|
|
|
* --> somewhere inside the script contents.
|
|
|
|
*/
|
|
|
|
function generateScriptFromToken($token) {
|
|
|
|
if (!$token->type == 'text') return $this->generateFromToken($token);
|
|
|
|
return '<!--' . PHP_EOL . $token->data . PHP_EOL . '// -->';
|
|
|
|
// more advanced version:
|
|
|
|
// return '<!--//--><![CDATA[//><!--' . PHP_EOL . $token->data . PHP_EOL . '//--><!]]>';
|
|
|
|
}
|
|
|
|
|
2006-08-20 20:59:13 +00:00
|
|
|
/**
|
|
|
|
* Generates attribute declarations from attribute array.
|
|
|
|
* @param $assoc_array_of_attributes Attribute array
|
|
|
|
* @return Generate HTML fragment for insertion.
|
|
|
|
*/
|
2006-08-19 17:53:59 +00:00
|
|
|
function generateAttributes($assoc_array_of_attributes) {
|
2006-07-23 00:11:03 +00:00
|
|
|
$html = '';
|
|
|
|
foreach ($assoc_array_of_attributes as $key => $value) {
|
2006-09-16 00:37:33 +00:00
|
|
|
if (!$this->_xhtml) {
|
|
|
|
// remove namespaced attributes
|
|
|
|
if (strpos($key, ':') !== false) continue;
|
|
|
|
// also needed: check for attribute minimization
|
|
|
|
}
|
2006-08-19 17:53:59 +00:00
|
|
|
$html .= $key.'="'.$this->escape($value).'" ';
|
2006-07-23 00:11:03 +00:00
|
|
|
}
|
|
|
|
return rtrim($html);
|
|
|
|
}
|
|
|
|
|
2006-08-20 20:59:13 +00:00
|
|
|
/**
|
|
|
|
* Escapes raw text data.
|
|
|
|
* @param $string String data to escape for HTML.
|
|
|
|
* @return String escaped data.
|
|
|
|
*/
|
2006-08-19 17:53:59 +00:00
|
|
|
function escape($string) {
|
2006-08-20 20:59:13 +00:00
|
|
|
if ($this->_clean_utf8) $string = HTMLPurifier_Lexer::cleanUTF8($string);
|
2006-08-19 17:53:59 +00:00
|
|
|
return htmlspecialchars($string, ENT_COMPAT, 'UTF-8');
|
|
|
|
}
|
|
|
|
|
2006-07-23 00:11:03 +00:00
|
|
|
}
|
|
|
|
|
2006-04-16 03:27:40 +00:00
|
|
|
?>
|