HTMLPurifier 4.4.0
|
00001 <?php 00002 00014 class HTMLPurifier_Strategy_MakeWellFormed extends HTMLPurifier_Strategy 00015 { 00016 00020 protected $tokens; 00021 00025 protected $t; 00026 00030 protected $stack; 00031 00035 protected $injectors; 00036 00040 protected $config; 00041 00045 protected $context; 00046 00047 public function execute($tokens, $config, $context) { 00048 00049 $definition = $config->getHTMLDefinition(); 00050 00051 // local variables 00052 $generator = new HTMLPurifier_Generator($config, $context); 00053 $escape_invalid_tags = $config->get('Core.EscapeInvalidTags'); 00054 // used for autoclose early abortion 00055 $global_parent_allowed_elements = array(); 00056 if (isset($definition->info[$definition->info_parent])) { 00057 // may be unset under testing circumstances 00058 $global_parent_allowed_elements = $definition->info[$definition->info_parent]->child->getAllowedElements($config); 00059 } 00060 $e = $context->get('ErrorCollector', true); 00061 $t = false; // token index 00062 $i = false; // injector index 00063 $token = false; // the current token 00064 $reprocess = false; // whether or not to reprocess the same token 00065 $stack = array(); 00066 00067 // member variables 00068 $this->stack =& $stack; 00069 $this->t =& $t; 00070 $this->tokens =& $tokens; 00071 $this->config = $config; 00072 $this->context = $context; 00073 00074 // context variables 00075 $context->register('CurrentNesting', $stack); 00076 $context->register('InputIndex', $t); 00077 $context->register('InputTokens', $tokens); 00078 $context->register('CurrentToken', $token); 00079 00080 // -- begin INJECTOR -- 00081 00082 $this->injectors = array(); 00083 00084 $injectors = $config->getBatch('AutoFormat'); 00085 $def_injectors = $definition->info_injector; 00086 $custom_injectors = $injectors['Custom']; 00087 unset($injectors['Custom']); // special case 00088 foreach ($injectors as $injector => $b) { 00089 // XXX: Fix with a legitimate lookup table of enabled filters 00090 if (strpos($injector, '.') !== false) continue; 00091 $injector = "HTMLPurifier_Injector_$injector"; 00092 if (!$b) continue; 00093 $this->injectors[] = new $injector; 00094 } 00095 foreach ($def_injectors as $injector) { 00096 // assumed to be objects 00097 $this->injectors[] = $injector; 00098 } 00099 foreach ($custom_injectors as $injector) { 00100 if (!$injector) continue; 00101 if (is_string($injector)) { 00102 $injector = "HTMLPurifier_Injector_$injector"; 00103 $injector = new $injector; 00104 } 00105 $this->injectors[] = $injector; 00106 } 00107 00108 // give the injectors references to the definition and context 00109 // variables for performance reasons 00110 foreach ($this->injectors as $ix => $injector) { 00111 $error = $injector->prepare($config, $context); 00112 if (!$error) continue; 00113 array_splice($this->injectors, $ix, 1); // rm the injector 00114 trigger_error("Cannot enable {$injector->name} injector because $error is not allowed", E_USER_WARNING); 00115 } 00116 00117 // -- end INJECTOR -- 00118 00119 // a note on reprocessing: 00120 // In order to reduce code duplication, whenever some code needs 00121 // to make HTML changes in order to make things "correct", the 00122 // new HTML gets sent through the purifier, regardless of its 00123 // status. This means that if we add a start token, because it 00124 // was totally necessary, we don't have to update nesting; we just 00125 // punt ($reprocess = true; continue;) and it does that for us. 00126 00127 // isset is in loop because $tokens size changes during loop exec 00128 for ( 00129 $t = 0; 00130 $t == 0 || isset($tokens[$t - 1]); 00131 // only increment if we don't need to reprocess 00132 $reprocess ? $reprocess = false : $t++ 00133 ) { 00134 00135 // check for a rewind 00136 if (is_int($i) && $i >= 0) { 00137 // possibility: disable rewinding if the current token has a 00138 // rewind set on it already. This would offer protection from 00139 // infinite loop, but might hinder some advanced rewinding. 00140 $rewind_to = $this->injectors[$i]->getRewind(); 00141 if (is_int($rewind_to) && $rewind_to < $t) { 00142 if ($rewind_to < 0) $rewind_to = 0; 00143 while ($t > $rewind_to) { 00144 $t--; 00145 $prev = $tokens[$t]; 00146 // indicate that other injectors should not process this token, 00147 // but we need to reprocess it 00148 unset($prev->skip[$i]); 00149 $prev->rewind = $i; 00150 if ($prev instanceof HTMLPurifier_Token_Start) array_pop($this->stack); 00151 elseif ($prev instanceof HTMLPurifier_Token_End) $this->stack[] = $prev->start; 00152 } 00153 } 00154 $i = false; 00155 } 00156 00157 // handle case of document end 00158 if (!isset($tokens[$t])) { 00159 // kill processing if stack is empty 00160 if (empty($this->stack)) break; 00161 00162 // peek 00163 $top_nesting = array_pop($this->stack); 00164 $this->stack[] = $top_nesting; 00165 00166 // send error [TagClosedSuppress] 00167 if ($e && !isset($top_nesting->armor['MakeWellFormed_TagClosedError'])) { 00168 $e->send(E_NOTICE, 'Strategy_MakeWellFormed: Tag closed by document end', $top_nesting); 00169 } 00170 00171 // append, don't splice, since this is the end 00172 $tokens[] = new HTMLPurifier_Token_End($top_nesting->name); 00173 00174 // punt! 00175 $reprocess = true; 00176 continue; 00177 } 00178 00179 $token = $tokens[$t]; 00180 00181 //echo '<br>'; printTokens($tokens, $t); printTokens($this->stack); 00182 //flush(); 00183 00184 // quick-check: if it's not a tag, no need to process 00185 if (empty($token->is_tag)) { 00186 if ($token instanceof HTMLPurifier_Token_Text) { 00187 foreach ($this->injectors as $i => $injector) { 00188 if (isset($token->skip[$i])) continue; 00189 if ($token->rewind !== null && $token->rewind !== $i) continue; 00190 $injector->handleText($token); 00191 $this->processToken($token, $i); 00192 $reprocess = true; 00193 break; 00194 } 00195 } 00196 // another possibility is a comment 00197 continue; 00198 } 00199 00200 if (isset($definition->info[$token->name])) { 00201 $type = $definition->info[$token->name]->child->type; 00202 } else { 00203 $type = false; // Type is unknown, treat accordingly 00204 } 00205 00206 // quick tag checks: anything that's *not* an end tag 00207 $ok = false; 00208 if ($type === 'empty' && $token instanceof HTMLPurifier_Token_Start) { 00209 // claims to be a start tag but is empty 00210 $token = new HTMLPurifier_Token_Empty($token->name, $token->attr, $token->line, $token->col, $token->armor); 00211 $ok = true; 00212 } elseif ($type && $type !== 'empty' && $token instanceof HTMLPurifier_Token_Empty) { 00213 // claims to be empty but really is a start tag 00214 $this->swap(new HTMLPurifier_Token_End($token->name)); 00215 $this->insertBefore(new HTMLPurifier_Token_Start($token->name, $token->attr, $token->line, $token->col, $token->armor)); 00216 // punt (since we had to modify the input stream in a non-trivial way) 00217 $reprocess = true; 00218 continue; 00219 } elseif ($token instanceof HTMLPurifier_Token_Empty) { 00220 // real empty token 00221 $ok = true; 00222 } elseif ($token instanceof HTMLPurifier_Token_Start) { 00223 // start tag 00224 00225 // ...unless they also have to close their parent 00226 if (!empty($this->stack)) { 00227 00228 // Performance note: you might think that it's rather 00229 // inefficient, recalculating the autoclose information 00230 // for every tag that a token closes (since when we 00231 // do an autoclose, we push a new token into the 00232 // stream and then /process/ that, before 00233 // re-processing this token.) But this is 00234 // necessary, because an injector can make an 00235 // arbitrary transformations to the autoclosing 00236 // tokens we introduce, so things may have changed 00237 // in the meantime. Also, doing the inefficient thing is 00238 // "easy" to reason about (for certain perverse definitions 00239 // of "easy") 00240 00241 $parent = array_pop($this->stack); 00242 $this->stack[] = $parent; 00243 00244 if (isset($definition->info[$parent->name])) { 00245 $elements = $definition->info[$parent->name]->child->getAllowedElements($config); 00246 $autoclose = !isset($elements[$token->name]); 00247 } else { 00248 $autoclose = false; 00249 } 00250 00251 if ($autoclose && $definition->info[$token->name]->wrap) { 00252 // Check if an element can be wrapped by another 00253 // element to make it valid in a context (for 00254 // example, <ul><ul> needs a <li> in between) 00255 $wrapname = $definition->info[$token->name]->wrap; 00256 $wrapdef = $definition->info[$wrapname]; 00257 $elements = $wrapdef->child->getAllowedElements($config); 00258 $parent_elements = $definition->info[$parent->name]->child->getAllowedElements($config); 00259 if (isset($elements[$token->name]) && isset($parent_elements[$wrapname])) { 00260 $newtoken = new HTMLPurifier_Token_Start($wrapname); 00261 $this->insertBefore($newtoken); 00262 $reprocess = true; 00263 continue; 00264 } 00265 } 00266 00267 $carryover = false; 00268 if ($autoclose && $definition->info[$parent->name]->formatting) { 00269 $carryover = true; 00270 } 00271 00272 if ($autoclose) { 00273 // check if this autoclose is doomed to fail 00274 // (this rechecks $parent, which his harmless) 00275 $autoclose_ok = isset($global_parent_allowed_elements[$token->name]); 00276 if (!$autoclose_ok) { 00277 foreach ($this->stack as $ancestor) { 00278 $elements = $definition->info[$ancestor->name]->child->getAllowedElements($config); 00279 if (isset($elements[$token->name])) { 00280 $autoclose_ok = true; 00281 break; 00282 } 00283 if ($definition->info[$token->name]->wrap) { 00284 $wrapname = $definition->info[$token->name]->wrap; 00285 $wrapdef = $definition->info[$wrapname]; 00286 $wrap_elements = $wrapdef->child->getAllowedElements($config); 00287 if (isset($wrap_elements[$token->name]) && isset($elements[$wrapname])) { 00288 $autoclose_ok = true; 00289 break; 00290 } 00291 } 00292 } 00293 } 00294 if ($autoclose_ok) { 00295 // errors need to be updated 00296 $new_token = new HTMLPurifier_Token_End($parent->name); 00297 $new_token->start = $parent; 00298 if ($carryover) { 00299 $element = clone $parent; 00300 // [TagClosedAuto] 00301 $element->armor['MakeWellFormed_TagClosedError'] = true; 00302 $element->carryover = true; 00303 $this->processToken(array($new_token, $token, $element)); 00304 } else { 00305 $this->insertBefore($new_token); 00306 } 00307 // [TagClosedSuppress] 00308 if ($e && !isset($parent->armor['MakeWellFormed_TagClosedError'])) { 00309 if (!$carryover) { 00310 $e->send(E_NOTICE, 'Strategy_MakeWellFormed: Tag auto closed', $parent); 00311 } else { 00312 $e->send(E_NOTICE, 'Strategy_MakeWellFormed: Tag carryover', $parent); 00313 } 00314 } 00315 } else { 00316 $this->remove(); 00317 } 00318 $reprocess = true; 00319 continue; 00320 } 00321 00322 } 00323 $ok = true; 00324 } 00325 00326 if ($ok) { 00327 foreach ($this->injectors as $i => $injector) { 00328 if (isset($token->skip[$i])) continue; 00329 if ($token->rewind !== null && $token->rewind !== $i) continue; 00330 $injector->handleElement($token); 00331 $this->processToken($token, $i); 00332 $reprocess = true; 00333 break; 00334 } 00335 if (!$reprocess) { 00336 // ah, nothing interesting happened; do normal processing 00337 $this->swap($token); 00338 if ($token instanceof HTMLPurifier_Token_Start) { 00339 $this->stack[] = $token; 00340 } elseif ($token instanceof HTMLPurifier_Token_End) { 00341 throw new HTMLPurifier_Exception('Improper handling of end tag in start code; possible error in MakeWellFormed'); 00342 } 00343 } 00344 continue; 00345 } 00346 00347 // sanity check: we should be dealing with a closing tag 00348 if (!$token instanceof HTMLPurifier_Token_End) { 00349 throw new HTMLPurifier_Exception('Unaccounted for tag token in input stream, bug in HTML Purifier'); 00350 } 00351 00352 // make sure that we have something open 00353 if (empty($this->stack)) { 00354 if ($escape_invalid_tags) { 00355 if ($e) $e->send(E_WARNING, 'Strategy_MakeWellFormed: Unnecessary end tag to text'); 00356 $this->swap(new HTMLPurifier_Token_Text( 00357 $generator->generateFromToken($token) 00358 )); 00359 } else { 00360 $this->remove(); 00361 if ($e) $e->send(E_WARNING, 'Strategy_MakeWellFormed: Unnecessary end tag removed'); 00362 } 00363 $reprocess = true; 00364 continue; 00365 } 00366 00367 // first, check for the simplest case: everything closes neatly. 00368 // Eventually, everything passes through here; if there are problems 00369 // we modify the input stream accordingly and then punt, so that 00370 // the tokens get processed again. 00371 $current_parent = array_pop($this->stack); 00372 if ($current_parent->name == $token->name) { 00373 $token->start = $current_parent; 00374 foreach ($this->injectors as $i => $injector) { 00375 if (isset($token->skip[$i])) continue; 00376 if ($token->rewind !== null && $token->rewind !== $i) continue; 00377 $injector->handleEnd($token); 00378 $this->processToken($token, $i); 00379 $this->stack[] = $current_parent; 00380 $reprocess = true; 00381 break; 00382 } 00383 continue; 00384 } 00385 00386 // okay, so we're trying to close the wrong tag 00387 00388 // undo the pop previous pop 00389 $this->stack[] = $current_parent; 00390 00391 // scroll back the entire nest, trying to find our tag. 00392 // (feature could be to specify how far you'd like to go) 00393 $size = count($this->stack); 00394 // -2 because -1 is the last element, but we already checked that 00395 $skipped_tags = false; 00396 for ($j = $size - 2; $j >= 0; $j--) { 00397 if ($this->stack[$j]->name == $token->name) { 00398 $skipped_tags = array_slice($this->stack, $j); 00399 break; 00400 } 00401 } 00402 00403 // we didn't find the tag, so remove 00404 if ($skipped_tags === false) { 00405 if ($escape_invalid_tags) { 00406 $this->swap(new HTMLPurifier_Token_Text( 00407 $generator->generateFromToken($token) 00408 )); 00409 if ($e) $e->send(E_WARNING, 'Strategy_MakeWellFormed: Stray end tag to text'); 00410 } else { 00411 $this->remove(); 00412 if ($e) $e->send(E_WARNING, 'Strategy_MakeWellFormed: Stray end tag removed'); 00413 } 00414 $reprocess = true; 00415 continue; 00416 } 00417 00418 // do errors, in REVERSE $j order: a,b,c with </a></b></c> 00419 $c = count($skipped_tags); 00420 if ($e) { 00421 for ($j = $c - 1; $j > 0; $j--) { 00422 // notice we exclude $j == 0, i.e. the current ending tag, from 00423 // the errors... [TagClosedSuppress] 00424 if (!isset($skipped_tags[$j]->armor['MakeWellFormed_TagClosedError'])) { 00425 $e->send(E_NOTICE, 'Strategy_MakeWellFormed: Tag closed by element end', $skipped_tags[$j]); 00426 } 00427 } 00428 } 00429 00430 // insert tags, in FORWARD $j order: c,b,a with </a></b></c> 00431 $replace = array($token); 00432 for ($j = 1; $j < $c; $j++) { 00433 // ...as well as from the insertions 00434 $new_token = new HTMLPurifier_Token_End($skipped_tags[$j]->name); 00435 $new_token->start = $skipped_tags[$j]; 00436 array_unshift($replace, $new_token); 00437 if (isset($definition->info[$new_token->name]) && $definition->info[$new_token->name]->formatting) { 00438 // [TagClosedAuto] 00439 $element = clone $skipped_tags[$j]; 00440 $element->carryover = true; 00441 $element->armor['MakeWellFormed_TagClosedError'] = true; 00442 $replace[] = $element; 00443 } 00444 } 00445 $this->processToken($replace); 00446 $reprocess = true; 00447 continue; 00448 } 00449 00450 $context->destroy('CurrentNesting'); 00451 $context->destroy('InputTokens'); 00452 $context->destroy('InputIndex'); 00453 $context->destroy('CurrentToken'); 00454 00455 unset($this->injectors, $this->stack, $this->tokens, $this->t); 00456 return $tokens; 00457 } 00458 00479 protected function processToken($token, $injector = -1) { 00480 00481 // normalize forms of token 00482 if (is_object($token)) $token = array(1, $token); 00483 if (is_int($token)) $token = array($token); 00484 if ($token === false) $token = array(1); 00485 if (!is_array($token)) throw new HTMLPurifier_Exception('Invalid token type from injector'); 00486 if (!is_int($token[0])) array_unshift($token, 1); 00487 if ($token[0] === 0) throw new HTMLPurifier_Exception('Deleting zero tokens is not valid'); 00488 00489 // $token is now an array with the following form: 00490 // array(number nodes to delete, new node 1, new node 2, ...) 00491 00492 $delete = array_shift($token); 00493 $old = array_splice($this->tokens, $this->t, $delete, $token); 00494 00495 if ($injector > -1) { 00496 // determine appropriate skips 00497 $oldskip = isset($old[0]) ? $old[0]->skip : array(); 00498 foreach ($token as $object) { 00499 $object->skip = $oldskip; 00500 $object->skip[$injector] = true; 00501 } 00502 } 00503 00504 } 00505 00510 private function insertBefore($token) { 00511 array_splice($this->tokens, $this->t, 0, array($token)); 00512 } 00513 00518 private function remove() { 00519 array_splice($this->tokens, $this->t, 1); 00520 } 00521 00526 private function swap($token) { 00527 $this->tokens[$this->t] = $token; 00528 } 00529 00530 } 00531 00532 // vim: et sw=4 sts=4