HTMLPurifier 4.4.0
/home/ezyang/Dev/htmlpurifier/library/HTMLPurifier/AttrDef/CSS/Color.php
Go to the documentation of this file.
00001 <?php
00002 
00006 class HTMLPurifier_AttrDef_CSS_Color extends HTMLPurifier_AttrDef
00007 {
00008 
00009     public function validate($color, $config, $context) {
00010 
00011         static $colors = null;
00012         if ($colors === null) $colors = $config->get('Core.ColorKeywords');
00013 
00014         $color = trim($color);
00015         if ($color === '') return false;
00016 
00017         $lower = strtolower($color);
00018         if (isset($colors[$lower])) return $colors[$lower];
00019 
00020         if (strpos($color, 'rgb(') !== false) {
00021             // rgb literal handling
00022             $length = strlen($color);
00023             if (strpos($color, ')') !== $length - 1) return false;
00024             $triad = substr($color, 4, $length - 4 - 1);
00025             $parts = explode(',', $triad);
00026             if (count($parts) !== 3) return false;
00027             $type = false; // to ensure that they're all the same type
00028             $new_parts = array();
00029             foreach ($parts as $part) {
00030                 $part = trim($part);
00031                 if ($part === '') return false;
00032                 $length = strlen($part);
00033                 if ($part[$length - 1] === '%') {
00034                     // handle percents
00035                     if (!$type) {
00036                         $type = 'percentage';
00037                     } elseif ($type !== 'percentage') {
00038                         return false;
00039                     }
00040                     $num = (float) substr($part, 0, $length - 1);
00041                     if ($num < 0) $num = 0;
00042                     if ($num > 100) $num = 100;
00043                     $new_parts[] = "$num%";
00044                 } else {
00045                     // handle integers
00046                     if (!$type) {
00047                         $type = 'integer';
00048                     } elseif ($type !== 'integer') {
00049                         return false;
00050                     }
00051                     $num = (int) $part;
00052                     if ($num < 0) $num = 0;
00053                     if ($num > 255) $num = 255;
00054                     $new_parts[] = (string) $num;
00055                 }
00056             }
00057             $new_triad = implode(',', $new_parts);
00058             $color = "rgb($new_triad)";
00059         } else {
00060             // hexadecimal handling
00061             if ($color[0] === '#') {
00062                 $hex = substr($color, 1);
00063             } else {
00064                 $hex = $color;
00065                 $color = '#' . $color;
00066             }
00067             $length = strlen($hex);
00068             if ($length !== 3 && $length !== 6) return false;
00069             if (!ctype_xdigit($hex)) return false;
00070         }
00071 
00072         return $color;
00073 
00074     }
00075 
00076 }
00077 
00078 // vim: et sw=4 sts=4