Support flashvars.
[htmlpurifier.git] / library / HTMLPurifier / Injector / SafeObject.php
blob4939f2771cd7b946dd37aa3b679e27c9bd559f46
1 <?php
3 /**
4 * Adds important param elements to inside of object in order to make
5 * things safe.
6 */
7 class HTMLPurifier_Injector_SafeObject extends HTMLPurifier_Injector
9 public $name = 'SafeObject';
10 public $needed = array('object', 'param');
12 protected $objectStack = array();
13 protected $paramStack = array();
15 // Keep this synchronized with AttrTransform/SafeParam.php
16 protected $addParam = array(
17 'allowScriptAccess' => 'never',
18 'allowNetworking' => 'internal',
20 protected $allowedParam = array(
21 'wmode' => true,
22 'movie' => true,
23 'flashvars' => true,
26 public function prepare($config, $context) {
27 parent::prepare($config, $context);
30 public function handleElement(&$token) {
31 if ($token->name == 'object') {
32 $this->objectStack[] = $token;
33 $this->paramStack[] = array();
34 $new = array($token);
35 foreach ($this->addParam as $name => $value) {
36 $new[] = new HTMLPurifier_Token_Empty('param', array('name' => $name, 'value' => $value));
38 $token = $new;
39 } elseif ($token->name == 'param') {
40 $nest = count($this->currentNesting) - 1;
41 if ($nest >= 0 && $this->currentNesting[$nest]->name === 'object') {
42 $i = count($this->objectStack) - 1;
43 if (!isset($token->attr['name'])) {
44 $token = false;
45 return;
47 $n = $token->attr['name'];
48 // We need this fix because YouTube doesn't supply a data
49 // attribute, which we need if a type is specified. This is
50 // *very* Flash specific.
51 if (!isset($this->objectStack[$i]->attr['data']) && $token->attr['name'] == 'movie') {
52 $this->objectStack[$i]->attr['data'] = $token->attr['value'];
54 // Check if the parameter is the correct value but has not
55 // already been added
56 if (
57 !isset($this->paramStack[$i][$n]) &&
58 isset($this->addParam[$n]) &&
59 $token->attr['name'] === $this->addParam[$n]
60 ) {
61 // keep token, and add to param stack
62 $this->paramStack[$i][$n] = true;
63 } elseif (isset($this->allowedParam[$n])) {
64 // keep token, don't do anything to it
65 // (could possibly check for duplicates here)
66 } else {
67 $token = false;
69 } else {
70 // not directly inside an object, DENY!
71 $token = false;
76 public function handleEnd(&$token) {
77 // This is the WRONG way of handling the object and param stacks;
78 // we should be inserting them directly on the relevant object tokens
79 // so that the global stack handling handles it.
80 if ($token->name == 'object') {
81 array_pop($this->objectStack);
82 array_pop($this->paramStack);
88 // vim: et sw=4 sts=4