IPv4.php 998 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. * @type string
  11. */
  12. protected $ip4;
  13. /**
  14. * @param string $aIP
  15. * @param HTMLPurifier_Config $config
  16. * @param HTMLPurifier_Context $context
  17. * @return bool|string
  18. */
  19. public function validate($aIP, $config, $context)
  20. {
  21. if (!$this->ip4) {
  22. $this->_loadRegex();
  23. }
  24. if (preg_match('#^' . $this->ip4 . '$#s', $aIP)) {
  25. return $aIP;
  26. }
  27. return false;
  28. }
  29. /**
  30. * Lazy load function to prevent regex from being stuffed in
  31. * cache.
  32. */
  33. protected function _loadRegex()
  34. {
  35. $oct = '(?:25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]|[0-9])'; // 0-255
  36. $this->ip4 = "(?:{$oct}\\.{$oct}\\.{$oct}\\.{$oct})";
  37. }
  38. }
  39. // vim: et sw=4 sts=4