AttrValidator.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. /**
  3. * Validates the attributes of a token. Doesn't manage required attributes
  4. * very well. The only reason we factored this out was because RemoveForeignElements
  5. * also needed it besides ValidateAttributes.
  6. */
  7. class HTMLPurifier_AttrValidator
  8. {
  9. /**
  10. * Validates the attributes of a token, returning a modified token
  11. * that has valid tokens
  12. * @param $token Reference to token to validate. We require a reference
  13. * because the operation this class performs on the token are
  14. * not atomic, so the context CurrentToken to be updated
  15. * throughout
  16. * @param $config Instance of HTMLPurifier_Config
  17. * @param $context Instance of HTMLPurifier_Context
  18. */
  19. public function validateToken(&$token, &$config, $context) {
  20. $definition = $config->getHTMLDefinition();
  21. $e =& $context->get('ErrorCollector', true);
  22. // initialize IDAccumulator if necessary
  23. $ok =& $context->get('IDAccumulator', true);
  24. if (!$ok) {
  25. $id_accumulator = HTMLPurifier_IDAccumulator::build($config, $context);
  26. $context->register('IDAccumulator', $id_accumulator);
  27. }
  28. // initialize CurrentToken if necessary
  29. $current_token =& $context->get('CurrentToken', true);
  30. if (!$current_token) $context->register('CurrentToken', $token);
  31. if (
  32. !$token instanceof HTMLPurifier_Token_Start &&
  33. !$token instanceof HTMLPurifier_Token_Empty
  34. ) return $token;
  35. // create alias to global definition array, see also $defs
  36. // DEFINITION CALL
  37. $d_defs = $definition->info_global_attr;
  38. // don't update token until the very end, to ensure an atomic update
  39. $attr = $token->attr;
  40. // do global transformations (pre)
  41. // nothing currently utilizes this
  42. foreach ($definition->info_attr_transform_pre as $transform) {
  43. $attr = $transform->transform($o = $attr, $config, $context);
  44. if ($e) {
  45. if ($attr != $o) $e->send(E_NOTICE, 'AttrValidator: Attributes transformed', $o, $attr);
  46. }
  47. }
  48. // do local transformations only applicable to this element (pre)
  49. // ex. <p align="right"> to <p style="text-align:right;">
  50. foreach ($definition->info[$token->name]->attr_transform_pre as $transform) {
  51. $attr = $transform->transform($o = $attr, $config, $context);
  52. if ($e) {
  53. if ($attr != $o) $e->send(E_NOTICE, 'AttrValidator: Attributes transformed', $o, $attr);
  54. }
  55. }
  56. // create alias to this element's attribute definition array, see
  57. // also $d_defs (global attribute definition array)
  58. // DEFINITION CALL
  59. $defs = $definition->info[$token->name]->attr;
  60. $attr_key = false;
  61. $context->register('CurrentAttr', $attr_key);
  62. // iterate through all the attribute keypairs
  63. // Watch out for name collisions: $key has previously been used
  64. foreach ($attr as $attr_key => $value) {
  65. // call the definition
  66. if ( isset($defs[$attr_key]) ) {
  67. // there is a local definition defined
  68. if ($defs[$attr_key] === false) {
  69. // We've explicitly been told not to allow this element.
  70. // This is usually when there's a global definition
  71. // that must be overridden.
  72. // Theoretically speaking, we could have a
  73. // AttrDef_DenyAll, but this is faster!
  74. $result = false;
  75. } else {
  76. // validate according to the element's definition
  77. $result = $defs[$attr_key]->validate(
  78. $value, $config, $context
  79. );
  80. }
  81. } elseif ( isset($d_defs[$attr_key]) ) {
  82. // there is a global definition defined, validate according
  83. // to the global definition
  84. $result = $d_defs[$attr_key]->validate(
  85. $value, $config, $context
  86. );
  87. } else {
  88. // system never heard of the attribute? DELETE!
  89. $result = false;
  90. }
  91. // put the results into effect
  92. if ($result === false || $result === null) {
  93. // this is a generic error message that should replaced
  94. // with more specific ones when possible
  95. if ($e) $e->send(E_ERROR, 'AttrValidator: Attribute removed');
  96. // remove the attribute
  97. unset($attr[$attr_key]);
  98. } elseif (is_string($result)) {
  99. // generally, if a substitution is happening, there
  100. // was some sort of implicit correction going on. We'll
  101. // delegate it to the attribute classes to say exactly what.
  102. // simple substitution
  103. $attr[$attr_key] = $result;
  104. } else {
  105. // nothing happens
  106. }
  107. // we'd also want slightly more complicated substitution
  108. // involving an array as the return value,
  109. // although we're not sure how colliding attributes would
  110. // resolve (certain ones would be completely overriden,
  111. // others would prepend themselves).
  112. }
  113. $context->destroy('CurrentAttr');
  114. // post transforms
  115. // global (error reporting untested)
  116. foreach ($definition->info_attr_transform_post as $transform) {
  117. $attr = $transform->transform($o = $attr, $config, $context);
  118. if ($e) {
  119. if ($attr != $o) $e->send(E_NOTICE, 'AttrValidator: Attributes transformed', $o, $attr);
  120. }
  121. }
  122. // local (error reporting untested)
  123. foreach ($definition->info[$token->name]->attr_transform_post as $transform) {
  124. $attr = $transform->transform($o = $attr, $config, $context);
  125. if ($e) {
  126. if ($attr != $o) $e->send(E_NOTICE, 'AttrValidator: Attributes transformed', $o, $attr);
  127. }
  128. }
  129. $token->attr = $attr;
  130. // destroy CurrentToken if we made it ourselves
  131. if (!$current_token) $context->destroy('CurrentToken');
  132. }
  133. }
  134. // vim: et sw=4 sts=4