Guest User

Untitled

a guest
Mar 16th, 2012
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.00 KB | None | 0 0
  1. define("NET_INVALID_IP",          0x1);
  2. define("NET_INVALID_PORT",        0x2);
  3. define("NET_VALID_IPv4",          0x4);
  4. define("NET_VALID_IPv6",          0x8);
  5. define("NET_VALID_IP_WITH_PORT",  0x10)
  6.    
  7. /**
  8.  * Validate a port number
  9.  * @param integer $port
  10.  * @return boolean
  11.  */
  12. public static function isValidPort($port) {
  13.     return $port > 0 && $port < 65535;
  14. }
  15.  
  16. /**
  17.  * Validate an address, and point out if port is part of the address
  18.  * @param string $ip
  19.  * @param integer $port
  20.  * @return string
  21.  */
  22. public static function isValidIp($ip, &$port) {
  23.     // IPv6 with port
  24.     $ipv6_with_port = Array();
  25.     preg_match("/^\[(?P<address>.*?)\]:(?P<port>\d{0,})$/", $ip, $ipv6_with_port);
  26.  
  27.     // IPv4 with port
  28.     $ipv4_with_port = Array();
  29.     preg_match("/^(?P<address>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}):(?P<port>\d{0,})$/", $ip, $ipv4_with_port);
  30.  
  31.     if(count($ipv6_with_port) > 0) {
  32.         if(!NET_IPv6::checkIPv6($ipv6_with_port["address"])) {
  33.             return NET_INVALID_IP;
  34.         } else {
  35.             $port = $ipv6_with_port["port"];
  36.  
  37.             if(ODDNS::isValidPort($ipv6_with_port["port"])) {
  38.                 return NET_VALID_IPv6 + ODDNS_VALID_IP_WITH_PORT;
  39.             } else {
  40.                 return NET_INVALID_PORT;
  41.             }
  42.         }
  43.     } else if(count($ipv4_with_port) > 0) {
  44.         if(!NET_IPv4::validateIP($ipv4_with_port["address"])) {
  45.             return NET_INVALID_IP;
  46.         } else {
  47.             $port = $ipv4_with_port["port"];
  48.  
  49.             if(ODDNS::isValidPort($ipv4_with_port["port"])) {
  50.                 return NET_VALID_IPv4 + ODDNS_VALID_IP_WITH_PORT;
  51.             } else {
  52.                 return NET_INVALID_PORT;
  53.             }
  54.         }
  55.     } else if(preg_match("/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/", $ip) == 1) {
  56.         if(NET_IPv4::validateIP($ip)) {
  57.             return NET_VALID_IPv4;
  58.         }
  59.     } else {
  60.         if(NET_IPv6::checkIPv6($ip)) {
  61.             return NET_VALID_IPv6;
  62.         }
  63.     }
  64.  
  65.     return NET_INVALID_IP;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment