You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 line
2.5KB

  1. <?php
  2. /**
  3. * Validates an integer.
  4. * @note While this class was modeled off the CSS definition, no currently
  5. * allowed CSS uses this type. The properties that do are: widows,
  6. * orphans, z-index, counter-increment, counter-reset. Some of the
  7. * HTML attributes, however, find use for a non-negative version of this.
  8. */
  9. class HTMLPurifier_AttrDef_Integer extends HTMLPurifier_AttrDef
  10. {
  11. /**
  12. * Whether or not negative values are allowed.
  13. * @type bool
  14. */
  15. protected $negative = true;
  16. /**
  17. * Whether or not zero is allowed.
  18. * @type bool
  19. */
  20. protected $zero = true;
  21. /**
  22. * Whether or not positive values are allowed.
  23. * @type bool
  24. */
  25. protected $positive = true;
  26. /**
  27. * @param $negative Bool indicating whether or not negative values are allowed
  28. * @param $zero Bool indicating whether or not zero is allowed
  29. * @param $positive Bool indicating whether or not positive values are allowed
  30. */
  31. public function __construct($negative = true, $zero = true, $positive = true)
  32. {
  33. $this->negative = $negative;
  34. $this->zero = $zero;
  35. $this->positive = $positive;
  36. }
  37. /**
  38. * @param string $integer
  39. * @param HTMLPurifier_Config $config
  40. * @param HTMLPurifier_Context $context
  41. * @return bool|string
  42. */
  43. public function validate($integer, $config, $context)
  44. {
  45. $integer = $this->parseCDATA($integer);
  46. if ($integer === '') {
  47. return false;
  48. }
  49. // we could possibly simply typecast it to integer, but there are
  50. // certain fringe cases that must not return an integer.
  51. // clip leading sign
  52. if ($this->negative && $integer[0] === '-') {
  53. $digits = substr($integer, 1);
  54. if ($digits === '0') {
  55. $integer = '0';
  56. } // rm minus sign for zero
  57. } elseif ($this->positive && $integer[0] === '+') {
  58. $digits = $integer = substr($integer, 1); // rm unnecessary plus
  59. } else {
  60. $digits = $integer;
  61. }
  62. // test if it's numeric
  63. if (!ctype_digit($digits)) {
  64. return false;
  65. }
  66. // perform scope tests
  67. if (!$this->zero && $integer == 0) {
  68. return false;
  69. }
  70. if (!$this->positive && $integer > 0) {
  71. return false;
  72. }
  73. if (!$this->negative && $integer < 0) {
  74. return false;
  75. }
  76. return $integer;
  77. }
  78. }
  79. // vim: et sw=4 sts=4