SafeObject.php 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /**
  3. * Adds important param elements to inside of object in order to make
  4. * things safe.
  5. */
  6. class HTMLPurifier_Injector_SafeObject extends HTMLPurifier_Injector
  7. {
  8. public $name = 'SafeObject';
  9. public $needed = array('object', 'param');
  10. protected $objectStack = array();
  11. protected $paramStack = array();
  12. // Keep this synchronized with AttrTransform/SafeParam.php
  13. protected $addParam = array(
  14. 'allowScriptAccess' => 'never',
  15. 'allowNetworking' => 'internal',
  16. 'allowscriptaccess' => 'never',
  17. 'allownetworking' => 'internal',
  18. );
  19. protected $allowedParam = array(
  20. 'wmode' => true,
  21. 'movie' => true,
  22. 'flashvars' => true,
  23. 'src' => true,
  24. 'allowFullScreen' => true, // if omitted, assume to be 'false'
  25. 'allowfullscreen' => true, // if omitted, assume to be 'false'
  26. );
  27. public function prepare($config, $context) {
  28. parent::prepare($config, $context);
  29. }
  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));
  37. }
  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;
  46. }
  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']) &&
  52. ($token->attr['name'] == 'movie' || $token->attr['name'] == 'src')) {
  53. $this->objectStack[$i]->attr['data'] = $token->attr['value'];
  54. }
  55. // Check if the parameter is the correct value but has not
  56. // already been added
  57. if (
  58. !isset($this->paramStack[$i][$n]) &&
  59. isset($this->addParam[$n]) &&
  60. $token->attr['name'] === $this->addParam[$n]
  61. ) {
  62. // keep token, and add to param stack
  63. $this->paramStack[$i][$n] = true;
  64. } elseif (isset($this->allowedParam[$n])) {
  65. // keep token, don't do anything to it
  66. // (could possibly check for duplicates here)
  67. } else {
  68. $token = false;
  69. }
  70. } else {
  71. // not directly inside an object, DENY!
  72. $token = false;
  73. }
  74. }
  75. }
  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);
  83. }
  84. }
  85. }
  86. // vim: et sw=4 sts=4