Pixels.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. /**
  3. * Validates an integer representation of pixels according to the HTML spec.
  4. */
  5. class HTMLPurifier_AttrDef_HTML_Pixels extends HTMLPurifier_AttrDef
  6. {
  7. protected $max;
  8. public function __construct($max = null) {
  9. $this->max = $max;
  10. }
  11. public function validate($string, $config, $context) {
  12. $string = trim($string);
  13. if ($string === '0') return $string;
  14. if ($string === '') return false;
  15. $length = strlen($string);
  16. if (substr($string, $length - 2) == 'px') {
  17. $string = substr($string, 0, $length - 2);
  18. }
  19. if (!is_numeric($string)) return false;
  20. $int = (int) $string;
  21. if ($int < 0) return '0';
  22. // upper-bound value, extremely high values can
  23. // crash operating systems, see <http://ha.ckers.org/imagecrash.html>
  24. // WARNING, above link WILL crash you if you're using Windows
  25. if ($this->max !== null && $int > $this->max) return (string) $this->max;
  26. return (string) $int;
  27. }
  28. public function make($string) {
  29. if ($string === '') $max = null;
  30. else $max = (int) $string;
  31. $class = get_class($this);
  32. return new $class($max);
  33. }
  34. }
  35. // vim: et sw=4 sts=4