File indexing completed on 2024-12-22 05:36:18

0001 <?php
0002 
0003 /**
0004  * Microsoft's proprietary filter: CSS property
0005  * @note Currently supports the alpha filter. In the future, this will
0006  *       probably need an extensible framework
0007  */
0008 class HTMLPurifier_AttrDef_CSS_Filter extends HTMLPurifier_AttrDef
0009 {
0010     /**
0011      * @type HTMLPurifier_AttrDef_Integer
0012      */
0013     protected $intValidator;
0014 
0015     public function __construct()
0016     {
0017         $this->intValidator = new HTMLPurifier_AttrDef_Integer();
0018     }
0019 
0020     /**
0021      * @param string $value
0022      * @param HTMLPurifier_Config $config
0023      * @param HTMLPurifier_Context $context
0024      * @return bool|string
0025      */
0026     public function validate($value, $config, $context)
0027     {
0028         $value = $this->parseCDATA($value);
0029         if ($value === 'none') {
0030             return $value;
0031         }
0032         // if we looped this we could support multiple filters
0033         $function_length = strcspn($value, '(');
0034         $function = trim(substr($value, 0, $function_length));
0035         if ($function !== 'alpha' &&
0036             $function !== 'Alpha' &&
0037             $function !== 'progid:DXImageTransform.Microsoft.Alpha'
0038         ) {
0039             return false;
0040         }
0041         $cursor = $function_length + 1;
0042         $parameters_length = strcspn($value, ')', $cursor);
0043         $parameters = substr($value, $cursor, $parameters_length);
0044         $params = explode(',', $parameters);
0045         $ret_params = array();
0046         $lookup = array();
0047         foreach ($params as $param) {
0048             list($key, $value) = explode('=', $param);
0049             $key = trim($key);
0050             $value = trim($value);
0051             if (isset($lookup[$key])) {
0052                 continue;
0053             }
0054             if ($key !== 'opacity') {
0055                 continue;
0056             }
0057             $value = $this->intValidator->validate($value, $config, $context);
0058             if ($value === false) {
0059                 continue;
0060             }
0061             $int = (int)$value;
0062             if ($int > 100) {
0063                 $value = '100';
0064             }
0065             if ($int < 0) {
0066                 $value = '0';
0067             }
0068             $ret_params[] = "$key=$value";
0069             $lookup[$key] = true;
0070         }
0071         $ret_parameters = implode(',', $ret_params);
0072         $ret_function = "$function($ret_parameters)";
0073         return $ret_function;
0074     }
0075 }
0076 
0077 // vim: et sw=4 sts=4