Background.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /**
  3. * Validates shorthand CSS property background.
  4. * @warning Does not support url tokens that have internal spaces.
  5. */
  6. class HTMLPurifier_AttrDef_CSS_Background extends HTMLPurifier_AttrDef
  7. {
  8. /**
  9. * Local copy of component validators.
  10. * @type HTMLPurifier_AttrDef[]
  11. * @note See HTMLPurifier_AttrDef_Font::$info for a similar impl.
  12. */
  13. protected $info;
  14. /**
  15. * @param HTMLPurifier_Config $config
  16. */
  17. public function __construct($config)
  18. {
  19. $def = $config->getCSSDefinition();
  20. $this->info['background-color'] = $def->info['background-color'];
  21. $this->info['background-image'] = $def->info['background-image'];
  22. $this->info['background-repeat'] = $def->info['background-repeat'];
  23. $this->info['background-attachment'] = $def->info['background-attachment'];
  24. $this->info['background-position'] = $def->info['background-position'];
  25. $this->info['background-size'] = $def->info['background-size'];
  26. }
  27. /**
  28. * @param string $string
  29. * @param HTMLPurifier_Config $config
  30. * @param HTMLPurifier_Context $context
  31. * @return bool|string
  32. */
  33. public function validate($string, $config, $context)
  34. {
  35. // regular pre-processing
  36. $string = $this->parseCDATA($string);
  37. if ($string === '') {
  38. return false;
  39. }
  40. // munge rgb() decl if necessary
  41. $string = $this->mungeRgb($string);
  42. // assumes URI doesn't have spaces in it
  43. $bits = explode(' ', $string); // bits to process
  44. $caught = array();
  45. $caught['color'] = false;
  46. $caught['image'] = false;
  47. $caught['repeat'] = false;
  48. $caught['attachment'] = false;
  49. $caught['position'] = false;
  50. $caught['size'] = false;
  51. $i = 0; // number of catches
  52. foreach ($bits as $bit) {
  53. if ($bit === '') {
  54. continue;
  55. }
  56. foreach ($caught as $key => $status) {
  57. if ($key != 'position') {
  58. if ($status !== false) {
  59. continue;
  60. }
  61. $r = $this->info['background-' . $key]->validate($bit, $config, $context);
  62. } else {
  63. $r = $bit;
  64. }
  65. if ($r === false) {
  66. continue;
  67. }
  68. if ($key == 'position') {
  69. if ($caught[$key] === false) {
  70. $caught[$key] = '';
  71. }
  72. $caught[$key] .= $r . ' ';
  73. } else {
  74. $caught[$key] = $r;
  75. }
  76. $i++;
  77. break;
  78. }
  79. }
  80. if (!$i) {
  81. return false;
  82. }
  83. if ($caught['position'] !== false) {
  84. $caught['position'] = $this->info['background-position']->
  85. validate($caught['position'], $config, $context);
  86. }
  87. $ret = array();
  88. foreach ($caught as $value) {
  89. if ($value === false) {
  90. continue;
  91. }
  92. $ret[] = $value;
  93. }
  94. if (empty($ret)) {
  95. return false;
  96. }
  97. return implode(' ', $ret);
  98. }
  99. }
  100. // vim: et sw=4 sts=4