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