123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387 |
- <?php
- class HTMLPurifier_Lexer
- {
-
- public $tracksLineNumbers = false;
-
- private $_entity_parser;
-
-
- public static function create($config)
- {
- if (!($config instanceof HTMLPurifier_Config)) {
- $lexer = $config;
- trigger_error(
- "Passing a prototype to
- HTMLPurifier_Lexer::create() is deprecated, please instead
- use %Core.LexerImpl",
- E_USER_WARNING
- );
- } else {
- $lexer = $config->get('Core.LexerImpl');
- }
- $needs_tracking =
- $config->get('Core.MaintainLineNumbers') ||
- $config->get('Core.CollectErrors');
- $inst = null;
- if (is_object($lexer)) {
- $inst = $lexer;
- } else {
- if (is_null($lexer)) {
- do {
-
- if ($needs_tracking) {
- $lexer = 'DirectLex';
- break;
- }
- if (class_exists('DOMDocument') &&
- method_exists('DOMDocument', 'loadHTML') &&
- !extension_loaded('domxml')
- ) {
-
-
-
-
- $lexer = 'DOMLex';
- } else {
- $lexer = 'DirectLex';
- }
- } while (0);
- }
-
- switch ($lexer) {
- case 'DOMLex':
- $inst = new HTMLPurifier_Lexer_DOMLex();
- break;
- case 'DirectLex':
- $inst = new HTMLPurifier_Lexer_DirectLex();
- break;
- case 'PH5P':
- $inst = new HTMLPurifier_Lexer_PH5P();
- break;
- default:
- throw new HTMLPurifier_Exception(
- "Cannot instantiate unrecognized Lexer type " .
- htmlspecialchars($lexer)
- );
- }
- }
- if (!$inst) {
- throw new HTMLPurifier_Exception('No lexer was instantiated');
- }
-
-
- if ($needs_tracking && !$inst->tracksLineNumbers) {
- throw new HTMLPurifier_Exception(
- 'Cannot use lexer that does not support line numbers with ' .
- 'Core.MaintainLineNumbers or Core.CollectErrors (use DirectLex instead)'
- );
- }
- return $inst;
- }
-
- public function __construct()
- {
- $this->_entity_parser = new HTMLPurifier_EntityParser();
- }
-
- protected $_special_entity2str =
- array(
- '"' => '"',
- '&' => '&',
- '<' => '<',
- '>' => '>',
- ''' => "'",
- ''' => "'",
- ''' => "'"
- );
- public function parseText($string, $config) {
- return $this->parseData($string, false, $config);
- }
- public function parseAttr($string, $config) {
- return $this->parseData($string, true, $config);
- }
-
- public function parseData($string, $is_attr, $config)
- {
-
- if ($string === '') {
- return '';
- }
-
- $num_amp = substr_count($string, '&') - substr_count($string, '& ') -
- ($string[strlen($string) - 1] === '&' ? 1 : 0);
- if (!$num_amp) {
- return $string;
- }
- $num_esc_amp = substr_count($string, '&');
- $string = strtr($string, $this->_special_entity2str);
-
- $num_amp_2 = substr_count($string, '&') - substr_count($string, '& ') -
- ($string[strlen($string) - 1] === '&' ? 1 : 0);
- if ($num_amp_2 <= $num_esc_amp) {
- return $string;
- }
-
- if ($config->get('Core.LegacyEntityDecoder')) {
- $string = $this->_entity_parser->substituteSpecialEntities($string);
- } else {
- if ($is_attr) {
- $string = $this->_entity_parser->substituteAttrEntities($string);
- } else {
- $string = $this->_entity_parser->substituteTextEntities($string);
- }
- }
- return $string;
- }
-
- public function tokenizeHTML($string, $config, $context)
- {
- trigger_error('Call to abstract class', E_USER_ERROR);
- }
-
- protected static function escapeCDATA($string)
- {
- return preg_replace_callback(
- '/<!\[CDATA\[(.+?)\]\]>/s',
- array('HTMLPurifier_Lexer', 'CDATACallback'),
- $string
- );
- }
-
- protected static function escapeCommentedCDATA($string)
- {
- return preg_replace_callback(
- '#<!--//--><!\[CDATA\[//><!--(.+?)//--><!\]\]>#s',
- array('HTMLPurifier_Lexer', 'CDATACallback'),
- $string
- );
- }
-
- protected static function removeIEConditional($string)
- {
- return preg_replace(
- '#<!--\[if [^>]+\]>.*?<!\[endif\]-->#si',
- '',
- $string
- );
- }
-
- protected static function CDATACallback($matches)
- {
-
- return htmlspecialchars($matches[1], ENT_COMPAT, 'UTF-8');
- }
-
- public function normalize($html, $config, $context)
- {
-
- if ($config->get('Core.NormalizeNewlines')) {
- $html = str_replace("\r\n", "\n", (string)$html);
- $html = str_replace("\r", "\n", (string)$html);
- }
- if ($config->get('HTML.Trusted')) {
-
- $html = $this->escapeCommentedCDATA($html);
- }
-
- $html = $this->escapeCDATA($html);
- $html = $this->removeIEConditional($html);
-
- if ($config->get('Core.ConvertDocumentToFragment')) {
- $e = false;
- if ($config->get('Core.CollectErrors')) {
- $e =& $context->get('ErrorCollector');
- }
- $new_html = $this->extractBody($html);
- if ($e && $new_html != $html) {
- $e->send(E_WARNING, 'Lexer: Extracted body');
- }
- $html = $new_html;
- }
-
- if ($config->get('Core.LegacyEntityDecoder')) {
- $html = $this->_entity_parser->substituteNonSpecialEntities($html);
- }
-
-
-
- $html = HTMLPurifier_Encoder::cleanUTF8($html);
-
- if ($config->get('Core.RemoveProcessingInstructions')) {
- $html = preg_replace('#<\?.+?\?>#s', '', $html);
- }
- $hidden_elements = $config->get('Core.HiddenElements');
- if ($config->get('Core.AggressivelyRemoveScript') &&
- !($config->get('HTML.Trusted') || !$config->get('Core.RemoveScriptContents')
- || empty($hidden_elements["script"]))) {
- $html = preg_replace('#<script[^>]*>.*?</script>#i', '', $html);
- }
- return $html;
- }
-
- public function extractBody($html)
- {
- $matches = array();
- $result = preg_match('|(.*?)<body[^>]*>(.*)</body>|is', $html, $matches);
- if ($result) {
-
- $comment_start = strrpos($matches[1], '<!--');
- $comment_end = strrpos($matches[1], '-->');
- if ($comment_start === false ||
- ($comment_end !== false && $comment_end > $comment_start)) {
- return $matches[2];
- }
- }
- return $html;
- }
- }
|