IPv4.php 806 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. <?php
  2. /**
  3. * Validates an IPv4 address
  4. * @author Feyd @ forums.devnetwork.net (public domain)
  5. */
  6. class HTMLPurifier_AttrDef_URI_IPv4 extends HTMLPurifier_AttrDef
  7. {
  8. /**
  9. * IPv4 regex, protected so that IPv6 can reuse it
  10. */
  11. protected $ip4;
  12. public function validate($aIP, $config, $context) {
  13. if (!$this->ip4) $this->_loadRegex();
  14. if (preg_match('#^' . $this->ip4 . '$#s', $aIP))
  15. {
  16. return $aIP;
  17. }
  18. return false;
  19. }
  20. /**
  21. * Lazy load function to prevent regex from being stuffed in
  22. * cache.
  23. */
  24. protected function _loadRegex() {
  25. $oct = '(?:25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])'; // 0-255
  26. $this->ip4 = "(?:{$oct}\\.{$oct}\\.{$oct}\\.{$oct})";
  27. }
  28. }
  29. // vim: et sw=4 sts=4