CSS Filter

  1. function filter_css($css, $allowed_properties = array(
  2.    'azimuth',
  3.    'background',
  4.    'background-color',
  5.    'background-image',
  6.    'background-repeat',
  7.    'background-attachment',
  8.    'background-position',
  9.    'border',
  10.    'border-top-width',
  11.    'border-right-width',
  12.    'border-bottom-width',
  13.    'border-left-width',
  14.    'border-width',
  15.    'border-top-color',
  16.    'border-right-color',
  17.    'border-bottom-color',
  18.    'border-left-color',
  19.    'border-color',
  20.    'border-top-style',
  21.    'border-right-style',
  22.    'border-bottom-style',
  23.    'border-left-style',
  24.    'border-style',
  25.    'border-top',
  26.    'border-right',
  27.    'border-bottom',
  28.    'border-left',
  29.    'clear',
  30.    'color',
  31.    'cursor',
  32.    'direction',
  33.    'display',
  34.    'elevation',
  35.    'float',
  36.    'font',
  37.    'font-family',
  38.    'font-size',
  39.    'font-style',
  40.    'font-variant',
  41.    'font-weight',
  42.    'height',
  43.    'letter-spacing',
  44.    'line-height',
  45.    'margin',
  46.    'margin-top',
  47.    'margin-right',
  48.    'margin-bottom',
  49.    'margin-left',
  50.    'overflow',
  51.    'padding',
  52.    'padding-top',
  53.    'padding-right',
  54.    'padding-bottom',
  55.    'padding-left',
  56.    'pause',
  57.    'pause-after',
  58.    'pause-before',
  59.    'pitch',
  60.    'pitch-range',
  61.    'richness',
  62.    'speak',
  63.    'speak-header',
  64.    'speak-numeral',
  65.    'speak-punctuation',
  66.    'speech-rate',
  67.    'stress',
  68.    'text-align',
  69.    'text-decoration',
  70.    'text-indent',
  71.    'unicode-bidi',
  72.    'vertical-align',
  73.    'voice-family',
  74.    'volume',
  75.    'white-space',
  76.    'width',
  77.    'fill',
  78.    'fill-opacity',
  79.    'fill-rule',
  80.    'stroke',
  81.    'stroke-width',
  82.    'stroke-linecap',
  83.    'stroke-linejoin',
  84.    'stroke-opacity',
  85.   ), $allowed_values = array(
  86.     'auto',
  87.     'aqua',
  88.     'black',
  89.     'block',
  90.     'blue',
  91.     'bold',
  92.     'both',
  93.     'bottom',
  94.     'brown',
  95.     'center',
  96.     'collapse',
  97.     'dashed',
  98.     'dotted',
  99.     'fuchsia',
  100.     'gray',
  101.     'green',
  102.     'italic',
  103.     'left',
  104.     'lime',
  105.     'maroon',
  106.     'medium',
  107.     'none',
  108.     'navy',
  109.     'normal',
  110.     'nowrap',
  111.     'olive',
  112.     'pointer',
  113.     'purple',
  114.     'red',
  115.     'right',
  116.     'solid',
  117.     'silver',
  118.     'teal',
  119.     'top',
  120.     'transparent',
  121.     'underline',
  122.     'white',
  123.     'yellow',
  124.   ), $allowed_values_regex = '/(#[0-9a-f]+|rgb\(\d+%?,\d*%?,?\d*%?\)?|\d{0,2}\.?\d{0,2}(cm|em|ex|in|mm|pc|pt|px|%|,|\))?)/', $disallowed_url_regex = '/url\s*\(\s*[^\s)]+?\s*\)\s*/') {
  125.  
  126.   foreach ($css as $statement_key => $statement) {
  127.     foreach ($statement['values'] as $key => $value) {
  128.       if (!in_array($key, $allowed_keys)) {
  129.         unset($css[$statement_key]);
  130.         continue;
  131.       }
  132.       $value = str_replace('!important', $value);
  133.       if (!in_array($value, $allowed_values) || !preg_match($allowed_values_regex, $value) || preg_match($disallowed_url_regex, $value)) {
  134.         unset($css[$statement_key]);
  135.         continue;
  136.       }
  137.     }
  138.   }
  139.   return $css;
  140. }