Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

74 linhas
2.2KB

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