]> git.mxchange.org Git - friendica.git/blob - library/HTMLPurifier/Injector/SafeObject.php
Merge pull request #482 from pixelroot/master
[friendica.git] / library / HTMLPurifier / Injector / SafeObject.php
1 <?php
2
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
8 {
9     public $name = 'SafeObject';
10     public $needed = array('object', 'param');
11
12     protected $objectStack = array();
13     protected $paramStack  = array();
14
15     // Keep this synchronized with AttrTransform/SafeParam.php
16     protected $addParam = array(
17         'allowScriptAccess' => 'never',
18         'allowNetworking' => 'internal',
19     );
20     protected $allowedParam = array(
21         'wmode' => true,
22         'movie' => true,
23         'flashvars' => true,
24         'src' => true,
25     );
26
27     public function prepare($config, $context) {
28         parent::prepare($config, $context);
29     }
30
31     public function handleElement(&$token) {
32         if ($token->name == 'object') {
33             $this->objectStack[] = $token;
34             $this->paramStack[] = array();
35             $new = array($token);
36             foreach ($this->addParam as $name => $value) {
37                 $new[] = new HTMLPurifier_Token_Empty('param', array('name' => $name, 'value' => $value));
38             }
39             $token = $new;
40         } elseif ($token->name == 'param') {
41             $nest = count($this->currentNesting) - 1;
42             if ($nest >= 0 && $this->currentNesting[$nest]->name === 'object') {
43                 $i = count($this->objectStack) - 1;
44                 if (!isset($token->attr['name'])) {
45                     $token = false;
46                     return;
47                 }
48                 $n = $token->attr['name'];
49                 // We need this fix because YouTube doesn't supply a data
50                 // attribute, which we need if a type is specified. This is
51                 // *very* Flash specific.
52                 if (!isset($this->objectStack[$i]->attr['data']) &&
53                     ($token->attr['name'] == 'movie' || $token->attr['name'] == 'src')) {
54                     $this->objectStack[$i]->attr['data'] = $token->attr['value'];
55                 }
56                 // Check if the parameter is the correct value but has not
57                 // already been added
58                 if (
59                     !isset($this->paramStack[$i][$n]) &&
60                     isset($this->addParam[$n]) &&
61                     $token->attr['name'] === $this->addParam[$n]
62                 ) {
63                     // keep token, and add to param stack
64                     $this->paramStack[$i][$n] = true;
65                 } elseif (isset($this->allowedParam[$n])) {
66                     // keep token, don't do anything to it
67                     // (could possibly check for duplicates here)
68                 } else {
69                     $token = false;
70                 }
71             } else {
72                 // not directly inside an object, DENY!
73                 $token = false;
74             }
75         }
76     }
77
78     public function handleEnd(&$token) {
79         // This is the WRONG way of handling the object and param stacks;
80         // we should be inserting them directly on the relevant object tokens
81         // so that the global stack handling handles it.
82         if ($token->name == 'object') {
83             array_pop($this->objectStack);
84             array_pop($this->paramStack);
85         }
86     }
87
88 }
89
90 // vim: et sw=4 sts=4