2006-07-30 00:54:38 +00:00
|
|
|
<?php
|
|
|
|
|
2006-08-16 15:12:48 +00:00
|
|
|
require_once 'HTMLPurifier/AttrDef.php';
|
|
|
|
|
2006-07-30 00:54:38 +00:00
|
|
|
// Enum = Enumerated
|
2006-08-20 21:47:15 +00:00
|
|
|
/**
|
|
|
|
* Validates a keyword against a list of valid values.
|
2007-04-30 21:19:15 +00:00
|
|
|
* @warning The case-insensitive compare of this function uses PHP's
|
|
|
|
* built-in strtolower and ctype_lower functions, which may
|
|
|
|
* cause problems with international comparisons
|
2006-08-20 21:47:15 +00:00
|
|
|
*/
|
2006-07-30 00:54:38 +00:00
|
|
|
class HTMLPurifier_AttrDef_Enum extends HTMLPurifier_AttrDef
|
|
|
|
{
|
|
|
|
|
2006-08-20 21:47:15 +00:00
|
|
|
/**
|
|
|
|
* Lookup table of valid values.
|
2007-11-25 02:24:39 +00:00
|
|
|
* @todo Make protected
|
2006-08-20 21:47:15 +00:00
|
|
|
*/
|
2007-11-25 02:24:39 +00:00
|
|
|
public $valid_values = array();
|
2006-08-20 21:47:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Bool indicating whether or not enumeration is case sensitive.
|
|
|
|
* @note In general this is always case insensitive.
|
|
|
|
*/
|
2007-11-25 02:24:39 +00:00
|
|
|
protected $case_sensitive = false; // values according to W3C spec
|
2006-07-30 00:54:38 +00:00
|
|
|
|
2006-08-20 21:47:15 +00:00
|
|
|
/**
|
|
|
|
* @param $valid_values List of valid values
|
|
|
|
* @param $case_sensitive Bool indicating whether or not case sensitive
|
|
|
|
*/
|
2007-11-29 04:29:51 +00:00
|
|
|
public function __construct(
|
2007-02-14 22:21:07 +00:00
|
|
|
$valid_values = array(), $case_sensitive = false
|
|
|
|
) {
|
2006-07-30 00:54:38 +00:00
|
|
|
$this->valid_values = array_flip($valid_values);
|
|
|
|
$this->case_sensitive = $case_sensitive;
|
|
|
|
}
|
|
|
|
|
2007-11-25 02:24:39 +00:00
|
|
|
public function validate($string, $config, &$context) {
|
2006-08-05 00:30:31 +00:00
|
|
|
$string = trim($string);
|
2006-07-30 00:54:38 +00:00
|
|
|
if (!$this->case_sensitive) {
|
2007-04-30 21:19:15 +00:00
|
|
|
// we may want to do full case-insensitive libraries
|
2006-07-30 00:54:38 +00:00
|
|
|
$string = ctype_lower($string) ? $string : strtolower($string);
|
|
|
|
}
|
2006-08-04 00:11:54 +00:00
|
|
|
$result = isset($this->valid_values[$string]);
|
|
|
|
|
2006-08-05 00:30:31 +00:00
|
|
|
return $result ? $string : false;
|
2006-07-30 00:54:38 +00:00
|
|
|
}
|
|
|
|
|
2007-05-23 00:39:07 +00:00
|
|
|
/**
|
|
|
|
* @param $string In form of comma-delimited list of case-insensitive
|
2007-05-29 16:51:32 +00:00
|
|
|
* valid values. Example: "foo,bar,baz". Prepend "s:" to make
|
|
|
|
* case sensitive
|
2007-05-23 00:39:07 +00:00
|
|
|
*/
|
2007-11-25 02:24:39 +00:00
|
|
|
public function make($string) {
|
2007-05-29 16:51:32 +00:00
|
|
|
if (strlen($string) > 2 && $string[0] == 's' && $string[1] == ':') {
|
|
|
|
$string = substr($string, 2);
|
|
|
|
$sensitive = true;
|
|
|
|
} else {
|
|
|
|
$sensitive = false;
|
|
|
|
}
|
2007-05-23 00:39:07 +00:00
|
|
|
$values = explode(',', $string);
|
2007-05-29 16:51:32 +00:00
|
|
|
return new HTMLPurifier_AttrDef_Enum($values, $sensitive);
|
2007-05-23 00:39:07 +00:00
|
|
|
}
|
|
|
|
|
2006-07-30 00:54:38 +00:00
|
|
|
}
|
|
|
|
|