SafeObject.php 3.3 KB

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