File indexing completed on 2024-05-12 06:02:04

0001 <?php
0002 
0003 // Enum = Enumerated
0004 /**
0005  * Validates a keyword against a list of valid values.
0006  * @warning The case-insensitive compare of this function uses PHP's
0007  *          built-in strtolower and ctype_lower functions, which may
0008  *          cause problems with international comparisons
0009  */
0010 class HTMLPurifier_AttrDef_Enum extends HTMLPurifier_AttrDef
0011 {
0012 
0013     /**
0014      * Lookup table of valid values.
0015      * @type array
0016      * @todo Make protected
0017      */
0018     public $valid_values = array();
0019 
0020     /**
0021      * Bool indicating whether or not enumeration is case sensitive.
0022      * @note In general this is always case insensitive.
0023      */
0024     protected $case_sensitive = false; // values according to W3C spec
0025 
0026     /**
0027      * @param array $valid_values List of valid values
0028      * @param bool $case_sensitive Whether or not case sensitive
0029      */
0030     public function __construct($valid_values = array(), $case_sensitive = false)
0031     {
0032         $this->valid_values = array_flip($valid_values);
0033         $this->case_sensitive = $case_sensitive;
0034     }
0035 
0036     /**
0037      * @param string $string
0038      * @param HTMLPurifier_Config $config
0039      * @param HTMLPurifier_Context $context
0040      * @return bool|string
0041      */
0042     public function validate($string, $config, $context)
0043     {
0044         $string = trim($string);
0045         if (!$this->case_sensitive) {
0046             // we may want to do full case-insensitive libraries
0047             $string = ctype_lower($string) ? $string : strtolower($string);
0048         }
0049         $result = isset($this->valid_values[$string]);
0050 
0051         return $result ? $string : false;
0052     }
0053 
0054     /**
0055      * @param string $string In form of comma-delimited list of case-insensitive
0056      *      valid values. Example: "foo,bar,baz". Prepend "s:" to make
0057      *      case sensitive
0058      * @return HTMLPurifier_AttrDef_Enum
0059      */
0060     public function make($string)
0061     {
0062         if (strlen($string) > 2 && $string[0] == 's' && $string[1] == ':') {
0063             $string = substr($string, 2);
0064             $sensitive = true;
0065         } else {
0066             $sensitive = false;
0067         }
0068         $values = explode(',', $string);
0069         return new HTMLPurifier_AttrDef_Enum($values, $sensitive);
0070     }
0071 }
0072 
0073 // vim: et sw=4 sts=4