ILIAS  release_5-2 Revision v5.2.25-18-g3f80b82851
Injector.php
Go to the documentation of this file.
1 <?php
2 
16 abstract class HTMLPurifier_Injector
17 {
18 
23  public $name;
24 
28  protected $htmlDefinition;
29 
35  protected $currentNesting;
36 
41  protected $currentToken;
42 
47  protected $inputZipper;
48 
55  public $needed = array();
56 
61  protected $rewindOffset = false;
62 
72  public function rewindOffset($offset)
73  {
74  $this->rewindOffset = $offset;
75  }
76 
81  public function getRewindOffset()
82  {
84  $this->rewindOffset = false;
85  return $r;
86  }
87 
97  public function prepare($config, $context)
98  {
99  $this->htmlDefinition = $config->getHTMLDefinition();
100  // Even though this might fail, some unit tests ignore this and
101  // still test checkNeeded, so be careful. Maybe get rid of that
102  // dependency.
103  $result = $this->checkNeeded($config);
104  if ($result !== false) {
105  return $result;
106  }
107  $this->currentNesting =& $context->get('CurrentNesting');
108  $this->currentToken =& $context->get('CurrentToken');
109  $this->inputZipper =& $context->get('InputZipper');
110  return false;
111  }
112 
120  public function checkNeeded($config)
121  {
122  $def = $config->getHTMLDefinition();
123  foreach ($this->needed as $element => $attributes) {
124  if (is_int($element)) {
125  $element = $attributes;
126  }
127  if (!isset($def->info[$element])) {
128  return $element;
129  }
130  if (!is_array($attributes)) {
131  continue;
132  }
133  foreach ($attributes as $name) {
134  if (!isset($def->info[$element]->attr[$name])) {
135  return "$element.$name";
136  }
137  }
138  }
139  return false;
140  }
141 
147  public function allowsElement($name)
148  {
149  if (!empty($this->currentNesting)) {
150  $parent_token = array_pop($this->currentNesting);
151  $this->currentNesting[] = $parent_token;
152  $parent = $this->htmlDefinition->info[$parent_token->name];
153  } else {
154  $parent = $this->htmlDefinition->info_parent_def;
155  }
156  if (!isset($parent->child->elements[$name]) || isset($parent->excludes[$name])) {
157  return false;
158  }
159  // check for exclusion
160  for ($i = count($this->currentNesting) - 2; $i >= 0; $i--) {
161  $node = $this->currentNesting[$i];
162  $def = $this->htmlDefinition->info[$node->name];
163  if (isset($def->excludes[$name])) {
164  return false;
165  }
166  }
167  return true;
168  }
169 
180  protected function forward(&$i, &$current)
181  {
182  if ($i === null) {
183  $i = count($this->inputZipper->back) - 1;
184  } else {
185  $i--;
186  }
187  if ($i < 0) {
188  return false;
189  }
190  $current = $this->inputZipper->back[$i];
191  return true;
192  }
193 
204  protected function forwardUntilEndToken(&$i, &$current, &$nesting)
205  {
206  $result = $this->forward($i, $current);
207  if (!$result) {
208  return false;
209  }
210  if ($nesting === null) {
211  $nesting = 0;
212  }
213  if ($current instanceof HTMLPurifier_Token_Start) {
214  $nesting++;
215  } elseif ($current instanceof HTMLPurifier_Token_End) {
216  if ($nesting <= 0) {
217  return false;
218  }
219  $nesting--;
220  }
221  return true;
222  }
223 
234  protected function backward(&$i, &$current)
235  {
236  if ($i === null) {
237  $i = count($this->inputZipper->front) - 1;
238  } else {
239  $i--;
240  }
241  if ($i < 0) {
242  return false;
243  }
244  $current = $this->inputZipper->front[$i];
245  return true;
246  }
247 
251  public function handleText(&$token)
252  {
253  }
254 
258  public function handleElement(&$token)
259  {
260  }
261 
265  public function handleEnd(&$token)
266  {
267  $this->notifyEnd($token);
268  }
269 
276  public function notifyEnd($token)
277  {
278  }
279 }
280 
281 // vim: et sw=4 sts=4
allowsElement($name)
Tests if the context node allows a certain element.
Definition: Injector.php:147
$htmlDefinition
HTMLPurifier_HTMLDefinition
Definition: Injector.php:28
Concrete end token class.
Definition: End.php:10
$inputZipper
Reference to InputZipper variable in Context.
Definition: Injector.php:47
$result
$currentToken
Reference to current token.
Definition: Injector.php:41
Concrete start token class.
Definition: Start.php:6
forward(&$i, &$current)
Iterator function, which starts with the next token and continues until you reach the end of the inpu...
Definition: Injector.php:180
prepare($config, $context)
Prepares the injector by giving it the config and context objects: this allows references to importan...
Definition: Injector.php:97
$r
Definition: example_031.php:79
getRewindOffset()
Retrieves rewind offset, and then unsets it.
Definition: Injector.php:81
forwardUntilEndToken(&$i, &$current, &$nesting)
Similar to _forward, but accepts a third parameter $nesting (which should be initialized at 0) and st...
Definition: Injector.php:204
notifyEnd($token)
Notifier that is called when an end token is processed.
Definition: Injector.php:276
$name
Advisory name of injector, this is for friendly error messages.
Definition: Injector.php:23
Create styles array
The data for the language used.
checkNeeded($config)
This function checks if the HTML environment will work with the Injector: if p tags are not allowed...
Definition: Injector.php:120
Injects tokens into the document while parsing for well-formedness.
Definition: Injector.php:16
$rewindOffset
Number of elements to rewind backwards (relative).
Definition: Injector.php:61
backward(&$i, &$current)
Iterator function, starts with the previous token and continues until you reach the beginning of inpu...
Definition: Injector.php:234
handleText(&$token)
Handler that is called when a text token is processed.
Definition: Injector.php:251
handleEnd(&$token)
Handler that is called when an end token is processed.
Definition: Injector.php:265
$currentNesting
Reference to CurrentNesting variable in Context.
Definition: Injector.php:35
rewindOffset($offset)
Rewind to a spot to re-perform processing.
Definition: Injector.php:72
handleElement(&$token)
Handler that is called when a start or empty token is processed.
Definition: Injector.php:258
$needed
Array of elements and attributes this injector creates and therefore need to be allowed by the defini...
Definition: Injector.php:55