friendica/vendor/ezyang/htmlpurifier/library/HTMLPurifier/Injector.php

282 lines
8.7 KiB
PHP
Raw Normal View History

2010-09-09 05:14:17 +02:00
<?php
/**
* Injects tokens into the document while parsing for well-formedness.
* This enables "formatter-like" functionality such as auto-paragraphing,
* smiley-ification and linkification to take place.
*
* A note on how handlers create changes; this is done by assigning a new
* value to the $token reference. These values can take a variety of forms and
* are best described HTMLPurifier_Strategy_MakeWellFormed->processToken()
* documentation.
*
* @todo Allow injectors to request a re-run on their output. This
* would help if an operation is recursive.
*/
abstract class HTMLPurifier_Injector
{
/**
2016-02-09 11:06:17 +01:00
* Advisory name of injector, this is for friendly error messages.
* @type string
2010-09-09 05:14:17 +02:00
*/
public $name;
/**
2016-02-09 11:06:17 +01:00
* @type HTMLPurifier_HTMLDefinition
2010-09-09 05:14:17 +02:00
*/
protected $htmlDefinition;
/**
* Reference to CurrentNesting variable in Context. This is an array
* list of tokens that we are currently "inside"
2016-02-09 11:06:17 +01:00
* @type array
2010-09-09 05:14:17 +02:00
*/
protected $currentNesting;
/**
2016-02-09 11:06:17 +01:00
* Reference to current token.
* @type HTMLPurifier_Token
2010-09-09 05:14:17 +02:00
*/
2016-02-09 11:06:17 +01:00
protected $currentToken;
2010-09-09 05:14:17 +02:00
/**
2016-02-09 11:06:17 +01:00
* Reference to InputZipper variable in Context.
* @type HTMLPurifier_Zipper
2010-09-09 05:14:17 +02:00
*/
2016-02-09 11:06:17 +01:00
protected $inputZipper;
2010-09-09 05:14:17 +02:00
/**
* Array of elements and attributes this injector creates and therefore
* need to be allowed by the definition. Takes form of
* array('element' => array('attr', 'attr2'), 'element2')
2016-02-09 11:06:17 +01:00
* @type array
2010-09-09 05:14:17 +02:00
*/
public $needed = array();
/**
2016-02-09 11:06:17 +01:00
* Number of elements to rewind backwards (relative).
* @type bool|int
2010-09-09 05:14:17 +02:00
*/
2016-02-09 11:06:17 +01:00
protected $rewindOffset = false;
2010-09-09 05:14:17 +02:00
/**
* Rewind to a spot to re-perform processing. This is useful if you
* deleted a node, and now need to see if this change affected any
* earlier nodes. Rewinding does not affect other injectors, and can
* result in infinite loops if not used carefully.
2016-02-09 11:06:17 +01:00
* @param bool|int $offset
2010-09-09 05:14:17 +02:00
* @warning HTML Purifier will prevent you from fast-forwarding with this
* function.
*/
2016-02-09 11:06:17 +01:00
public function rewindOffset($offset)
{
$this->rewindOffset = $offset;
2010-09-09 05:14:17 +02:00
}
/**
2016-02-09 11:06:17 +01:00
* Retrieves rewind offset, and then unsets it.
* @return bool|int
2010-09-09 05:14:17 +02:00
*/
2016-02-09 11:06:17 +01:00
public function getRewindOffset()
{
$r = $this->rewindOffset;
$this->rewindOffset = false;
2010-09-09 05:14:17 +02:00
return $r;
}
/**
* Prepares the injector by giving it the config and context objects:
* this allows references to important variables to be made within
* the injector. This function also checks if the HTML environment
* will work with the Injector (see checkNeeded()).
2016-02-09 11:06:17 +01:00
* @param HTMLPurifier_Config $config
* @param HTMLPurifier_Context $context
* @return bool|string Boolean false if success, string of missing needed element/attribute if failure
2010-09-09 05:14:17 +02:00
*/
2016-02-09 11:06:17 +01:00
public function prepare($config, $context)
{
2010-09-09 05:14:17 +02:00
$this->htmlDefinition = $config->getHTMLDefinition();
// Even though this might fail, some unit tests ignore this and
// still test checkNeeded, so be careful. Maybe get rid of that
// dependency.
$result = $this->checkNeeded($config);
2016-02-09 11:06:17 +01:00
if ($result !== false) {
return $result;
}
2010-09-09 05:14:17 +02:00
$this->currentNesting =& $context->get('CurrentNesting');
2016-02-09 11:06:17 +01:00
$this->currentToken =& $context->get('CurrentToken');
$this->inputZipper =& $context->get('InputZipper');
2010-09-09 05:14:17 +02:00
return false;
}
/**
* This function checks if the HTML environment
* will work with the Injector: if p tags are not allowed, the
* Auto-Paragraphing injector should not be enabled.
2016-02-09 11:06:17 +01:00
* @param HTMLPurifier_Config $config
* @return bool|string Boolean false if success, string of missing needed element/attribute if failure
2010-09-09 05:14:17 +02:00
*/
2016-02-09 11:06:17 +01:00
public function checkNeeded($config)
{
2010-09-09 05:14:17 +02:00
$def = $config->getHTMLDefinition();
foreach ($this->needed as $element => $attributes) {
2016-02-09 11:06:17 +01:00
if (is_int($element)) {
$element = $attributes;
}
if (!isset($def->info[$element])) {
return $element;
}
if (!is_array($attributes)) {
continue;
}
2010-09-09 05:14:17 +02:00
foreach ($attributes as $name) {
2016-02-09 11:06:17 +01:00
if (!isset($def->info[$element]->attr[$name])) {
return "$element.$name";
}
2010-09-09 05:14:17 +02:00
}
}
return false;
}
/**
* Tests if the context node allows a certain element
2016-02-09 11:06:17 +01:00
* @param string $name Name of element to test for
* @return bool True if element is allowed, false if it is not
2010-09-09 05:14:17 +02:00
*/
2016-02-09 11:06:17 +01:00
public function allowsElement($name)
{
2010-09-09 05:14:17 +02:00
if (!empty($this->currentNesting)) {
$parent_token = array_pop($this->currentNesting);
$this->currentNesting[] = $parent_token;
$parent = $this->htmlDefinition->info[$parent_token->name];
} else {
$parent = $this->htmlDefinition->info_parent_def;
}
if (!isset($parent->child->elements[$name]) || isset($parent->excludes[$name])) {
return false;
}
// check for exclusion
for ($i = count($this->currentNesting) - 2; $i >= 0; $i--) {
$node = $this->currentNesting[$i];
$def = $this->htmlDefinition->info[$node->name];
2016-02-09 11:06:17 +01:00
if (isset($def->excludes[$name])) {
return false;
}
2010-09-09 05:14:17 +02:00
}
return true;
}
/**
* Iterator function, which starts with the next token and continues until
* you reach the end of the input tokens.
* @warning Please prevent previous references from interfering with this
* functions by setting $i = null beforehand!
2016-02-09 11:06:17 +01:00
* @param int $i Current integer index variable for inputTokens
* @param HTMLPurifier_Token $current Current token variable.
* Do NOT use $token, as that variable is also a reference
* @return bool
*/
protected function forward(&$i, &$current)
{
if ($i === null) {
$i = count($this->inputZipper->back) - 1;
} else {
$i--;
}
if ($i < 0) {
return false;
}
$current = $this->inputZipper->back[$i];
2010-09-09 05:14:17 +02:00
return true;
}
/**
* Similar to _forward, but accepts a third parameter $nesting (which
* should be initialized at 0) and stops when we hit the end tag
* for the node $this->inputIndex starts in.
2016-02-09 11:06:17 +01:00
* @param int $i Current integer index variable for inputTokens
* @param HTMLPurifier_Token $current Current token variable.
* Do NOT use $token, as that variable is also a reference
* @param int $nesting
* @return bool
2010-09-09 05:14:17 +02:00
*/
2016-02-09 11:06:17 +01:00
protected function forwardUntilEndToken(&$i, &$current, &$nesting)
{
2010-09-09 05:14:17 +02:00
$result = $this->forward($i, $current);
2016-02-09 11:06:17 +01:00
if (!$result) {
return false;
}
if ($nesting === null) {
$nesting = 0;
}
if ($current instanceof HTMLPurifier_Token_Start) {
$nesting++;
} elseif ($current instanceof HTMLPurifier_Token_End) {
if ($nesting <= 0) {
return false;
}
2010-09-09 05:14:17 +02:00
$nesting--;
}
return true;
}
/**
* Iterator function, starts with the previous token and continues until
* you reach the beginning of input tokens.
* @warning Please prevent previous references from interfering with this
* functions by setting $i = null beforehand!
2016-02-09 11:06:17 +01:00
* @param int $i Current integer index variable for inputTokens
* @param HTMLPurifier_Token $current Current token variable.
* Do NOT use $token, as that variable is also a reference
* @return bool
2010-09-09 05:14:17 +02:00
*/
2016-02-09 11:06:17 +01:00
protected function backward(&$i, &$current)
{
if ($i === null) {
$i = count($this->inputZipper->front) - 1;
} else {
$i--;
}
if ($i < 0) {
return false;
}
$current = $this->inputZipper->front[$i];
return true;
2010-09-09 05:14:17 +02:00
}
/**
* Handler that is called when a text token is processed
*/
2016-02-09 11:06:17 +01:00
public function handleText(&$token)
{
}
2010-09-09 05:14:17 +02:00
/**
* Handler that is called when a start or empty token is processed
*/
2016-02-09 11:06:17 +01:00
public function handleElement(&$token)
{
}
2010-09-09 05:14:17 +02:00
/**
* Handler that is called when an end token is processed
*/
2016-02-09 11:06:17 +01:00
public function handleEnd(&$token)
{
2010-09-09 05:14:17 +02:00
$this->notifyEnd($token);
}
/**
* Notifier that is called when an end token is processed
2016-02-09 11:06:17 +01:00
* @param HTMLPurifier_Token $token Current token variable.
2010-09-09 05:14:17 +02:00
* @note This differs from handlers in that the token is read-only
* @deprecated
*/
2016-02-09 11:06:17 +01:00
public function notifyEnd($token)
{
}
2010-09-09 05:14:17 +02:00
}
// vim: et sw=4 sts=4