CSS.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /**
  3. * Validates the HTML attribute style, otherwise known as CSS.
  4. * @note We don't implement the whole CSS specification, so it might be
  5. * difficult to reuse this component in the context of validating
  6. * actual stylesheet declarations.
  7. * @note If we were really serious about validating the CSS, we would
  8. * tokenize the styles and then parse the tokens. Obviously, we
  9. * are not doing that. Doing that could seriously harm performance,
  10. * but would make these components a lot more viable for a CSS
  11. * filtering solution.
  12. */
  13. class HTMLPurifier_AttrDef_CSS extends HTMLPurifier_AttrDef
  14. {
  15. /**
  16. * @param string $css
  17. * @param HTMLPurifier_Config $config
  18. * @param HTMLPurifier_Context $context
  19. * @return bool|string
  20. */
  21. public function validate($css, $config, $context)
  22. {
  23. $css = $this->parseCDATA($css);
  24. $definition = $config->getCSSDefinition();
  25. $allow_duplicates = $config->get("CSS.AllowDuplicates");
  26. $universal_attrdef = new HTMLPurifier_AttrDef_Enum(
  27. array(
  28. 'initial',
  29. 'inherit',
  30. 'unset',
  31. )
  32. );
  33. // According to the CSS2.1 spec, the places where a
  34. // non-delimiting semicolon can appear are in strings
  35. // escape sequences. So here is some dumb hack to
  36. // handle quotes.
  37. $len = strlen($css);
  38. $accum = "";
  39. $declarations = array();
  40. $quoted = false;
  41. for ($i = 0; $i < $len; $i++) {
  42. $c = strcspn($css, ";'\"", $i);
  43. $accum .= substr($css, $i, $c);
  44. $i += $c;
  45. if ($i == $len) break;
  46. $d = $css[$i];
  47. if ($quoted) {
  48. $accum .= $d;
  49. if ($d == $quoted) {
  50. $quoted = false;
  51. }
  52. } else {
  53. if ($d == ";") {
  54. $declarations[] = $accum;
  55. $accum = "";
  56. } else {
  57. $accum .= $d;
  58. $quoted = $d;
  59. }
  60. }
  61. }
  62. if ($accum != "") $declarations[] = $accum;
  63. $propvalues = array();
  64. $new_declarations = '';
  65. /**
  66. * Name of the current CSS property being validated.
  67. */
  68. $property = false;
  69. $context->register('CurrentCSSProperty', $property);
  70. foreach ($declarations as $declaration) {
  71. if (!$declaration) {
  72. continue;
  73. }
  74. if (!strpos($declaration, ':')) {
  75. continue;
  76. }
  77. list($property, $value) = explode(':', $declaration, 2);
  78. $property = trim($property);
  79. $value = trim($value);
  80. $ok = false;
  81. do {
  82. if (isset($definition->info[$property])) {
  83. $ok = true;
  84. break;
  85. }
  86. if (ctype_lower($property)) {
  87. break;
  88. }
  89. $property = strtolower($property);
  90. if (isset($definition->info[$property])) {
  91. $ok = true;
  92. break;
  93. }
  94. } while (0);
  95. if (!$ok) {
  96. continue;
  97. }
  98. $result = $universal_attrdef->validate($value, $config, $context);
  99. if ($result === false) {
  100. $result = $definition->info[$property]->validate(
  101. $value,
  102. $config,
  103. $context
  104. );
  105. }
  106. if ($result === false) {
  107. continue;
  108. }
  109. if ($allow_duplicates) {
  110. $new_declarations .= "$property:$result;";
  111. } else {
  112. $propvalues[$property] = $result;
  113. }
  114. }
  115. $context->destroy('CurrentCSSProperty');
  116. // procedure does not write the new CSS simultaneously, so it's
  117. // slightly inefficient, but it's the only way of getting rid of
  118. // duplicates. Perhaps config to optimize it, but not now.
  119. foreach ($propvalues as $prop => $value) {
  120. $new_declarations .= "$prop:$value;";
  121. }
  122. return $new_declarations ? $new_declarations : false;
  123. }
  124. }
  125. // vim: et sw=4 sts=4