[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/se3master/var/www/se3/includes/library/HTMLPurifier/AttrDef/CSS/ -> Color.php (source)

   1  <?php
   2  
   3  /**
   4   * Validates Color as defined by CSS.
   5   */
   6  class HTMLPurifier_AttrDef_CSS_Color extends HTMLPurifier_AttrDef
   7  {
   8  
   9      /**
  10       * @param string $color
  11       * @param HTMLPurifier_Config $config
  12       * @param HTMLPurifier_Context $context
  13       * @return bool|string
  14       */
  15      public function validate($color, $config, $context)
  16      {
  17          static $colors = null;
  18          if ($colors === null) {
  19              $colors = $config->get('Core.ColorKeywords');
  20          }
  21  
  22          $color = trim($color);
  23          if ($color === '') {
  24              return false;
  25          }
  26  
  27          $lower = strtolower($color);
  28          if (isset($colors[$lower])) {
  29              return $colors[$lower];
  30          }
  31  
  32          if (strpos($color, 'rgb(') !== false) {
  33              // rgb literal handling
  34              $length = strlen($color);
  35              if (strpos($color, ')') !== $length - 1) {
  36                  return false;
  37              }
  38              $triad = substr($color, 4, $length - 4 - 1);
  39              $parts = explode(',', $triad);
  40              if (count($parts) !== 3) {
  41                  return false;
  42              }
  43              $type = false; // to ensure that they're all the same type
  44              $new_parts = array();
  45              foreach ($parts as $part) {
  46                  $part = trim($part);
  47                  if ($part === '') {
  48                      return false;
  49                  }
  50                  $length = strlen($part);
  51                  if ($part[$length - 1] === '%') {
  52                      // handle percents
  53                      if (!$type) {
  54                          $type = 'percentage';
  55                      } elseif ($type !== 'percentage') {
  56                          return false;
  57                      }
  58                      $num = (float)substr($part, 0, $length - 1);
  59                      if ($num < 0) {
  60                          $num = 0;
  61                      }
  62                      if ($num > 100) {
  63                          $num = 100;
  64                      }
  65                      $new_parts[] = "$num%";
  66                  } else {
  67                      // handle integers
  68                      if (!$type) {
  69                          $type = 'integer';
  70                      } elseif ($type !== 'integer') {
  71                          return false;
  72                      }
  73                      $num = (int)$part;
  74                      if ($num < 0) {
  75                          $num = 0;
  76                      }
  77                      if ($num > 255) {
  78                          $num = 255;
  79                      }
  80                      $new_parts[] = (string)$num;
  81                  }
  82              }
  83              $new_triad = implode(',', $new_parts);
  84              $color = "rgb($new_triad)";
  85          } else {
  86              // hexadecimal handling
  87              if ($color[0] === '#') {
  88                  $hex = substr($color, 1);
  89              } else {
  90                  $hex = $color;
  91                  $color = '#' . $color;
  92              }
  93              $length = strlen($hex);
  94              if ($length !== 3 && $length !== 6) {
  95                  return false;
  96              }
  97              if (!ctype_xdigit($hex)) {
  98                  return false;
  99              }
 100          }
 101          return $color;
 102      }
 103  }
 104  
 105  // vim: et sw=4 sts=4


Generated: Tue Mar 17 22:47:18 2015 Cross-referenced by PHPXref 0.7.1