ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
Lexer.php
Go to the documentation of this file.
1<?php
2
43{
44
49 public $tracksLineNumbers = false;
50
51 // -- STATIC ----------------------------------------------------------
52
69 public static function create($config)
70 {
71 if (!($config instanceof HTMLPurifier_Config)) {
72 $lexer = $config;
73 trigger_error(
74 "Passing a prototype to
75 HTMLPurifier_Lexer::create() is deprecated, please instead
76 use %Core.LexerImpl",
77 E_USER_WARNING
78 );
79 } else {
80 $lexer = $config->get('Core.LexerImpl');
81 }
82
83 $needs_tracking =
84 $config->get('Core.MaintainLineNumbers') ||
85 $config->get('Core.CollectErrors');
86
87 $inst = null;
88 if (is_object($lexer)) {
89 $inst = $lexer;
90 } else {
91 if (is_null($lexer)) {
92 do {
93 // auto-detection algorithm
94 if ($needs_tracking) {
95 $lexer = 'DirectLex';
96 break;
97 }
98
99 if (class_exists('DOMDocument', false) &&
100 method_exists('DOMDocument', 'loadHTML') &&
101 !extension_loaded('domxml')
102 ) {
103 // check for DOM support, because while it's part of the
104 // core, it can be disabled compile time. Also, the PECL
105 // domxml extension overrides the default DOM, and is evil
106 // and nasty and we shan't bother to support it
107 $lexer = 'DOMLex';
108 } else {
109 $lexer = 'DirectLex';
110 }
111 } while (0);
112 } // do..while so we can break
113
114 // instantiate recognized string names
115 switch ($lexer) {
116 case 'DOMLex':
117 $inst = new HTMLPurifier_Lexer_DOMLex();
118 break;
119 case 'DirectLex':
120 $inst = new HTMLPurifier_Lexer_DirectLex();
121 break;
122 case 'PH5P':
123 $inst = new HTMLPurifier_Lexer_PH5P();
124 break;
125 default:
126 throw new HTMLPurifier_Exception(
127 "Cannot instantiate unrecognized Lexer type " .
128 htmlspecialchars($lexer)
129 );
130 }
131 }
132
133 if (!$inst) {
134 throw new HTMLPurifier_Exception('No lexer was instantiated');
135 }
136
137 // once PHP DOM implements native line numbers, or we
138 // hack out something using XSLT, remove this stipulation
139 if ($needs_tracking && !$inst->tracksLineNumbers) {
140 throw new HTMLPurifier_Exception(
141 'Cannot use lexer that does not support line numbers with ' .
142 'Core.MaintainLineNumbers or Core.CollectErrors (use DirectLex instead)'
143 );
144 }
145
146 return $inst;
147
148 }
149
150 // -- CONVENIENCE MEMBERS ---------------------------------------------
151
152 public function __construct()
153 {
154 $this->_entity_parser = new HTMLPurifier_EntityParser();
155 }
156
162 array(
163 '&quot;' => '"',
164 '&amp;' => '&',
165 '&lt;' => '<',
166 '&gt;' => '>',
167 '&#39;' => "'",
168 '&#039;' => "'",
169 '&#x27;' => "'"
170 );
171
172 public function parseText($string, $config) {
173 return $this->parseData($string, false, $config);
174 }
175
176 public function parseAttr($string, $config) {
177 return $this->parseData($string, true, $config);
178 }
179
189 public function parseData($string, $is_attr, $config)
190 {
191 // following functions require at least one character
192 if ($string === '') {
193 return '';
194 }
195
196 // subtracts amps that cannot possibly be escaped
197 $num_amp = substr_count($string, '&') - substr_count($string, '& ') -
198 ($string[strlen($string) - 1] === '&' ? 1 : 0);
199
200 if (!$num_amp) {
201 return $string;
202 } // abort if no entities
203 $num_esc_amp = substr_count($string, '&amp;');
204 $string = strtr($string, $this->_special_entity2str);
205
206 // code duplication for sake of optimization, see above
207 $num_amp_2 = substr_count($string, '&') - substr_count($string, '& ') -
208 ($string[strlen($string) - 1] === '&' ? 1 : 0);
209
210 if ($num_amp_2 <= $num_esc_amp) {
211 return $string;
212 }
213
214 // hmm... now we have some uncommon entities. Use the callback.
215 if ($config->get('Core.LegacyEntityDecoder')) {
216 $string = $this->_entity_parser->substituteSpecialEntities($string);
217 } else {
218 if ($is_attr) {
219 $string = $this->_entity_parser->substituteAttrEntities($string);
220 } else {
221 $string = $this->_entity_parser->substituteTextEntities($string);
222 }
223 }
224 return $string;
225 }
226
234 public function tokenizeHTML($string, $config, $context)
235 {
236 trigger_error('Call to abstract class', E_USER_ERROR);
237 }
238
244 protected static function escapeCDATA($string)
245 {
246 return preg_replace_callback(
247 '/<!\[CDATA\[(.+?)\]\]>/s',
248 array('HTMLPurifier_Lexer', 'CDATACallback'),
249 $string
250 );
251 }
252
258 protected static function escapeCommentedCDATA($string)
259 {
260 return preg_replace_callback(
261 '#<!--//--><!\[CDATA\[//><!--(.+?)//--><!\]\]>#s',
262 array('HTMLPurifier_Lexer', 'CDATACallback'),
263 $string
264 );
265 }
266
272 protected static function removeIEConditional($string)
273 {
274 return preg_replace(
275 '#<!--\[if [^>]+\]>.*?<!\[endif\]-->#si', // probably should generalize for all strings
276 '',
277 $string
278 );
279 }
280
290 protected static function CDATACallback($matches)
291 {
292 // not exactly sure why the character set is needed, but whatever
293 return htmlspecialchars($matches[1], ENT_COMPAT, 'UTF-8');
294 }
295
305 public function normalize($html, $config, $context)
306 {
307 // normalize newlines to \n
308 if ($config->get('Core.NormalizeNewlines')) {
309 $html = str_replace("\r\n", "\n", $html);
310 $html = str_replace("\r", "\n", $html);
311 }
312
313 if ($config->get('HTML.Trusted')) {
314 // escape convoluted CDATA
316 }
317
318 // escape CDATA
319 $html = $this->escapeCDATA($html);
320
322
323 // extract body from document if applicable
324 if ($config->get('Core.ConvertDocumentToFragment')) {
325 $e = false;
326 if ($config->get('Core.CollectErrors')) {
327 $e =& $context->get('ErrorCollector');
328 }
329 $new_html = $this->extractBody($html);
330 if ($e && $new_html != $html) {
331 $e->send(E_WARNING, 'Lexer: Extracted body');
332 }
333 $html = $new_html;
334 }
335
336 // expand entities that aren't the big five
337 if ($config->get('Core.LegacyEntityDecoder')) {
338 $html = $this->_entity_parser->substituteNonSpecialEntities($html);
339 }
340
341 // clean into wellformed UTF-8 string for an SGML context: this has
342 // to be done after entity expansion because the entities sometimes
343 // represent non-SGML characters (horror, horror!)
345
346 // if processing instructions are to removed, remove them now
347 if ($config->get('Core.RemoveProcessingInstructions')) {
348 $html = preg_replace('#<\?.+?\?>#s', '', $html);
349 }
350
351 $hidden_elements = $config->get('Core.HiddenElements');
352 if ($config->get('Core.AggressivelyRemoveScript') &&
353 !($config->get('HTML.Trusted') || !$config->get('Core.RemoveScriptContents')
354 || empty($hidden_elements["script"]))) {
355 $html = preg_replace('#<script[^>]*>.*?</script>#i', '', $html);
356 }
357
358 return $html;
359 }
360
365 public function extractBody($html)
366 {
367 $matches = array();
368 $result = preg_match('|(.*?)<body[^>]*>(.*)</body>|is', $html, $matches);
369 if ($result) {
370 // Make sure it's not in a comment
371 $comment_start = strrpos($matches[1], '<!--');
372 $comment_end = strrpos($matches[1], '-->');
373 if ($comment_start === false ||
374 ($comment_end !== false && $comment_end > $comment_start)) {
375 return $matches[2];
376 }
377 }
378 return $html;
379 }
380}
381
382// vim: et sw=4 sts=4
$result
An exception for terminatinating execution or to throw for unit testing.
Configuration object that triggers customizable behavior.
Definition: Config.php:18
static cleanUTF8($str, $force_php=false)
Cleans a UTF-8 string for well-formedness and SGML validity.
Definition: Encoder.php:134
Handles referencing and derefencing character entities.
Global exception class for HTML Purifier; any exceptions we throw are from here.
Definition: Exception.php:8
Parser that uses PHP 5's DOM extension (part of the core).
Definition: DOMLex.php:28
Our in-house implementation of a parser.
Definition: DirectLex.php:14
Experimental HTML5-based parser using Jeroen van der Meer's PH5P library.
Definition: PH5P.php:14
Forgivingly lexes HTML (SGML-style) markup into tokens.
Definition: Lexer.php:43
normalize($html, $config, $context)
Takes a piece of HTML and normalizes it by converting entities, fixing encoding, extracting bits,...
Definition: Lexer.php:305
parseData($string, $is_attr, $config)
Parses special entities into the proper characters.
Definition: Lexer.php:189
parseText($string, $config)
Definition: Lexer.php:172
tokenizeHTML($string, $config, $context)
Lexes an HTML string into tokens.
Definition: Lexer.php:234
parseAttr($string, $config)
Definition: Lexer.php:176
static CDATACallback($matches)
Callback function for escapeCDATA() that does the work.
Definition: Lexer.php:290
static escapeCDATA($string)
Translates CDATA sections into regular sections (through escaping).
Definition: Lexer.php:244
static removeIEConditional($string)
Special Internet Explorer conditional comments should be removed.
Definition: Lexer.php:272
extractBody($html)
Takes a string of HTML (fragment or document) and returns the content.
Definition: Lexer.php:365
static escapeCommentedCDATA($string)
Special CDATA case that is especially convoluted for <script>
Definition: Lexer.php:258
static create($config)
Retrieves or sets the default Lexer as a Prototype Factory.
Definition: Lexer.php:69
$tracksLineNumbers
Whether or not this lexer implements line-number/column-number tracking.
Definition: Lexer.php:49
$_special_entity2str
Most common entity to raw value conversion table for special entities.
Definition: Lexer.php:161
$html
Definition: example_001.php:87