File indexing completed on 2024-05-26 06:02:31

0001 <?php
0002 
0003 /**
0004  * Validates shorthand CSS property background.
0005  * @warning Does not support url tokens that have internal spaces.
0006  */
0007 class HTMLPurifier_AttrDef_CSS_Background extends HTMLPurifier_AttrDef
0008 {
0009 
0010     /**
0011      * Local copy of component validators.
0012      * @type HTMLPurifier_AttrDef[]
0013      * @note See HTMLPurifier_AttrDef_Font::$info for a similar impl.
0014      */
0015     protected $info;
0016 
0017     /**
0018      * @param HTMLPurifier_Config $config
0019      */
0020     public function __construct($config)
0021     {
0022         $def = $config->getCSSDefinition();
0023         $this->info['background-color'] = $def->info['background-color'];
0024         $this->info['background-image'] = $def->info['background-image'];
0025         $this->info['background-repeat'] = $def->info['background-repeat'];
0026         $this->info['background-attachment'] = $def->info['background-attachment'];
0027         $this->info['background-position'] = $def->info['background-position'];
0028     }
0029 
0030     /**
0031      * @param string $string
0032      * @param HTMLPurifier_Config $config
0033      * @param HTMLPurifier_Context $context
0034      * @return bool|string
0035      */
0036     public function validate($string, $config, $context)
0037     {
0038         // regular pre-processing
0039         $string = $this->parseCDATA($string);
0040         if ($string === '') {
0041             return false;
0042         }
0043 
0044         // munge rgb() decl if necessary
0045         $string = $this->mungeRgb($string);
0046 
0047         // assumes URI doesn't have spaces in it
0048         $bits = explode(' ', $string); // bits to process
0049 
0050         $caught = array();
0051         $caught['color'] = false;
0052         $caught['image'] = false;
0053         $caught['repeat'] = false;
0054         $caught['attachment'] = false;
0055         $caught['position'] = false;
0056 
0057         $i = 0; // number of catches
0058 
0059         foreach ($bits as $bit) {
0060             if ($bit === '') {
0061                 continue;
0062             }
0063             foreach ($caught as $key => $status) {
0064                 if ($key != 'position') {
0065                     if ($status !== false) {
0066                         continue;
0067                     }
0068                     $r = $this->info['background-' . $key]->validate($bit, $config, $context);
0069                 } else {
0070                     $r = $bit;
0071                 }
0072                 if ($r === false) {
0073                     continue;
0074                 }
0075                 if ($key == 'position') {
0076                     if ($caught[$key] === false) {
0077                         $caught[$key] = '';
0078                     }
0079                     $caught[$key] .= $r . ' ';
0080                 } else {
0081                     $caught[$key] = $r;
0082                 }
0083                 $i++;
0084                 break;
0085             }
0086         }
0087 
0088         if (!$i) {
0089             return false;
0090         }
0091         if ($caught['position'] !== false) {
0092             $caught['position'] = $this->info['background-position']->
0093                 validate($caught['position'], $config, $context);
0094         }
0095 
0096         $ret = array();
0097         foreach ($caught as $value) {
0098             if ($value === false) {
0099                 continue;
0100             }
0101             $ret[] = $value;
0102         }
0103 
0104         if (empty($ret)) {
0105             return false;
0106         }
0107         return implode(' ', $ret);
0108     }
0109 }
0110 
0111 // vim: et sw=4 sts=4