| 1 | <?php |
|---|
| 2 | |
|---|
| 3 | // Enum = Enumerated |
|---|
| 4 | /** |
|---|
| 5 | * Validates a keyword against a list of valid values. |
|---|
| 6 | * @warning The case-insensitive compare of this function uses PHP's |
|---|
| 7 | * built-in strtolower and ctype_lower functions, which may |
|---|
| 8 | * cause problems with international comparisons |
|---|
| 9 | */ |
|---|
| 10 | class HTMLPurifier_AttrDef_Enum extends HTMLPurifier_AttrDef |
|---|
| 11 | { |
|---|
| 12 | |
|---|
| 13 | /** |
|---|
| 14 | * Lookup table of valid values. |
|---|
| 15 | * @todo Make protected |
|---|
| 16 | */ |
|---|
| 17 | public $valid_values = array(); |
|---|
| 18 | |
|---|
| 19 | /** |
|---|
| 20 | * Bool indicating whether or not enumeration is case sensitive. |
|---|
| 21 | * @note In general this is always case insensitive. |
|---|
| 22 | */ |
|---|
| 23 | protected $case_sensitive = false; // values according to W3C spec |
|---|
| 24 | |
|---|
| 25 | /** |
|---|
| 26 | * @param $valid_values List of valid values |
|---|
| 27 | * @param $case_sensitive Bool indicating whether or not case sensitive |
|---|
| 28 | */ |
|---|
| 29 | public function __construct( |
|---|
| 30 | $valid_values = array(), $case_sensitive = false |
|---|
| 31 | ) { |
|---|
| 32 | $this->valid_values = array_flip($valid_values); |
|---|
| 33 | $this->case_sensitive = $case_sensitive; |
|---|
| 34 | } |
|---|
| 35 | |
|---|
| 36 | public function validate($string, $config, $context) { |
|---|
| 37 | $string = trim($string); |
|---|
| 38 | if (!$this->case_sensitive) { |
|---|
| 39 | // we may want to do full case-insensitive libraries |
|---|
| 40 | $string = ctype_lower($string) ? $string : strtolower($string); |
|---|
| 41 | } |
|---|
| 42 | $result = isset($this->valid_values[$string]); |
|---|
| 43 | |
|---|
| 44 | return $result ? $string : false; |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | /** |
|---|
| 48 | * @param $string In form of comma-delimited list of case-insensitive |
|---|
| 49 | * valid values. Example: "foo,bar,baz". Prepend "s:" to make |
|---|
| 50 | * case sensitive |
|---|
| 51 | */ |
|---|
| 52 | public function make($string) { |
|---|
| 53 | if (strlen($string) > 2 && $string[0] == 's' && $string[1] == ':') { |
|---|
| 54 | $string = substr($string, 2); |
|---|
| 55 | $sensitive = true; |
|---|
| 56 | } else { |
|---|
| 57 | $sensitive = false; |
|---|
| 58 | } |
|---|
| 59 | $values = explode(',', $string); |
|---|
| 60 | return new HTMLPurifier_AttrDef_Enum($values, $sensitive); |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | } |
|---|
| 64 | |
|---|